CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. cmake_minimum_required(VERSION 3.7)
  2. project(libdatachannel
  3. VERSION 0.9.4
  4. LANGUAGES CXX)
  5. set(PROJECT_DESCRIPTION "WebRTC Data Channels Library")
  6. # Options
  7. option(USE_GNUTLS "Use GnuTLS instead of OpenSSL" OFF)
  8. option(USE_NICE "Use libnice instead of libjuice" OFF)
  9. option(USE_SYSTEM_SRTP "Use system libSRTP" OFF)
  10. option(NO_WEBSOCKET "Disable WebSocket support" OFF)
  11. option(NO_MEDIA "Disable media transport support" OFF)
  12. option(NO_EXAMPLES "Disable examples" OFF)
  13. option(NO_TESTS "Disable tests build" OFF)
  14. option(WARNINGS_AS_ERRORS "Treat warnings as errors" OFF)
  15. option(RSA_KEY_BITS_2048 "Use 2048-bit RSA key instead of 3072-bit" OFF)
  16. option(CAPI_STDCALL "Set calling convention of C API callbacks stdcall" OFF)
  17. if(USE_NICE)
  18. option(USE_JUICE "Use libjuice" OFF)
  19. else()
  20. option(USE_JUICE "Use libjuice" ON)
  21. endif()
  22. if(USE_GNUTLS)
  23. option(USE_NETTLE "Use Nettle in libjuice" ON)
  24. else()
  25. option(USE_NETTLE "Use Nettle in libjuice" OFF)
  26. endif()
  27. set(CMAKE_POSITION_INDEPENDENT_CODE ON)
  28. set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
  29. if(WIN32)
  30. add_definitions(-DWIN32_LEAN_AND_MEAN)
  31. if (MSVC)
  32. add_definitions(-DNOMINMAX)
  33. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  34. add_definitions(-D_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
  35. endif()
  36. endif()
  37. set(LIBDATACHANNEL_SOURCES
  38. ${CMAKE_CURRENT_SOURCE_DIR}/src/candidate.cpp
  39. ${CMAKE_CURRENT_SOURCE_DIR}/src/certificate.cpp
  40. ${CMAKE_CURRENT_SOURCE_DIR}/src/channel.cpp
  41. ${CMAKE_CURRENT_SOURCE_DIR}/src/configuration.cpp
  42. ${CMAKE_CURRENT_SOURCE_DIR}/src/datachannel.cpp
  43. ${CMAKE_CURRENT_SOURCE_DIR}/src/description.cpp
  44. ${CMAKE_CURRENT_SOURCE_DIR}/src/dtlssrtptransport.cpp
  45. ${CMAKE_CURRENT_SOURCE_DIR}/src/dtlstransport.cpp
  46. ${CMAKE_CURRENT_SOURCE_DIR}/src/icetransport.cpp
  47. ${CMAKE_CURRENT_SOURCE_DIR}/src/init.cpp
  48. ${CMAKE_CURRENT_SOURCE_DIR}/src/log.cpp
  49. ${CMAKE_CURRENT_SOURCE_DIR}/src/message.cpp
  50. ${CMAKE_CURRENT_SOURCE_DIR}/src/peerconnection.cpp
  51. ${CMAKE_CURRENT_SOURCE_DIR}/src/rtcp.cpp
  52. ${CMAKE_CURRENT_SOURCE_DIR}/src/sctptransport.cpp
  53. ${CMAKE_CURRENT_SOURCE_DIR}/src/threadpool.cpp
  54. ${CMAKE_CURRENT_SOURCE_DIR}/src/tls.cpp
  55. ${CMAKE_CURRENT_SOURCE_DIR}/src/track.cpp
  56. ${CMAKE_CURRENT_SOURCE_DIR}/src/processor.cpp
  57. ${CMAKE_CURRENT_SOURCE_DIR}/src/capi.cpp
  58. )
  59. set(LIBDATACHANNEL_WEBSOCKET_SOURCES
  60. ${CMAKE_CURRENT_SOURCE_DIR}/src/base64.cpp
  61. ${CMAKE_CURRENT_SOURCE_DIR}/src/tcptransport.cpp
  62. ${CMAKE_CURRENT_SOURCE_DIR}/src/tlstransport.cpp
  63. ${CMAKE_CURRENT_SOURCE_DIR}/src/verifiedtlstransport.cpp
  64. ${CMAKE_CURRENT_SOURCE_DIR}/src/websocket.cpp
  65. ${CMAKE_CURRENT_SOURCE_DIR}/src/wstransport.cpp
  66. )
  67. set(LIBDATACHANNEL_HEADERS
  68. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/candidate.hpp
  69. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/channel.hpp
  70. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/configuration.hpp
  71. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/configuration.hpp
  72. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/datachannel.hpp
  73. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/description.hpp
  74. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtcp.hpp
  75. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/include.hpp
  76. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/init.hpp
  77. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/log.hpp
  78. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/message.hpp
  79. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/peerconnection.hpp
  80. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/queue.hpp
  81. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/reliability.hpp
  82. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtc.h
  83. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtc.hpp
  84. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtp.hpp
  85. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/track.hpp
  86. ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/websocket.hpp
  87. )
  88. set(TESTS_SOURCES
  89. ${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp
  90. ${CMAKE_CURRENT_SOURCE_DIR}/test/connectivity.cpp
  91. ${CMAKE_CURRENT_SOURCE_DIR}/test/track.cpp
  92. ${CMAKE_CURRENT_SOURCE_DIR}/test/capi_connectivity.cpp
  93. ${CMAKE_CURRENT_SOURCE_DIR}/test/capi_track.cpp
  94. ${CMAKE_CURRENT_SOURCE_DIR}/test/websocket.cpp
  95. ${CMAKE_CURRENT_SOURCE_DIR}/test/benchmark.cpp
  96. )
  97. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  98. set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  99. find_package(Threads REQUIRED)
  100. set(CMAKE_POLICY_DEFAULT_CMP0048 NEW)
  101. add_subdirectory(deps/plog)
  102. option(sctp_build_programs 0)
  103. add_subdirectory(deps/usrsctp EXCLUDE_FROM_ALL)
  104. if (MSYS OR MINGW)
  105. target_compile_definitions(usrsctp PUBLIC -DSCTP_STDINT_INCLUDE=<stdint.h>)
  106. target_compile_definitions(usrsctp-static PUBLIC -DSCTP_STDINT_INCLUDE=<stdint.h>)
  107. endif()
  108. if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  109. target_compile_options(usrsctp PRIVATE -Wno-error=format-truncation)
  110. target_compile_options(usrsctp-static PRIVATE -Wno-error=format-truncation)
  111. endif()
  112. add_library(Usrsctp::Usrsctp ALIAS usrsctp)
  113. add_library(Usrsctp::UsrsctpStatic ALIAS usrsctp-static)
  114. if (NO_WEBSOCKET)
  115. add_library(datachannel SHARED
  116. ${LIBDATACHANNEL_SOURCES})
  117. add_library(datachannel-static STATIC EXCLUDE_FROM_ALL
  118. ${LIBDATACHANNEL_SOURCES})
  119. target_compile_definitions(datachannel PUBLIC RTC_ENABLE_WEBSOCKET=0)
  120. target_compile_definitions(datachannel-static PUBLIC RTC_ENABLE_WEBSOCKET=0)
  121. else()
  122. add_library(datachannel SHARED
  123. ${LIBDATACHANNEL_SOURCES}
  124. ${LIBDATACHANNEL_WEBSOCKET_SOURCES})
  125. add_library(datachannel-static STATIC EXCLUDE_FROM_ALL
  126. ${LIBDATACHANNEL_SOURCES}
  127. ${LIBDATACHANNEL_WEBSOCKET_SOURCES})
  128. target_compile_definitions(datachannel PUBLIC RTC_ENABLE_WEBSOCKET=1)
  129. target_compile_definitions(datachannel-static PUBLIC RTC_ENABLE_WEBSOCKET=1)
  130. endif()
  131. set_target_properties(datachannel PROPERTIES
  132. VERSION ${PROJECT_VERSION}
  133. CXX_STANDARD 17)
  134. set_target_properties(datachannel-static PROPERTIES
  135. VERSION ${PROJECT_VERSION}
  136. CXX_STANDARD 17)
  137. target_include_directories(datachannel PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
  138. target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
  139. target_include_directories(datachannel PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
  140. target_link_libraries(datachannel PUBLIC Threads::Threads plog::plog)
  141. target_link_libraries(datachannel PRIVATE Usrsctp::UsrsctpStatic)
  142. target_include_directories(datachannel-static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
  143. target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include/rtc)
  144. target_include_directories(datachannel-static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
  145. target_link_libraries(datachannel-static PUBLIC Threads::Threads plog::plog)
  146. target_link_libraries(datachannel-static PRIVATE Usrsctp::UsrsctpStatic)
  147. if(WIN32)
  148. target_link_libraries(datachannel PRIVATE ws2_32) # winsock2
  149. target_link_libraries(datachannel-static PRIVATE ws2_32) # winsock2
  150. endif()
  151. if(NO_MEDIA)
  152. target_compile_definitions(datachannel PUBLIC RTC_ENABLE_MEDIA=0)
  153. target_compile_definitions(datachannel-static PUBLIC RTC_ENABLE_MEDIA=0)
  154. else()
  155. target_compile_definitions(datachannel PUBLIC RTC_ENABLE_MEDIA=1)
  156. target_compile_definitions(datachannel-static PUBLIC RTC_ENABLE_MEDIA=1)
  157. if(USE_SYSTEM_SRTP)
  158. find_package(SRTP REQUIRED)
  159. if(NOT TARGET SRTP::SRTP)
  160. add_library(SRTP::SRTP UNKNOWN IMPORTED)
  161. set_target_properties(SRTP::SRTP PROPERTIES
  162. INTERFACE_INCLUDE_DIRECTORIES ${SRTP_INCLUDE_DIRS}
  163. IMPORTED_LINK_INTERFACE_LANGUAGES C
  164. IMPORTED_LOCATION ${SRTP_LIBRARIES})
  165. endif()
  166. target_compile_definitions(datachannel PRIVATE RTC_SYSTEM_SRTP=1)
  167. target_compile_definitions(datachannel-static PRIVATE RTC_SYSTEM_SRTP=1)
  168. target_link_libraries(datachannel PRIVATE SRTP::SRTP)
  169. target_link_libraries(datachannel-static PRIVATE SRTP::SRTP)
  170. else()
  171. add_subdirectory(deps/libsrtp EXCLUDE_FROM_ALL)
  172. target_compile_definitions(datachannel PRIVATE RTC_SYSTEM_SRTP=0)
  173. target_compile_definitions(datachannel-static PRIVATE RTC_SYSTEM_SRTP=0)
  174. target_link_libraries(datachannel PRIVATE srtp2)
  175. target_link_libraries(datachannel-static PRIVATE srtp2)
  176. endif()
  177. endif()
  178. if (USE_GNUTLS)
  179. find_package(GnuTLS REQUIRED)
  180. if(NOT TARGET GnuTLS::GnuTLS)
  181. add_library(GnuTLS::GnuTLS UNKNOWN IMPORTED)
  182. set_target_properties(GnuTLS::GnuTLS PROPERTIES
  183. INTERFACE_INCLUDE_DIRECTORIES "${GNUTLS_INCLUDE_DIRS}"
  184. INTERFACE_COMPILE_DEFINITIONS "${GNUTLS_DEFINITIONS}"
  185. IMPORTED_LINK_INTERFACE_LANGUAGES C
  186. IMPORTED_LOCATION "${GNUTLS_LIBRARIES}")
  187. endif()
  188. target_compile_definitions(datachannel PRIVATE USE_GNUTLS=1)
  189. target_compile_definitions(datachannel-static PRIVATE USE_GNUTLS=1)
  190. target_link_libraries(datachannel PRIVATE GnuTLS::GnuTLS)
  191. target_link_libraries(datachannel-static PRIVATE GnuTLS::GnuTLS)
  192. else()
  193. find_package(OpenSSL REQUIRED)
  194. target_compile_definitions(datachannel PRIVATE USE_GNUTLS=0)
  195. target_compile_definitions(datachannel-static PRIVATE USE_GNUTLS=0)
  196. target_link_libraries(datachannel PRIVATE OpenSSL::SSL)
  197. target_link_libraries(datachannel-static PRIVATE OpenSSL::SSL)
  198. endif()
  199. if (USE_NICE OR NOT USE_JUICE)
  200. find_package(LibNice REQUIRED)
  201. target_compile_definitions(datachannel PRIVATE USE_NICE=1)
  202. target_compile_definitions(datachannel-static PRIVATE USE_NICE=1)
  203. target_link_libraries(datachannel PRIVATE LibNice::LibNice)
  204. target_link_libraries(datachannel-static PRIVATE LibNice::LibNice)
  205. else()
  206. add_subdirectory(deps/libjuice EXCLUDE_FROM_ALL)
  207. target_compile_definitions(datachannel PRIVATE USE_NICE=0)
  208. target_compile_definitions(datachannel-static PRIVATE USE_NICE=0)
  209. target_link_libraries(datachannel PRIVATE LibJuice::LibJuiceStatic)
  210. target_link_libraries(datachannel-static PRIVATE LibJuice::LibJuiceStatic)
  211. endif()
  212. if(RSA_KEY_BITS_2048)
  213. target_compile_definitions(datachannel PUBLIC RSA_KEY_BITS_2048)
  214. target_compile_definitions(datachannel-static PUBLIC RSA_KEY_BITS_2048)
  215. endif()
  216. if(CAPI_STDCALL)
  217. target_compile_definitions(datachannel PUBLIC CAPI_STDCALL)
  218. target_compile_definitions(datachannel-static PUBLIC CAPI_STDCALL)
  219. endif()
  220. add_library(LibDataChannel::LibDataChannel ALIAS datachannel)
  221. add_library(LibDataChannel::LibDataChannelStatic ALIAS datachannel-static)
  222. install(TARGETS datachannel LIBRARY DESTINATION lib)
  223. install(FILES ${LIBDATACHANNEL_HEADERS} DESTINATION include/rtc)
  224. if(NOT MSVC)
  225. target_compile_options(datachannel PRIVATE -Wall -Wextra)
  226. target_compile_options(datachannel-static PRIVATE -Wall -Wextra)
  227. endif()
  228. if(WARNINGS_AS_ERRORS)
  229. if(MSVC)
  230. target_compile_options(datachannel PRIVATE /WX)
  231. target_compile_options(datachannel-static PRIVATE /WX)
  232. else()
  233. target_compile_options(datachannel PRIVATE -Werror)
  234. target_compile_options(datachannel-static PRIVATE -Werror)
  235. endif()
  236. endif()
  237. # Tests
  238. if(NOT NO_TESTS)
  239. add_executable(datachannel-tests ${TESTS_SOURCES})
  240. set_target_properties(datachannel-tests PROPERTIES
  241. VERSION ${PROJECT_VERSION}
  242. CXX_STANDARD 17)
  243. set_target_properties(datachannel-tests PROPERTIES OUTPUT_NAME tests)
  244. target_include_directories(datachannel-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
  245. if(WIN32)
  246. target_link_libraries(datachannel-tests datachannel-static) # DLL exports only the C API
  247. else()
  248. target_link_libraries(datachannel-tests datachannel)
  249. endif()
  250. # Benchmark
  251. add_executable(datachannel-benchmark test/benchmark.cpp)
  252. set_target_properties(datachannel-benchmark PROPERTIES
  253. VERSION ${PROJECT_VERSION}
  254. CXX_STANDARD 17)
  255. set_target_properties(datachannel-benchmark PROPERTIES OUTPUT_NAME benchmark)
  256. target_compile_definitions(datachannel-benchmark PRIVATE BENCHMARK_MAIN=1)
  257. target_include_directories(datachannel-benchmark PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
  258. if(WIN32)
  259. target_link_libraries(datachannel-benchmark datachannel-static) # DLL exports only the C API
  260. else()
  261. target_link_libraries(datachannel-benchmark datachannel)
  262. endif()
  263. endif()
  264. # Examples
  265. if(NOT NO_EXAMPLES)
  266. set(JSON_BuildTests OFF CACHE INTERNAL "")
  267. add_subdirectory(deps/json)
  268. add_subdirectory(examples/client)
  269. add_subdirectory(examples/media)
  270. add_subdirectory(examples/sfu-media)
  271. add_subdirectory(examples/copy-paste)
  272. add_subdirectory(examples/copy-paste-capi)
  273. endif()