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, libks2
  36. project(LibKS2 VERSION 2.0.6 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. # Guess version release from environment variable
  206. # (usage of CPACK_DEBIAN_PACKAGE_RELEASE breaks ability to set CPACK_DEBIAN_PACKAGE_VERSION properly)
  207. if (NOT "$ENV{PACKAGE_RELEASE}" STREQUAL "")
  208. set(PACKAGE_RELEASE "$ENV{PACKAGE_RELEASE}")
  209. else()
  210. if(DATE_CMD)
  211. execute_process(COMMAND ${DATE_CMD} +%Y%m%d OUTPUT_VARIABLE DATE_YMD)
  212. set(PACKAGE_RELEASE ${DATE_YMD})
  213. else()
  214. set(PACKAGE_RELEASE "1")
  215. endif()
  216. endif()
  217. # Set package version
  218. set(CPACK_DEBIAN_PACKAGE_VERSION ${PROJECT_VERSION}-${PACKAGE_RELEASE}~${DISTRO_CODENAME})
  219. # Set debian file name format
  220. set(CPACK_DEBIAN_FILE_NAME "${PACKAGE_NAME}_${PROJECT_VERSION}-${PACKAGE_RELEASE}_${DISTRO_CODENAME}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.deb")
  221. # Set a Debian compliant changelog header
  222. set(CHANGELOG_HEADER "libks (${CPACK_DEBIAN_PACKAGE_VERSION}) ${DISTRO_CODENAME}\; urgency=${CPACK_DEBIAN_PACKAGE_PRIORITY}")
  223. # Generate a Debian compliant changelog
  224. if(GIT_FOUND AND GZIP_CMD AND DATE_CMD)
  225. # Get git log info
  226. message("Detecting last git tag to generate a Debian complian changelog.")
  227. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0
  228. OUTPUT_VARIABLE LAST_TAG
  229. WORKING_DIRECTORY .
  230. OUTPUT_STRIP_TRAILING_WHITESPACE)
  231. # Commits count since last tag
  232. execute_process(COMMAND ${GIT_EXECUTABLE} rev-list ${LAST_TAG}.. --count
  233. OUTPUT_VARIABLE COMMITS_COUNT_SINCE_LAST_TAG
  234. WORKING_DIRECTORY .
  235. OUTPUT_STRIP_TRAILING_WHITESPACE)
  236. message("Found last git tag: ${LAST_TAG}")
  237. # Check if we have commits since last tag
  238. if (COMMITS_COUNT_SINCE_LAST_TAG STREQUAL "0")
  239. # if not, find second tag so we could have a changelog
  240. message("No commits since git tag '${LAST_TAG}' to generate a changelog, looking for a previous tag")
  241. execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 ${LAST_TAG}^
  242. OUTPUT_VARIABLE LAST_TAG
  243. WORKING_DIRECTORY .
  244. OUTPUT_STRIP_TRAILING_WHITESPACE)
  245. endif()
  246. message("Generating a changelog using commits since git tag: ${LAST_TAG}")
  247. execute_process(COMMAND ${GIT_EXECUTABLE} log --no-merges --pretty=format:"%n [%an]%n * %s" ${LAST_TAG}..HEAD
  248. OUTPUT_VARIABLE CHANGELOG
  249. WORKING_DIRECTORY .
  250. OUTPUT_STRIP_TRAILING_WHITESPACE)
  251. string(REPLACE "\"" "" CHANGELOG ${CHANGELOG})
  252. # Create changelog
  253. file(WRITE changelog.Debian ${CHANGELOG_HEADER}\n${CHANGELOG}\n\n${CHANGELOG_FOOTER})
  254. execute_process(COMMAND ${GZIP_CMD} -f -9 -n changelog.Debian)
  255. # Install changelog
  256. install(FILES "${CMAKE_BINARY_DIR}/changelog.Debian.gz" COMPONENT "runtime" DESTINATION "share/doc/libks2")
  257. # Delete changelog related variables
  258. unset(CHANGELOG_HEADER)
  259. unset(CHANGELOG_FOOTER)
  260. unset(RFC2822_TIMESTAMP)
  261. else()
  262. 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.")
  263. endif()
  264. endif()
  265. # Enable packaging module
  266. include(CPack)
  267. # Include our libks cmake utils
  268. include(cmake/ksutil.cmake)
  269. # Load our source and header files with a glob
  270. file(GLOB_RECURSE KsDeps [LIST_DIRECTORIES false]
  271. ${CMAKE_CURRENT_LIST_DIR}/src/*.c
  272. ${CMAKE_CURRENT_LIST_DIR}/src/include/libks/*.h
  273. ${CMAKE_CURRENT_LIST_DIR}/src/include/crypt/*.c
  274. ${CMAKE_CURRENT_LIST_DIR}/src/include/crypt/*.h
  275. )
  276. if (KS_PLAT_WIN)
  277. source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${KsDeps})
  278. endif()
  279. if (KS_STATIC)
  280. add_library(ks2 STATIC ${KsDeps})
  281. target_compile_definitions(ks2 PUBLIC -DKS_DECLARE_STATIC=1)
  282. set_target_properties(ks2 PROPERTIES VERSION ${PROJECT_VERSION})
  283. else()
  284. add_library(ks2 SHARED ${KsDeps})
  285. set_target_properties(ks2 PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR})
  286. endif()
  287. set_target_properties(ks2 PROPERTIES DEBUG_POSTFIX "")
  288. if (KS_PLAT_LIN AND "${CMAKE_BUILD_TYPE}" STREQUAL "Sanitize")
  289. target_link_libraries(ks2 PUBLIC asan ubsan)
  290. endif()
  291. # Use libbacktrace if requested
  292. if (NOT KS_PLAT_WIN AND WITH_LIBBACKTRACE)
  293. include(cmake/AddLibBacktrace.cmake)
  294. target_link_libraries(ks2 PRIVATE LibBacktrace)
  295. target_compile_definitions(ks2 PUBLIC -DHAVE_LIBBACKTRACE=1)
  296. endif()
  297. if (NOT KS_PLAT_WIN)
  298. # Now auto detect our dependencies
  299. ksutil_target_check_library_exists_hdr(ks2 PUBLIC rt clock_gettime time.h HAVE_CLOCK_GETTIME)
  300. ksutil_target_check_library_exists_hdr(ks2 PUBLIC rt clock_getres time.h HAVE_CLOCK_GETRES)
  301. ksutil_target_check_library_exists_hdr(ks2 PUBLIC rt clock_nanosleep time.h HAVE_CLOCK_NANOSLEEP)
  302. ksutil_target_check_library_exists_hdr(ks2 PUBLIC rt strftime time.h HAVE_STRFTIME)
  303. ksutil_target_check_library_exists(ks2 PUBLIC c sched_setscheduler HAVE_SCHED_SETSCHEDULER)
  304. ksutil_target_check_library_exists(ks2 PUBLIC c malloc HAVE_MALLOC)
  305. ksutil_target_check_library_exists(ks2 PUBLIC c usleep HAVE_USLEEP)
  306. ksutil_target_check_function_exists(ks2 PUBLIC pthread_attr_setschedparam HAVE_PTHREAD_ATTR_SETSCHEDPARAM)
  307. ksutil_target_check_function_exists(ks2 PUBLIC memmem HAVE_MEMMEM)
  308. ksutil_target_check_include_file(ks2 PUBLIC stdlib.h HAVE_STDLIB_H)
  309. ksutil_target_check_include_file(ks2 PUBLIC sys/types.h HAVE_SYS_TYPES_H)
  310. ksutil_target_check_include_file(ks2 PUBLIC sys/stat.h HAVE_SYS_STAT_H)
  311. ksutil_target_check_include_file(ks2 PUBLIC string.h HAVE_STRING_H)
  312. ksutil_target_check_include_file(ks2 PUBLIC unistd.h HAVE_UNISTD_H)
  313. ksutil_target_check_include_file(ks2 PUBLIC memory.h HAVE_MEMORY_H)
  314. ksutil_target_check_include_file(ks2 PUBLIC strings.h HAVE_STRINGS_H)
  315. ksutil_target_check_include_file(ks2 PUBLIC inttypes.h HAVE_INTTYPES_H)
  316. ksutil_target_check_include_file(ks2 PUBLIC stdint.h HAVE_STDINT_H)
  317. ksutil_target_check_include_file(ks2 PUBLIC dlfcn.h HAVE_DLFCN_H)
  318. ksutil_target_check_include_file(ks2 PUBLIC sched.h HAVE_SCHED_H)
  319. ksutil_target_check_include_file(ks2 PUBLIC byteswap.h HAVE_BYTESWAP_H)
  320. ksutil_target_check_include_file(ks2 PUBLIC dirent.h HAVE_DIRENT_H)
  321. endif()
  322. # Check endianess for our swap macros
  323. TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
  324. # Need libm on linux
  325. if (KS_PLAT_LIN)
  326. include(cmake/FindLibM.cmake)
  327. endif()
  328. if (KS_PLAT_WIN)
  329. # We use RpcString on windows
  330. target_link_libraries(ks2 PUBLIC Rpcrt4.lib)
  331. else()
  332. # Locate libuuid on non-win platforms
  333. include(cmake/FindUUID.cmake)
  334. endif()
  335. # Locate ssl
  336. if (KS_PLAT_MAC)
  337. set(OPENSSL_ROOT_DIR /usr/local/opt/openssl)
  338. endif()
  339. include(FindOpenSSL)
  340. find_package(OpenSSL REQUIRED Crypto SSL)
  341. if (NOT KS_PLAT_WIN)
  342. # Use pthreads
  343. set(THREADS_PREFER_PTHREAD_FLAG ON)
  344. find_package(Threads REQUIRED)
  345. endif()
  346. set_target_properties(ks2 PROPERTIES DEFINE_SYMBOL KS_EXPORTS)
  347. # Setup ks core definitions, these are private defintions
  348. # meaning they won't be exported to public linkers
  349. target_compile_definitions(ks2 PRIVATE
  350. -DPACKAGE_NAME="libks2"
  351. -DPACKAGE_TARNAME="libks2"
  352. -DPACKAGE_VERSION="${PROJECT_VERSION}"
  353. -DPACKAGE_STRING="libks2 ${PROJECT_VERSION}"
  354. -DPACKAGE_BUGREPORT="bugs@freeswitch.org"
  355. -DPACKAGE_URL=""
  356. -DVERSION="${PROJECT_VERSION}"
  357. -D_GNU_SOURCE=1
  358. -DCJSON_HIDE_SYMBOLS=1
  359. )
  360. # Setup our public facing definitions
  361. target_compile_definitions(ks2 PUBLIC
  362. -DSTDC_HEADERS=1
  363. -DTIME_WITH_SYS_TIME=1
  364. -DRETSIGTYPE=void
  365. -DHAVE_LIBCRYPTO=1
  366. -DHAVE_LIBSSL=1
  367. -D_REENTRANT=1
  368. ${PublicHaves}
  369. )
  370. # Handle debug mode
  371. if (KS_DEBUG_HANDLE)
  372. target_compile_definitions(ks2 PUBLIC
  373. -DKS_DEBUG_HANDLE=1
  374. )
  375. endif()
  376. # JSON debug mode
  377. if (KS_DEBUG_JSON)
  378. target_compile_definitions(ks2 PUBLIC
  379. -DKS_DEBUG_JSON=1
  380. )
  381. endif()
  382. # Pool debug mode
  383. if (KS_DEBUG_POOL)
  384. target_compile_definitions(ks2 PUBLIC
  385. -DKS_DEBUG_POOL=1
  386. )
  387. endif()
  388. # Spinlock debug mode
  389. if (KS_DEBUG_SPINLOCK)
  390. target_compile_definitions(ks2 PUBLIC
  391. -DKS_DEBUG_SPINLOCK=1
  392. )
  393. endif()
  394. # Mutex debug mode
  395. if (KS_DEBUG_MUTEX)
  396. target_compile_definitions(ks2 PUBLIC
  397. -DKS_DEBUG_MUTEX=1
  398. )
  399. endif()
  400. if (NOT KS_PLAT_WIN)
  401. # All symbols are hidden by default, unless exposed explicitly
  402. include(CheckCXXCompilerFlag)
  403. check_cxx_compiler_flag(-fvisibility=hidden COMPILER_HAS_HIDDEN_VISIBILITY)
  404. if (COMPILER_HAS_HIDDEN_VISIBILITY)
  405. message("Compiler supports visibility")
  406. set_target_properties(ks2 PROPERTIES CXX_VISIBILITY_PRESET hidden)
  407. set_target_properties(ks2 PROPERTIES C_VISIBILITY_PRESET hidden)
  408. target_compile_definitions(ks2 PRIVATE
  409. -DKS_API_VISIBILITY=1
  410. )
  411. else()
  412. message("Compiler does not support visibility")
  413. endif()
  414. endif()
  415. if (IS_BIG_ENDIAN)
  416. target_compile_definitions(ks2 PUBLIC
  417. -D__BYTE_ORDER=__BIG_ENDIAN
  418. )
  419. else()
  420. target_compile_definitions(ks2 PUBLIC
  421. -D__BYTE_ORDER=__LITTLE_ENDIAN
  422. )
  423. endif()
  424. # Setup ks core dependent linkages
  425. target_link_libraries(
  426. ks2 PUBLIC
  427. OpenSSL::SSL
  428. OpenSSL::Crypto
  429. )
  430. if (NOT KS_PLAT_WIN)
  431. target_link_libraries(
  432. ks2 PRIVATE
  433. LIBUUID::LIBUUID
  434. Threads::Threads
  435. )
  436. endif()
  437. if (KS_PLAT_LIN)
  438. target_link_libraries(ks2 PRIVATE LIBM::LIBM)
  439. target_link_libraries(ks2 PUBLIC rt atomic)
  440. endif()
  441. # Include key paths
  442. target_include_directories(
  443. ks2 PUBLIC
  444. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include/libks2> # Include both include paths in the install config
  445. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include> # so we don't break any includes in installed areas
  446. $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/src/include> # Within the build tree however, encforce the root
  447. $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}> # include path only
  448. )
  449. target_include_directories(
  450. ks2 PRIVATE
  451. ${OPENSSL_INCLUDE_DIR}
  452. )
  453. # Setup a useful export file for inclusion in other projects
  454. export(TARGETS ks2
  455. NAMESPACE ${PROJECT_NAME}
  456. FILE LibKS2Config.cmake
  457. EXPORT_LINK_INTERFACE_LIBRARIES
  458. )
  459. if (NOT KS_PLAT_WIN)
  460. # Set install targets
  461. install(TARGETS ks2 COMPONENT "runtime" EXPORT LibKS2Config DESTINATION lib)
  462. install(DIRECTORY src/include/libks COMPONENT "runtime" DESTINATION include/libks2 PATTERN src/include/libks/internal EXCLUDE)
  463. # Set path for pkg-config based on ARCH and distro type
  464. if("${CMAKE_OS_NAME}" STREQUAL "Centos") # pkg-config --variable pc_path pkg-config
  465. if(${CPACK_SYSTEM_NAME} MATCHES "x86_64")
  466. install(FILES ${PROJECT_BINARY_DIR}/libks2.pc COMPONENT "runtime" DESTINATION lib64/pkgconfig)
  467. else()
  468. install(FILES ${PROJECT_BINARY_DIR}/libks2.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  469. endif()
  470. else()
  471. install(FILES ${PROJECT_BINARY_DIR}/libks2.pc COMPONENT "runtime" DESTINATION lib/pkgconfig)
  472. endif()
  473. install(FILES cmake/ksutil.cmake COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  474. install(FILES cmake/FindPCRE.cmake COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  475. install(FILES cmake/AddLibBacktrace.cmake COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  476. install(FILES ${PROJECT_SOURCE_DIR}/copyright COMPONENT "runtime" DESTINATION share/doc/libks2)
  477. install(EXPORT LibKS2Config COMPONENT "runtime" DESTINATION include/libks2/libks/cmake)
  478. # Set uninstall target
  479. if(NOT TARGET uninstall)
  480. configure_file(
  481. "${CMAKE_CURRENT_SOURCE_DIR}/uninstall.cmake.in"
  482. "${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
  483. IMMEDIATE @ONLY)
  484. add_custom_target(uninstall
  485. COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake)
  486. endif()
  487. # Create a package config for registration with the system
  488. # prefix=@PC_PREFIX@
  489. # exec_prefix=@PC_EXEC_PREFIX@
  490. # libdir=@PC_LIB_DIR@
  491. # includedir=@PC_INCLUDE_DIR@
  492. # definitions=@PC_DEFINITIONS@
  493. # cmakedir=@PC_CMAKE_DIR@
  494. # Name: @PACKAGE_NAME@
  495. # Version: @PACKAGE_VERSION@
  496. set(PC_PREFIX ${CMAKE_INSTALL_PREFIX})
  497. set(PACKAGE_VERSION ${PROJECT_VERSION})
  498. get_property(PC_DEFINITIONS TARGET ks2 PROPERTY INTERFACE_COMPILE_DEFINITIONS)
  499. configure_file("${CMAKE_CURRENT_LIST_DIR}/libks.pc.in" "${PROJECT_BINARY_DIR}/libks2.pc" @ONLY)
  500. endif()
  501. #cotire(ks)
  502. # Add tests
  503. enable_testing()
  504. add_subdirectory(tests)
  505. if (WITH_KS_TEST)
  506. enable_testing()
  507. add_subdirectory(KSTest)
  508. endif()