CMakeLists.txt 13 KB

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