CMakeLists.txt 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. find_program(MEMORYCHECK_COMMAND valgrind HINTS ${VALGRIND_DIR} )
  39. set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full --error-exitcode=1")
  40. message(STATUS "valgrind location: ${MEMORYCHECK_COMMAND}")
  41. if (REQUIRE_VALGRIND AND MEMORYCHECK_COMMAND MATCHES "MEMORYCHECK_COMMAND-NOTFOUND" )
  42. message(FATAL_ERROR "valgrind was required but not found. "
  43. "The path can be included via a -DVALGRIND_DIR=... flag passed to CMake.")
  44. endif (REQUIRE_VALGRIND AND MEMORYCHECK_COMMAND MATCHES "MEMORYCHECK_COMMAND-NOTFOUND" )
  45. # Find the SASL library to use. If you don't want to require a sasl library,
  46. # define -DNO_SASL=1 in your cmake call
  47. # Prefer Cyrus SASL, but use GSASL if it is found
  48. # Note that the packages can be disabled by setting CMAKE_DISABLE_FIND_PACKAGE_GSasl or
  49. # CMAKE_DISABLE_FIND_PACKAGE_CyrusSASL, respectively (case sensitive)
  50. set (SASL_LIBRARIES)
  51. set (SASL_INCLUDE_DIR)
  52. if (NOT NO_SASL)
  53. if (CYRUS_SASL_FOUND)
  54. message(STATUS "Using Cyrus SASL; link with ${CYRUS_SASL_SHARED_LIB}")
  55. set (SASL_INCLUDE_DIR ${CYRUS_SASL_INCLUDE_DIR})
  56. set (SASL_LIBRARIES ${CYRUS_SASL_SHARED_LIB})
  57. set (CMAKE_USING_CYRUS_SASL 1)
  58. add_definitions(-DUSE_SASL -DUSE_CYRUS_SASL)
  59. else (CYRUS_SASL_FOUND)
  60. if (REQUIRE_CYRUS_SASL)
  61. message(FATAL_ERROR "Cyrus SASL was required but not found. "
  62. "The path can be included via a -DCYRUS_SASL_DIR=... flag passed to CMake.")
  63. endif (REQUIRE_CYRUS_SASL)
  64. # If we didn't pick Cyrus, use GSASL instead
  65. if (GSASL_FOUND)
  66. message(STATUS "Using GSASL; link with ${GSASL_LIBRARIES}")
  67. set (SASL_INCLUDE_DIR ${GSASL_INCLUDE_DIR})
  68. set (SASL_LIBRARIES ${GSASL_LIBRARIES})
  69. set (CMAKE_USING_GSASL 1)
  70. add_definitions(-DUSE_SASL -DUSE_GSASL)
  71. else (GSASL_FOUND)
  72. if (REQUIRE_GSASL)
  73. message(FATAL_ERROR "GSASL was required but not found. "
  74. "The path can be included via a -DGSASL_DIR=... flag passed to CMake.")
  75. endif (REQUIRE_GSASL)
  76. # No SASL was found, but NO_SASL was not defined
  77. message(FATAL_ERROR "Cound not find a SASL library (GSASL (gsasl) or Cyrus SASL (libsasl2). "
  78. "Install/configure one of them or define NO_SASL=1 in your cmake call")
  79. endif (GSASL_FOUND)
  80. endif (CYRUS_SASL_FOUND)
  81. else (NOT NO_SASL)
  82. message(STATUS "Compiling with NO SASL SUPPORT")
  83. endif (NOT NO_SASL)
  84. add_definitions(-DASIO_STANDALONE -DASIO_CPP11_DATE_TIME)
  85. # Disable optimizations if compiling debug
  86. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
  87. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
  88. if(UNIX)
  89. set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -std=c++11 -g -fPIC -fno-strict-aliasing")
  90. set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fPIC -fno-strict-aliasing")
  91. endif()
  92. # Mac OS 10.7 and later deprecates most of the methods in OpenSSL.
  93. # Add -Wno-deprecated-declarations to avoid the warnings.
  94. if(APPLE)
  95. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++ -Wno-deprecated-declarations -Wno-unused-local-typedef")
  96. endif()
  97. if(DOXYGEN_FOUND)
  98. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile @ONLY)
  99. add_custom_target(doc ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doc/Doxyfile
  100. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  101. COMMENT "Generating API documentation with Doxygen" VERBATIM)
  102. endif(DOXYGEN_FOUND)
  103. # Copy files from the hadoop tree into the output/extern directory if
  104. # they've changed
  105. function (copy_on_demand input_src_glob input_dest_dir)
  106. get_filename_component(src_glob ${input_src_glob} REALPATH)
  107. get_filename_component(dest_dir ${input_dest_dir} REALPATH)
  108. get_filename_component(src_dir ${src_glob} PATH)
  109. message(STATUS "Syncing ${src_glob} to ${dest_dir}")
  110. file(GLOB_RECURSE src_files ${src_glob})
  111. foreach(src_path ${src_files})
  112. file(RELATIVE_PATH relative_src ${src_dir} ${src_path})
  113. set(dest_path "${dest_dir}/${relative_src}")
  114. add_custom_command(TARGET copy_hadoop_files
  115. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${src_path}" "${dest_path}"
  116. )
  117. endforeach()
  118. endfunction()
  119. # If we're building in the hadoop tree, pull the Hadoop files that
  120. # libhdfspp depends on. This allows us to ensure that
  121. # the distribution will have a consistent set of headers and
  122. # .proto files
  123. if(HADOOP_BUILD)
  124. set(HADOOP_IMPORT_DIR ${PROJECT_BINARY_DIR}/extern)
  125. get_filename_component(HADOOP_IMPORT_DIR ${HADOOP_IMPORT_DIR} REALPATH)
  126. add_custom_target(copy_hadoop_files ALL)
  127. # Gather the Hadoop files and resources that libhdfs++ needs to build
  128. copy_on_demand(../libhdfs/include/*.h* ${HADOOP_IMPORT_DIR}/include)
  129. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../hadoop-hdfs-client/src/main/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hdfs)
  130. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../../hadoop-common-project/hadoop-common/src/main/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hadoop)
  131. copy_on_demand(${CMAKE_CURRENT_LIST_DIR}/../../../../../../hadoop-common-project/hadoop-common/src/test/proto/*.proto ${HADOOP_IMPORT_DIR}/proto/hadoop_test)
  132. else(HADOOP_BUILD)
  133. set(HADOOP_IMPORT_DIR ${CMAKE_CURRENT_LIST_DIR}/extern)
  134. endif(HADOOP_BUILD)
  135. # Paths to find the imported files
  136. set(PROTO_HDFS_DIR ${HADOOP_IMPORT_DIR}/proto/hdfs)
  137. set(PROTO_HADOOP_DIR ${HADOOP_IMPORT_DIR}/proto/hadoop)
  138. set(PROTO_HADOOP_TEST_DIR ${HADOOP_IMPORT_DIR}/proto/hadoop_test)
  139. include_directories(
  140. include
  141. lib
  142. ${HADOOP_IMPORT_DIR}/include
  143. )
  144. include_directories( SYSTEM
  145. ${PROJECT_BINARY_DIR}/lib/proto
  146. third_party/asio-1.10.2/include
  147. third_party/rapidxml-1.13
  148. third_party/gmock-1.7.0
  149. third_party/tr2
  150. third_party/protobuf
  151. third_party/uriparser2
  152. ${OPENSSL_INCLUDE_DIR}
  153. ${SASL_INCLUDE_DIR}
  154. ${PROTOBUF_INCLUDE_DIRS}
  155. )
  156. add_subdirectory(third_party/gmock-1.7.0)
  157. add_subdirectory(third_party/uriparser2)
  158. add_subdirectory(lib)
  159. if(NOT HDFSPP_LIBRARY_ONLY)
  160. add_subdirectory(tests)
  161. add_subdirectory(examples)
  162. add_subdirectory(tools)
  163. endif()
  164. # create an empty file; hadoop_add_dual_library wraps add_library which
  165. # requires at least one file as an argument
  166. set(EMPTY_FILE_CC ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/empty.cc)
  167. file(WRITE ${EMPTY_FILE_CC} "")
  168. # Build the output libraries
  169. if(NEED_LINK_DL)
  170. set(LIB_DL dl)
  171. endif()
  172. 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>)
  173. if (HADOOP_BUILD)
  174. hadoop_add_dual_library(hdfspp ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  175. hadoop_target_link_dual_libraries(hdfspp
  176. ${LIB_DL}
  177. ${PROTOBUF_LIBRARY}
  178. ${OPENSSL_LIBRARIES}
  179. ${SASL_LIBRARIES}
  180. ${CMAKE_THREAD_LIBS_INIT}
  181. )
  182. else (HADOOP_BUILD)
  183. add_library(hdfspp_static STATIC ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  184. target_link_libraries(hdfspp_static
  185. ${LIB_DL}
  186. ${PROTOBUF_LIBRARY}
  187. ${OPENSSL_LIBRARIES}
  188. ${SASL_LIBRARIES}
  189. ${CMAKE_THREAD_LIBS_INIT}
  190. )
  191. add_library(hdfspp SHARED ${EMPTY_FILE_CC} ${LIBHDFSPP_ALL_OBJECTS})
  192. target_link_libraries(hdfspp_static
  193. ${LIB_DL}
  194. ${PROTOBUF_LIBRARY}
  195. ${OPENSSL_LIBRARIES}
  196. ${SASL_LIBRARIES}
  197. ${CMAKE_THREAD_LIBS_INIT}
  198. )
  199. endif (HADOOP_BUILD)
  200. set(LIBHDFSPP_VERSION "0.1.0")
  201. set_target_properties(hdfspp PROPERTIES
  202. SOVERSION ${LIBHDFSPP_VERSION})
  203. # Set up make install targets
  204. # Can be installed to a particular location via "make DESTDIR=... install"
  205. file(GLOB_RECURSE LIBHDFSPP_HEADER_FILES "${CMAKE_CURRENT_LIST_DIR}/include/*.h*")
  206. file(GLOB_RECURSE LIBHDFS_HEADER_FILES "${HADOOP_IMPORT_DIR}/include/*.h*")
  207. install(FILES ${LIBHDFSPP_HEADER_FILES} DESTINATION include/hdfspp)
  208. install(FILES ${LIBHDFS_HEADER_FILES} DESTINATION include/hdfs)
  209. install(TARGETS hdfspp_static ARCHIVE DESTINATION lib)
  210. install(TARGETS hdfspp LIBRARY DESTINATION lib)
  211. add_custom_target(
  212. InstallToBuildDirectory
  213. COMMAND "${CMAKE_MAKE_PROGRAM}" install DESTDIR=${PROJECT_BINARY_DIR}/output
  214. )
  215. set(LIBHDFSPP_DIR ${PROJECT_BINARY_DIR}/output)