CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 2.6 FATAL_ERROR)
  19. # Default to release builds
  20. set(CMAKE_BUILD_TYPE, Release)
  21. include(JNIFlags.cmake NO_POLICY_SCOPE)
  22. # Compile a library with both shared and static variants
  23. function(add_dual_library LIBNAME)
  24. add_library(${LIBNAME} SHARED ${ARGN})
  25. add_library(${LIBNAME}_static STATIC ${ARGN})
  26. set_target_properties(${LIBNAME}_static PROPERTIES OUTPUT_NAME ${LIBNAME})
  27. endfunction(add_dual_library)
  28. # Link both a static and a dynamic target against some libraries
  29. function(target_link_dual_libraries LIBNAME)
  30. target_link_libraries(${LIBNAME} ${ARGN})
  31. target_link_libraries(${LIBNAME}_static ${ARGN})
  32. endfunction(target_link_dual_libraries)
  33. function(output_directory TGT DIR)
  34. SET_TARGET_PROPERTIES(${TGT} PROPERTIES
  35. RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${DIR}")
  36. SET_TARGET_PROPERTIES(${TGT} PROPERTIES
  37. ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${DIR}")
  38. SET_TARGET_PROPERTIES(${TGT} PROPERTIES
  39. LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${DIR}")
  40. endfunction(output_directory TGT DIR)
  41. function(dual_output_directory TGT DIR)
  42. output_directory(${TGT} "${DIR}")
  43. output_directory(${TGT}_static "${DIR}")
  44. endfunction(dual_output_directory TGT DIR)
  45. #
  46. # This macro alters the behavior of find_package and find_library.
  47. # It does this by setting the CMAKE_FIND_LIBRARY_SUFFIXES global variable.
  48. # You should save that variable before calling this function and restore it
  49. # after you have accomplished your goal.
  50. #
  51. # The behavior is altered in two ways:
  52. # 1. We always find shared libraries, never static;
  53. # 2. We find shared libraries with the given version number.
  54. #
  55. # On Windows this function is a no-op. Windows does not encode
  56. # version number information information into library path names.
  57. #
  58. macro(set_find_shared_library_version LVERS)
  59. IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  60. # Mac OS uses .dylib
  61. SET(CMAKE_FIND_LIBRARY_SUFFIXES ".${LVERS}.dylib")
  62. ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
  63. # FreeBSD has always .so installed.
  64. SET(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
  65. ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  66. # Windows doesn't support finding shared libraries by version.
  67. ELSE()
  68. # Most UNIX variants use .so
  69. SET(CMAKE_FIND_LIBRARY_SUFFIXES ".so.${LVERS}")
  70. ENDIF()
  71. endmacro(set_find_shared_library_version LVERS)
  72. #
  73. # Alter the behavior of find_package and find_library so that we find only
  74. # shared libraries without any version suffix. You should save
  75. # CMAKE_FIND_LIBRARY_SUFFIXES before calling this function and restore it
  76. # afterwards.
  77. #
  78. macro(set_find_shared_library_without_version)
  79. IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  80. # Mac OS uses .dylib
  81. SET(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib")
  82. ELSEIF(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  83. # No effect
  84. ELSE()
  85. # Most UNIX variants use .so
  86. SET(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
  87. ENDIF()
  88. endmacro(set_find_shared_library_version LVERS)
  89. if (NOT GENERATED_JAVAH)
  90. # Must identify where the generated headers have been placed
  91. MESSAGE(FATAL_ERROR "You must set the cmake variable GENERATED_JAVAH")
  92. endif (NOT GENERATED_JAVAH)
  93. find_package(JNI REQUIRED)
  94. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  95. set_find_shared_library_version("1")
  96. find_package(ZLIB REQUIRED)
  97. SET(CMAKE_FIND_LIBRARY_SUFFIXES ${STORED_CMAKE_FIND_LIBRARY_SUFFIXES})
  98. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2")
  99. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_REENTRANT -D_GNU_SOURCE")
  100. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64")
  101. set(D main/native/src/org/apache/hadoop)
  102. set(T main/native/src/test/org/apache/hadoop)
  103. GET_FILENAME_COMPONENT(HADOOP_ZLIB_LIBRARY ${ZLIB_LIBRARIES} NAME)
  104. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  105. set_find_shared_library_version("1")
  106. find_package(BZip2 QUIET)
  107. if (BZIP2_INCLUDE_DIR AND BZIP2_LIBRARIES)
  108. GET_FILENAME_COMPONENT(HADOOP_BZIP2_LIBRARY ${BZIP2_LIBRARIES} NAME)
  109. set(BZIP2_SOURCE_FILES
  110. "${D}/io/compress/bzip2/Bzip2Compressor.c"
  111. "${D}/io/compress/bzip2/Bzip2Decompressor.c")
  112. else (BZIP2_INCLUDE_DIR AND BZIP2_LIBRARIES)
  113. set(BZIP2_SOURCE_FILES "")
  114. set(BZIP2_INCLUDE_DIR "")
  115. IF(REQUIRE_BZIP2)
  116. MESSAGE(FATAL_ERROR "Required bzip2 library and/or header files could not be found.")
  117. ENDIF(REQUIRE_BZIP2)
  118. endif (BZIP2_INCLUDE_DIR AND BZIP2_LIBRARIES)
  119. SET(CMAKE_FIND_LIBRARY_SUFFIXES ${STORED_CMAKE_FIND_LIBRARY_SUFFIXES})
  120. INCLUDE(CheckFunctionExists)
  121. INCLUDE(CheckCSourceCompiles)
  122. INCLUDE(CheckLibraryExists)
  123. CHECK_FUNCTION_EXISTS(sync_file_range HAVE_SYNC_FILE_RANGE)
  124. CHECK_FUNCTION_EXISTS(posix_fadvise HAVE_POSIX_FADVISE)
  125. CHECK_LIBRARY_EXISTS(dl dlopen "" NEED_LINK_DL)
  126. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  127. set_find_shared_library_version("1")
  128. find_library(SNAPPY_LIBRARY
  129. NAMES snappy
  130. PATHS ${CUSTOM_SNAPPY_PREFIX} ${CUSTOM_SNAPPY_PREFIX}/lib
  131. ${CUSTOM_SNAPPY_PREFIX}/lib64 ${CUSTOM_SNAPPY_LIB})
  132. SET(CMAKE_FIND_LIBRARY_SUFFIXES ${STORED_CMAKE_FIND_LIBRARY_SUFFIXES})
  133. find_path(SNAPPY_INCLUDE_DIR
  134. NAMES snappy.h
  135. PATHS ${CUSTOM_SNAPPY_PREFIX} ${CUSTOM_SNAPPY_PREFIX}/include
  136. ${CUSTOM_SNAPPY_INCLUDE})
  137. if (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  138. GET_FILENAME_COMPONENT(HADOOP_SNAPPY_LIBRARY ${SNAPPY_LIBRARY} NAME)
  139. set(SNAPPY_SOURCE_FILES
  140. "${D}/io/compress/snappy/SnappyCompressor.c"
  141. "${D}/io/compress/snappy/SnappyDecompressor.c")
  142. else (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  143. set(SNAPPY_INCLUDE_DIR "")
  144. set(SNAPPY_SOURCE_FILES "")
  145. IF(REQUIRE_SNAPPY)
  146. MESSAGE(FATAL_ERROR "Required snappy library could not be found. SNAPPY_LIBRARY=${SNAPPY_LIBRARY}, SNAPPY_INCLUDE_DIR=${SNAPPY_INCLUDE_DIR}, CUSTOM_SNAPPY_INCLUDE_DIR=${CUSTOM_SNAPPY_INCLUDE_DIR}, CUSTOM_SNAPPY_PREFIX=${CUSTOM_SNAPPY_PREFIX}, CUSTOM_SNAPPY_INCLUDE=${CUSTOM_SNAPPY_INCLUDE}")
  147. ENDIF(REQUIRE_SNAPPY)
  148. endif (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  149. # Find the no-suffix version of libcrypto.
  150. # See HADOOP-11216 for details.
  151. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  152. set_find_shared_library_without_version()
  153. SET(OPENSSL_NAME "crypto")
  154. IF(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
  155. SET(OPENSSL_NAME "eay32")
  156. ENDIF()
  157. MESSAGE("CUSTOM_OPENSSL_PREFIX = ${CUSTOM_OPENSSL_PREFIX}")
  158. find_library(OPENSSL_LIBRARY
  159. NAMES ${OPENSSL_NAME}
  160. PATHS ${CUSTOM_OPENSSL_PREFIX} ${CUSTOM_OPENSSL_PREFIX}/lib
  161. ${CUSTOM_OPENSSL_PREFIX}/lib64 ${CUSTOM_OPENSSL_LIB} NO_DEFAULT_PATH)
  162. find_library(OPENSSL_LIBRARY NAMES ${OPENSSL_NAME})
  163. find_path(OPENSSL_INCLUDE_DIR
  164. NAMES openssl/evp.h
  165. PATHS ${CUSTOM_OPENSSL_PREFIX} ${CUSTOM_OPENSSL_PREFIX}/include
  166. ${CUSTOM_OPENSSL_INCLUDE} NO_DEFAULT_PATH)
  167. find_path(OPENSSL_INCLUDE_DIR NAMES openssl/evp.h)
  168. SET(CMAKE_FIND_LIBRARY_SUFFIXES ${STORED_CMAKE_FIND_LIBRARY_SUFFIXES})
  169. SET(USABLE_OPENSSL 0)
  170. if (OPENSSL_LIBRARY AND OPENSSL_INCLUDE_DIR)
  171. GET_FILENAME_COMPONENT(HADOOP_OPENSSL_LIBRARY ${OPENSSL_LIBRARY} NAME)
  172. INCLUDE(CheckCSourceCompiles)
  173. SET(OLD_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES})
  174. SET(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
  175. CHECK_C_SOURCE_COMPILES("#include \"${OPENSSL_INCLUDE_DIR}/openssl/evp.h\"\nint main(int argc, char **argv) { return !EVP_aes_256_ctr; }" HAS_NEW_ENOUGH_OPENSSL)
  176. SET(CMAKE_REQUIRED_INCLUDES ${OLD_CMAKE_REQUIRED_INCLUDES})
  177. if(NOT HAS_NEW_ENOUGH_OPENSSL)
  178. 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.")
  179. else(NOT HAS_NEW_ENOUGH_OPENSSL)
  180. SET(USABLE_OPENSSL 1)
  181. endif(NOT HAS_NEW_ENOUGH_OPENSSL)
  182. endif (OPENSSL_LIBRARY AND OPENSSL_INCLUDE_DIR)
  183. if (USABLE_OPENSSL)
  184. SET(OPENSSL_SOURCE_FILES
  185. "${D}/crypto/OpensslCipher.c"
  186. "${D}/crypto/random/OpensslSecureRandom.c")
  187. else (USABLE_OPENSSL)
  188. MESSAGE("Cannot find a usable OpenSSL library. OPENSSL_LIBRARY=${OPENSSL_LIBRARY}, OPENSSL_INCLUDE_DIR=${OPENSSL_INCLUDE_DIR}, CUSTOM_OPENSSL_INCLUDE_DIR=${CUSTOM_OPENSSL_INCLUDE_DIR}, CUSTOM_OPENSSL_PREFIX=${CUSTOM_OPENSSL_PREFIX}, CUSTOM_OPENSSL_INCLUDE=${CUSTOM_OPENSSL_INCLUDE}")
  189. IF(REQUIRE_OPENSSL)
  190. MESSAGE(FATAL_ERROR "Terminating build because require.openssl was specified.")
  191. ENDIF(REQUIRE_OPENSSL)
  192. SET(OPENSSL_LIBRARY "")
  193. SET(OPENSSL_INCLUDE_DIR "")
  194. SET(OPENSSL_SOURCE_FILES "")
  195. endif (USABLE_OPENSSL)
  196. include_directories(
  197. ${GENERATED_JAVAH}
  198. main/native/src
  199. ${CMAKE_CURRENT_SOURCE_DIR}
  200. ${CMAKE_CURRENT_SOURCE_DIR}/src
  201. ${CMAKE_BINARY_DIR}
  202. ${JNI_INCLUDE_DIRS}
  203. ${ZLIB_INCLUDE_DIRS}
  204. ${BZIP2_INCLUDE_DIR}
  205. ${SNAPPY_INCLUDE_DIR}
  206. ${OPENSSL_INCLUDE_DIR}
  207. ${D}/util
  208. )
  209. CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/config.h.cmake ${CMAKE_BINARY_DIR}/config.h)
  210. add_executable(test_bulk_crc32
  211. ${D}/util/bulk_crc32.c
  212. ${T}/util/test_bulk_crc32.c
  213. )
  214. SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
  215. add_dual_library(hadoop
  216. main/native/src/exception.c
  217. ${D}/io/compress/lz4/Lz4Compressor.c
  218. ${D}/io/compress/lz4/Lz4Decompressor.c
  219. ${D}/io/compress/lz4/lz4.c
  220. ${D}/io/compress/lz4/lz4hc.c
  221. ${SNAPPY_SOURCE_FILES}
  222. ${OPENSSL_SOURCE_FILES}
  223. ${D}/io/compress/zlib/ZlibCompressor.c
  224. ${D}/io/compress/zlib/ZlibDecompressor.c
  225. ${BZIP2_SOURCE_FILES}
  226. ${D}/io/nativeio/NativeIO.c
  227. ${D}/io/nativeio/errno_enum.c
  228. ${D}/io/nativeio/file_descriptor.c
  229. ${D}/io/nativeio/SharedFileDescriptorFactory.c
  230. ${D}/net/unix/DomainSocket.c
  231. ${D}/net/unix/DomainSocketWatcher.c
  232. ${D}/security/JniBasedUnixGroupsMapping.c
  233. ${D}/security/JniBasedUnixGroupsNetgroupMapping.c
  234. ${D}/security/hadoop_group_info.c
  235. ${D}/security/hadoop_user_info.c
  236. ${D}/util/NativeCodeLoader.c
  237. ${D}/util/NativeCrc32.c
  238. ${D}/util/bulk_crc32.c
  239. )
  240. if (NEED_LINK_DL)
  241. set(LIB_DL dl)
  242. endif (NEED_LINK_DL)
  243. IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  244. #
  245. # By embedding '$ORIGIN' into the RPATH of libhadoop.so,
  246. # dlopen will look in the directory containing libhadoop.so.
  247. # However, $ORIGIN is not supported by all operating systems.
  248. #
  249. SET_TARGET_PROPERTIES(hadoop
  250. PROPERTIES INSTALL_RPATH "\$ORIGIN/")
  251. ENDIF()
  252. target_link_dual_libraries(hadoop
  253. ${LIB_DL}
  254. ${JAVA_JVM_LIBRARY}
  255. )
  256. SET(LIBHADOOP_VERSION "1.0.0")
  257. SET_TARGET_PROPERTIES(hadoop PROPERTIES
  258. SOVERSION ${LIBHADOOP_VERSION})
  259. dual_output_directory(hadoop target/usr/local/lib)