CMakeLists.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. if (NOT GENERATED_JAVAH)
  73. # Must identify where the generated headers have been placed
  74. MESSAGE(FATAL_ERROR "You must set the cmake variable GENERATED_JAVAH")
  75. endif (NOT GENERATED_JAVAH)
  76. find_package(JNI REQUIRED)
  77. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES CMAKE_FIND_LIBRARY_SUFFIXES)
  78. set_find_shared_library_version("1")
  79. find_package(ZLIB REQUIRED)
  80. SET(CMAKE_FIND_LIBRARY_SUFFIXES STORED_CMAKE_FIND_LIBRARY_SUFFIXES)
  81. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -O2")
  82. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_REENTRANT -D_GNU_SOURCE")
  83. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64")
  84. set(D main/native/src/org/apache/hadoop)
  85. set(T main/native/src/test/org/apache/hadoop)
  86. GET_FILENAME_COMPONENT(HADOOP_ZLIB_LIBRARY ${ZLIB_LIBRARIES} NAME)
  87. INCLUDE(CheckFunctionExists)
  88. INCLUDE(CheckCSourceCompiles)
  89. INCLUDE(CheckLibraryExists)
  90. CHECK_FUNCTION_EXISTS(sync_file_range HAVE_SYNC_FILE_RANGE)
  91. CHECK_FUNCTION_EXISTS(posix_fadvise HAVE_POSIX_FADVISE)
  92. CHECK_LIBRARY_EXISTS(dl dlopen "" NEED_LINK_DL)
  93. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES CMAKE_FIND_LIBRARY_SUFFIXES)
  94. set_find_shared_library_version("1")
  95. find_library(SNAPPY_LIBRARY
  96. NAMES snappy
  97. PATHS ${CUSTOM_SNAPPY_PREFIX} ${CUSTOM_SNAPPY_PREFIX}/lib
  98. ${CUSTOM_SNAPPY_PREFIX}/lib64 ${CUSTOM_SNAPPY_LIB})
  99. SET(CMAKE_FIND_LIBRARY_SUFFIXES STORED_CMAKE_FIND_LIBRARY_SUFFIXES)
  100. find_path(SNAPPY_INCLUDE_DIR
  101. NAMES snappy.h
  102. PATHS ${CUSTOM_SNAPPY_PREFIX} ${CUSTOM_SNAPPY_PREFIX}/include
  103. ${CUSTOM_SNAPPY_INCLUDE})
  104. if (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  105. GET_FILENAME_COMPONENT(HADOOP_SNAPPY_LIBRARY ${SNAPPY_LIBRARY} NAME)
  106. set(SNAPPY_SOURCE_FILES
  107. "${D}/io/compress/snappy/SnappyCompressor.c"
  108. "${D}/io/compress/snappy/SnappyDecompressor.c")
  109. else (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  110. set(SNAPPY_INCLUDE_DIR "")
  111. set(SNAPPY_SOURCE_FILES "")
  112. IF(REQUIRE_SNAPPY)
  113. 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}")
  114. ENDIF(REQUIRE_SNAPPY)
  115. endif (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  116. include_directories(
  117. ${GENERATED_JAVAH}
  118. main/native/src
  119. ${CMAKE_CURRENT_SOURCE_DIR}
  120. ${CMAKE_CURRENT_SOURCE_DIR}/src
  121. ${CMAKE_BINARY_DIR}
  122. ${JNI_INCLUDE_DIRS}
  123. ${ZLIB_INCLUDE_DIRS}
  124. ${SNAPPY_INCLUDE_DIR}
  125. ${D}/util
  126. )
  127. CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/config.h.cmake ${CMAKE_BINARY_DIR}/config.h)
  128. add_executable(test_bulk_crc32
  129. ${D}/util/bulk_crc32.c
  130. ${T}/util/test_bulk_crc32.c
  131. )
  132. set_property(SOURCE main.cpp PROPERTY INCLUDE_DIRECTORIES "\"-Werror\" \"-Wall\"")
  133. SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
  134. add_dual_library(hadoop
  135. ${D}/io/compress/lz4/Lz4Compressor.c
  136. ${D}/io/compress/lz4/Lz4Decompressor.c
  137. ${D}/io/compress/lz4/lz4.c
  138. ${SNAPPY_SOURCE_FILES}
  139. ${D}/io/compress/zlib/ZlibCompressor.c
  140. ${D}/io/compress/zlib/ZlibDecompressor.c
  141. ${D}/io/nativeio/NativeIO.c
  142. ${D}/io/nativeio/errno_enum.c
  143. ${D}/io/nativeio/file_descriptor.c
  144. ${D}/security/JniBasedUnixGroupsMapping.c
  145. ${D}/security/JniBasedUnixGroupsNetgroupMapping.c
  146. ${D}/security/getGroup.c
  147. ${D}/util/NativeCodeLoader.c
  148. ${D}/util/NativeCrc32.c
  149. ${D}/util/bulk_crc32.c
  150. )
  151. if (NEED_LINK_DL)
  152. set(LIB_DL dl)
  153. endif (NEED_LINK_DL)
  154. IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  155. #
  156. # By embedding '$ORIGIN' into the RPATH of libhadoop.so,
  157. # dlopen will look in the directory containing libhadoop.so.
  158. # However, $ORIGIN is not supported by all operating systems.
  159. #
  160. SET_TARGET_PROPERTIES(hadoop
  161. PROPERTIES INSTALL_RPATH "\$ORIGIN/")
  162. ENDIF()
  163. target_link_dual_libraries(hadoop
  164. ${LIB_DL}
  165. ${JAVA_JVM_LIBRARY}
  166. )
  167. SET(LIBHADOOP_VERSION "1.0.0")
  168. SET_TARGET_PROPERTIES(hadoop PROPERTIES
  169. SOVERSION ${LIBHADOOP_VERSION})
  170. dual_output_directory(hadoop target/usr/local/lib)