CMakeLists.txt 12 KB

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