2
0

CMakeLists.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. cmake_minimum_required(VERSION 3.7.2)
  2. option(KS_DEBUG_MUTEX "Debug mutexes, only works on linux for now" OFF)
  3. option(KS_DEBUG_SPINLOCK "Debug spinlocks" OFF)
  4. option(KS_DEBUG_POOL "Track pool allocations and provide hooks for rendering them" OFF)
  5. option(KS_DEBUG_HANDLE "Track pool allocations and provide hooks for rendering them" OFF)
  6. option(KS_STATIC "Build ks statically" OFF)
  7. option(WITH_KS_TEST "Enable KsTest c++ harness" OFF)
  8. option(WITH_LIBBACKTRACE "Enables linking to libbacktrace as an external project, used in abort areas in libks" OFF)
  9. # Must include cotire before anything else for auto pch setup
  10. #include(cmake/cotire.cmake)
  11. # Load our common utility api and setup the platfomrm and build
  12. include(cmake/ksutil.cmake)
  13. ksutil_setup_platform()
  14. # Find stuff we need for packaging on UNIX
  15. if(KS_PLAT_LIN)
  16. find_package(Git)
  17. find_program(GZIP_CMD gzip)
  18. find_program(DATE_CMD date)
  19. endif()
  20. # Fixes build on older gcc, Debian Jessie
  21. if(KS_PLAT_LIN)
  22. set(CMAKE_C_STANDARD 99)
  23. endif()
  24. if (KS_PLAT_WIN OR WITH_KS_TEST)
  25. # Setup hunter gate to auto grab our 3rd party dependencies
  26. # (may be included by our super project so check HUNTER_WIKI)
  27. if (NOT HUNTER_WIKI)
  28. include(cmake/HunterGate.cmake)
  29. HunterGate(
  30. URL "https://github.com/ruslo/hunter/archive/v0.20.39.tar.gz"
  31. SHA1 "b49c4b58e17c1473e822f8b21fcde92fee25791d"
  32. )
  33. endif()
  34. endif()
  35. # Declare our project, libks2
  36. project(LibKS2 VERSION 2.0.0 LANGUAGES C CXX)
  37. message("LibKS2 Version ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
  38. # Set package version
  39. set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
  40. set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
  41. set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
  42. # Fetch Catch2 testing framework
  43. if (WITH_KS_KSTEST)
  44. hunter_add_package(Catch)
  45. find_package(Catch2 CONFIG REQUIRED)
  46. endif()
  47. # This setting defers the rpath generation to be set at install time, allowing
  48. # us to run the apps from the build dir without installing (come install time
  49. # the binary is re-linked with an rpath matching that of the install prefix)
  50. set(SKIP_BUILD_RPATH TRUE)
  51. set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
  52. # Now detect a bunch of platform stuff
  53. include(CheckFunctionExists)
  54. include(CheckLibraryExists)
  55. include(CheckIncludeFile)
  56. include(TestBigEndian)
  57. # Set package name
  58. string(TOLOWER "${CMAKE_PROJECT_NAME}" PACKAGE_NAME)
  59. # Set package version
  60. set(PACKAGE_VERSION ${PROJECT_VERSION})
  61. # Set package contact
  62. set(CPACK_PACKAGE_CONTACT "support@signalwire.com")
  63. # Detect architecture
  64. if(NOT CPACK_SYSTEM_NAME)
  65. set(CPACK_SYSTEM_NAME ${CMAKE_SYSTEM_PROCESSOR})
  66. # sanity check
  67. if(CPACK_SYSTEM_NAME STREQUAL x86_64)
  68. set(CPACK_SYSTEM_NAME amd64)
  69. endif()
  70. endif()
  71. # Get UNIX type
  72. if(KS_PLAT_LIN)
  73. find_file(DEBIAN_FOUND debian_version debconf.conf
  74. PATHS /etc
  75. )
  76. find_file(CENTOS_FOUND centos-release
  77. PATHS /etc
  78. )
  79. if(DEBIAN_FOUND)
  80. set(CMAKE_OS_NAME "Debian" CACHE STRING "Operating system name" FORCE)
  81. endif()
  82. if(CENTOS_FOUND)
  83. set(CMAKE_OS_NAME "Centos" CACHE STRING "Operating system name" FORCE)
  84. endif()
  85. endif()
  86. # Centos packaging
  87. if("${CMAKE_OS_NAME}" STREQUAL "Centos")
  88. # Enable component install
  89. set(CPACK_RPM_COMPONENT_INSTALL ON)
  90. # Find stuff we need for packaging on Centos
  91. find_program(ARCH_CMD arch)
  92. find_program(RPM_CMD rpm)
  93. # Set package architecture
  94. if(ARCH_CMD)
  95. execute_process(COMMAND ${ARCH_CMD} OUTPUT_VARIABLE CENTOS_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
  96. set(CPACK_RPM_RUNTIME_PACKAGE_ARCHITECTURE ${CENTOS_ARCH})
  97. set(CPACK_SYSTEM_NAME ${CENTOS_ARCH})
  98. unset(CENTOS_ARCH)
  99. endif()
  100. # Get RPM DIST tag, rpm --eval '%{dist}'
  101. if(RPM_CMD)
  102. execute_process(COMMAND ${RPM_CMD} --eval %{dist}
  103. OUTPUT_VARIABLE DIST_TAG
  104. OUTPUT_STRIP_TRAILING_WHITESPACE
  105. ERROR_STRIP_TRAILING_WHITESPACE)
  106. endif()
  107. # https://gitlab.kitware.com/cmake/cmake/issues/16517
  108. set(CPACK_COMPONENTS_ALL runtime)
  109. # Set package type to build
  110. set(CPACK_GENERATOR RPM)
  111. # Set RPM package name
  112. set(CPACK_RPM_RUNTIME_PACKAGE_NAME "libks2")
  113. # Set RPM package dependencies
  114. set(CPACK_RPM_RUNTIME_PACKAGE_REQUIRES "libuuid-devel")
  115. # Set version release from environment variable
  116. if (NOT "$ENV{PACKAGE_RELEASE}" STREQUAL "")
  117. set(CPACK_RPM_PACKAGE_RELEASE "$ENV{PACKAGE_RELEASE}")
  118. else()
  119. if(DATE_CMD)
  120. execute_process(COMMAND ${DATE_CMD} +%Y%m%d OUTPUT_VARIABLE DATE_YMD)
  121. set(CPACK_RPM_PACKAGE_RELEASE ${DATE_YMD})
  122. else()
  123. set(CPACK_RPM_PACKAGE_RELEASE "1")
  124. endif()
  125. endif()
  126. # Set RPM version
  127. set(CPACK_RPM_PACKAGE_VERSION ${PROJECT_VERSION})
  128. # Set OS DIST tag
  129. set(CPACK_RPM_PACKAGE_RELEASE_DIST ${DIST_TAG})
  130. # Set RPM filename
  131. set(CPACK_RPM_RUNTIME_FILE_NAME "${CPACK_RPM_RUNTIME_PACKAGE_NAME}-${PROJECT_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}${DIST_TAG}.${CENTOS_ARCH}rpm")
  132. # Set license
  133. set(CPACK_RPM_PACKAGE_LICENSE "MIT")
  134. # Set RPM group
  135. set(CPACK_RPM_PACKAGE_GROUP "Development/Libraries/C and C++")
  136. # Set RPM vendor
  137. set(CPACK_RPM_PACKAGE_VENDOR "SignalWire")
  138. # Set RPM URL
  139. set(CPACK_RPM_RUNTIME_PACKAGE_URL "https://signalwire.com/")
  140. # Set RPM Description
  141. set(CPACK_RPM_PACKAGE_DESCRIPTION "SignalWire LibKitchenSink and Headers")
  142. # Set RPM package summary
  143. set(CPACK_RPM_PACKAGE_SUMMARY "SignalWire's LibKitchenSink")
  144. # Set auto dependency detection
  145. set(CPACK_RPM_PACKAGE_AUTOREQPROV "yes")
  146. # Create changelog
  147. string(TIMESTAMP NOW "%a %b %d %Y" UTC)
  148. FILE(WRITE ${CMAKE_CURRENT_LIST_DIR}/changelog "* ${NOW} ${CPACK_RPM_PACKAGE_VENDOR} <${CPACK_PACKAGE_CONTACT}> ${PROJECT_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}${DIST_TAG}\n")
  149. FILE(APPEND ${CMAKE_CURRENT_LIST_DIR}/changelog "- autogenerated RPM package\n")
  150. unset(NOW)
  151. set(CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_LIST_DIR}/changelog")
  152. endif()
  153. # Debian packaging
  154. if("${CMAKE_OS_NAME}" STREQUAL "Debian")
  155. # Find stuff we need for packaging on Debian
  156. find_program(DPKG_CMD dpkg)
  157. find_program(LSB_RELEASE_CMD lsb_release)
  158. # Get distro codename
  159. if(NOT LSB_RELEASE_CMD)
  160. message(WARNING "DEB Generator: Can't find lsb_release in your path. Setting DISTRO_CODENAME to unknown.")
  161. set(DISTRO_CODENAME unknown)
  162. else()
  163. execute_process(COMMAND ${LSB_RELEASE_CMD} -cs
  164. OUTPUT_VARIABLE DISTRO_CODENAME
  165. OUTPUT_STRIP_TRAILING_WHITESPACE)
  166. endif()
  167. # https://gitlab.kitware.com/cmake/cmake/issues/16517
  168. set(CPACK_COMPONENTS_ALL runtime)
  169. set(CPACK_DEB_COMPONENT_INSTALL "ON")
  170. # Set package name
  171. set(CPACK_DEBIAN_PACKAGE_NAME "libks2")
  172. # Abuse Cmake runtime component name
  173. set(CPACK_DEBIAN_RUNTIME_PACKAGE_NAME ${CPACK_DEBIAN_PACKAGE_NAME})
  174. # Set package type to build
  175. set(CPACK_GENERATOR DEB)
  176. # Set package maintainer
  177. set(CPACK_DEBIAN_PACKAGE_MAINTAINER ${CPACK_PACKAGE_CONTACT})
  178. # Manual package dependencies
  179. set(CPACK_DEBIAN_PACKAGE_DEPENDS "")
  180. # Detect package dependencies
  181. set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
  182. set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS ON)
  183. set(CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS_POLICY ">=")
  184. # Set package description
  185. set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "Foundational support for signalwire C products")
  186. # Set package section
  187. set(CPACK_DEBIAN_PACKAGE_SECTION "devel")
  188. # Set package priority
  189. set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
  190. # Set package architecture
  191. if(DPKG_CMD)
  192. execute_process(COMMAND ${DPKG_CMD} --print-architecture OUTPUT_VARIABLE DEB_ARCH OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
  193. set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${DEB_ARCH})
  194. set(CPACK_SYSTEM_NAME ${DEB_ARCH})
  195. unset(DEB_ARCH)
  196. endif()
  197. # Enforce Debian policy permission rules
  198. set(CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION ON)
  199. # Set a Debian compliant changelog footer
  200. if(DATE_CMD)
  201. execute_process(COMMAND ${DATE_CMD} -R OUTPUT_VARIABLE RFC2822_TIMESTAMP)
  202. # Two spaces between maintainer and timestamp is NOT a mistake
  203. set(CHANGELOG_FOOTER " -- ${CPACK_DEBIAN_PACKAGE_MAINTAINER} ${RFC2822_TIMESTAMP}")
  204. endif()
  205. # Set version release from environment variable
  206. if (NOT "$ENV{PACKAGE_RELEASE}" STREQUAL "")
  207. set(CPACK_DEBIAN_PACKAGE_RELEASE "$ENV{PACKAGE_RELEASE}")
  208. else()
  209. if(DATE_CMD)
  210. execute_process(COMMAND ${DATE_CMD} +%Y%m%d OUTPUT_VARIABLE DATE_YMD)
  211. set(CPACK_DEBIAN_PACKAGE_RELEASE ${DATE_YMD})
  212. else()
  213. set(CPACK_DEBIAN_PACKAGE_RELEASE "1")
  214. endif()
  215. endif()
  216. # Set package version
  217. set(CPACK_DEBIAN_PACKAGE_VERSION ${PROJECT_VERSION}-${CPACK_DEBIAN_PACKAGE_RELEASE}~${DISTRO_CODENAME})
  218. # Set debian file name format
  219. set(CPACK_DEBIAN_FILE_NAME "${PACKAGE_NAME}_${PROJECT_VERSION}-${CPACK_DEBIAN_PACKAGE_RELEASE}_${DISTRO_CODENAME}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.deb")
  220. # Set a Debian compliant changelog header
  221. set(CHANGELOG_HEADER "${PACKAGE_NAME} (${CPACK_DEBIAN_PACKAGE_VERSION}) ${DISTRO_CODENAME}\; urgency=${CPACK_DEBIAN_PACKAGE_PRIORITY}")
  222. # Generate a Debian compliant changelog
  223. if(GIT_FOUND AND GZIP_CMD AND DATE_CMD)
  224. # Get git log info
  225. message("Detecting last git tag to generate a Debian complian changelog.")
  226. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0
  227. OUTPUT_VARIABLE LAST_TAG
  228. WORKING_DIRECTORY .
  229. OUTPUT_STRIP_TRAILING_WHITESPACE)
  230. # Commits count since last tag
  231. execute_process(COMMAND ${GIT_EXECUTABLE} rev-list ${LAST_TAG}.. --count
  232. OUTPUT_VARIABLE COMMITS_COUNT_SINCE_LAST_TAG
  233. WORKING_DIRECTORY .
  234. OUTPUT_STRIP_TRAILING_WHITESPACE)
  235. message("Found last git tag: ${LAST_TAG}")
  236. # Check if we have commits since last tag
  237. if (COMMITS_COUNT_SINCE_LAST_TAG STREQUAL "0")
  238. # if not, find second tag so we could have a changelog
  239. message("No commits since git tag '${LAST_TAG}' to generate a changelog, looking for a previous tag")
  240. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 ${LAST_TAG}^
  241. OUTPUT_VARIABLE LAST_TAG
  242. WORKING_DIRECTORY .
  243. OUTPUT_STRIP_TRAILING_WHITESPACE)
  244. endif()
  245. message("Generating a changelog using commits since git tag: ${LAST_TAG}")
  246. execute_process(COMMAND ${GIT_EXECUTABLE} log --no-merges --pretty=format:"%n [%an]%n * %s" ${LAST_TAG}..HEAD
  247. OUTPUT_VARIABLE CHANGELOG
  248. WORKING_DIRECTORY .
  249. OUTPUT_STRIP_TRAILING_WHITESPACE)
  250. string(REPLACE "\"" "" CHANGELOG ${CHANGELOG})
  251. # Create changelog
  252. file(WRITE changelog.Debian ${CHANGELOG_HEADER}\n${CHANGELOG}\n\n${CHANGELOG_FOOTER})
  253. execute_process(COMMAND ${GZIP_CMD} -f -9 -n changelog.Debian)
  254. # Install changelog
  255. install(FILES "${CMAKE_BINARY_DIR}/changelog.Debian.gz" COMPONENT "runtime" DESTINATION "share/doc/${CPACK_DEBIAN_PACKAGE_NAME}")
  256. # Delete changelog related variables
  257. unset(CHANGELOG_HEADER)
  258. unset(CHANGELOG_FOOTER)
  259. unset(RFC2822_TIMESTAMP)
  260. else()
  261. 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.")
  262. endif()
  263. endif()
  264. # Enable packaging module
  265. include(CPack)
  266. # Include our libks2 cmake utils
  267. include(cmake/ksutil.cmake)
  268. # Load our source and header files with a glob
  269. file(GLOB_RECURSE KsDeps [LIST_DIRECTORIES false]
  270. ${CMAKE_CURRENT_LIST_DIR}/src/*.c
  271. ${CMAKE_CURRENT_LIST_DIR}/src/include/libks/*.h
  272. ${CMAKE_CURRENT_LIST_DIR}/src/include/crypt/*.c
  273. ${CMAKE_CURRENT_LIST_DIR}/src/include/crypt/*.h
  274. )
  275. if (KS_PLAT_WIN)
  276. source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${KsDeps})
  277. endif()
  278. if (KS_STATIC)
  279. add_library(ks STATIC ${KsDeps})
  280. target_compile_definitions(ks PUBLIC -DKS_DECLARE_STATIC=1)
  281. set_target_properties(ks PROPERTIES VERSION ${PROJECT_VERSION})
  282. else()
  283. add_library(ks SHARED ${KsDeps})
  284. set_target_properties(ks PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR})
  285. endif()
  286. set_target_properties(ks PROPERTIES DEBUG_POSTFIX "")
  287. # Set the output library name
  288. set_target_properties(ks PROPERTIES OUTPUT_NAME "ks2")
  289. if (KS_PLAT_LIN AND "${CMAKE_BUILD_TYPE}" STREQUAL "Sanitize")
  290. target_link_libraries(ks PUBLIC asan ubsan)
  291. endif()
  292. # Use libbacktrace if requested
  293. if (NOT KS_PLAT_WIN AND WITH_LIBBACKTRACE)
  294. include(cmake/AddLibBacktrace.cmake)
  295. target_link_libraries(ks PRIVATE LibBacktrace)
  296. target_compile_definitions(ks PUBLIC -DHAVE_LIBBACKTRACE=1)
  297. endif()
  298. if (NOT KS_PLAT_WIN)
  299. # Now auto detect our dependencies
  300. ksutil_target_check_library_exists_hdr(ks PUBLIC rt clock_gettime time.h HAVE_CLOCK_GETTIME)
  301. ksutil_target_check_library_exists_hdr(ks PUBLIC rt clock_getres time.h HAVE_CLOCK_GETRES)
  302. ksutil_target_check_library_exists_hdr(ks PUBLIC rt clock_nanosleep time.h HAVE_CLOCK_NANOSLEEP)
  303. ksutil_target_check_library_exists_hdr(ks PUBLIC rt strftime time.h HAVE_STRFTIME)
  304. ksutil_target_check_library_exists(ks PUBLIC c sched_setscheduler HAVE_SCHED_SETSCHEDULER)
  305. ksutil_target_check_library_exists(ks PUBLIC c malloc HAVE_MALLOC)
  306. ksutil_target_check_library_exists(ks PUBLIC c usleep HAVE_USLEEP)
  307. ksutil_target_check_function_exists(ks PUBLIC pthread_attr_setschedparam HAVE_PTHREAD_ATTR_SETSCHEDPARAM)
  308. ksutil_target_check_function_exists(ks PUBLIC memmem HAVE_MEMMEM)
  309. ksutil_target_check_include_file(ks PUBLIC stdlib.h HAVE_STDLIB_H)
  310. ksutil_target_check_include_file(ks PUBLIC sys/types.h HAVE_SYS_TYPES_H)
  311. ksutil_target_check_include_file(ks PUBLIC sys/stat.h HAVE_SYS_STAT_H)
  312. ksutil_target_check_include_file(ks PUBLIC string.h HAVE_STRING_H)
  313. ksutil_target_check_include_file(ks PUBLIC unistd.h HAVE_UNISTD_H)
  314. ksutil_target_check_include_file(ks PUBLIC memory.h HAVE_MEMORY_H)
  315. ksutil_target_check_include_file(ks PUBLIC strings.h HAVE_STRINGS_H)
  316. ksutil_target_check_include_file(ks PUBLIC inttypes.h HAVE_INTTYPES_H)
  317. ksutil_target_check_include_file(ks PUBLIC stdint.h HAVE_STDINT_H)
  318. ksutil_target_check_include_file(ks PUBLIC dlfcn.h HAVE_DLFCN_H)
  319. ksutil_target_check_include_file(ks PUBLIC sched.h HAVE_SCHED_H)
  320. ksutil_target_check_include_file(ks PUBLIC byteswap.h HAVE_BYTESWAP_H)
  321. ksutil_target_check_include_file(ks PUBLIC dirent.h HAVE_DIRENT_H)
  322. endif()
  323. # Check endianess for our swap macros
  324. TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
  325. # Need libm on linux
  326. if (KS_PLAT_LIN)
  327. include(cmake/FindLibM.cmake)
  328. endif()
  329. if (KS_PLAT_WIN)
  330. # We use RpcString on windows
  331. target_link_libraries(ks PUBLIC Rpcrt4.lib)
  332. else()
  333. # Locate libuuid on non-win platforms
  334. include(cmake/FindUUID.cmake)
  335. endif()
  336. # Locate ssl
  337. if (KS_PLAT_MAC)
  338. set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
  339. endif()
  340. include(FindOpenSSL)
  341. find_package(OpenSSL REQUIRED Crypto SSL)
  342. if (NOT KS_PLAT_WIN)
  343. # Use pthreads
  344. set(THREADS_PREFER_PTHREAD_FLAG ON)
  345. find_package(Threads REQUIRED)
  346. endif()
  347. set_target_properties(ks PROPERTIES DEFINE_SYMBOL KS_EXPORTS)
  348. # Setup ks core definitions, these are private defintions
  349. # meaning they won't be exported to public linkers
  350. target_compile_definitions(ks PRIVATE
  351. -DPACKAGE_NAME="libks2"
  352. -DPACKAGE_TARNAME="libks2"
  353. -DPACKAGE_VERSION="${PROJECT_VERSION}"
  354. -DPACKAGE_STRING="libks2 ${PROJECT_VERSION}"
  355. -DPACKAGE_BUGREPORT="bugs@freeswitch.org"
  356. -DPACKAGE_URL=""
  357. -DVERSION="${PROJECT_VERSION}"
  358. -D_GNU_SOURCE=1
  359. -DCJSON_HIDE_SYMBOLS=1
  360. )
  361. # Setup our public facing definitions
  362. target_compile_definitions(ks PUBLIC
  363. -DSTDC_HEADERS=1
  364. -DTIME_WITH_SYS_TIME=1
  365. -DRETSIGTYPE=void
  366. -DHAVE_LIBCRYPTO=1
  367. -DHAVE_LIBSSL=1
  368. -D_REENTRANT=1
  369. ${PublicHaves}
  370. )
  371. # Handle debug mode
  372. if (KS_DEBUG_HANDLE)
  373. target_compile_definitions(ks PUBLIC
  374. -DKS_DEBUG_HANDLE=1
  375. )
  376. endif()
  377. # JSON debug mode
  378. if (KS_DEBUG_JSON)
  379. target_compile_definitions(ks PUBLIC
  380. -DKS_DEBUG_JSON=1
  381. )
  382. endif()
  383. # Pool debug mode
  384. if (KS_DEBUG_POOL)
  385. target_compile_definitions(ks PUBLIC
  386. -DKS_DEBUG_POOL=1
  387. )
  388. endif()
  389. # Spinlock debug mode
  390. if (KS_DEBUG_SPINLOCK)
  391. target_compile_definitions(ks PUBLIC
  392. -DKS_DEBUG_SPINLOCK=1
  393. )
  394. endif()
  395. # Mutex debug mode
  396. if (KS_DEBUG_MUTEX)
  397. target_compile_definitions(ks PUBLIC
  398. -DKS_DEBUG_MUTEX=1
  399. )
  400. endif()
  401. if (NOT KS_PLAT_WIN)
  402. # All symbols are hidden by default, unless exposed explicitly
  403. include(CheckCXXCompilerFlag)
  404. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  405. if (COMPILER_HAS_HIDDEN_VISIBILITY)
  406. message("Compiler supports visibility")
  407. set_target_properties(ks PROPERTIES CXX_VISIBILITY_PRESET hidden)
  408. set_target_properties(ks PROPERTIES C_VISIBILITY_PRESET hidden)
  409. target_compile_definitions(ks PRIVATE
  410. -DKS_API_VISIBILITY=1
  411. )
  412. else()
  413. message("Compiler does not support visibility")
  414. endif()
  415. endif()
  416. if (IS_BIG_ENDIAN)
  417. target_compile_definitions(ks PUBLIC
  418. -D__BYTE_ORDER=__BIG_ENDIAN
  419. )
  420. else()
  421. target_compile_definitions(ks PUBLIC
  422. -D__BYTE_ORDER=__LITTLE_ENDIAN
  423. )
  424. endif()
  425. # Setup ks core dependent linkages
  426. target_link_libraries(
  427. ks PUBLIC
  428. OpenSSL::SSL
  429. OpenSSL::Crypto
  430. )
  431. if (NOT KS_PLAT_WIN)
  432. target_link_libraries(
  433. ks PRIVATE
  434. LIBUUID::LIBUUID
  435. Threads::Threads
  436. )
  437. endif()
  438. if (KS_PLAT_LIN)
  439. target_link_libraries(ks PRIVATE LIBM::LIBM)
  440. target_link_libraries(ks PUBLIC rt atomic)
  441. endif()
  442. # Include key paths
  443. target_include_directories(
  444. ks PUBLIC
  445. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/libks> # Include both include paths in the install config
  446. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/> # so we don't break any includes in installed areas
  447. $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src/include> # Within the build tree however, encforce the root
  448. $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> # include path only
  449. )
  450. target_include_directories(
  451. ks PRIVATE
  452. ${OPENSSL_INCLUDE_DIR}
  453. )
  454. # Setup a useful export file for inclusion in other projects
  455. export(TARGETS ks
  456. NAMESPACE ${PROJECT_NAME}
  457. FILE LibKSConfig.cmake
  458. EXPORT_LINK_INTERFACE_LIBRARIES
  459. )
  460. if (NOT KS_PLAT_WIN)
  461. # Set install targets
  462. install(TARGETS ks COMPONENT "runtime" EXPORT LibKS2Config DESTINATION lib)
  463. install(DIRECTORY src/include/libks COMPONENT "runtime" DESTINATION include/libks2 PATTERN src/include/libks/internal EXCLUDE)
  464. # Set path for pkg-config based on ARCH and distro type
  465. if("${CMAKE_OS_NAME}" STREQUAL "Centos") # pkg-config --variable pc_path pkg-config
  466. if(${CPACK_SYSTEM_NAME} MATCHES "x86_64")
  467. install(FILES ${PROJECT_BINARY_DIR}/libks2.pc COMPONENT "runtime" DESTINATION lib64/pkgconfig)
  468. else()
  469. install(FILES ${PROJECT_BINARY_DIR}/libks2.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  470. endif()
  471. else()
  472. install(FILES ${PROJECT_BINARY_DIR}/libks2.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  473. endif()
  474. install(FILES cmake/ksutil.cmake COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  475. install(FILES cmake/FindPCRE.cmake COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  476. install(FILES cmake/AddLibBacktrace.cmake COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  477. install(FILES ${PROJECT_SOURCE_DIR}/copyright COMPONENT "runtime" DESTINATION share/doc/libks2)
  478. install(EXPORT LibKS2Config COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  479. # Set uninstall target
  480. if(NOT TARGET uninstall)
  481. configure_file(
  482. "${CMAKE_CURRENT_SOURCE_DIR}/uninstall.cmake.in"
  483. "${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
  484. IMMEDIATE @ONLY)
  485. add_custom_target(uninstall
  486. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake)
  487. endif()
  488. # Create a package config for registration with the system
  489. # prefix=@PC_PREFIX@
  490. # exec_prefix=@PC_EXEC_PREFIX@
  491. # libdir=@PC_LIB_DIR@
  492. # includedir=@PC_INCLUDE_DIR@
  493. # definitions=@PC_DEFINITIONS@
  494. # cmakedir=@PC_CMAKE_DIR@
  495. # Name: @PACKAGE_NAME@
  496. # Version: @PACKAGE_VERSION@
  497. set(PC_PREFIX ${CMAKE_INSTALL_PREFIX})
  498. set(PACKAGE_VERSION ${PROJECT_VERSION})
  499. get_property(PC_DEFINITIONS TARGET ks PROPERTY INTERFACE_COMPILE_DEFINITIONS)
  500. configure_file("${CMAKE_CURRENT_LIST_DIR}/libks.pc.in" "${PROJECT_BINARY_DIR}/libks2.pc" @ONLY)
  501. endif()
  502. #cotire(ks)
  503. # Add tests
  504. enable_testing()
  505. add_subdirectory(tests)
  506. if (WITH_KS_TEST)
  507. enable_testing()
  508. add_subdirectory(KSTest)
  509. endif()