CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # If cmake variable HDFSPP_LIBRARY_ONLY is set, then tests, examples, and
  19. # tools will not be built. This allows for faster builds of the libhdfspp
  20. # library alone, avoids looking for a JDK, valgrind, and gmock, and
  21. # prevents the generation of multiple binaries that might not be relevant
  22. # to other projects during normal use.
  23. # Example of cmake invocation with HDFSPP_LIBRARY_ONLY enabled:
  24. # cmake -DHDFSPP_LIBRARY_ONLY=1
  25. project (libhdfspp)
  26. cmake_minimum_required(VERSION 2.8)
  27. find_package (Boost 1.72.0 REQUIRED)
  28. enable_testing()
  29. set(CMAKE_CXX_STANDARD 17)
  30. include (CTest)
  31. SET(BUILD_SHARED_HDFSPP TRUE CACHE STRING "BUILD_SHARED_HDFSPP defaulting to 'TRUE'")
  32. SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH})
  33. # If there's a better way to inform FindCyrusSASL.cmake, let's make this cleaner:
  34. SET(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${CYRUS_SASL_DIR};${GSASL_DIR};$ENV{PROTOBUF_HOME}")
  35. # Specify PROTOBUF_HOME so that find_package picks up the correct version
  36. SET(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};$ENV{PROTOBUF_HOME}")
  37. find_package(Doxygen)
  38. find_package(OpenSSL REQUIRED)
  39. find_package(Protobuf REQUIRED)
  40. find_package(CyrusSASL)
  41. find_package(GSasl)
  42. find_package(Threads)
  43. include(CheckCXXSourceCompiles)
  44. include(CheckSymbolExists)
  45. # Download and build gtest
  46. configure_file(CMakeLists-gtest.txt.in googletest-download/CMakeLists.txt)
  47. execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  48. RESULT_VARIABLE result
  49. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
  50. if(result)
  51. message(FATAL_ERROR "CMake step for googletest failed: ${result}")
  52. endif()
  53. execute_process(COMMAND ${CMAKE_COMMAND} --build .
  54. RESULT_VARIABLE result
  55. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download)
  56. if(result)
  57. message(FATAL_ERROR "Build step for googletest failed: ${result}")
  58. endif()
  59. # Prevent overriding the parent project's compiler/linker
  60. # settings on Windows
  61. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  62. # Add googletest directly to our build. This defines
  63. # the gtest and gtest_main targets.
  64. add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
  65. ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
  66. EXCLUDE_FROM_ALL)
  67. # Check if thread_local is supported
  68. unset (THREAD_LOCAL_SUPPORTED CACHE)
  69. set (CMAKE_CXX_STANDARD_REQUIRED ON)
  70. set (CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  71. check_cxx_source_compiles(
  72. "#include <thread>
  73. int main(void) {
  74. thread_local int s;
  75. return 0;
  76. }"
  77. THREAD_LOCAL_SUPPORTED)
  78. if (NOT THREAD_LOCAL_SUPPORTED)
  79. message(FATAL_ERROR
  80. "FATAL ERROR: The required feature thread_local storage is not supported by your compiler. \
  81. Known compilers that support this feature: GCC 4.8+, Visual Studio 2015+, Clang (community \
  82. version 3.3+), Clang (version for Xcode 8+ and iOS 9+).")
  83. endif (NOT THREAD_LOCAL_SUPPORTED)
  84. # Check if PROTOC library was compiled with the compatible compiler by trying
  85. # to compile some dummy code
  86. unset (PROTOC_IS_COMPATIBLE CACHE)
  87. set (CMAKE_REQUIRED_INCLUDES ${PROTOBUF_INCLUDE_DIRS})
  88. set (CMAKE_REQUIRED_LIBRARIES ${PROTOBUF_LIBRARY} ${PROTOBUF_PROTOC_LIBRARY})
  89. check_cxx_source_compiles(
  90. "#include <google/protobuf/io/printer.h>
  91. #include <string>
  92. int main(void) {
  93. ::google::protobuf::io::ZeroCopyOutputStream *out = NULL;
  94. ::google::protobuf::io::Printer printer(out, '$');
  95. printer.PrintRaw(std::string(\"test\"));
  96. return 0;
  97. }"
  98. PROTOC_IS_COMPATIBLE)
  99. if (NOT PROTOC_IS_COMPATIBLE)
  100. message(WARNING
  101. "WARNING: the Protocol Buffers Library and the Libhdfs++ Library must both be compiled \
  102. with the same (or compatible) compiler. Normally only the same major versions of the same \
  103. compiler are compatible with each other.")
  104. endif (NOT PROTOC_IS_COMPATIBLE)
  105. find_program(MEMORYCHECK_COMMAND valgrind HINTS ${VALGRIND_DIR} )
  106. set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=no --leak-check=full --error-exitcode=1 --suppressions=${PROJECT_SOURCE_DIR}/tests/memcheck.supp")
  107. message(STATUS "valgrind location: ${MEMORYCHECK_COMMAND}")
  108. if (REQUIRE_VALGRIND AND MEMORYCHECK_COMMAND MATCHES "MEMORYCHECK_COMMAND-NOTFOUND" )
  109. message(FATAL_ERROR "valgrind was required but not found. "
  110. "The path can be included via a -DVALGRIND_DIR=... flag passed to CMake.")
  111. endif (REQUIRE_VALGRIND AND MEMORYCHECK_COMMAND MATCHES "MEMORYCHECK_COMMAND-NOTFOUND" )
  112. # Find the SASL library to use. If you don't want to require a sasl library,
  113. # define -DNO_SASL=1 in your cmake call
  114. # Prefer Cyrus SASL, but use GSASL if it is found
  115. # Note that the packages can be disabled by setting CMAKE_DISABLE_FIND_PACKAGE_GSasl or
  116. # CMAKE_DISABLE_FIND_PACKAGE_CyrusSASL, respectively (case sensitive)
  117. set (SASL_LIBRARIES)
  118. set (SASL_INCLUDE_DIR)
  119. if (NOT NO_SASL)
  120. if (CYRUS_SASL_FOUND)
  121. message(STATUS "Using Cyrus SASL; link with ${CYRUS_SASL_SHARED_LIB}")
  122. set (SASL_INCLUDE_DIR ${CYRUS_SASL_INCLUDE_DIR})
  123. set (SASL_LIBRARIES ${CYRUS_SASL_SHARED_LIB})
  124. set (CMAKE_USING_CYRUS_SASL 1)
  125. add_definitions(-DUSE_SASL -DUSE_CYRUS_SASL)
  126. else (CYRUS_SASL_FOUND)
  127. if (REQUIRE_CYRUS_SASL)
  128. message(FATAL_ERROR "Cyrus SASL was required but not found. "
  129. "The path can be included via a -DCYRUS_SASL_DIR=... flag passed to CMake.")
  130. endif (REQUIRE_CYRUS_SASL)
  131. # If we didn't pick Cyrus, use GSASL instead
  132. if (GSASL_FOUND)
  133. message(STATUS "Using GSASL; link with ${GSASL_LIBRARIES}")
  134. set (SASL_INCLUDE_DIR ${GSASL_INCLUDE_DIR})
  135. set (SASL_LIBRARIES ${GSASL_LIBRARIES})
  136. set (CMAKE_USING_GSASL 1)
  137. add_definitions(-DUSE_SASL -DUSE_GSASL)
  138. else (GSASL_FOUND)
  139. if (REQUIRE_GSASL)
  140. message(FATAL_ERROR "GSASL was required but not found. "
  141. "The path can be included via a -DGSASL_DIR=... flag passed to CMake.")
  142. endif (REQUIRE_GSASL)
  143. # No SASL was found, but NO_SASL was not defined
  144. message(FATAL_ERROR "Cound not find a SASL library (GSASL (gsasl) or Cyrus SASL (libsasl2). "
  145. "Install/configure one of them or define NO_SASL=1 in your cmake call")
  146. endif (GSASL_FOUND)
  147. endif (CYRUS_SASL_FOUND)
  148. else (NOT NO_SASL)
  149. message(STATUS "Compiling with NO SASL SUPPORT")
  150. endif (NOT NO_SASL)
  151. check_symbol_exists(explicit_bzero "string.h" HAVE_EXPLICIT_BZERO)
  152. if(HAVE_EXPLICIT_BZERO)
  153. add_definitions(-DHAVE_EXPLICIT_BZERO)
  154. endif()
  155. add_definitions(-DASIO_STANDALONE -DASIO_CPP11_DATE_TIME)
  156. # Disable optimizations if compiling debug
  157. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
  158. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
  159. if(UNIX)
  160. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -g -fPIC -fno-strict-aliasing")
  161. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fPIC -fno-strict-aliasing")
  162. endif()
  163. if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  164. add_definitions(-DASIO_HAS_STD_ADDRESSOF -DASIO_HAS_STD_ARRAY -DASIO_HAS_STD_ATOMIC -DASIO_HAS_CSTDINT -DASIO_HAS_STD_SHARED_PTR -DASIO_HAS_STD_TYPE_TRAITS -DASIO_HAS_VARIADIC_TEMPLATES -DASIO_HAS_STD_FUNCTION -DASIO_HAS_STD_CHRONO -DASIO_HAS_STD_SYSTEM_ERROR)
  165. endif ()
  166. # Mac OS 10.7 and later deprecates most of the methods in OpenSSL.
  167. # Add -Wno-deprecated-declarations to avoid the warnings.
  168. if(APPLE)
  169. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -Wno-deprecated-declarations -Wno-unused-local-typedef")
  170. endif()
  171. if(DOXYGEN_FOUND)
  172. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile @ONLY)
  173. add_custom_target(doc ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile
  174. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  175. COMMENT "Generating API documentation with Doxygen" VERBATIM)
  176. endif(DOXYGEN_FOUND)
  177. # Copy files from the hadoop tree into the output/extern directory if
  178. # they've changed
  179. function (copy_on_demand input_src_glob input_dest_dir)
  180. get_filename_component(src_glob ${input_src_glob} REALPATH)
  181. get_filename_component(dest_dir ${input_dest_dir} REALPATH)
  182. get_filename_component(src_dir ${src_glob} PATH)
  183. message(STATUS "Syncing ${src_glob} to ${dest_dir}")
  184. file(GLOB_RECURSE src_files ${src_glob})
  185. foreach(src_path ${src_files})
  186. file(RELATIVE_PATH relative_src ${src_dir} ${src_path})
  187. set(dest_path "${dest_dir}/${relative_src}")
  188. add_custom_command(TARGET copy_hadoop_files
  189. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${src_path}" "${dest_path}"
  190. )
  191. endforeach()
  192. endfunction()
  193. # If we're building in the hadoop tree, pull the Hadoop files that
  194. # libhdfspp depends on. This allows us to ensure that
  195. # the distribution will have a consistent set of headers and
  196. # .proto files
  197. if(HADOOP_BUILD)
  198. set(HADOOP_IMPORT_DIR ${PROJECT_BINARY_DIR}/extern)
  199. get_filename_component(HADOOP_IMPORT_DIR ${HADOOP_IMPORT_DIR} REALPATH)
  200. add_custom_target(copy_hadoop_files ALL)
  201. # Gather the Hadoop files and resources that libhdfs++ needs to build
  202. copy_on_demand(../libhdfs/include/*.h* ${HADOOP_IMPORT_DIR}/include)
  203. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../hadoop-hdfs-client/src/main/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hdfs)
  204. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../../hadoop-common-project/hadoop-common/src/main/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hadoop)
  205. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../../hadoop-common-project/hadoop-common/src/test/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hadoop_test)
  206. else(HADOOP_BUILD)
  207. set(HADOOP_IMPORT_DIR ${CMAKE_CURRENT_LIST_DIR}/extern)
  208. endif(HADOOP_BUILD)
  209. # Paths to find the imported files
  210. set(PROTO_HDFS_DIR ${HADOOP_IMPORT_DIR}/proto/hdfs)
  211. set(PROTO_HADOOP_DIR ${HADOOP_IMPORT_DIR}/proto/hadoop)
  212. set(PROTO_HADOOP_TEST_DIR ${HADOOP_IMPORT_DIR}/proto/hadoop_test)
  213. include_directories(
  214. include
  215. lib
  216. ${HADOOP_IMPORT_DIR}/include
  217. )
  218. include_directories( SYSTEM
  219. ${PROJECT_BINARY_DIR}/lib/proto
  220. ${Boost_INCLUDE_DIRS}
  221. third_party/rapidxml-1.13
  222. ${gtest_SOURCE_DIR}/include
  223. ${gmock_SOURCE_DIR}/include
  224. third_party/tr2
  225. third_party/protobuf
  226. third_party/uriparser2
  227. ${OPENSSL_INCLUDE_DIR}
  228. ${SASL_INCLUDE_DIR}
  229. ${PROTOBUF_INCLUDE_DIRS}
  230. )
  231. add_subdirectory(third_party/uriparser2)
  232. add_subdirectory(lib)
  233. if(NOT HDFSPP_LIBRARY_ONLY)
  234. add_subdirectory(tests)
  235. add_subdirectory(examples)
  236. add_subdirectory(tools)
  237. endif()
  238. # create an empty file; hadoop_add_dual_library wraps add_library which
  239. # requires at least one file as an argument
  240. set(EMPTY_FILE_CC ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/empty.cc)
  241. file(WRITE ${EMPTY_FILE_CC} "")
  242. # Build the output libraries
  243. if(NEED_LINK_DL)
  244. set(LIB_DL dl)
  245. endif()
  246. set(LIBHDFSPP_VERSION "0.1.0")
  247. set(LIBHDFSPP_ALL_OBJECTS $<TARGET_OBJECTS:x_platform_obj> $<TARGET_OBJECTS:bindings_c_obj> $<TARGET_OBJECTS:fs_obj> $<TARGET_OBJECTS:rpc_obj> $<TARGET_OBJECTS:reader_obj> $<TARGET_OBJECTS:proto_obj> $<TARGET_OBJECTS:connection_obj> $<TARGET_OBJECTS:common_obj> $<TARGET_OBJECTS:uriparser2_obj>)
  248. if (HADOOP_BUILD)
  249. hadoop_add_dual_library(hdfspp ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  250. hadoop_target_link_dual_libraries(hdfspp
  251. ${LIB_DL}
  252. ${PROTOBUF_LIBRARY}
  253. ${OPENSSL_LIBRARIES}
  254. ${SASL_LIBRARIES}
  255. ${CMAKE_THREAD_LIBS_INIT}
  256. )
  257. set_target_properties(hdfspp PROPERTIES SOVERSION ${LIBHDFSPP_VERSION})
  258. hadoop_dual_output_directory(hdfspp ${OUT_DIR})
  259. else (HADOOP_BUILD)
  260. add_library(hdfspp_static STATIC ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  261. target_link_libraries(hdfspp_static
  262. ${LIB_DL}
  263. ${PROTOBUF_LIBRARY}
  264. ${OPENSSL_LIBRARIES}
  265. ${SASL_LIBRARIES}
  266. ${CMAKE_THREAD_LIBS_INIT}
  267. )
  268. if(BUILD_SHARED_HDFSPP)
  269. add_library(hdfspp SHARED ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  270. set_target_properties(hdfspp PROPERTIES SOVERSION ${LIBHDFSPP_VERSION})
  271. endif(BUILD_SHARED_HDFSPP)
  272. endif (HADOOP_BUILD)
  273. # Set up make install targets
  274. # Can be installed to a particular location via "make DESTDIR=... install"
  275. file(GLOB_RECURSE LIBHDFSPP_HEADER_FILES "${CMAKE_CURRENT_LIST_DIR}/include/*.h*")
  276. file(GLOB_RECURSE LIBHDFS_HEADER_FILES "${HADOOP_IMPORT_DIR}/include/*.h*")
  277. install(FILES ${LIBHDFSPP_HEADER_FILES} DESTINATION include/hdfspp)
  278. install(FILES ${LIBHDFS_HEADER_FILES} DESTINATION include/hdfs)
  279. install(TARGETS hdfspp_static ARCHIVE DESTINATION lib)
  280. if(BUILD_SHARED_HDFSPP)
  281. install(TARGETS hdfspp LIBRARY DESTINATION lib)
  282. endif(BUILD_SHARED_HDFSPP)
  283. add_custom_target(
  284. InstallToBuildDirectory
  285. COMMAND "${CMAKE_MAKE_PROGRAM}" install DESTDIR=${PROJECT_BINARY_DIR}/output
  286. )
  287. set(LIBHDFSPP_DIR ${PROJECT_BINARY_DIR}/output)