CMakeLists.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
  19. # Need this CMake policy for using the MultiThreaded MSVC runtime library.
  20. cmake_policy(SET CMP0091 NEW)
  21. # Need this CMake policy for invoking target_link_libraries on a target in any directory.
  22. cmake_policy(SET CMP0079 NEW)
  23. project(hadoop_hdfs_native_client)
  24. enable_testing()
  25. set(CMAKE_CXX_STANDARD 17)
  26. if (MSVC)
  27. # Instructs MSVC to use the static version of the runtime library.
  28. # More info - https://docs.microsoft.com/en-us/cpp/build/reference/md-mt-ld-use-run-time-library?view=msvc-170
  29. set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded")
  30. # This indicates that Windows 10 and above are supported.
  31. # More info - https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt?view=msvc-170
  32. set(NT_REQUIRED_VERSION 0x0A00)
  33. add_definitions(-D_WIN32_WINNT=${NT_REQUIRED_VERSION})
  34. # Windows.h defines the "min" and "max" macros which conflicts with the std::min and std::max from C++ STL.
  35. # Adding the below definition will disable this macro.
  36. add_definitions(-DNOMINMAX)
  37. endif (MSVC)
  38. list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/../../../hadoop-common-project/hadoop-common)
  39. include(HadoopCommon)
  40. # Check to see if our compiler and linker support the __thread attribute.
  41. # On Linux and some other operating systems, this is a more efficient
  42. # alternative to POSIX thread local storage.
  43. include(CheckCSourceCompiles)
  44. check_c_source_compiles("int main(void) { static __thread int i = 0; return 0; }" HAVE_BETTER_TLS)
  45. # Check to see if we have Intel SSE intrinsics.
  46. check_c_source_compiles("#include <emmintrin.h>\nint main(void) { __m128d sum0 = _mm_set_pd(0.0,0.0); return 0; }" HAVE_INTEL_SSE_INTRINSICS)
  47. set(_FUSE_DFS_VERSION 0.1.0)
  48. configure_file(${CMAKE_SOURCE_DIR}/config.h.cmake ${CMAKE_BINARY_DIR}/config.h)
  49. # Check if we need to link dl library to get dlopen.
  50. # dlopen on Linux is in separate library but on FreeBSD its in libc
  51. include(CheckLibraryExists)
  52. check_library_exists(dl dlopen "" NEED_LINK_DL)
  53. if(WIN32)
  54. # Set the optimizer level.
  55. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /O2")
  56. # Set warning level 4.
  57. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W4")
  58. # Skip "unreferenced formal parameter".
  59. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4100")
  60. # Skip "conditional expression is constant".
  61. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4127")
  62. # Skip deprecated POSIX function warnings.
  63. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_NONSTDC_NO_DEPRECATE")
  64. # Skip CRT non-secure function warnings. If we can convert usage of
  65. # strerror, getenv and ctime to their secure CRT equivalents, then we can
  66. # re-enable the CRT non-secure function warnings.
  67. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS")
  68. # Omit unneeded headers.
  69. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWIN32_LEAN_AND_MEAN")
  70. set(OS_DIR ${CMAKE_SOURCE_DIR}/main/native/libhdfs/os/windows)
  71. # IMPORTANT: OUT_DIR MUST be relative to maven's
  72. # project.build.directory (=target) and match dist-copynativelibs
  73. # in order to be in a release
  74. set(OUT_DIR bin)
  75. else()
  76. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
  77. # using old default behavior on GCC >= 10.0
  78. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fcommon")
  79. set(OS_DIR ${CMAKE_SOURCE_DIR}/main/native/libhdfs/os/posix)
  80. # IMPORTANT: OUT_DIR MUST be relative to maven's
  81. # project.build.directory (=target) and match dist-copynativelibs
  82. # in order to be in a release
  83. set(OUT_DIR native/target/usr/local/lib)
  84. endif()
  85. # Configure JNI.
  86. include(HadoopJNI)
  87. function(build_libhdfs_test NAME LIBRARY)
  88. set(FILES)
  89. foreach(FIL ${ARGN})
  90. if (IS_ABSOLUTE ${FIL})
  91. list(APPEND FILES ${FIL})
  92. else()
  93. list(APPEND FILES ${CMAKE_SOURCE_DIR}/main/native/libhdfs-tests/${FIL})
  94. endif()
  95. endforeach()
  96. add_executable("${NAME}_${LIBRARY}" $<TARGET_OBJECTS:x_platform_obj_c_api> $<TARGET_OBJECTS:x_platform_obj> ${FILES})
  97. target_include_directories("${NAME}_${LIBRARY}" PRIVATE main/native/libhdfspp/lib)
  98. endfunction()
  99. function(add_libhdfs_test NAME LIBRARY)
  100. add_test("test_${NAME}_${LIBRARY}" "${NAME}_${LIBRARY}")
  101. endfunction()
  102. function(link_libhdfs_test NAME LIBRARY)
  103. target_link_libraries("${NAME}_${LIBRARY}" ${LIBRARY} ${ARGN})
  104. endfunction()
  105. set(STORED_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  106. hadoop_set_find_shared_library_without_version()
  107. set(OPENSSL_NAME "crypto")
  108. if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  109. SET(OPENSSL_NAME "libcrypto")
  110. endif()
  111. message("CUSTOM_OPENSSL_PREFIX = ${CUSTOM_OPENSSL_PREFIX}")
  112. find_library(OPENSSL_LIBRARY
  113. NAMES ${OPENSSL_NAME}
  114. PATHS ${CUSTOM_OPENSSL_PREFIX} ${CUSTOM_OPENSSL_PREFIX}/lib
  115. ${CUSTOM_OPENSSL_PREFIX}/lib64 ${CUSTOM_OPENSSL_LIB} NO_DEFAULT_PATH)
  116. find_library(OPENSSL_LIBRARY NAMES ${OPENSSL_NAME})
  117. find_path(OPENSSL_INCLUDE_DIR
  118. NAMES openssl/evp.h
  119. PATHS ${CUSTOM_OPENSSL_PREFIX} ${CUSTOM_OPENSSL_PREFIX}/include
  120. ${CUSTOM_OPENSSL_INCLUDE} NO_DEFAULT_PATH)
  121. find_path(OPENSSL_INCLUDE_DIR NAMES openssl/evp.h)
  122. set(CMAKE_FIND_LIBRARY_SUFFIXES ${STORED_CMAKE_FIND_LIBRARY_SUFFIXES})
  123. set(USABLE_OPENSSL 0)
  124. if(OPENSSL_LIBRARY AND OPENSSL_INCLUDE_DIR)
  125. include(CheckCSourceCompiles)
  126. set(OLD_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES})
  127. set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
  128. set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARY})
  129. check_c_source_compiles("#include <openssl/evp.h>\nint main(int argc, char **argv) { return !EVP_aes_256_ctr; }" HAS_NEW_ENOUGH_OPENSSL)
  130. set(CMAKE_REQUIRED_INCLUDES ${OLD_CMAKE_REQUIRED_INCLUDES})
  131. if(NOT HAS_NEW_ENOUGH_OPENSSL)
  132. message("The OpenSSL library installed at ${OPENSSL_LIBRARY} is too old. You need a version at least new enough to have EVP_aes_256_ctr.")
  133. else()
  134. SET(USABLE_OPENSSL 1)
  135. endif()
  136. endif()
  137. if(USABLE_OPENSSL)
  138. get_filename_component(HADOOP_OPENSSL_LIBRARY ${OPENSSL_LIBRARY} NAME)
  139. set(OPENSSL_SOURCE_FILES
  140. "${SRC}/crypto/OpensslCipher.c"
  141. "${SRC}/crypto/random/OpensslSecureRandom.c")
  142. set(REQUIRE_OPENSSL ${REQUIRE_OPENSSL}) # Stop warning about unused variable.
  143. else()
  144. message("Cannot find a usable OpenSSL library. OPENSSL_LIBRARY=${OPENSSL_LIBRARY}, OPENSSL_INCLUDE_DIR=${OPENSSL_INCLUDE_DIR}, CUSTOM_OPENSSL_LIB=${CUSTOM_OPENSSL_LIB}, CUSTOM_OPENSSL_PREFIX=${CUSTOM_OPENSSL_PREFIX}, CUSTOM_OPENSSL_INCLUDE=${CUSTOM_OPENSSL_INCLUDE}")
  145. if(REQUIRE_OPENSSL)
  146. message(FATAL_ERROR "Terminating build because require.openssl was specified.")
  147. endif()
  148. set(OPENSSL_LIBRARY "")
  149. set(OPENSSL_INCLUDE_DIR "")
  150. set(OPENSSL_SOURCE_FILES "")
  151. endif()
  152. add_subdirectory(main/native/libhdfs)
  153. add_subdirectory(main/native/libhdfs-tests)
  154. add_subdirectory(main/native/libhdfs-examples)
  155. # Temporary fix to disable Libhdfs++ build on older systems that do not support thread_local
  156. include(CheckCXXSourceCompiles)
  157. unset (THREAD_LOCAL_SUPPORTED CACHE)
  158. set (CMAKE_CXX_STANDARD 11)
  159. set (CMAKE_CXX_STANDARD_REQUIRED ON)
  160. set (CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
  161. check_cxx_source_compiles(
  162. "#include <thread>
  163. int main(void) {
  164. thread_local int s;
  165. return 0;
  166. }"
  167. THREAD_LOCAL_SUPPORTED)
  168. if (THREAD_LOCAL_SUPPORTED)
  169. add_subdirectory(main/native/libhdfspp)
  170. else()
  171. message(WARNING
  172. "WARNING: Libhdfs++ library was not built because the required feature thread_local storage \
  173. is not supported by your compiler. Known compilers that support this feature: GCC 4.8+, Visual Studio 2015+, \
  174. Clang (community version 3.3+), Clang (version for Xcode 8+ and iOS 9+).")
  175. endif (THREAD_LOCAL_SUPPORTED)
  176. if(REQUIRE_LIBWEBHDFS)
  177. add_subdirectory(contrib/libwebhdfs)
  178. endif()
  179. # Find Linux FUSE
  180. if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  181. find_package(PkgConfig REQUIRED)
  182. pkg_check_modules(FUSE fuse)
  183. if(FUSE_FOUND)
  184. add_subdirectory(main/native/fuse-dfs)
  185. else()
  186. message(STATUS "Failed to find Linux FUSE libraries or include files. Will not build FUSE client.")
  187. if(REQUIRE_FUSE)
  188. message(FATAL_ERROR "Required component fuse_dfs could not be built.")
  189. endif()
  190. endif(FUSE_FOUND)
  191. else()
  192. message(STATUS "Non-Linux system detected. Will not build FUSE client.")
  193. if(REQUIRE_FUSE)
  194. message(FATAL_ERROR "Required component fuse_dfs could not be built.")
  195. endif()
  196. endif()