CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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.3.1 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. message("Detecting last git tag to generate a Debian complian changelog.")
  200. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0
  201. OUTPUT_VARIABLE LAST_TAG
  202. WORKING_DIRECTORY .
  203. OUTPUT_STRIP_TRAILING_WHITESPACE)
  204. # Commits count since last tag
  205. execute_process(COMMAND ${GIT_EXECUTABLE} rev-list ${LAST_TAG}.. --count
  206. OUTPUT_VARIABLE COMMITS_COUNT_SINCE_LAST_TAG
  207. WORKING_DIRECTORY .
  208. OUTPUT_STRIP_TRAILING_WHITESPACE)
  209. message("Found last git tag: ${LAST_TAG}")
  210. # Check if we have commits since last tag
  211. if (COMMITS_COUNT_SINCE_LAST_TAG STREQUAL "0")
  212. # if not, find second tag so we could have a changelog
  213. message("No commits since git tag '${LAST_TAG}' to generate a changelog, looking for a previous tag")
  214. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 ${LAST_TAG}^
  215. OUTPUT_VARIABLE LAST_TAG
  216. WORKING_DIRECTORY .
  217. OUTPUT_STRIP_TRAILING_WHITESPACE)
  218. endif()
  219. message("Generating a changelog using commits since git tag: ${LAST_TAG}")
  220. execute_process(COMMAND ${GIT_EXECUTABLE} log --no-merges --pretty=format:"%n [%an]%n * %s" ${LAST_TAG}..HEAD
  221. OUTPUT_VARIABLE CHANGELOG
  222. WORKING_DIRECTORY .
  223. OUTPUT_STRIP_TRAILING_WHITESPACE)
  224. string(REPLACE "\"" "" CHANGELOG "${CHANGELOG}")
  225. # Create changelog
  226. file(WRITE changelog.Debian ${CHANGELOG_HEADER}\n${CHANGELOG}\n\n${CHANGELOG_FOOTER})
  227. execute_process(COMMAND ${GZIP_CMD} -f -9 -n changelog.Debian)
  228. # Install changelog
  229. install(FILES "${CMAKE_BINARY_DIR}/changelog.Debian.gz" COMPONENT "runtime" DESTINATION "share/doc/${CPACK_DEBIAN_PACKAGE_NAME}")
  230. # Delete changelog related variables
  231. unset(CHANGELOG_HEADER)
  232. unset(CHANGELOG_FOOTER)
  233. unset(RFC2822_TIMESTAMP)
  234. else()
  235. 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.")
  236. endif()
  237. endif()
  238. # Enable packaging module
  239. include(CPack)
  240. # This setting defers the rpath generation to be set at install time, allowing
  241. # us to run the apps from the build dir without installing (come install time
  242. # the binary is re-linked with an rpath matching that of the install prefix)
  243. set(SKIP_BUILD_RPATH TRUE)
  244. set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
  245. # Use the link path for the rpath
  246. set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
  247. file(GLOB_RECURSE SWCltSrc [LIST_DIRECTORIES false]
  248. ${CMAKE_CURRENT_LIST_DIR}/src/*.c
  249. )
  250. file(GLOB_RECURSE SWCltInc [LIST_DIRECTORIES false]
  251. ${CMAKE_CURRENT_LIST_DIR}/inc/*.h
  252. )
  253. if (KS_PLAT_WIN)
  254. source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${SWCltSrc})
  255. source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${SWCltInc})
  256. endif()
  257. add_library(signalwire_client SHARED ${SWCltSrc} ${SWCltInc})
  258. set_target_properties(signalwire_client PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR})
  259. set_target_properties(signalwire_client PROPERTIES DEBUG_POSTFIX "")
  260. # Define our exports symbol to key any definitions to toggle the visibility type
  261. set_target_properties(signalwire_client PROPERTIES DEFINE_SYMBOL SWCLT_EXPORTS)
  262. # Setup blade core definnitions
  263. target_compile_definitions(signalwire_client PUBLIC
  264. -DSWCLT_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
  265. -DSWCLT_VERSION_MINOR=${PROJECT_VERSION_MINOR}
  266. -DSWCLT_VERSION_REVISION=${PROJECT_VERSION_PATCH}
  267. )
  268. if (KS_PLAT_WIN)
  269. target_compile_definitions(signalwire_client PUBLIC)
  270. endif()
  271. if (SWCLT_DEBUG_JSON)
  272. target_compile_definitions(signalwire_client PUBLIC
  273. -DSWCLT_DEBUG_JSON=1
  274. )
  275. endif()
  276. # Copy our ca dir to the binary dir so we can run these things in the build env
  277. if (EXISTS ${CMAKE_CURRENT_LIST_DIR}/ca)
  278. file(COPY ${CMAKE_CURRENT_LIST_DIR}/ca DESTINATION ${CMAKE_BINARY_DIR})
  279. endif()
  280. # Setup ks core dependent linkages
  281. if (NOT WIN32)
  282. target_link_libraries(signalwire_client PUBLIC ks)
  283. else()
  284. target_link_libraries(
  285. signalwire_client PUBLIC
  286. ${LIBKS_LIBRARY_PATH}/ks.lib
  287. OpenSSL::SSL
  288. OpenSSL::Crypto
  289. )
  290. endif()
  291. # Include key paths
  292. target_include_directories(
  293. signalwire_client PUBLIC
  294. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/libks>
  295. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/inc>
  296. )
  297. if (WIN32)
  298. # Include key paths
  299. target_include_directories(
  300. signalwire_client PUBLIC
  301. $<BUILD_INTERFACE:;${LIBKS_INCLUDE_DIRS}/src/include>
  302. $<BUILD_INTERFACE:;${OPENSSL_INCLUDE_DIR}>
  303. )
  304. endif()
  305. target_include_directories(
  306. signalwire_client PRIVATE
  307. )
  308. # Include the bin dir for config discovery
  309. set(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
  310. if (NOT KS_PLAT_WIN)
  311. # Set install targets
  312. install(TARGETS signalwire_client COMPONENT "runtime" EXPORT SignalWireClientConfig DESTINATION lib)
  313. install(DIRECTORY inc/signalwire-client-c COMPONENT "runtime" DESTINATION include PATTERN internal EXCLUDE)
  314. # Set path for pkg-config based on ARCH and distro type
  315. if("${CMAKE_OS_NAME}" STREQUAL "Centos") # pkg-config --variable pc_path pkg-config
  316. if(${CPACK_SYSTEM_NAME} MATCHES "x86_64")
  317. install(FILES ${PROJECT_BINARY_DIR}/signalwire_client.pc COMPONENT "runtime" DESTINATION lib64/pkgconfig)
  318. else()
  319. install(FILES ${PROJECT_BINARY_DIR}/signalwire_client.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  320. endif()
  321. else()
  322. install(FILES ${PROJECT_BINARY_DIR}/signalwire_client.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  323. endif()
  324. install(EXPORT SignalWireClientConfig COMPONENT "runtime" DESTINATION include/signalwire-client-c/cmake)
  325. install(FILES ${PROJECT_BINARY_DIR}/copyright COMPONENT "runtime" DESTINATION share/doc/signalwire-client-c)
  326. # Set uninstall target
  327. if(NOT TARGET uninstall)
  328. configure_file(
  329. "${CMAKE_CURRENT_SOURCE_DIR}/uninstall.cmake.in"
  330. "${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
  331. IMMEDIATE @ONLY)
  332. add_custom_target(uninstall
  333. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake)
  334. endif()
  335. # Create a package config for registration with the system
  336. # prefix=@PC_PREFIX@
  337. # exec_prefix=@PC_EXEC_PREFIX@
  338. # libdir=@PC_LIB_DIR@
  339. # includedir=@PC_INCLUDE_DIR@
  340. # definitions=@PC_DEFINITIONS@
  341. # cmakedir=@PC_CMAKE_DIR@
  342. # Name: @PACKAGE_NAME@
  343. # Version: @PACKAGE_VERSION@
  344. set(PC_PREFIX ${CMAKE_INSTALL_PREFIX})
  345. get_property(PC_DEFINITIONS TARGET signalwire_client PROPERTY INTERFACE_COMPILE_DEFINITIONS)
  346. get_property(PC_DEFINITIONS TARGET ks PROPERTY INTERFACE_COMPILE_DEFINITIONS)
  347. set(PACKAGE_NAME signalwire_client)
  348. set(PACKAGE_VERSION ${PROJECT_VERSION})
  349. configure_file("${CMAKE_CURRENT_LIST_DIR}/signalwire_client.pc.in" "${PROJECT_BINARY_DIR}/signalwire_client.pc" @ONLY)
  350. endif()
  351. cotire(signalwire_client)
  352. add_subdirectory(swclt_bench)
  353. enable_testing()
  354. add_subdirectory(swclt_test)