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 6cc706f commit 5bfe0f9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 50 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
117 changes: 71 additions & 46 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,13 +248,11 @@ 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
Expand All @@ -298,21 +266,76 @@ set_target_properties(datachannel-static PROPERTIES
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 +372,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 +450,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
1 change: 0 additions & 1 deletion examples/client-benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ set(GETOPT_SOURCES

if(WIN32)
add_executable(datachannel-client-benchmark ${CLIENT_SOURCES} ${GETOPT_SOURCES})
target_compile_definitions(datachannel-client-benchmark PUBLIC STATIC_GETOPT)
else()
add_executable(datachannel-client-benchmark ${CLIENT_SOURCES})
endif()
Expand Down
1 change: 0 additions & 1 deletion examples/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ if(WIN32)
else()
add_executable(datachannel-client ${CLIENT_SOURCES} ${GETOPT_SOURCES})
endif()
target_compile_definitions(datachannel-client PUBLIC STATIC_GETOPT)
else()
add_executable(datachannel-client ${CLIENT_SOURCES})
endif()
Expand Down

0 comments on commit 5bfe0f9

Please sign in to comment.