Skip to content

Commit

Permalink
Make datachannel target comply with BUILD_SHARED_LIBS option
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed Oct 22, 2024
1 parent 7209b95 commit 0ca00b6
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 49 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build-nomedia.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ jobs:
set CL=/MP
nmake
- name: test
run: build/tests.exe
run: |
cd build
./tests
4 changes: 3 additions & 1 deletion .github/workflows/build-openssl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@ jobs:
set CL=/MP
nmake
- name: test
run: build/tests.exe
run: |
cd build
./tests
123 changes: 76 additions & 47 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.7)
cmake_minimum_required(VERSION 3.13)
project(libdatachannel
VERSION 0.21.2
LANGUAGES CXX)
Expand All @@ -7,10 +7,12 @@ set(PROJECT_DESCRIPTION "C/C++ WebRTC network library featuring Data Channels, M
include(GNUInstallDirs)

# Options
option(BUILD_SHARED_LIBS "Build shared library" ON)
option(BUILD_SHARED_DEPS_LIBS "Build submodules as shared libraries" OFF)
option(USE_GNUTLS "Use GnuTLS instead of OpenSSL" OFF)
option(USE_MBEDTLS "Use Mbed TLS instead of OpenSSL" OFF)
option(USE_NICE "Use libnice instead of libjuice" OFF)
option(PREFER_SYSTEM_LIB "Prefer system libraries over deps folder" OFF)
option(PREFER_SYSTEM_LIB "Prefer system libraries over submodules" OFF)
option(USE_SYSTEM_SRTP "Use system libSRTP" ${PREFER_SYSTEM_LIB})
option(USE_SYSTEM_JUICE "Use system libjuice" ${PREFER_SYSTEM_LIB})
option(USE_SYSTEM_USRSCTP "Use system libusrsctp" ${PREFER_SYSTEM_LIB})
Expand Down Expand Up @@ -49,7 +51,6 @@ endif()

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_SHARED_LIBS OFF) # to force usrsctp to be built static

if(WIN32)
add_definitions(-DWIN32_LEAN_AND_MEAN)
Expand Down Expand Up @@ -230,45 +231,14 @@ set(BENCHMARK_UWP_RESOURCES
${CMAKE_CURRENT_SOURCE_DIR}/test/uwp/benchmark/Windows_TemporaryKey.pfx
)

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

if(USE_SYSTEM_PLOG)
find_package(plog REQUIRED)
else()
set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
add_subdirectory(deps/plog EXCLUDE_FROM_ALL)
endif()

if(SCTP_DEBUG)
add_definitions(-DSCTP_DEBUG)
endif()

if(USE_SYSTEM_USRSCTP)
find_package(Usrsctp REQUIRED)
else()
option(sctp_build_shared_lib OFF)
option(sctp_build_programs OFF)
option(sctp_inet OFF)
option(sctp_inet6 OFF)
option(sctp_werror OFF)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
add_subdirectory(deps/usrsctp EXCLUDE_FROM_ALL)
if (MSYS OR MINGW)
target_compile_definitions(usrsctp PUBLIC -DSCTP_STDINT_INCLUDE=<stdint.h>)
endif()
add_library(Usrsctp::Usrsctp ALIAS usrsctp)
endif()

if(RTC_UPDATE_VERSION_HEADER)
configure_file (
${PROJECT_SOURCE_DIR}/cmake/version.h.in
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/version.h
)
endif()

