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 4e1c6a0
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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=<stdint.h>)
endif()
Expand All @@ -268,7 +271,7 @@ if(RTC_UPDATE_VERSION_HEADER)
)
endif()

add_library(datachannel SHARED
add_library(datachannel
${LIBDATACHANNEL_SOURCES}
${LIBDATACHANNEL_HEADERS}
${LIBDATACHANNEL_IMPL_SOURCES}
Expand Down Expand Up @@ -301,18 +304,24 @@ target_compile_definitions(datachannel-static PUBLIC RTC_STATIC)
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 @@ -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)
Expand All @@ -369,22 +378,22 @@ 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)
find_package(MbedTLS 3 REQUIRED)
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
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4e1c6a0

Please sign in to comment.