CMakeLists.txt 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. cmake_minimum_required(VERSION 3.5)
  17. project(zookeeper VERSION 3.10.0)
  18. set(email user@zookeeper.apache.org)
  19. set(description "zookeeper C client")
  20. list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../../tools/cmake/Modules")
  21. # general options
  22. if(UNIX)
  23. add_compile_options(-Wall -fPIC)
  24. elseif(WIN32)
  25. add_compile_options(/W3)
  26. endif()
  27. add_definitions(-DUSE_STATIC_LIB)
  28. # TODO: Enable /WX and /W4 on Windows. Currently there are ~1000 warnings.
  29. # TODO: Add Solaris support.
  30. # TODO: Add a shared library option.
  31. # TODO: Specify symbols to export.
  32. # TODO: Generate doxygen documentation.
  33. # Sync API option
  34. option(WANT_SYNCAPI "Enables Sync API support" ON)
  35. if(WANT_SYNCAPI)
  36. add_definitions(-DTHREADED)
  37. endif()
  38. # CppUnit option
  39. if(WIN32 OR APPLE)
  40. # The tests do not yet compile on Windows or macOS,
  41. # so we set this to off by default.
  42. #
  43. # Note that CMake does not have expressions except in conditionals,
  44. # so we're left with this if/else/endif pattern.
  45. set(DEFAULT_WANT_CPPUNIT OFF)
  46. else()
  47. set(DEFAULT_WANT_CPPUNIT ON)
  48. endif()
  49. option(WANT_CPPUNIT "Enables CppUnit and tests" ${DEFAULT_WANT_CPPUNIT})
  50. # SOCK_CLOEXEC
  51. option(WANT_SOCK_CLOEXEC "Enables SOCK_CLOEXEC on sockets" OFF)
  52. include(CheckSymbolExists)
  53. check_symbol_exists(SOCK_CLOEXEC sys/socket.h HAVE_SOCK_CLOEXEC)
  54. if(WANT_SOCK_CLOEXEC AND HAVE_SOCK_CLOEXEC)
  55. set(SOCK_CLOEXEC_ENABLED 1)
  56. endif()
  57. # Cyrus SASL 2.x
  58. option(WITH_CYRUS_SASL "turn ON/OFF Cyrus SASL 2.x support, or define SASL library location (default: ON)" ON)
  59. message("-- using WITH_CYRUS_SASL=${WITH_CYRUS_SASL}")
  60. if(NOT WITH_CYRUS_SASL STREQUAL "OFF")
  61. if(NOT WITH_CYRUS_SASL STREQUAL "ON")
  62. set(CYRUS_SASL_ROOT_DIR "${WITH_CYRUS_SASL}")
  63. endif()
  64. find_package(CyrusSASL)
  65. if(CYRUS_SASL_FOUND)
  66. message("-- Cyrus SASL 2.x found! will build with SASL support.")
  67. else()
  68. message("-- WARNING: unable to find Cyrus SASL 2.x! will build without SASL support.")
  69. endif()
  70. endif()
  71. # The function `to_have(in out)` converts a header name like `arpa/inet.h`
  72. # into an Autotools style preprocessor definition `HAVE_ARPA_INET_H`.
  73. # This is then set or unset in `configure_file()` step.
  74. #
  75. # Note that CMake functions do not have return values; instead an "out"
  76. # variable must be passed, and explicitly set with parent scope.
  77. function(to_have in out)
  78. string(TOUPPER ${in} str)
  79. string(REGEX REPLACE "/|\\." "_" str ${str})
  80. set(${out} "HAVE_${str}" PARENT_SCOPE)
  81. endfunction()
  82. # include file checks
  83. foreach(f generated/zookeeper.jute.h generated/zookeeper.jute.c)
  84. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${f}")
  85. to_have(${f} name)
  86. set(${name} 1)
  87. else()
  88. message(FATAL_ERROR
  89. "jute files are missing!\n"
  90. "Please run 'ant compile_jute' while in the ZooKeeper top level directory.")
  91. endif()
  92. endforeach()
  93. # header checks
  94. include(CheckIncludeFile)
  95. set(check_headers
  96. arpa/inet.h
  97. dlfcn.h
  98. fcntl.h
  99. inttypes.h
  100. memory.h
  101. netdb.h
  102. netinet/in.h
  103. stdint.h
  104. stdlib.h
  105. string.h
  106. strings.h
  107. sys/socket.h
  108. sys/stat.h
  109. sys/time.h
  110. sys/types.h
  111. unistd.h
  112. sys/utsname.h)
  113. foreach(f ${check_headers})
  114. to_have(${f} name)
  115. check_include_file(${f} ${name})
  116. endforeach()
  117. # function checks
  118. include(CheckFunctionExists)
  119. set(check_functions
  120. getcwd
  121. gethostbyname
  122. gethostname
  123. getlogin
  124. getpwuid_r
  125. gettimeofday
  126. getuid
  127. memmove
  128. memset
  129. poll
  130. socket
  131. strchr
  132. strdup
  133. strerror
  134. strtol)
  135. foreach(fn ${check_functions})
  136. to_have(${fn} name)
  137. check_function_exists(${fn} ${name})
  138. endforeach()
  139. # library checks
  140. set(check_libraries rt m pthread)
  141. foreach(lib ${check_libraries})
  142. to_have("lib${lib}" name)
  143. find_library(${name} ${lib})
  144. endforeach()
  145. # IPv6 check
  146. include(CheckStructHasMember)
  147. check_struct_has_member("struct sockaddr_in6" sin6_addr "netinet/in.h" ZOO_IPV6_ENABLED)
  148. # configure
  149. configure_file(cmake_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/config.h)
  150. # hashtable library
  151. set(hashtable_sources src/hashtable/hashtable_itr.c src/hashtable/hashtable.c)
  152. add_library(hashtable STATIC ${hashtable_sources})
  153. target_include_directories(hashtable PUBLIC include)
  154. target_link_libraries(hashtable PUBLIC $<$<OR:$<PLATFORM_ID:Linux>,$<PLATFORM_ID:FreeBSD>>:m>)
  155. # zookeeper library
  156. set(zookeeper_sources
  157. src/zookeeper.c
  158. src/recordio.c
  159. generated/zookeeper.jute.c
  160. src/zk_log.c
  161. src/zk_hashtable.c
  162. src/addrvec.c)
  163. if(WANT_SYNCAPI)
  164. list(APPEND zookeeper_sources src/mt_adaptor.c)
  165. else()
  166. list(APPEND zookeeper_sources src/st_adaptor.c)
  167. endif()
  168. if(CYRUS_SASL_FOUND)
  169. list(APPEND zookeeper_sources src/zk_sasl.c)
  170. endif()
  171. if(WIN32)
  172. list(APPEND zookeeper_sources src/winport.c)
  173. endif()
  174. add_library(zookeeper STATIC ${zookeeper_sources})
  175. target_include_directories(zookeeper PUBLIC include ${CMAKE_CURRENT_BINARY_DIR}/include generated)
  176. target_link_libraries(zookeeper PUBLIC
  177. hashtable
  178. $<$<PLATFORM_ID:Linux>:rt> # clock_gettime
  179. $<$<PLATFORM_ID:Windows>:ws2_32>) # Winsock 2.0
  180. option(WITH_OPENSSL "turn ON/OFF SSL support, or define openssl library location (default: ON)" ON)
  181. message("-- using WITH_OPENSSL=${WITH_OPENSSL}")
  182. if(NOT WITH_OPENSSL STREQUAL "OFF")
  183. if(NOT WITH_OPENSSL STREQUAL "ON")
  184. set(OPENSSL_ROOT_DIR,${WITH_OPENSSL})
  185. endif()
  186. find_package(OpenSSL)
  187. if(OPENSSL_FOUND)
  188. target_compile_definitions(zookeeper PUBLIC HAVE_OPENSSL_H)
  189. target_link_libraries(zookeeper PUBLIC OpenSSL::SSL OpenSSL::Crypto)
  190. message("-- OpenSSL libraries found! will build with SSL support.")
  191. else()
  192. message("-- WARNING: unable to find OpenSSL libraries! will build without SSL support.")
  193. endif()
  194. endif()
  195. if(WANT_SYNCAPI AND NOT WIN32)
  196. find_package(Threads REQUIRED)
  197. target_link_libraries(zookeeper PUBLIC Threads::Threads)
  198. endif()
  199. if(CYRUS_SASL_FOUND)
  200. target_compile_definitions(zookeeper PUBLIC HAVE_CYRUS_SASL_H)
  201. target_link_libraries(zookeeper PUBLIC CyrusSASL)
  202. endif()
  203. # cli executable
  204. add_executable(cli src/cli.c)
  205. target_link_libraries(cli zookeeper)
  206. # load_gen executable
  207. if(WANT_SYNCAPI AND NOT WIN32)
  208. add_executable(load_gen src/load_gen.c)
  209. target_link_libraries(load_gen zookeeper)
  210. endif()
  211. # tests
  212. set(test_sources
  213. tests/TestDriver.cc
  214. tests/LibCMocks.cc
  215. tests/LibCSymTable.cc
  216. tests/MocksBase.cc
  217. tests/ZKMocks.cc
  218. tests/Util.cc
  219. tests/ThreadingUtil.cc
  220. tests/TestZookeeperInit.cc
  221. tests/TestZookeeperClose.cc
  222. tests/TestReconfig.cc
  223. tests/TestReconfigServer.cc
  224. tests/TestClientRetry.cc
  225. tests/TestOperations.cc
  226. tests/TestMulti.cc
  227. tests/TestWatchers.cc
  228. tests/TestClient.cc
  229. tests/ZooKeeperQuorumServer.cc
  230. tests/TestReadOnlyClient.cc
  231. tests/TestLogClientEnv.cc)
  232. if(WANT_SYNCAPI)
  233. list(APPEND test_sources tests/PthreadMocks.cc)
  234. endif()
  235. if(WANT_CPPUNIT)
  236. set (CMAKE_CXX_STANDARD 11)
  237. add_executable(zktest ${test_sources})
  238. target_include_directories(zktest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
  239. target_compile_definitions(zktest
  240. PRIVATE -DZKSERVER_CMD="${CMAKE_CURRENT_SOURCE_DIR}/tests/zkServer.sh")
  241. # TODO: Use `find_library()` for `cppunit`.
  242. target_link_libraries(zktest zookeeper cppunit dl)
  243. # This reads the link flags from the file `tests/wrappers.opt` into
  244. # the variable `symbol_wrappers` for use in `target_link_libraries`.
  245. # It is a holdover from the original build system.
  246. file(STRINGS tests/wrappers.opt symbol_wrappers)
  247. if(WANT_SYNCAPI)
  248. file(STRINGS tests/wrappers-mt.opt symbol_wrappers_mt)
  249. endif()
  250. target_link_libraries(zktest ${symbol_wrappers} ${symbol_wrappers_mt})
  251. enable_testing()
  252. add_test(NAME zktest_runner COMMAND zktest)
  253. set_property(TEST zktest_runner PROPERTY ENVIRONMENT
  254. "ZKROOT=${CMAKE_CURRENT_SOURCE_DIR}/../.."
  255. "CLASSPATH=$CLASSPATH:$CLOVER_HOME/lib/clover*.jar")
  256. endif()