add_library(datachannel SHARED
add_library(datachannel
${LIBDATACHANNEL_SOURCES}
${LIBDATACHANNEL_HEADERS}
${LIBDATACHANNEL_IMPL_SOURCES}
Expand All @@ -278,15 +248,12 @@ set_target_properties(datachannel PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
CXX_STANDARD 17
CXX_VISIBILITY_PRESET default)

if(APPLE)
set_target_properties(datachannel PROPERTIES
VERSION ${PROJECT_VERSION_MAJOR}
SOVERSION ${PROJECT_VERSION_MAJOR})
endif()

target_compile_definitions(datachannel PRIVATE RTC_EXPORTS)

add_library(datachannel-static STATIC EXCLUDE_FROM_ALL
${LIBDATACHANNEL_SOURCES}
${LIBDATACHANNEL_HEADERS}
Expand All @@ -295,24 +262,84 @@ add_library(datachannel-static STATIC EXCLUDE_FROM_ALL
set_target_properties(datachannel-static PROPERTIES
VERSION ${PROJECT_VERSION}
CXX_STANDARD 17)

target_compile_definitions(datachannel PRIVATE RTC_EXPORTS)
if (NOT BUILD_SHARED_LIBS)
target_compile_definitions(datachannel PUBLIC RTC_STATIC)
endif()
target_compile_definitions(datachannel-static PRIVATE RTC_EXPORTS)
target_compile_definitions(datachannel-static PUBLIC RTC_STATIC)

if(NOT BUILD_SHARED_DEPS_LIBS)
set(BUILD_SHARED_LIBS OFF)
endif()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

if(USE_SYSTEM_PLOG)
find_package(plog REQUIRED)
else()
set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
add_subdirectory(deps/plog EXCLUDE_FROM_ALL)
endif()

if(SCTP_DEBUG)
add_definitions(-DSCTP_DEBUG)
endif()

if(USE_SYSTEM_USRSCTP)
find_package(Usrsctp REQUIRED)
else()
option(sctp_build_shared_lib OFF)
option(sctp_build_programs OFF)
option(sctp_inet OFF)
option(sctp_inet6 OFF)
option(sctp_werror OFF)
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
add_subdirectory(deps/usrsctp EXCLUDE_FROM_ALL)
if (MSYS OR MINGW)
target_compile_definitions(usrsctp PUBLIC -DSCTP_STDINT_INCLUDE=<stdint.h>)
endif()
add_library(Usrsctp::Usrsctp ALIAS usrsctp)

# usrsctp lacks an export set
install(TARGETS usrsctp EXPORT UsrsctpTargets)
install(EXPORT UsrsctpTargets
FILE UsrsctpTargets.cmake
NAMESPACE Usrsctp::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/usrsctp
EXCLUDE_FROM_ALL)

# Fix directories
set_target_properties(usrsctp PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "")
target_include_directories(usrsctp INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/deps/usrsctp/usrsctplib>
$<INSTALL_INTERFACE:>)
endif()

target_include_directories(datachannel PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(datachannel PRIVATE Threads::Threads)
target_link_libraries(datachannel PRIVATE Usrsctp::Usrsctp plog::plog)
target_include_directories(datachannel PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc
${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(datachannel PRIVATE
Threads::Threads
Usrsctp::Usrsctp
$<BUILD_INTERFACE:plog::plog>)

target_include_directories(datachannel-static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(datachannel-static PRIVATE Threads::Threads)
target_link_libraries(datachannel-static PRIVATE Usrsctp::Usrsctp plog::plog)
target_include_directories(datachannel-static PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc
${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(datachannel-static PRIVATE
Threads::Threads
Usrsctp::Usrsctp
$<BUILD_INTERFACE:plog::plog>)

if(WIN32)
target_link_libraries(datachannel PUBLIC ws2_32) # winsock2
Expand Down Expand Up @@ -349,6 +376,7 @@ else()
else()
if(NOT TARGET srtp2)
add_subdirectory(deps/libsrtp EXCLUDE_FROM_ALL)
install(TARGETS srtp2)
endif()
target_compile_definitions(datachannel PRIVATE RTC_SYSTEM_SRTP=0)
target_compile_definitions(datachannel-static PRIVATE RTC_SYSTEM_SRTP=0)
Expand Down Expand Up @@ -426,9 +454,10 @@ else()
target_link_libraries(datachannel-static PRIVATE LibJuice::LibJuice)
else()
add_subdirectory(deps/libjuice EXCLUDE_FROM_ALL)
install(TARGETS juice)
target_compile_definitions(datachannel PRIVATE RTC_SYSTEM_JUICE=0)
target_compile_definitions(datachannel-static PRIVATE RTC_SYSTEM_JUICE=0)
target_link_libraries(datachannel PRIVATE LibJuice::LibJuiceStatic)
target_link_libraries(datachannel PRIVATE LibJuice::LibJuice)
target_link_libraries(datachannel-static PRIVATE LibJuice::LibJuiceStatic)
endif()
endif()
Expand Down

0 comments on commit 0ca00b6

Please sign in to comment.