CMakeLists.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 COMPONENTS date_time)
  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. include(FetchContent)
  46. # Install googletest
  47. # As per the approach documented in the GoogleTest repo -
  48. # https://github.com/google/googletest/blob/e649993a402d96afe25fbf3413749adf0f2947f6/googletest/README.md#incorporating-into-an-existing-cmake-project
  49. FetchContent_Declare(
  50. googletest
  51. URL https://github.com/google/googletest/archive/703bd9caab50b139428cea1aaff9974ebee5742e.zip
  52. )
  53. # The commit SHA 703bd9caab50b139428cea1aaff9974ebee5742e in the URL above corresponds to the
  54. # tag release-1.10.0 in the googletest repository.
  55. # For Windows: Prevent overriding the parent project's compiler/linker settings
  56. set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  57. FetchContent_MakeAvailable(googletest)
  58. # Check if thread_local is supported
  59. unset (THREAD_LOCAL_SUPPORTED CACHE)
  60. set (CMAKE_CXX_STANDARD_REQUIRED ON)
  61. set (CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  62. check_cxx_source_compiles(
  63. "#include <thread>
  64. int main(void) {
  65. thread_local int s;
  66. return 0;
  67. }"
  68. THREAD_LOCAL_SUPPORTED)
  69. if (NOT THREAD_LOCAL_SUPPORTED)
  70. message(FATAL_ERROR
  71. "FATAL ERROR: The required feature thread_local storage is not supported by your compiler. \
  72. Known compilers that support this feature: GCC 4.8+, Visual Studio 2015+, Clang (community \
  73. version 3.3+), Clang (version for Xcode 8+ and iOS 9+).")
  74. endif (NOT THREAD_LOCAL_SUPPORTED)
  75. # Check if PROTOC library was compiled with the compatible compiler by trying
  76. # to compile some dummy code
  77. unset (PROTOC_IS_COMPATIBLE CACHE)
  78. set (CMAKE_REQUIRED_INCLUDES ${PROTOBUF_INCLUDE_DIRS})
  79. set (CMAKE_REQUIRED_LIBRARIES ${PROTOBUF_LIBRARY} ${PROTOBUF_PROTOC_LIBRARY})
  80. check_cxx_source_compiles(
  81. "#include <google/protobuf/io/printer.h>
  82. #include <string>
  83. int main(void) {
  84. ::google::protobuf::io::ZeroCopyOutputStream *out = NULL;
  85. ::google::protobuf::io::Printer printer(out, '$');
  86. printer.PrintRaw(std::string(\"test\"));
  87. return 0;
  88. }"
  89. PROTOC_IS_COMPATIBLE)
  90. if (NOT PROTOC_IS_COMPATIBLE)
  91. message(WARNING
  92. "WARNING: the Protocol Buffers Library and the Libhdfs++ Library must both be compiled \
  93. with the same (or compatible) compiler. Normally only the same major versions of the same \
  94. compiler are compatible with each other.")
  95. endif (NOT PROTOC_IS_COMPATIBLE)
  96. find_program(MEMORYCHECK_COMMAND valgrind HINTS ${VALGRIND_DIR} )
  97. set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=no --leak-check=full --error-exitcode=1 --suppressions=${PROJECT_SOURCE_DIR}/tests/memcheck.supp")
  98. message(STATUS "valgrind location: ${MEMORYCHECK_COMMAND}")
  99. if (REQUIRE_VALGRIND AND MEMORYCHECK_COMMAND MATCHES "MEMORYCHECK_COMMAND-NOTFOUND" )
  100. message(FATAL_ERROR "valgrind was required but not found. "
  101. "The path can be included via a -DVALGRIND_DIR=... flag passed to CMake.")
  102. endif (REQUIRE_VALGRIND AND MEMORYCHECK_COMMAND MATCHES "MEMORYCHECK_COMMAND-NOTFOUND" )
  103. # Find the SASL library to use. If you don't want to require a sasl library,
  104. # define -DNO_SASL=1 in your cmake call
  105. # Prefer Cyrus SASL, but use GSASL if it is found
  106. # Note that the packages can be disabled by setting CMAKE_DISABLE_FIND_PACKAGE_GSasl or
  107. # CMAKE_DISABLE_FIND_PACKAGE_CyrusSASL, respectively (case sensitive)
  108. set (SASL_LIBRARIES)
  109. set (SASL_INCLUDE_DIR)
  110. if (NOT NO_SASL)
  111. if (CYRUS_SASL_FOUND)
  112. message(STATUS "Using Cyrus SASL; link with ${CYRUS_SASL_SHARED_LIB}")
  113. set (SASL_INCLUDE_DIR ${CYRUS_SASL_INCLUDE_DIR})
  114. set (SASL_LIBRARIES ${CYRUS_SASL_SHARED_LIB})
  115. set (CMAKE_USING_CYRUS_SASL 1)
  116. add_definitions(-DUSE_SASL -DUSE_CYRUS_SASL)
  117. else (CYRUS_SASL_FOUND)
  118. if (REQUIRE_CYRUS_SASL)
  119. message(FATAL_ERROR "Cyrus SASL was required but not found. "
  120. "The path can be included via a -DCYRUS_SASL_DIR=... flag passed to CMake.")
  121. endif (REQUIRE_CYRUS_SASL)
  122. # If we didn't pick Cyrus, use GSASL instead
  123. if (GSASL_FOUND)
  124. message(STATUS "Using GSASL; link with ${GSASL_LIBRARIES}")
  125. set (SASL_INCLUDE_DIR ${GSASL_INCLUDE_DIR})
  126. set (SASL_LIBRARIES ${GSASL_LIBRARIES})
  127. set (CMAKE_USING_GSASL 1)
  128. add_definitions(-DUSE_SASL -DUSE_GSASL)
  129. else (GSASL_FOUND)
  130. if (REQUIRE_GSASL)
  131. message(FATAL_ERROR "GSASL was required but not found. "
  132. "The path can be included via a -DGSASL_DIR=... flag passed to CMake.")
  133. endif (REQUIRE_GSASL)
  134. # No SASL was found, but NO_SASL was not defined
  135. message(FATAL_ERROR "Cound not find a SASL library (GSASL (gsasl) or Cyrus SASL (libsasl2). "
  136. "Install/configure one of them or define NO_SASL=1 in your cmake call")
  137. endif (GSASL_FOUND)
  138. endif (CYRUS_SASL_FOUND)
  139. else (NOT NO_SASL)
  140. message(STATUS "Compiling with NO SASL SUPPORT")
  141. endif (NOT NO_SASL)
  142. check_symbol_exists(explicit_bzero "string.h" HAVE_EXPLICIT_BZERO)
  143. if(HAVE_EXPLICIT_BZERO)
  144. add_definitions(-DHAVE_EXPLICIT_BZERO)
  145. endif()
  146. add_definitions(-DASIO_STANDALONE -DASIO_CPP11_DATE_TIME)
  147. # Disable optimizations if compiling debug
  148. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
  149. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
  150. if(UNIX)
  151. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -g -fPIC -fno-strict-aliasing")
  152. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fPIC -fno-strict-aliasing")
  153. endif()
  154. if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  155. 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)
  156. endif ()
  157. # Mac OS 10.7 and later deprecates most of the methods in OpenSSL.
  158. # Add -Wno-deprecated-declarations to avoid the warnings.
  159. if(APPLE)
  160. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -Wno-deprecated-declarations -Wno-unused-local-typedef")
  161. endif()
  162. if(DOXYGEN_FOUND)
  163. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile @ONLY)
  164. add_custom_target(doc ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile
  165. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  166. COMMENT "Generating API documentation with Doxygen" VERBATIM)
  167. endif(DOXYGEN_FOUND)
  168. # Copy files from the hadoop tree into the output/extern directory if
  169. # they've changed
  170. function (copy_on_demand input_src_glob input_dest_dir)
  171. get_filename_component(src_glob ${input_src_glob} REALPATH)
  172. get_filename_component(dest_dir ${input_dest_dir} REALPATH)
  173. get_filename_component(src_dir ${src_glob} PATH)
  174. message(STATUS "Syncing ${src_glob} to ${dest_dir}")
  175. file(GLOB_RECURSE src_files ${src_glob})
  176. foreach(src_path ${src_files})
  177. file(RELATIVE_PATH relative_src ${src_dir} ${src_path})
  178. set(dest_path "${dest_dir}/${relative_src}")
  179. add_custom_command(TARGET copy_hadoop_files
  180. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${src_path}" "${dest_path}"
  181. )
  182. endforeach()
  183. endfunction()
  184. # If we're building in the hadoop tree, pull the Hadoop files that
  185. # libhdfspp depends on. This allows us to ensure that
  186. # the distribution will have a consistent set of headers and
  187. # .proto files
  188. if(HADOOP_BUILD)
  189. set(HADOOP_IMPORT_DIR ${PROJECT_BINARY_DIR}/extern)
  190. get_filename_component(HADOOP_IMPORT_DIR ${HADOOP_IMPORT_DIR} REALPATH)
  191. add_custom_target(copy_hadoop_files ALL)
  192. # Gather the Hadoop files and resources that libhdfs++ needs to build
  193. copy_on_demand(../libhdfs/include/*.h* ${HADOOP_IMPORT_DIR}/include)
  194. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../hadoop-hdfs-client/src/main/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hdfs)
  195. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../../hadoop-common-project/hadoop-common/src/main/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hadoop)
  196. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../../hadoop-common-project/hadoop-common/src/test/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hadoop_test)
  197. else(HADOOP_BUILD)
  198. set(HADOOP_IMPORT_DIR ${CMAKE_CURRENT_LIST_DIR}/extern)
  199. endif(HADOOP_BUILD)
  200. # Paths to find the imported files
  201. set(PROTO_HDFS_DIR ${HADOOP_IMPORT_DIR}/proto/hdfs)
  202. set(PROTO_HADOOP_DIR ${HADOOP_IMPORT_DIR}/proto/hadoop)
  203. set(PROTO_HADOOP_TEST_DIR ${HADOOP_IMPORT_DIR}/proto/hadoop_test)
  204. include_directories(
  205. include
  206. lib
  207. ${HADOOP_IMPORT_DIR}/include
  208. )
  209. include_directories( SYSTEM
  210. ${PROJECT_BINARY_DIR}/lib/proto
  211. ${Boost_INCLUDE_DIRS}
  212. third_party/rapidxml-1.13
  213. ${gtest_SOURCE_DIR}/include
  214. ${gmock_SOURCE_DIR}/include
  215. third_party/tr2
  216. third_party/protobuf
  217. third_party/uriparser2
  218. ${OPENSSL_INCLUDE_DIR}
  219. ${SASL_INCLUDE_DIR}
  220. ${PROTOBUF_INCLUDE_DIRS}
  221. )
  222. add_subdirectory(third_party/uriparser2)
  223. add_subdirectory(lib)
  224. if(NOT HDFSPP_LIBRARY_ONLY)
  225. add_subdirectory(tests)
  226. add_subdirectory(examples)
  227. add_subdirectory(tools)
  228. endif()
  229. # create an empty file; hadoop_add_dual_library wraps add_library which
  230. # requires at least one file as an argument
  231. set(EMPTY_FILE_CC ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/empty.cc)
  232. file(WRITE ${EMPTY_FILE_CC} "")
  233. # Build the output libraries
  234. if(NEED_LINK_DL)
  235. set(LIB_DL dl)
  236. endif()
  237. set(LIBHDFSPP_VERSION "0.1.0")
  238. 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>)
  239. # HDFS-16464: We don't support building Hadoop DLL for Windows yet.
  240. if (HADOOP_BUILD AND NOT MSVC)
  241. hadoop_add_dual_library(hdfspp ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  242. hadoop_target_link_dual_libraries(hdfspp
  243. ${LIB_DL}
  244. ${PROTOBUF_LIBRARY}
  245. ${OPENSSL_LIBRARIES}
  246. ${SASL_LIBRARIES}
  247. ${CMAKE_THREAD_LIBS_INIT}
  248. ${Boost_LIBRARIES})
  249. set_target_properties(hdfspp PROPERTIES SOVERSION ${LIBHDFSPP_VERSION})
  250. hadoop_dual_output_directory(hdfspp ${OUT_DIR})
  251. else (HADOOP_BUILD AND NOT MSVC)
  252. add_library(hdfspp_static STATIC ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  253. target_link_libraries(hdfspp_static PUBLIC
  254. ${LIB_DL}
  255. ${PROTOBUF_LIBRARY}
  256. ${OPENSSL_LIBRARIES}
  257. ${SASL_LIBRARIES}
  258. ${CMAKE_THREAD_LIBS_INIT}
  259. ${Boost_LIBRARIES})
  260. if(BUILD_SHARED_HDFSPP)
  261. add_library(hdfspp SHARED ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  262. set_target_properties(hdfspp PROPERTIES SOVERSION ${LIBHDFSPP_VERSION})
  263. endif(BUILD_SHARED_HDFSPP)
  264. endif (HADOOP_BUILD AND NOT MSVC)
  265. # Set up make install targets
  266. # Can be installed to a particular location via "make DESTDIR=... install"
  267. file(GLOB_RECURSE LIBHDFSPP_HEADER_FILES "${CMAKE_CURRENT_LIST_DIR}/include/*.h*")
  268. file(GLOB_RECURSE LIBHDFS_HEADER_FILES "${HADOOP_IMPORT_DIR}/include/*.h*")
  269. install(FILES ${LIBHDFSPP_HEADER_FILES} DESTINATION include/hdfspp)
  270. install(FILES ${LIBHDFS_HEADER_FILES} DESTINATION include/hdfs)
  271. install(TARGETS hdfspp_static ARCHIVE DESTINATION lib)
  272. if(BUILD_SHARED_HDFSPP)
  273. install(TARGETS hdfspp LIBRARY DESTINATION lib)
  274. endif(BUILD_SHARED_HDFSPP)
  275. add_custom_target(
  276. InstallToBuildDirectory
  277. COMMAND "${CMAKE_MAKE_PROGRAM}" install DESTDIR=${PROJECT_BINARY_DIR}/output
  278. )
  279. set(LIBHDFSPP_DIR ${PROJECT_BINARY_DIR}/output)