CMakeLists.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. SET(CMAKE_FIND_LIBRARY_SUFFIXES STORED_CMAKE_FIND_LIBRARY_SUFFIXES)
  80. # primitive configs
  81. set(PRFLAGS "-DSIMPLE_MEMCPY")
  82. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PRFLAGS} -fno-strict-aliasing -Wall -Wno-sign-compare")
  83. set(CMAKE_LD_FLAGS "${CMAKE_LD_FLAGS} -no-undefined -version-info 0:1:0
  84. -L${_JAVA_HOME}/jre/lib/amd64/server -ljvm")
  85. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS} -g -O2 -DNDEBUG -fPIC")
  86. set(D main/native/)
  87. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES CMAKE_FIND_LIBRARY_SUFFIXES)
  88. set_find_shared_library_version("1")
  89. SET(CMAKE_FIND_LIBRARY_SUFFIXES STORED_CMAKE_FIND_LIBRARY_SUFFIXES)
  90. INCLUDE(CheckFunctionExists)
  91. INCLUDE(CheckCSourceCompiles)
  92. #INCLUDE(CheckLibraryExists)
  93. INCLUDE(CheckIncludeFiles)
  94. #CHECK_FUNCTION_EXISTS(sync_file_range HAVE_SYNC_FILE_RANGE)
  95. #CHECK_FUNCTION_EXISTS(posix_fadvise HAVE_POSIX_FADVISE)
  96. #CHECK_LIBRARY_EXISTS(dl dlopen "" NEED_LINK_DL)
  97. CHECK_INCLUDE_FILES(fcntl.h HAVE_FCNTL_H)
  98. CHECK_INCLUDE_FILES(malloc.h HAVE_MALLOC_H)
  99. CHECK_INCLUDE_FILES(mach/mach.h HAVE_MACH_MACH_H)
  100. CHECK_INCLUDE_FILES(memory.h HAVE_MEMORY_H)
  101. CHECK_INCLUDE_FILES(stddef.h HAVE_STDDEF_H)
  102. CHECK_INCLUDE_FILES(stdint.h HAVE_STDINT_H)
  103. CHECK_INCLUDE_FILES(stdlib.h HAVE_STDLIB_H)
  104. CHECK_INCLUDE_FILES(string.h HAVE_STRING_H)
  105. CHECK_INCLUDE_FILES(unistd.h HAVE_UNITSTD_H)
  106. CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
  107. CHECK_FUNCTION_EXISTS(localtime_r HAVE_LOCALTIME_R)
  108. CHECK_FUNCTION_EXISTS(memset HAVE_MEMSET)
  109. CHECK_FUNCTION_EXISTS(strchr HAVE_STRCHR)
  110. CHECK_FUNCTION_EXISTS(strtoul HAVE_STRTOUL)
  111. SET(STORED_CMAKE_FIND_LIBRARY_SUFFIXES CMAKE_FIND_LIBRARY_SUFFIXES)
  112. set_find_shared_library_version("1")
  113. find_library(SNAPPY_LIBRARY
  114. NAMES snappy
  115. PATHS ${CUSTOM_SNAPPY_PREFIX} ${CUSTOM_SNAPPY_PREFIX}/lib
  116. ${CUSTOM_SNAPPY_PREFIX}/lib64 ${CUSTOM_SNAPPY_LIB})
  117. SET(CMAKE_FIND_LIBRARY_SUFFIXES STORED_CMAKE_FIND_LIBRARY_SUFFIXES)
  118. find_path(SNAPPY_INCLUDE_DIR
  119. NAMES snappy.h
  120. PATHS ${CUSTOM_SNAPPY_PREFIX} ${CUSTOM_SNAPPY_PREFIX}/include
  121. ${CUSTOM_SNAPPY_INCLUDE})
  122. if (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  123. GET_FILENAME_COMPONENT(HADOOP_SNAPPY_LIBRARY ${SNAPPY_LIBRARY} NAME)
  124. set(SNAPPY_SOURCE_FILES
  125. "${D}/src/codec/SnappyCodec.cc")
  126. else (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  127. set(SNAPPY_LIBRARY "")
  128. set(SNAPPY_INCLUDE_DIR "")
  129. set(SNAPPY_SOURCE_FILES "")
  130. IF(REQUIRE_SNAPPY)
  131. 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}")
  132. ENDIF(REQUIRE_SNAPPY)
  133. endif (SNAPPY_LIBRARY AND SNAPPY_INCLUDE_DIR)
  134. CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/config.h.cmake ${CMAKE_BINARY_DIR}/config.h)
  135. include_directories(
  136. ${GENERATED_JAVAH}
  137. ${D}/src
  138. ${D}/src/util
  139. ${D}/src/lib
  140. ${D}/test
  141. ${CMAKE_CURRENT_SOURCE_DIR}
  142. ${CMAKE_BINARY_DIR}
  143. ${JNI_INCLUDE_DIRS}
  144. ${SNAPPY_INCLUDE_DIR}
  145. )
  146. # add gtest as system library to suppress gcc warnings
  147. include_directories(SYSTEM ${D}/gtest/include)
  148. SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
  149. if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  150. # macosx does not have -lrt
  151. set(NT_DEPEND_LIBRARY dl pthread z ${SNAPPY_LIBRARY} ${JAVA_JVM_LIBRARY})
  152. set(SYSTEM_MAC TRUE)
  153. else (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  154. set(NT_DEPEND_LIBRARY dl rt pthread z ${SNAPPY_LIBRARY} ${JAVA_JVM_LIBRARY})
  155. set(SYSTEM_MAC FALSE)
  156. endif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  157. configure_file(main/native/test.sh test/test.sh)
  158. add_dual_library(nativetask
  159. ${CMAKE_BINARY_DIR}/lz4.c
  160. ${D}/src/codec/BlockCodec.cc
  161. ${D}/src/codec/GzipCodec.cc
  162. ${D}/src/codec/Lz4Codec.cc
  163. ${SNAPPY_SOURCE_FILES}
  164. ${D}/src/handler/BatchHandler.cc
  165. ${D}/src/handler/MCollectorOutputHandler.cc
  166. ${D}/src/handler/AbstractMapHandler.cc
  167. ${D}/src/handler/CombineHandler.cc
  168. ${D}/src/lib/Buffers.cc
  169. ${D}/src/lib/BufferStream.cc
  170. ${D}/src/lib/Compressions.cc
  171. ${D}/src/lib/PartitionBucket.cc
  172. ${D}/src/lib/PartitionBucketIterator.cc
  173. ${D}/src/lib/FileSystem.cc
  174. ${D}/src/lib/IFile.cc
  175. ${D}/src/lib/jniutils.cc
  176. ${D}/src/lib/Log.cc
  177. ${D}/src/lib/MapOutputCollector.cc
  178. ${D}/src/lib/MapOutputSpec.cc
  179. ${D}/src/lib/MemoryBlock.cc
  180. ${D}/src/lib/Merge.cc
  181. ${D}/src/lib/NativeLibrary.cc
  182. ${D}/src/lib/Iterator.cc
  183. ${D}/src/lib/NativeObjectFactory.cc
  184. ${D}/src/lib/NativeRuntimeJniImpl.cc
  185. ${D}/src/lib/NativeTask.cc
  186. ${D}/src/lib/SpillInfo.cc
  187. ${D}/src/lib/Path.cc
  188. ${D}/src/lib/Streams.cc
  189. ${D}/src/lib/TaskCounters.cc
  190. ${D}/src/util/Checksum.cc
  191. ${D}/src/util/Random.cc
  192. ${D}/src/util/StringUtil.cc
  193. ${D}/src/util/SyncUtils.cc
  194. ${D}/src/util/Timer.cc
  195. ${D}/src/util/WritableUtils.cc
  196. )
  197. target_link_libraries(nativetask ${NT_DEPEND_LIBRARY})
  198. add_library(gtest ${D}/gtest/gtest-all.cc)
  199. set_target_properties(gtest PROPERTIES COMPILE_FLAGS "-w")
  200. add_executable(nttest
  201. ${D}/test/lib/TestByteArray.cc
  202. ${D}/test/lib/TestByteBuffer.cc
  203. ${D}/test/lib/TestComparatorForDualPivotQuickSort.cc
  204. ${D}/test/lib/TestComparatorForStdSort.cc
  205. ${D}/test/lib/TestFixSizeContainer.cc
  206. ${D}/test/lib/TestMemoryPool.cc
  207. ${D}/test/lib/TestIterator.cc
  208. ${D}/test/lib/TestKVBuffer.cc
  209. ${D}/test/lib/TestMemBlockIterator.cc
  210. ${D}/test/lib/TestMemoryBlock.cc
  211. ${D}/test/lib/TestPartitionBucket.cc
  212. ${D}/test/lib/TestReadBuffer.cc
  213. ${D}/test/lib/TestReadWriteBuffer.cc
  214. ${D}/test/util/TestChecksum.cc
  215. ${D}/test/util/TestStringUtil.cc
  216. ${D}/test/util/TestWritableUtils.cc
  217. ${D}/test/TestCommand.cc
  218. ${D}/test/TestConfig.cc
  219. ${D}/test/TestCounter.cc
  220. ${D}/test/TestCompressions.cc
  221. ${D}/test/TestFileSystem.cc
  222. ${D}/test/TestIFile.cc
  223. ${D}/test/TestPrimitives.cc
  224. ${D}/test/TestSort.cc
  225. ${D}/test/TestMain.cc
  226. ${D}/test/test_commons.cc)
  227. IF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
  228. # macos clang with libc++ does not have tr1/tuple, just tuple
  229. SET_TARGET_PROPERTIES(nttest PROPERTIES COMPILE_FLAGS "-DGTEST_USE_OWN_TR1_TUPLE=1")
  230. ENDIF()
  231. target_link_libraries(nttest
  232. nativetask_static
  233. gtest
  234. ${NT_DEPEND_LIBRARY}
  235. )
  236. IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
  237. #
  238. # By embedding '$ORIGIN' into the RPATH of libnativetask.so,
  239. # dlopen will look in the directory containing libnativetask.so.
  240. # However, $ORIGIN is not supported by all operating systems.
  241. #
  242. SET_TARGET_PROPERTIES(nativetask
  243. PROPERTIES INSTALL_RPATH "\$ORIGIN/")
  244. ENDIF()
  245. SET(LIBNATIVETASK_VERSION "1.0.0")
  246. SET_TARGET_PROPERTIES(nativetask PROPERTIES SOVERSION ${LIBNATIVETASK_VERSION})
  247. dual_output_directory(nativetask target/usr/local/lib)
  248. output_directory(nttest test)