CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. cmake_minimum_required(VERSION 3.7.2)
  2. option(SWCLT_DEBUG_JSON "Enables json frame logs to stdout" OFF)
  3. include(cmake/cotire.cmake)
  4. if (WIN32)
  5. # Setup hunter gate to auto grab our 3rd party dependencies
  6. # (may be included by our super project so check HUNTER_WIKI)
  7. if (NOT HUNTER_WIKI)
  8. include(cmake/HunterGate.cmake)
  9. HunterGate(
  10. URL "https://github.com/ruslo/hunter/archive/v0.20.39.tar.gz"
  11. SHA1 "b49c4b58e17c1473e822f8b21fcde92fee25791d"
  12. )
  13. endif()
  14. endif()
  15. # Declare our project, libks
  16. project(SignalWire-Client-C VERSION 1.1.0 LANGUAGES C)
  17. message("SignalWire-Client-C ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
  18. # Set package version
  19. set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
  20. set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
  21. set(CPACK_PACKAGE_VERSION_PATCH "0")
  22. include(cmake/FindLibKS.cmake)
  23. include(${LIBKS_CMAKE_DIR}/ksutil.cmake)
  24. ksutil_setup_platform()
  25. # Find stuff we need for packaging on UNIX
  26. if(KS_PLAT_LIN)
  27. find_package(Git)
  28. find_program(GZIP_CMD gzip)
  29. find_program(DATE_CMD date)
  30. endif()
  31. # Fixes build on older gcc, Debian Jessie
  32. if(KS_PLAT_LIN)
  33. set(CMAKE_C_STANDARD 99)
  34. endif()
  35. # Set package name
  36. string(TOLOWER ${CMAKE_PROJECT_NAME} PACKAGE_NAME)
  37. # Set package contact
  38. set(CPACK_PACKAGE_CONTACT "support@signalwire.com")
  39. # Detect architecture
  40. if(NOT CPACK_SYSTEM_NAME)
  41. set(CPACK_SYSTEM_NAME ${CMAKE_SYSTEM_PROCESSOR})
  42. # sanity check
  43. if(CPACK_SYSTEM_NAME STREQUAL x86_64)
  44. set(CPACK_SYSTEM_NAME amd64)
  45. endif()
  46. endif()
  47. # Get UNIX type
  48. if(KS_PLAT_LIN)
  49. find_file(DEBIAN_FOUND debian_version debconf.conf
  50. PATHS /etc
  51. )
  52. find_file(CENTOS_FOUND centos-release
  53. PATHS /etc
  54. )
  55. if(DEBIAN_FOUND)
  56. set(CMAKE_OS_NAME "Debian" CACHE STRING "Operating system name" FORCE)
  57. endif()
  58. if(CENTOS_FOUND)
  59. set(CMAKE_OS_NAME "Centos" CACHE STRING "Operating system name" FORCE)
  60. endif()
  61. endif()
  62. # Centos packaging
  63. if("${CMAKE_OS_NAME}" STREQUAL "Centos")
  64. # Enable component install
  65. set(CPACK_RPM_COMPONENT_INSTALL ON)
  66. # Find stuff we need for packaging on Centos
  67. find_program(ARCH_CMD arch)
  68. find_program(RPM_CMD rpm)
  69. # Set package architecture
  70. if(ARCH_CMD)
  71. execute_process(COMMAND ${ARCH_CMD} OUTPUT_VARIABLE CENTOS_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
  72. set(CPACK_RPM_RUNTIME_PACKAGE_ARCHITECTURE ${CENTOS_ARCH})
  73. set(CPACK_SYSTEM_NAME ${CENTOS_ARCH})
  74. unset(CENTOS_ARCH)
  75. endif()
  76. # Get RPM DIST tag, rpm --eval '%{dist}'
  77. if(RPM_CMD)
  78. execute_process(COMMAND ${RPM_CMD} --eval %{dist}
  79. OUTPUT_VARIABLE DIST_TAG
  80. OUTPUT_STRIP_TRAILING_WHITESPACE
  81. ERROR_STRIP_TRAILING_WHITESPACE)
  82. endif()
  83. # https://gitlab.kitware.com/cmake/cmake/issues/16517
  84. set(CPACK_COMPONENTS_ALL runtime)
  85. # Set package type to build
  86. set(CPACK_GENERATOR RPM)
  87. # Set RPM package name
  88. set(CPACK_RPM_RUNTIME_PACKAGE_NAME "signalwire-client-c")
  89. # Set version release from environment variable
  90. if (NOT "$ENV{PACKAGE_RELEASE}" STREQUAL "")
  91. set(CPACK_RPM_PACKAGE_RELEASE "$ENV{PACKAGE_RELEASE}")
  92. else()
  93. if(DATE_CMD)
  94. execute_process(COMMAND ${DATE_CMD} +%Y%m%d OUTPUT_VARIABLE DATE_YMD)
  95. set(CPACK_RPM_PACKAGE_RELEASE ${DATE_YMD})
  96. else()
  97. set(CPACK_RPM_PACKAGE_RELEASE "1")
  98. endif()
  99. endif()
  100. # Set RPM version
  101. set(CPACK_RPM_PACKAGE_VERSION ${PROJECT_VERSION})
  102. # Set OS DIST tag
  103. set(CPACK_RPM_PACKAGE_RELEASE_DIST ${DIST_TAG})
  104. # Set RPM filename
  105. set(CPACK_RPM_RUNTIME_FILE_NAME "${CPACK_RPM_RUNTIME_PACKAGE_NAME}-${PROJECT_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}${DIST_TAG}.${CENTOS_ARCH}rpm")
  106. # Set license
  107. set(CPACK_RPM_PACKAGE_LICENSE "MIT")
  108. # Set RPM group
  109. set(CPACK_RPM_PACKAGE_GROUP "Development/Libraries/C and C++")
  110. # Set RPM vendor
  111. set(CPACK_RPM_PACKAGE_VENDOR "SignalWire")
  112. # Set RPM URL
  113. set(CPACK_RPM_RUNTIME_PACKAGE_URL "https://signalwire.com/")
  114. # Set RPM Description
  115. set(CPACK_RPM_PACKAGE_DESCRIPTION "SignalWire C Client and Library Headers")
  116. # Set RPM package summary
  117. set(CPACK_RPM_PACKAGE_SUMMARY "SignalWire's C client")
  118. # Set auto dependency detection
  119. set(CPACK_RPM_PACKAGE_AUTOREQPROV "yes")
  120. # Create changelog
  121. string(TIMESTAMP NOW "%a %b %d %Y" UTC)
  122. FILE(WRITE ${CMAKE_CURRENT_LIST_DIR}/changelog "* ${NOW} ${CPACK_RPM_PACKAGE_VENDOR} <${CPACK_PACKAGE_CONTACT}> ${PROJECT_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}${DIST_TAG}\n")
  123. FILE(APPEND ${CMAKE_CURRENT_LIST_DIR}/changelog "- autogenerated RPM package\n")
  124. unset(NOW)
  125. set(CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_LIST_DIR}/changelog")
  126. endif()
  127. # Debian packaging
  128. if("${CMAKE_OS_NAME}" STREQUAL "Debian")
  129. # Find stuff we need for packaging on Debian
  130. find_program(DPKG_CMD dpkg)
  131. find_program(LSB_RELEASE_CMD lsb_release)
  132. # Get distro codename
  133. if(NOT LSB_RELEASE_CMD)
  134. message(WARNING "DEB Generator: Can't find lsb_release in your path. Setting DISTRO_CODENAME to unknown.")
  135. set(DISTRO_CODENAME unknown)
  136. else()
  137. execute_process(COMMAND ${LSB_RELEASE_CMD} -cs
  138. OUTPUT_VARIABLE DISTRO_CODENAME
  139. OUTPUT_STRIP_TRAILING_WHITESPACE)
  140. endif()
  141. # https://gitlab.kitware.com/cmake/cmake/issues/16517
  142. set(CPACK_COMPONENTS_ALL runtime)
  143. set(CPACK_DEB_COMPONENT_INSTALL "ON")
  144. # Set package name
  145. set(CPACK_DEBIAN_PACKAGE_NAME "signalwire-client-c")
  146. # Abuse Cmake component name
  147. set(CPACK_DEBIAN_RUNTIME_PACKAGE_NAME ${CPACK_DEBIAN_PACKAGE_NAME})
  148. # Set package type to build
  149. set(CPACK_GENERATOR DEB)
  150. # Set package maintainer
  151. set(CPACK_DEBIAN_PACKAGE_MAINTAINER ${CPACK_PACKAGE_CONTACT})
  152. # Manual package dependencies
  153. set(CPACK_DEBIAN_PACKAGE_DEPENDS "")
  154. # Detect package dependencies
  155. set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
  156. set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON)
  157. set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS_POLICY ">=")
  158. # Set package description
  159. set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "SignalWire Client C")
  160. # Set package section
  161. set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
  162. # Set package priority
  163. set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
  164. # Set package architecture
  165. if(DPKG_CMD)
  166. execute_process(COMMAND ${DPKG_CMD} --print-architecture OUTPUT_VARIABLE DEB_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
  167. set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${DEB_ARCH})
  168. set(CPACK_SYSTEM_NAME ${DEB_ARCH})
  169. unset(DEB_ARCH)
  170. endif()
  171. # Enforce Debian policy permission rules
  172. set(CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION ON)
  173. # Set a Debian compliant changelog footer
  174. if(DATE_CMD)
  175. execute_process(COMMAND ${DATE_CMD} -R OUTPUT_VARIABLE RFC2822_TIMESTAMP)
  176. # Two spaces between maintainer and timestamp is NOT a mistake
  177. set(CHANGELOG_FOOTER " -- ${CPACK_DEBIAN_PACKAGE_MAINTAINER} ${RFC2822_TIMESTAMP}")
  178. endif()
  179. # Set version release from environment variable
  180. if (NOT "$ENV{PACKAGE_RELEASE}" STREQUAL "")
  181. set(CPACK_DEBIAN_PACKAGE_RELEASE "$ENV{PACKAGE_RELEASE}")
  182. else()
  183. if(DATE_CMD)
  184. execute_process(COMMAND ${DATE_CMD} +%Y%m%d OUTPUT_VARIABLE DATE_YMD)
  185. set(CPACK_DEBIAN_PACKAGE_RELEASE ${DATE_YMD})
  186. else()
  187. set(CPACK_DEBIAN_PACKAGE_RELEASE "1")
  188. endif()
  189. endif()
  190. # Set package version
  191. set(CPACK_DEBIAN_PACKAGE_VERSION ${PROJECT_VERSION}-${CPACK_DEBIAN_PACKAGE_RELEASE}~${DISTRO_CODENAME})
  192. # Set debian file name format
  193. set(CPACK_DEBIAN_FILE_NAME "${PACKAGE_NAME}_${PROJECT_VERSION}-${CPACK_DEBIAN_PACKAGE_RELEASE}_${DISTRO_CODENAME}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.deb")
  194. # Set a Debian compliant changelog header
  195. set(CHANGELOG_HEADER "${PACKAGE_NAME} (${CPACK_DEBIAN_PACKAGE_VERSION}) ${DISTRO_CODENAME}\; urgency=${CPACK_DEBIAN_PACKAGE_PRIORITY}")
  196. # Generate a Debian compliant changelog
  197. if(GIT_FOUND AND GZIP_CMD AND DATE_CMD)
  198. # Get git log info
  199. execute_process(COMMAND ${GIT_EXECUTABLE} log --no-merges --pretty=format:"%n [%an]%n * %s" --since="last month"
  200. OUTPUT_VARIABLE CHANGELOG
  201. WORKING_DIRECTORY .
  202. OUTPUT_STRIP_TRAILING_WHITESPACE)
  203. string(REPLACE "\"" "" CHANGELOG ${CHANGELOG})
  204. # Create changelog
  205. file(WRITE changelog.Debian ${CHANGELOG_HEADER}\n${CHANGELOG}\n\n${CHANGELOG_FOOTER})
  206. execute_process(COMMAND ${GZIP_CMD} -f -9 -n changelog.Debian)
  207. # Install changelog
  208. install(FILES "${CMAKE_BINARY_DIR}/changelog.Debian.gz" COMPONENT "runtime" DESTINATION "share/doc/${CPACK_DEBIAN_PACKAGE_NAME}")
  209. # Delete changelog related variables
  210. unset(CHANGELOG_HEADER)
  211. unset(CHANGELOG_FOOTER)
  212. unset(RFC2822_TIMESTAMP)
  213. else()
  214. message(WARNING "DEB Generator: Can't find git and/or gzip and/or date in your path. DEB packages will be missing changelog.Debian.gz.")
  215. endif()
  216. endif()
  217. # Enable packaging module
  218. include(CPack)
  219. # This setting defers the rpath generation to be set at install time, allowing
  220. # us to run the apps from the build dir without installing (come install time
  221. # the binary is re-linked with an rpath matching that of the install prefix)
  222. set(SKIP_BUILD_RPATH TRUE)
  223. set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
  224. # Use the link path for the rpath
  225. set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  226. file(GLOB_RECURSE SWCltSrc [LIST_DIRECTORIES false]
  227. ${CMAKE_CURRENT_LIST_DIR}/src/*.c
  228. )
  229. file(GLOB_RECURSE SWCltInc [LIST_DIRECTORIES false]
  230. ${CMAKE_CURRENT_LIST_DIR}/inc/*.h
  231. )
  232. if (KS_PLAT_WIN)
  233. source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${SWCltSrc})
  234. source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${SWCltInc})
  235. endif()
  236. add_library(signalwire_client SHARED ${SWCltSrc} ${SWCltInc})
  237. set_target_properties(signalwire_client PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR})
  238. set_target_properties(signalwire_client PROPERTIES DEBUG_POSTFIX "")
  239. # Define our exports symbol to key any definitions to toggle the visibility type
  240. set_target_properties(signalwire_client PROPERTIES DEFINE_SYMBOL SWCLT_EXPORTS)
  241. # Setup blade core definnitions
  242. target_compile_definitions(signalwire_client PUBLIC
  243. -DSWCLT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
  244. -DSWCLT_VERSION_MINOR=${PROJECT_VERSION_MINOR}
  245. -DSWCLT_VERSION_REVISION=${PROJECT_VERSION_PATCH}
  246. )
  247. if (KS_PLAT_WIN)
  248. target_compile_definitions(signalwire_client PUBLIC)
  249. endif()
  250. if (SWCLT_DEBUG_JSON)
  251. target_compile_definitions(signalwire_client PUBLIC
  252. -DSWCLT_DEBUG_JSON=1
  253. )
  254. endif()
  255. # Copy our ca dir to the binary dir so we can run these things in the build env
  256. if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/ca)
  257. file(COPY ${CMAKE_CURRENT_LIST_DIR}/ca DESTINATION ${CMAKE_BINARY_DIR})
  258. endif()
  259. # Setup ks core dependent linkages
  260. if (NOT WIN32)
  261. target_link_libraries(signalwire_client PUBLIC ks)
  262. else()
  263. target_link_libraries(
  264. signalwire_client PUBLIC
  265. ${LIBKS_LIBRARY_PATH}/ks.lib
  266. OpenSSL::SSL
  267. OpenSSL::Crypto
  268. )
  269. endif()
  270. # Include key paths
  271. target_include_directories(
  272. signalwire_client PUBLIC
  273. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/libks>
  274. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/inc>
  275. )
  276. if (WIN32)
  277. # Include key paths
  278. target_include_directories(
  279. signalwire_client PUBLIC
  280. $<BUILD_INTERFACE:;${LIBKS_INCLUDE_DIRS}/src/include>
  281. $<BUILD_INTERFACE:;${OPENSSL_INCLUDE_DIR}>
  282. )
  283. endif()
  284. target_include_directories(
  285. signalwire_client PRIVATE
  286. )
  287. # Include the bin dir for config discovery
  288. set(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
  289. if (NOT KS_PLAT_WIN)
  290. # Set install targets
  291. install(TARGETS signalwire_client COMPONENT "runtime" EXPORT SignalWireClientConfig DESTINATION lib)
  292. install(DIRECTORY inc/signalwire-client-c COMPONENT "runtime" DESTINATION include PATTERN internal EXCLUDE)
  293. # Set path for pkg-config based on ARCH and distro type
  294. if("${CMAKE_OS_NAME}" STREQUAL "Centos") # pkg-config --variable pc_path pkg-config
  295. if(${CPACK_SYSTEM_NAME} MATCHES "x86_64")
  296. install(FILES ${PROJECT_BINARY_DIR}/signalwire_client.pc COMPONENT "runtime" DESTINATION lib64/pkgconfig)
  297. else()
  298. install(FILES ${PROJECT_BINARY_DIR}/signalwire_client.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  299. endif()
  300. else()
  301. install(FILES ${PROJECT_BINARY_DIR}/signalwire_client.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  302. endif()
  303. install(EXPORT SignalWireClientConfig COMPONENT "runtime" DESTINATION include/signalwire-client-c/cmake)
  304. install(FILES ${PROJECT_BINARY_DIR}/copyright COMPONENT "runtime" DESTINATION share/doc/signalwire-client-c)
  305. # Set uninstall target
  306. if(NOT TARGET uninstall)
  307. configure_file(
  308. "${CMAKE_CURRENT_SOURCE_DIR}/uninstall.cmake.in"
  309. "${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
  310. IMMEDIATE @ONLY)
  311. add_custom_target(uninstall
  312. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake)
  313. endif()
  314. # Create a package config for registration with the system
  315. # prefix=@PC_PREFIX@
  316. # exec_prefix=@PC_EXEC_PREFIX@
  317. # libdir=@PC_LIB_DIR@
  318. # includedir=@PC_INCLUDE_DIR@
  319. # definitions=@PC_DEFINITIONS@
  320. # cmakedir=@PC_CMAKE_DIR@
  321. # Name: @PACKAGE_NAME@
  322. # Version: @PACKAGE_VERSION@
  323. set(PC_PREFIX ${CMAKE_INSTALL_PREFIX})
  324. get_property(PC_DEFINITIONS TARGET signalwire_client PROPERTY INTERFACE_COMPILE_DEFINITIONS)
  325. get_property(PC_DEFINITIONS TARGET ks PROPERTY INTERFACE_COMPILE_DEFINITIONS)
  326. set(PACKAGE_NAME signalwire_client)
  327. set(PACKAGE_VERSION ${PROJECT_VERSION})
  328. configure_file("${CMAKE_CURRENT_LIST_DIR}/signalwire_client.pc.in" "${PROJECT_BINARY_DIR}/signalwire_client.pc" @ONLY)
  329. endif()
  330. cotire(signalwire_client)
  331. add_subdirectory(swclt_bench)
  332. enable_testing()
  333. add_subdirectory(swclt_test)