From 4e1c6a0dd8eb22facd4311eac3237859f5b2a0b4 Mon Sep 17 00:00:00 2001 From: Paul-Louis Ageneau Date: Tue, 22 Oct 2024 12:28:45 +0200 Subject: [PATCH] Make datachannel target comply with BUILD_SHARED_LIBS option --- CMakeLists.txt | 60 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b321c2ff9..31dc8ddf7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set(PROJECT_DESCRIPTION "C/C++ WebRTC network library featuring Data Channels, M include(GNUInstallDirs) # Options +option(BUILD_SHARED_LIBS "Build shared libraries" ON) 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) @@ -49,7 +50,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) @@ -254,7 +254,10 @@ else() option(sctp_inet6 OFF) option(sctp_werror OFF) set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) + set(OLD_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS}) + set(BUILD_SHARED_LIBS OFF) # to force usrsctp to be built static add_subdirectory(deps/usrsctp EXCLUDE_FROM_ALL) + set(BUILD_SHARED_LIBS ${OLD_BUILD_SHARED_LIBS}) if (MSYS OR MINGW) target_compile_definitions(usrsctp PUBLIC -DSCTP_STDINT_INCLUDE=) endif() @@ -268,7 +271,7 @@ if(RTC_UPDATE_VERSION_HEADER) ) endif() -add_library(datachannel SHARED +add_library(datachannel ${LIBDATACHANNEL_SOURCES} ${LIBDATACHANNEL_HEADERS} ${LIBDATACHANNEL_IMPL_SOURCES} @@ -301,18 +304,24 @@ target_compile_definitions(datachannel-static PUBLIC RTC_STATIC) target_include_directories(datachannel PUBLIC $ $) -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 + $) target_include_directories(datachannel-static PUBLIC $ $) -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 + $) if(WIN32) target_link_libraries(datachannel PUBLIC ws2_32) # winsock2 @@ -348,7 +357,7 @@ else() target_link_libraries(datachannel-static PRIVATE libSRTP::srtp2) else() if(NOT TARGET srtp2) - add_subdirectory(deps/libsrtp EXCLUDE_FROM_ALL) + add_subdirectory(deps/libsrtp) endif() target_compile_definitions(datachannel PRIVATE RTC_SYSTEM_SRTP=0) target_compile_definitions(datachannel-static PRIVATE RTC_SYSTEM_SRTP=0) @@ -369,13 +378,13 @@ if (USE_GNUTLS) endif() target_compile_definitions(datachannel PRIVATE USE_GNUTLS=1) target_compile_definitions(datachannel-static PRIVATE USE_GNUTLS=1) - target_link_libraries(datachannel PRIVATE GnuTLS::GnuTLS) - target_link_libraries(datachannel-static PRIVATE GnuTLS::GnuTLS) + target_link_libraries(datachannel PUBLIC GnuTLS::GnuTLS) + target_link_libraries(datachannel-static PUBLIC GnuTLS::GnuTLS) if (NOT NO_WEBSOCKET) # Needed for SHA1, it should be present as GnuTLS cryptography backend find_package(Nettle REQUIRED) - target_link_libraries(datachannel PRIVATE Nettle::Nettle) - target_link_libraries(datachannel-static PRIVATE Nettle::Nettle) + target_link_libraries(datachannel PUBLIC Nettle::Nettle) + target_link_libraries(datachannel-static PUBLIC Nettle::Nettle) endif() elseif(USE_MBEDTLS) if(NOT TARGET MbedTLS::MbedTLS) @@ -383,8 +392,8 @@ elseif(USE_MBEDTLS) endif() target_compile_definitions(datachannel PRIVATE USE_MBEDTLS=1) target_compile_definitions(datachannel-static PRIVATE USE_MBEDTLS=1) - target_link_libraries(datachannel PRIVATE MbedTLS::MbedTLS) - target_link_libraries(datachannel-static PRIVATE MbedTLS::MbedTLS) + target_link_libraries(datachannel PUBLIC MbedTLS::MbedTLS) + target_link_libraries(datachannel-static PUBLIC MbedTLS::MbedTLS) else() if(APPLE) # This is a bug in CMake that causes it to prefer the system version over @@ -405,8 +414,8 @@ else() find_package(OpenSSL REQUIRED) target_compile_definitions(datachannel PRIVATE USE_GNUTLS=0) target_compile_definitions(datachannel-static PRIVATE USE_GNUTLS=0) - target_link_libraries(datachannel PRIVATE OpenSSL::SSL) - target_link_libraries(datachannel-static PRIVATE OpenSSL::SSL) + target_link_libraries(datachannel PUBLIC OpenSSL::SSL) + target_link_libraries(datachannel-static PUBLIC OpenSSL::SSL) endif() if (USE_NICE) @@ -425,10 +434,10 @@ else() target_link_libraries(datachannel PRIVATE LibJuice::LibJuice) target_link_libraries(datachannel-static PRIVATE LibJuice::LibJuice) else() - add_subdirectory(deps/libjuice EXCLUDE_FROM_ALL) + add_subdirectory(deps/libjuice) 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() @@ -470,6 +479,15 @@ install(FILES ${LIBDATACHANNEL_HEADERS} ) # Export targets +if(NOT USE_SYSTEM_USRSCTP AND NOT BUILD_SHARED_LIBS) + # usrsctp lacks an export set + set_target_properties(usrsctp PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "") + install(TARGETS usrsctp EXPORT UsrsctpTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(EXPORT UsrsctpTargets + FILE UsrsctpTargets.cmake + NAMESPACE Usrsctp:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/usrsctp) +endif() install( EXPORT LibDataChannelTargets FILE LibDataChannelTargets.cmake