2
0

CMakeLists.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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, libks
  36. project(LibKS VERSION 1.6.1 LANGUAGES C CXX)
  37. message("LibKS 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 "libks")
  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 "libks")
  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 libks 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. if (KS_PLAT_LIN AND "${CMAKE_BUILD_TYPE}" STREQUAL "Sanitize")
  288. target_link_libraries(ks PUBLIC asan ubsan)
  289. endif()
  290. # Use libbacktrace if requested
  291. if (NOT KS_PLAT_WIN AND WITH_LIBBACKTRACE)
  292. include(cmake/AddLibBacktrace.cmake)
  293. target_link_libraries(ks PRIVATE LibBacktrace)
  294. target_compile_definitions(ks PUBLIC -DHAVE_LIBBACKTRACE=1)
  295. endif()
  296. if (NOT KS_PLAT_WIN)
  297. # Now auto detect our dependencies
  298. ksutil_target_check_library_exists_hdr(ks PUBLIC rt clock_gettime time.h HAVE_CLOCK_GETTIME)
  299. ksutil_target_check_library_exists_hdr(ks PUBLIC rt clock_getres time.h HAVE_CLOCK_GETRES)
  300. ksutil_target_check_library_exists_hdr(ks PUBLIC rt clock_nanosleep time.h HAVE_CLOCK_NANOSLEEP)
  301. ksutil_target_check_library_exists_hdr(ks PUBLIC rt strftime time.h HAVE_STRFTIME)
  302. ksutil_target_check_library_exists(ks PUBLIC c sched_setscheduler HAVE_SCHED_SETSCHEDULER)
  303. ksutil_target_check_library_exists(ks PUBLIC c malloc HAVE_MALLOC)
  304. ksutil_target_check_library_exists(ks PUBLIC c usleep HAVE_USLEEP)
  305. ksutil_target_check_function_exists(ks PUBLIC pthread_setschedparam HAVE_PTHREAD_SETSCHEDPARAM)
  306. ksutil_target_check_function_exists(ks PUBLIC memmem HAVE_MEMMEM)
  307. ksutil_target_check_include_file(ks PUBLIC stdlib.h HAVE_STDLIB_H)
  308. ksutil_target_check_include_file(ks PUBLIC sys/types.h HAVE_SYS_TYPES_H)
  309. ksutil_target_check_include_file(ks PUBLIC sys/stat.h HAVE_SYS_STAT_H)
  310. ksutil_target_check_include_file(ks PUBLIC string.h HAVE_STRING_H)
  311. ksutil_target_check_include_file(ks PUBLIC unistd.h HAVE_UNISTD_H)
  312. ksutil_target_check_include_file(ks PUBLIC memory.h HAVE_MEMORY_H)
  313. ksutil_target_check_include_file(ks PUBLIC strings.h HAVE_STRINGS_H)
  314. ksutil_target_check_include_file(ks PUBLIC inttypes.h HAVE_INTTYPES_H)
  315. ksutil_target_check_include_file(ks PUBLIC stdint.h HAVE_STDINT_H)
  316. ksutil_target_check_include_file(ks PUBLIC dlfcn.h HAVE_DLFCN_H)
  317. ksutil_target_check_include_file(ks PUBLIC sched.h HAVE_SCHED_H)
  318. ksutil_target_check_include_file(ks PUBLIC byteswap.h HAVE_BYTESWAP_H)
  319. ksutil_target_check_include_file(ks PUBLIC dirent.h HAVE_DIRENT_H)
  320. endif()
  321. # Check endianess for our swap macros
  322. TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
  323. # Need libm on linux
  324. if (KS_PLAT_LIN)
  325. include(cmake/FindLibM.cmake)
  326. endif()
  327. if (KS_PLAT_WIN)
  328. # We use RpcString on windows
  329. target_link_libraries(ks PUBLIC Rpcrt4.lib)
  330. else()
  331. # Locate libuuid on non-win platforms
  332. include(cmake/FindUUID.cmake)
  333. endif()
  334. # Locate ssl
  335. if (KS_PLAT_MAC)
  336. set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
  337. endif()
  338. include(FindOpenSSL)
  339. find_package(OpenSSL REQUIRED Crypto SSL)
  340. if (NOT KS_PLAT_WIN)
  341. # Use pthreads
  342. set(THREADS_PREFER_PTHREAD_FLAG ON)
  343. find_package(Threads REQUIRED)
  344. endif()
  345. set_target_properties(ks PROPERTIES DEFINE_SYMBOL KS_EXPORTS)
  346. # Setup ks core definitions, these are private defintions
  347. # meaning they won't be exported to public linkers
  348. target_compile_definitions(ks PRIVATE
  349. -DPACKAGE_NAME="libks"
  350. -DPACKAGE_TARNAME="libks"
  351. -DPACKAGE_VERSION="${PROJECT_VERSION}"
  352. -DPACKAGE_STRING="libks ${PROJECT_VERSION}"
  353. -DPACKAGE_BUGREPORT="bugs@freeswitch.org"
  354. -DPACKAGE_URL=""
  355. -DVERSION="${PROJECT_VERSION}"
  356. -D_GNU_SOURCE=1
  357. -DCJSON_HIDE_SYMBOLS=1
  358. )
  359. # Setup our public facing definitions
  360. target_compile_definitions(ks PUBLIC
  361. -DSTDC_HEADERS=1
  362. -DTIME_WITH_SYS_TIME=1
  363. -DRETSIGTYPE=void
  364. -DHAVE_LIBCRYPTO=1
  365. -DHAVE_LIBSSL=1
  366. -D_REENTRANT=1
  367. ${PublicHaves}
  368. )
  369. # Handle debug mode
  370. if (KS_DEBUG_HANDLE)
  371. target_compile_definitions(ks PUBLIC
  372. -DKS_DEBUG_HANDLE=1
  373. )
  374. endif()
  375. # JSON debug mode
  376. if (KS_DEBUG_JSON)
  377. target_compile_definitions(ks PUBLIC
  378. -DKS_DEBUG_JSON=1
  379. )
  380. endif()
  381. # Pool debug mode
  382. if (KS_DEBUG_POOL)
  383. target_compile_definitions(ks PUBLIC
  384. -DKS_DEBUG_POOL=1
  385. )
  386. endif()
  387. # Spinlock debug mode
  388. if (KS_DEBUG_SPINLOCK)
  389. target_compile_definitions(ks PUBLIC
  390. -DKS_DEBUG_SPINLOCK=1
  391. )
  392. endif()
  393. # Mutex debug mode
  394. if (KS_DEBUG_MUTEX)
  395. target_compile_definitions(ks PUBLIC
  396. -DKS_DEBUG_MUTEX=1
  397. )
  398. endif()
  399. if (NOT KS_PLAT_WIN)
  400. # All symbols are hidden by default, unless exposed explicitly
  401. include(CheckCXXCompilerFlag)
  402. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  403. if (COMPILER_HAS_HIDDEN_VISIBILITY)
  404. message("Compiler supports visibility")
  405. set_target_properties(ks PROPERTIES CXX_VISIBILITY_PRESET hidden)
  406. set_target_properties(ks PROPERTIES C_VISIBILITY_PRESET hidden)
  407. target_compile_definitions(ks PRIVATE
  408. -DKS_API_VISIBILITY=1
  409. )
  410. else()
  411. message("Compiler does not support visibility")
  412. endif()
  413. endif()
  414. if (IS_BIG_ENDIAN)
  415. target_compile_definitions(ks PUBLIC
  416. -D__BYTE_ORDER=__BIG_ENDIAN
  417. )
  418. else()
  419. target_compile_definitions(ks PUBLIC
  420. -D__BYTE_ORDER=__LITTLE_ENDIAN
  421. )
  422. endif()
  423. # Setup ks core dependent linkages
  424. target_link_libraries(
  425. ks PUBLIC
  426. OpenSSL::SSL
  427. OpenSSL::Crypto
  428. )
  429. if (NOT KS_PLAT_WIN)
  430. target_link_libraries(
  431. ks PRIVATE
  432. LIBUUID::LIBUUID
  433. Threads::Threads
  434. )
  435. endif()
  436. if (KS_PLAT_LIN)
  437. target_link_libraries(ks PRIVATE LIBM::LIBM)
  438. target_link_libraries(ks PUBLIC rt atomic)
  439. endif()
  440. # Include key paths
  441. target_include_directories(
  442. ks PUBLIC
  443. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/libks> # Include both include paths in the install config
  444. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include> # so we don't break any includes in installed areas
  445. $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src/include> # Within the build tree however, encforce the root
  446. $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> # include path only
  447. )
  448. target_include_directories(
  449. ks PRIVATE
  450. ${OPENSSL_INCLUDE_DIR}
  451. )
  452. # Setup a useful export file for inclusion in other projects
  453. export(TARGETS ks
  454. NAMESPACE ${PROJECT_NAME}
  455. FILE LibKSConfig.cmake
  456. EXPORT_LINK_INTERFACE_LIBRARIES
  457. )
  458. if (NOT KS_PLAT_WIN)
  459. # Set install targets
  460. install(TARGETS ks COMPONENT "runtime" EXPORT LibKSConfig DESTINATION lib)
  461. install(DIRECTORY src/include/libks COMPONENT "runtime" DESTINATION include PATTERN src/include/libks/internal EXCLUDE)
  462. # Set path for pkg-config based on ARCH and distro type
  463. if("${CMAKE_OS_NAME}" STREQUAL "Centos") # pkg-config --variable pc_path pkg-config
  464. if(${CPACK_SYSTEM_NAME} MATCHES "x86_64")
  465. install(FILES ${PROJECT_BINARY_DIR}/libks.pc COMPONENT "runtime" DESTINATION lib64/pkgconfig)
  466. else()
  467. install(FILES ${PROJECT_BINARY_DIR}/libks.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  468. endif()
  469. else()
  470. install(FILES ${PROJECT_BINARY_DIR}/libks.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  471. endif()
  472. install(FILES cmake/ksutil.cmake COMPONENT "runtime" DESTINATION include/libks/cmake)
  473. install(FILES cmake/FindPCRE.cmake COMPONENT "runtime" DESTINATION include/libks/cmake)
  474. install(FILES cmake/FindConfig.cmake COMPONENT "runtime" DESTINATION include/libks/cmake)
  475. install(FILES cmake/FindConfigCpp.cmake COMPONENT "runtime" DESTINATION include/libks/cmake)
  476. install(FILES cmake/AddLibBacktrace.cmake COMPONENT "runtime" DESTINATION include/libks/cmake)
  477. install(FILES ${PROJECT_BINARY_DIR}/copyright COMPONENT "runtime" DESTINATION share/doc/libks)
  478. install(EXPORT LibKSConfig COMPONENT "runtime" DESTINATION include/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}/libks.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()