From cc9fa7fcdb35fdd12505053b2a6cd38140c93d3b Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Mon, 5 Aug 2019 19:47:33 -0700 Subject: [PATCH] cmake: cmake related cleanups (#5662) Summary: - cmake: use the builtin FindBzip2.cmake from CMake - cmake: require CMake v3.5.1 - cmake: add imported target for 3rd party libraries - cmake: extract ReadVersion.cmake out and refactor it Pull Request resolved: https://github.com/facebook/rocksdb/pull/5662 Differential Revision: D16660974 Pulled By: maysamyabandeh fbshipit-source-id: 681594910e74253251fe14ad0befc41a4d0f4fd4 --- CMakeLists.txt | 65 ++++++++++++-------------------- cmake/modules/FindJeMalloc.cmake | 24 ++++++++---- cmake/modules/FindNUMA.cmake | 16 ++++++-- cmake/modules/FindTBB.cmake | 26 ++++++++----- cmake/modules/Findbzip2.cmake | 21 ----------- cmake/modules/Findlz4.cmake | 28 +++++++++----- cmake/modules/Findsnappy.cmake | 26 ++++++++----- cmake/modules/Findzstd.cmake | 28 +++++++++----- cmake/modules/ReadVersion.cmake | 10 +++++ 9 files changed, 132 insertions(+), 112 deletions(-) delete mode 100644 cmake/modules/Findbzip2.cmake create mode 100644 cmake/modules/ReadVersion.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 7266f3b55..bb99d1b7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,18 +32,19 @@ # 3. cmake .. # 4. make -j -cmake_minimum_required(VERSION 2.8.12) -project(rocksdb) -enable_language(CXX) -enable_language(C) -enable_language(ASM) +cmake_minimum_required(VERSION 3.5.1) + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules/") +include(ReadVersion) +get_rocksdb_version(rocksdb_VERSION) +project(rocksdb + VERSION ${rocksdb_VERSION} + LANGUAGES CXX C ASM) if(POLICY CMP0042) cmake_policy(SET CMP0042 NEW) endif() -list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules/") - find_program(CCACHE_FOUND ccache) if(CCACHE_FOUND) set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) @@ -74,8 +75,7 @@ else() if(WITH_JEMALLOC) find_package(JeMalloc REQUIRED) add_definitions(-DROCKSDB_JEMALLOC -DJEMALLOC_NO_DEMANGLE) - include_directories(${JEMALLOC_INCLUDE_DIR}) - list(APPEND THIRDPARTY_LIBS ${JEMALLOC_LIBRARIES}) + list(APPEND THIRDPARTY_LIBS JeMalloc::JeMalloc) endif() endif() @@ -93,43 +93,38 @@ else() if(WITH_SNAPPY) find_package(snappy REQUIRED) add_definitions(-DSNAPPY) - include_directories(${SNAPPY_INCLUDE_DIR}) - list(APPEND THIRDPARTY_LIBS ${SNAPPY_LIBRARIES}) + list(APPEND THIRDPARTY_LIBS snappy::snappy) endif() if(WITH_ZLIB) find_package(ZLIB REQUIRED) add_definitions(-DZLIB) - if(ZLIB_INCLUDE_DIRS) - # CMake 3 - include_directories(${ZLIB_INCLUDE_DIRS}) - else() - # CMake 2 - include_directories(${ZLIB_INCLUDE_DIR}) - endif() - list(APPEND THIRDPARTY_LIBS ${ZLIB_LIBRARIES}) + list(APPEND THIRDPARTY_LIBS ZLIB::ZLIB) endif() option(WITH_BZ2 "build with bzip2" OFF) if(WITH_BZ2) - find_package(bzip2 REQUIRED) + find_package(BZip2 REQUIRED) add_definitions(-DBZIP2) - include_directories(${BZIP2_INCLUDE_DIR}) + if(BZIP2_INCLUDE_DIRS) + include_directories(${BZIP2_INCLUDE_DIRS}) + else() + include_directories(${BZIP2_INCLUDE_DIR}) + endif() list(APPEND THIRDPARTY_LIBS ${BZIP2_LIBRARIES}) endif() if(WITH_LZ4) find_package(lz4 REQUIRED) add_definitions(-DLZ4) - include_directories(${LZ4_INCLUDE_DIR}) - list(APPEND THIRDPARTY_LIBS ${LZ4_LIBRARIES}) + list(APPEND THIRDPARTY_LIBS lz4::lz4) endif() if(WITH_ZSTD) find_package(zstd REQUIRED) add_definitions(-DZSTD) include_directories(${ZSTD_INCLUDE_DIR}) - list(APPEND THIRDPARTY_LIBS ${ZSTD_LIBRARIES}) + list(APPEND THIRDPARTY_LIBS zstd::zstd) endif() endif() @@ -150,17 +145,6 @@ endif() string(REGEX REPLACE "[^0-9a-f]+" "" GIT_SHA "${GIT_SHA}") -# Read rocksdb version from version.h header file. -file(READ include/rocksdb/version.h version_header_file) -string(REGEX MATCH "#define ROCKSDB_MAJOR ([0-9]+)" _ ${version_header_file}) -set(ROCKSDB_VERSION_MAJOR ${CMAKE_MATCH_1}) -string(REGEX MATCH "#define ROCKSDB_MINOR ([0-9]+)" _ ${version_header_file}) -set(ROCKSDB_VERSION_MINOR ${CMAKE_MATCH_1}) -string(REGEX MATCH "#define ROCKSDB_PATCH ([0-9]+)" _ ${version_header_file}) -set(ROCKSDB_VERSION_PATCH ${CMAKE_MATCH_1}) -set(ROCKSDB_VERSION ${ROCKSDB_VERSION_MAJOR}.${ROCKSDB_VERSION_MINOR}.${ROCKSDB_VERSION_PATCH}) - - option(WITH_MD_LIBRARY "build with MD" ON) if(WIN32 AND MSVC) if(WITH_MD_LIBRARY) @@ -316,15 +300,14 @@ if(WITH_NUMA) find_package(NUMA REQUIRED) add_definitions(-DNUMA) include_directories(${NUMA_INCLUDE_DIR}) - list(APPEND THIRDPARTY_LIBS ${NUMA_LIBRARIES}) + list(APPEND THIRDPARTY_LIBS NUMA::NUMA) endif() option(WITH_TBB "build with Threading Building Blocks (TBB)" OFF) if(WITH_TBB) find_package(TBB REQUIRED) add_definitions(-DTBB) - include_directories(${TBB_INCLUDE_DIR}) - list(APPEND THIRDPARTY_LIBS ${TBB_LIBRARIES}) + list(APPEND THIRDPARTY_LIBS TBB::TBB) endif() # Stall notifications eat some performance from inserts @@ -777,8 +760,8 @@ else() ${THIRDPARTY_LIBS} ${SYSTEM_LIBS}) set_target_properties(${ROCKSDB_SHARED_LIB} PROPERTIES LINKER_LANGUAGE CXX - VERSION ${ROCKSDB_VERSION} - SOVERSION ${ROCKSDB_VERSION_MAJOR} + VERSION ${rocksdb_VERSION} + SOVERSION ${rocksdb_VERSION_MAJOR} CXX_STANDARD 11 OUTPUT_NAME "rocksdb") endif() @@ -833,7 +816,7 @@ if(NOT WIN32 OR ROCKSDB_INSTALL_ON_WINDOWS) write_basic_package_version_file( RocksDBConfigVersion.cmake - VERSION ${ROCKSDB_VERSION} + VERSION ${rocksdb_VERSION} COMPATIBILITY SameMajorVersion ) diff --git a/cmake/modules/FindJeMalloc.cmake b/cmake/modules/FindJeMalloc.cmake index 7911f77c4..f695b3ed1 100644 --- a/cmake/modules/FindJeMalloc.cmake +++ b/cmake/modules/FindJeMalloc.cmake @@ -1,21 +1,29 @@ # - Find JeMalloc library # Find the native JeMalloc includes and library # -# JEMALLOC_INCLUDE_DIR - where to find jemalloc.h, etc. -# JEMALLOC_LIBRARIES - List of libraries when using jemalloc. -# JEMALLOC_FOUND - True if jemalloc found. +# JeMalloc_INCLUDE_DIRS - where to find jemalloc.h, etc. +# JeMalloc_LIBRARIES - List of libraries when using jemalloc. +# JeMalloc_FOUND - True if jemalloc found. -find_path(JEMALLOC_INCLUDE_DIR +find_path(JeMalloc_INCLUDE_DIRS NAMES jemalloc/jemalloc.h HINTS ${JEMALLOC_ROOT_DIR}/include) -find_library(JEMALLOC_LIBRARIES +find_library(JeMalloc_LIBRARIES NAMES jemalloc HINTS ${JEMALLOC_ROOT_DIR}/lib) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(jemalloc DEFAULT_MSG JEMALLOC_LIBRARIES JEMALLOC_INCLUDE_DIR) +find_package_handle_standard_args(JeMalloc DEFAULT_MSG JeMalloc_LIBRARIES JeMalloc_INCLUDE_DIRS) mark_as_advanced( - JEMALLOC_LIBRARIES - JEMALLOC_INCLUDE_DIR) + JeMalloc_LIBRARIES + JeMalloc_INCLUDE_DIRS) + +if(JeMalloc_FOUND AND NOT (TARGET JeMalloc::JeMalloc)) + add_library (JeMalloc::JeMalloc UNKNOWN IMPORTED) + set_target_properties(JeMalloc::JeMalloc + PROPERTIES + IMPORTED_LOCATION ${JeMalloc_LIBRARIES} + INTERFACE_INCLUDE_DIRECTORIES ${JeMalloc_INCLUDE_DIRS}) +endif() diff --git a/cmake/modules/FindNUMA.cmake b/cmake/modules/FindNUMA.cmake index 02760344c..69b95c9b6 100644 --- a/cmake/modules/FindNUMA.cmake +++ b/cmake/modules/FindNUMA.cmake @@ -1,11 +1,11 @@ # - Find NUMA # Find the NUMA library and includes # -# NUMA_INCLUDE_DIR - where to find numa.h, etc. +# NUMA_INCLUDE_DIRS - where to find numa.h, etc. # NUMA_LIBRARIES - List of libraries when using NUMA. # NUMA_FOUND - True if NUMA found. -find_path(NUMA_INCLUDE_DIR +find_path(NUMA_INCLUDE_DIRS NAMES numa.h numaif.h HINTS ${NUMA_ROOT_DIR}/include) @@ -14,8 +14,16 @@ find_library(NUMA_LIBRARIES HINTS ${NUMA_ROOT_DIR}/lib) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(NUMA DEFAULT_MSG NUMA_LIBRARIES NUMA_INCLUDE_DIR) +find_package_handle_standard_args(NUMA DEFAULT_MSG NUMA_LIBRARIES NUMA_INCLUDE_DIRS) mark_as_advanced( NUMA_LIBRARIES - NUMA_INCLUDE_DIR) + NUMA_INCLUDE_DIRS) + +if(NUMA_FOUND AND NOT (TARGET NUMA::NUMA)) + add_library (NUMA::NUMA UNKNOWN IMPORTED) + set_target_properties(NUMA::NUMA + PROPERTIES + IMPORTED_LOCATION ${NUMA_LIBRARIES} + INTERFACE_INCLUDE_DIRECTORIES ${NUMA_INCLUDE_DIRS}) +endif() diff --git a/cmake/modules/FindTBB.cmake b/cmake/modules/FindTBB.cmake index 556ce872b..f6861fa55 100644 --- a/cmake/modules/FindTBB.cmake +++ b/cmake/modules/FindTBB.cmake @@ -1,7 +1,7 @@ # - Find TBB # Find the Thread Building Blocks library and includes # -# TBB_INCLUDE_DIR - where to find tbb.h, etc. +# TBB_INCLUDE_DIRS - where to find tbb.h, etc. # TBB_LIBRARIES - List of libraries when using TBB. # TBB_FOUND - True if TBB found. @@ -9,17 +9,25 @@ if(NOT DEFINED TBB_ROOT_DIR) set(TBB_ROOT_DIR "$ENV{TBBROOT}") endif() -find_path(TBB_INCLUDE_DIR -NAMES tbb/tbb.h -HINTS ${TBB_ROOT_DIR}/include) +find_path(TBB_INCLUDE_DIRS + NAMES tbb/tbb.h + HINTS ${TBB_ROOT_DIR}/include) find_library(TBB_LIBRARIES -NAMES tbb -HINTS ${TBB_ROOT_DIR}/lib ENV LIBRARY_PATH) + NAMES tbb + HINTS ${TBB_ROOT_DIR}/lib ENV LIBRARY_PATH) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(TBB DEFAULT_MSG TBB_LIBRARIES TBB_INCLUDE_DIR) +find_package_handle_standard_args(TBB DEFAULT_MSG TBB_LIBRARIES TBB_INCLUDE_DIRS) mark_as_advanced( -TBB_LIBRARIES -TBB_INCLUDE_DIR) + TBB_LIBRARIES + TBB_INCLUDE_DIRS) + +if(TBB_FOUND AND NOT (TARGET TBB::TBB)) + add_library (TBB::TBB UNKNOWN IMPORTED) + set_target_properties(TBB::TBB + PROPERTIES + IMPORTED_LOCATION ${TBB_LIBRARIES} + INTERFACE_INCLUDE_DIRECTORIES ${TBB_INCLUDE_DIRS}) +endif() diff --git a/cmake/modules/Findbzip2.cmake b/cmake/modules/Findbzip2.cmake deleted file mode 100644 index 87abbe941..000000000 --- a/cmake/modules/Findbzip2.cmake +++ /dev/null @@ -1,21 +0,0 @@ -# - Find Bzip2 -# Find the bzip2 compression library and includes -# -# BZIP2_INCLUDE_DIR - where to find bzlib.h, etc. -# BZIP2_LIBRARIES - List of libraries when using bzip2. -# BZIP2_FOUND - True if bzip2 found. - -find_path(BZIP2_INCLUDE_DIR - NAMES bzlib.h - HINTS ${BZIP2_ROOT_DIR}/include) - -find_library(BZIP2_LIBRARIES - NAMES bz2 - HINTS ${BZIP2_ROOT_DIR}/lib) - -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(bzip2 DEFAULT_MSG BZIP2_LIBRARIES BZIP2_INCLUDE_DIR) - -mark_as_advanced( - BZIP2_LIBRARIES - BZIP2_INCLUDE_DIR) diff --git a/cmake/modules/Findlz4.cmake b/cmake/modules/Findlz4.cmake index c34acef5e..7cf7d7f5f 100644 --- a/cmake/modules/Findlz4.cmake +++ b/cmake/modules/Findlz4.cmake @@ -1,21 +1,29 @@ # - Find Lz4 # Find the lz4 compression library and includes # -# LZ4_INCLUDE_DIR - where to find lz4.h, etc. -# LZ4_LIBRARIES - List of libraries when using lz4. -# LZ4_FOUND - True if lz4 found. +# lz4_INCLUDE_DIRS - where to find lz4.h, etc. +# lz4_LIBRARIES - List of libraries when using lz4. +# lz4_FOUND - True if lz4 found. -find_path(LZ4_INCLUDE_DIR +find_path(lz4_INCLUDE_DIRS NAMES lz4.h - HINTS ${LZ4_ROOT_DIR}/include) + HINTS ${lz4_ROOT_DIR}/include) -find_library(LZ4_LIBRARIES +find_library(lz4_LIBRARIES NAMES lz4 - HINTS ${LZ4_ROOT_DIR}/lib) + HINTS ${lz4_ROOT_DIR}/lib) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(lz4 DEFAULT_MSG LZ4_LIBRARIES LZ4_INCLUDE_DIR) +find_package_handle_standard_args(lz4 DEFAULT_MSG lz4_LIBRARIES lz4_INCLUDE_DIRS) mark_as_advanced( - LZ4_LIBRARIES - LZ4_INCLUDE_DIR) + lz4_LIBRARIES + lz4_INCLUDE_DIRS) + +if(lz4_FOUND AND NOT (TARGET lz4::lz4)) + add_library(lz4::lz4 UNKNOWN IMPORTED) + set_target_properties(lz4::lz4 + PROPERTIES + IMPORTED_LOCATION ${lz4_LIBRARIES} + INTERFACE_INCLUDE_DIRECTORIES ${lz4_INCLUDE_DIRS}) +endif() diff --git a/cmake/modules/Findsnappy.cmake b/cmake/modules/Findsnappy.cmake index 6ed5fda3d..2de2889c1 100644 --- a/cmake/modules/Findsnappy.cmake +++ b/cmake/modules/Findsnappy.cmake @@ -1,21 +1,29 @@ # - Find Snappy # Find the snappy compression library and includes # -# SNAPPY_INCLUDE_DIR - where to find snappy.h, etc. -# SNAPPY_LIBRARIES - List of libraries when using snappy. -# SNAPPY_FOUND - True if snappy found. +# snappy_INCLUDE_DIRS - where to find snappy.h, etc. +# snappy_LIBRARIES - List of libraries when using snappy. +# snappy_FOUND - True if snappy found. -find_path(SNAPPY_INCLUDE_DIR +find_path(snappy_INCLUDE_DIRS NAMES snappy.h - HINTS ${SNAPPY_ROOT_DIR}/include) + HINTS ${snappy_ROOT_DIR}/include) find_library(SNAPPY_LIBRARIES NAMES snappy - HINTS ${SNAPPY_ROOT_DIR}/lib) + HINTS ${snappy_ROOT_DIR}/lib) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(snappy DEFAULT_MSG SNAPPY_LIBRARIES SNAPPY_INCLUDE_DIR) +find_package_handle_standard_args(snappy DEFAULT_MSG snappy_LIBRARIES snappy_INCLUDE_DIRS) mark_as_advanced( - SNAPPY_LIBRARIES - SNAPPY_INCLUDE_DIR) + snappy_LIBRARIES + snappy_INCLUDE_DIRS) + +if(snappy_FOUND AND NOT (TARGET snappy::snappy)) + add_library (snappy::snappy UNKNOWN IMPORTED) + set_target_properties(snappy::snappy + PROPERTIES + IMPORTED_LOCATION ${snappy_LIBRARIES} + INTERFACE_INCLUDE_DIRECTORIES ${snappy_INCLUDE_DIRS}) +endif() diff --git a/cmake/modules/Findzstd.cmake b/cmake/modules/Findzstd.cmake index a2964aa9f..9430821df 100644 --- a/cmake/modules/Findzstd.cmake +++ b/cmake/modules/Findzstd.cmake @@ -1,21 +1,29 @@ # - Find zstd # Find the zstd compression library and includes # -# ZSTD_INCLUDE_DIR - where to find zstd.h, etc. -# ZSTD_LIBRARIES - List of libraries when using zstd. -# ZSTD_FOUND - True if zstd found. +# zstd_INCLUDE_DIRS - where to find zstd.h, etc. +# zstd_LIBRARIES - List of libraries when using zstd. +# zstd_FOUND - True if zstd found. -find_path(ZSTD_INCLUDE_DIR +find_path(zstd_INCLUDE_DIRS NAMES zstd.h - HINTS ${ZSTD_ROOT_DIR}/include) + HINTS ${zstd_ROOT_DIR}/include) -find_library(ZSTD_LIBRARIES +find_library(zstd_LIBRARIES NAMES zstd - HINTS ${ZSTD_ROOT_DIR}/lib) + HINTS ${zstd_ROOT_DIR}/lib) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(zstd DEFAULT_MSG ZSTD_LIBRARIES ZSTD_INCLUDE_DIR) +find_package_handle_standard_args(zstd DEFAULT_MSG zstd_LIBRARIES zstd_INCLUDE_DIRS) mark_as_advanced( - ZSTD_LIBRARIES - ZSTD_INCLUDE_DIR) + zstd_LIBRARIES + zstd_INCLUDE_DIRS) + +if(zstd_FOUND AND NOT (TARGET zstd::zstd)) + add_library (zstd::zstd UNKNOWN IMPORTED) + set_target_properties(zstd::zstd + PROPERTIES + IMPORTED_LOCATION ${zstd_LIBRARIES} + INTERFACE_INCLUDE_DIRECTORIES ${zstd_INCLUDE_DIRS}) +endif() diff --git a/cmake/modules/ReadVersion.cmake b/cmake/modules/ReadVersion.cmake new file mode 100644 index 000000000..ae356d996 --- /dev/null +++ b/cmake/modules/ReadVersion.cmake @@ -0,0 +1,10 @@ +# Read rocksdb version from version.h header file. + +function(get_rocksdb_version version_var) + file(READ "${CMAKE_SOURCE_DIR}/include/rocksdb/version.h" version_header_file) + foreach(component MAJOR MINOR PATCH) + string(REGEX MATCH "#define ROCKSDB_${component} ([0-9]+)" _ ${version_header_file}) + set(ROCKSDB_VERSION_${component} ${CMAKE_MATCH_1}) + endforeach() + set(${version_var} "${ROCKSDB_VERSION_MAJOR}.${ROCKSDB_VERSION_MINOR}.${ROCKSDB_VERSION_PATCH}" PARENT_SCOPE) +endfunction()