From f13083a8b215d430dbcd7b54566ed0482f1f281e 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 --- .github/workflows/build-nomedia.yml | 4 +- .github/workflows/build-openssl.yml | 4 +- CMakeLists.txt | 117 ++++++++++++++--------- examples/client-benchmark/CMakeLists.txt | 3 +- examples/client/CMakeLists.txt | 1 - examples/client/getopt.h | 2 +- 6 files changed, 79 insertions(+), 52 deletions(-) diff --git a/.github/workflows/build-nomedia.yml b/.github/workflows/build-nomedia.yml index 1fd75a6b4..fe6ab8d84 100644 --- a/.github/workflows/build-nomedia.yml +++ b/.github/workflows/build-nomedia.yml @@ -36,5 +36,7 @@ jobs: set CL=/MP nmake - name: test - run: build/tests.exe + run: | + cd build + ./tests diff --git a/.github/workflows/build-openssl.yml b/.github/workflows/build-openssl.yml index f1a925bea..8332d7cf6 100644 --- a/.github/workflows/build-openssl.yml +++ b/.github/workflows/build-openssl.yml @@ -52,5 +52,7 @@ jobs: set CL=/MP nmake - name: test - run: build/tests.exe + run: | + cd build + ./tests diff --git a/CMakeLists.txt b/CMakeLists.txt index b321c2ff9..6cf252b0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.7) +cmake_minimum_required(VERSION 3.13) project(libdatachannel VERSION 0.21.2 LANGUAGES CXX) @@ -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}) @@ -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) @@ -230,37 +231,6 @@ 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=) - endif() - add_library(Usrsctp::Usrsctp ALIAS usrsctp) -endif() - if(RTC_UPDATE_VERSION_HEADER) configure_file ( ${PROJECT_SOURCE_DIR}/cmake/version.h.in @@ -268,7 +238,7 @@ if(RTC_UPDATE_VERSION_HEADER) ) endif() -add_library(datachannel SHARED +add_library(datachannel ${LIBDATACHANNEL_SOURCES} ${LIBDATACHANNEL_HEADERS} ${LIBDATACHANNEL_IMPL_SOURCES} @@ -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 @@ -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=) + 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 + $ + $) +endif() + 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 @@ -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) @@ -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() diff --git a/examples/client-benchmark/CMakeLists.txt b/examples/client-benchmark/CMakeLists.txt index bd4f53989..ba4e8d8d7 100644 --- a/examples/client-benchmark/CMakeLists.txt +++ b/examples/client-benchmark/CMakeLists.txt @@ -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() @@ -34,7 +33,7 @@ target_link_libraries(datachannel-client-benchmark LibDataChannel::LibDataChanne if(MSVC) add_custom_command(TARGET datachannel-client-benchmark POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different - "$/datachannel.dll" + "$/datachannel.dll" # TODO $ ) endif() diff --git a/examples/client/CMakeLists.txt b/examples/client/CMakeLists.txt index 0976b32a0..aef02cc07 100644 --- a/examples/client/CMakeLists.txt +++ b/examples/client/CMakeLists.txt @@ -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() diff --git a/examples/client/getopt.h b/examples/client/getopt.h index b42babd7b..20ce835e3 100644 --- a/examples/client/getopt.h +++ b/examples/client/getopt.h @@ -71,7 +71,7 @@ EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. #define null_argument 0 /*Argument Null*/ #define no_argument 0 /*Argument Switch Only*/ #define required_argument 1 /*Argument Required*/ - #define optional_argument 2 /*Argument Optional*/ + #define optional_argument 2 /*Argument Optional*/ // Shorter Options #define ARG_NULL 0 /*Argument Null*/