CMakeLists.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.6.0)
  18. set(email user@zookeeper.apache.org)
  19. set(description "zookeeper C client")
  20. # general options
  21. if(UNIX)
  22. add_compile_options(-Wall -fPIC)
  23. elseif(WIN32)
  24. add_compile_options(/W3)
  25. endif()
  26. add_definitions(-DUSE_STATIC_LIB)
  27. # TODO: Enable /WX and /W4 on Windows. Currently there are ~1000 warnings.
  28. # TODO: Add Solaris support.
  29. # TODO: Add a shared library option.
  30. # TODO: Specify symbols to export.
  31. # TODO: Generate doxygen documentation.
  32. # Sync API option
  33. option(WANT_SYNCAPI "Enables Sync API support" ON)
  34. if(WANT_SYNCAPI)
  35. add_definitions(-DTHREADED)
  36. endif()
  37. # CppUnit option
  38. if(WIN32 OR APPLE)
  39. # The tests do not yet compile on Windows or macOS,
  40. # so we set this to off by default.
  41. #
  42. # Note that CMake does not have expressions except in conditionals,
  43. # so we're left with this if/else/endif pattern.
  44. set(DEFAULT_WANT_CPPUNIT OFF)
  45. else()
  46. set(DEFAULT_WANT_CPPUNIT ON)
  47. endif()
  48. option(WANT_CPPUNIT "Enables CppUnit and tests" ${DEFAULT_WANT_CPPUNIT})
  49. # SOCK_CLOEXEC
  50. option(WANT_SOCK_CLOEXEC "Enables SOCK_CLOEXEC on sockets" OFF)
  51. include(CheckSymbolExists)
  52. check_symbol_exists(SOCK_CLOEXEC sys/socket.h HAVE_SOCK_CLOEXEC)
  53. if(WANT_SOCK_CLOEXEC AND HAVE_SOCK_CLOEXEC)
  54. set(SOCK_CLOEXEC_ENABLED 1)
  55. endif()
  56. # The function `to_have(in out)` converts a header name like `arpa/inet.h`
  57. # into an Autotools style preprocessor definition `HAVE_ARPA_INET_H`.
  58. # This is then set or unset in `configure_file()` step.
  59. #
  60. # Note that CMake functions do not have return values; instead an "out"
  61. # variable must be passed, and explicitly set with parent scope.
  62. function(to_have in out)
  63. string(TOUPPER ${in} str)
  64. string(REGEX REPLACE "/|\\." "_" str ${str})
  65. set(${out} "HAVE_${str}" PARENT_SCOPE)
  66. endfunction()
  67. # include file checks
  68. foreach(f generated/zookeeper.jute.h generated/zookeeper.jute.c)
  69. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${f}")
  70. to_have(${f} name)
  71. set(${name} 1)
  72. else()
  73. message(FATAL_ERROR
  74. "jute files are missing!\n"
  75. "Please run 'ant compile_jute' while in the ZooKeeper top level directory.")
  76. endif()
  77. endforeach()
  78. # header checks
  79. include(CheckIncludeFile)
  80. set(check_headers
  81. arpa/inet.h
  82. dlfcn.h
  83. fcntl.h
  84. inttypes.h
  85. memory.h
  86. netdb.h
  87. netinet/in.h
  88. stdint.h
  89. stdlib.h
  90. string.h
  91. strings.h
  92. sys/socket.h
  93. sys/stat.h
  94. sys/time.h
  95. sys/types.h
  96. unistd.h
  97. sys/utsname.h)
  98. foreach(f ${check_headers})
  99. to_have(${f} name)
  100. check_include_file(${f} ${name})
  101. endforeach()
  102. # function checks
  103. include(CheckFunctionExists)
  104. set(check_functions
  105. getcwd
  106. gethostbyname
  107. gethostname
  108. getlogin
  109. getpwuid_r
  110. gettimeofday
  111. getuid
  112. memmove
  113. memset
  114. poll
  115. socket
  116. strchr
  117. strdup
  118. strerror
  119. strtol)
  120. foreach(fn ${check_functions})
  121. to_have(${fn} name)
  122. check_function_exists(${fn} ${name})
  123. endforeach()
  124. # library checks
  125. set(check_libraries rt m pthread)
  126. foreach(lib ${check_libraries})
  127. to_have("lib${lib}" name)
  128. find_library(${name} ${lib})
  129. endforeach()
  130. # IPv6 check
  131. include(CheckStructHasMember)
  132. check_struct_has_member("struct sockaddr_in6" sin6_addr "netinet/in.h" ZOO_IPV6_ENABLED)
  133. # configure
  134. configure_file(cmake_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/config.h)
  135. # hashtable library
  136. set(hashtable_sources src/hashtable/hashtable_itr.c src/hashtable/hashtable.c)
  137. add_library(hashtable STATIC ${hashtable_sources})
  138. target_include_directories(hashtable PUBLIC include)
  139. target_link_libraries(hashtable PUBLIC $<$<OR:$<PLATFORM_ID:Linux>,$<PLATFORM_ID:FreeBSD>>:m>)
  140. # zookeeper library
  141. set(zookeeper_sources
  142. src/zookeeper.c
  143. src/recordio.c
  144. generated/zookeeper.jute.c
  145. src/zk_log.c
  146. src/zk_hashtable.c
  147. src/addrvec.c)
  148. if(WANT_SYNCAPI)
  149. list(APPEND zookeeper_sources src/mt_adaptor.c)
  150. else()
  151. list(APPEND zookeeper_sources src/st_adaptor.c)
  152. endif()
  153. if(WIN32)
  154. list(APPEND zookeeper_sources src/winport.c)
  155. endif()
  156. add_library(zookeeper STATIC ${zookeeper_sources})
  157. target_include_directories(zookeeper PUBLIC include ${CMAKE_CURRENT_BINARY_DIR}/include generated)
  158. target_link_libraries(zookeeper PUBLIC
  159. hashtable
  160. $<$<PLATFORM_ID:Linux>:rt> # clock_gettime
  161. $<$<PLATFORM_ID:Windows>:ws2_32>) # Winsock 2.0
  162. if(WANT_SYNCAPI AND NOT WIN32)
  163. find_package(Threads REQUIRED)
  164. target_link_libraries(zookeeper PUBLIC Threads::Threads)
  165. endif()
  166. # cli executable
  167. add_executable(cli src/cli.c)
  168. target_link_libraries(cli zookeeper)
  169. # load_gen executable
  170. if(WANT_SYNCAPI AND NOT WIN32)
  171. add_executable(load_gen src/load_gen.c)
  172. target_link_libraries(load_gen zookeeper)
  173. endif()
  174. # tests
  175. set(test_sources
  176. tests/TestDriver.cc
  177. tests/LibCMocks.cc
  178. tests/LibCSymTable.cc
  179. tests/MocksBase.cc
  180. tests/ZKMocks.cc
  181. tests/Util.cc
  182. tests/ThreadingUtil.cc
  183. tests/TestZookeeperInit.cc
  184. tests/TestZookeeperClose.cc
  185. tests/TestReconfig.cc
  186. tests/TestReconfigServer.cc
  187. tests/TestClientRetry.cc
  188. tests/TestOperations.cc
  189. tests/TestMulti.cc
  190. tests/TestWatchers.cc
  191. tests/TestClient.cc
  192. tests/ZooKeeperQuorumServer.cc
  193. tests/TestReadOnlyClient.cc
  194. tests/TestLogClientEnv.cc)
  195. if(WANT_SYNCAPI)
  196. list(APPEND test_sources tests/PthreadMocks.cc)
  197. endif()
  198. if(WANT_CPPUNIT)
  199. add_executable(zktest ${test_sources})
  200. target_include_directories(zktest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
  201. target_compile_definitions(zktest
  202. PRIVATE -DZKSERVER_CMD="${CMAKE_CURRENT_SOURCE_DIR}/tests/zkServer.sh")
  203. # TODO: Use `find_library()` for `cppunit`.
  204. target_link_libraries(zktest zookeeper cppunit dl)
  205. # This reads the link flags from the file `tests/wrappers.opt` into
  206. # the variable `symbol_wrappers` for use in `target_link_libraries`.
  207. # It is a holdover from the original build system.
  208. file(STRINGS tests/wrappers.opt symbol_wrappers)
  209. if(WANT_SYNCAPI)
  210. file(STRINGS tests/wrappers-mt.opt symbol_wrappers_mt)
  211. endif()
  212. target_link_libraries(zktest ${symbol_wrappers} ${symbol_wrappers_mt})
  213. enable_testing()
  214. add_test(NAME zktest_runner COMMAND zktest)
  215. set_property(TEST zktest_runner PROPERTY ENVIRONMENT
  216. "ZKROOT=${CMAKE_CURRENT_SOURCE_DIR}/../.."
  217. "CLASSPATH=$CLASSPATH:$CLOVER_HOME/lib/clover*.jar")
  218. endif()