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
This commit is contained in:
Kefu Chai 2019-08-05 19:47:33 -07:00 committed by Facebook Github Bot
parent f4a616ebf9
commit cc9fa7fcdb
9 changed files with 132 additions and 112 deletions

View File

@ -32,18 +32,19 @@
# 3. cmake .. # 3. cmake ..
# 4. make -j # 4. make -j
cmake_minimum_required(VERSION 2.8.12) cmake_minimum_required(VERSION 3.5.1)
project(rocksdb)
enable_language(CXX) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules/")
enable_language(C) include(ReadVersion)
enable_language(ASM) get_rocksdb_version(rocksdb_VERSION)
project(rocksdb
VERSION ${rocksdb_VERSION}
LANGUAGES CXX C ASM)
if(POLICY CMP0042) if(POLICY CMP0042)
cmake_policy(SET CMP0042 NEW) cmake_policy(SET CMP0042 NEW)
endif() endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules/")
find_program(CCACHE_FOUND ccache) find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND) if(CCACHE_FOUND)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
@ -74,8 +75,7 @@ else()
if(WITH_JEMALLOC) if(WITH_JEMALLOC)
find_package(JeMalloc REQUIRED) find_package(JeMalloc REQUIRED)
add_definitions(-DROCKSDB_JEMALLOC -DJEMALLOC_NO_DEMANGLE) add_definitions(-DROCKSDB_JEMALLOC -DJEMALLOC_NO_DEMANGLE)
include_directories(${JEMALLOC_INCLUDE_DIR}) list(APPEND THIRDPARTY_LIBS JeMalloc::JeMalloc)
list(APPEND THIRDPARTY_LIBS ${JEMALLOC_LIBRARIES})
endif() endif()
endif() endif()
@ -93,43 +93,38 @@ else()
if(WITH_SNAPPY) if(WITH_SNAPPY)
find_package(snappy REQUIRED) find_package(snappy REQUIRED)
add_definitions(-DSNAPPY) add_definitions(-DSNAPPY)
include_directories(${SNAPPY_INCLUDE_DIR}) list(APPEND THIRDPARTY_LIBS snappy::snappy)
list(APPEND THIRDPARTY_LIBS ${SNAPPY_LIBRARIES})
endif() endif()
if(WITH_ZLIB) if(WITH_ZLIB)
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
add_definitions(-DZLIB) add_definitions(-DZLIB)
if(ZLIB_INCLUDE_DIRS) list(APPEND THIRDPARTY_LIBS ZLIB::ZLIB)
# CMake 3
include_directories(${ZLIB_INCLUDE_DIRS})
else()
# CMake 2
include_directories(${ZLIB_INCLUDE_DIR})
endif()
list(APPEND THIRDPARTY_LIBS ${ZLIB_LIBRARIES})
endif() endif()
option(WITH_BZ2 "build with bzip2" OFF) option(WITH_BZ2 "build with bzip2" OFF)
if(WITH_BZ2) if(WITH_BZ2)
find_package(bzip2 REQUIRED) find_package(BZip2 REQUIRED)
add_definitions(-DBZIP2) add_definitions(-DBZIP2)
if(BZIP2_INCLUDE_DIRS)
include_directories(${BZIP2_INCLUDE_DIRS})
else()
include_directories(${BZIP2_INCLUDE_DIR}) include_directories(${BZIP2_INCLUDE_DIR})
endif()
list(APPEND THIRDPARTY_LIBS ${BZIP2_LIBRARIES}) list(APPEND THIRDPARTY_LIBS ${BZIP2_LIBRARIES})
endif() endif()
if(WITH_LZ4) if(WITH_LZ4)
find_package(lz4 REQUIRED) find_package(lz4 REQUIRED)
add_definitions(-DLZ4) add_definitions(-DLZ4)
include_directories(${LZ4_INCLUDE_DIR}) list(APPEND THIRDPARTY_LIBS lz4::lz4)
list(APPEND THIRDPARTY_LIBS ${LZ4_LIBRARIES})
endif() endif()
if(WITH_ZSTD) if(WITH_ZSTD)
find_package(zstd REQUIRED) find_package(zstd REQUIRED)
add_definitions(-DZSTD) add_definitions(-DZSTD)
include_directories(${ZSTD_INCLUDE_DIR}) include_directories(${ZSTD_INCLUDE_DIR})
list(APPEND THIRDPARTY_LIBS ${ZSTD_LIBRARIES}) list(APPEND THIRDPARTY_LIBS zstd::zstd)
endif() endif()
endif() endif()
@ -150,17 +145,6 @@ endif()
string(REGEX REPLACE "[^0-9a-f]+" "" GIT_SHA "${GIT_SHA}") 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) option(WITH_MD_LIBRARY "build with MD" ON)
if(WIN32 AND MSVC) if(WIN32 AND MSVC)
if(WITH_MD_LIBRARY) if(WITH_MD_LIBRARY)
@ -316,15 +300,14 @@ if(WITH_NUMA)
find_package(NUMA REQUIRED) find_package(NUMA REQUIRED)
add_definitions(-DNUMA) add_definitions(-DNUMA)
include_directories(${NUMA_INCLUDE_DIR}) include_directories(${NUMA_INCLUDE_DIR})
list(APPEND THIRDPARTY_LIBS ${NUMA_LIBRARIES}) list(APPEND THIRDPARTY_LIBS NUMA::NUMA)
endif() endif()
option(WITH_TBB "build with Threading Building Blocks (TBB)" OFF) option(WITH_TBB "build with Threading Building Blocks (TBB)" OFF)
if(WITH_TBB) if(WITH_TBB)
find_package(TBB REQUIRED) find_package(TBB REQUIRED)
add_definitions(-DTBB) add_definitions(-DTBB)
include_directories(${TBB_INCLUDE_DIR}) list(APPEND THIRDPARTY_LIBS TBB::TBB)
list(APPEND THIRDPARTY_LIBS ${TBB_LIBRARIES})
endif() endif()
# Stall notifications eat some performance from inserts # Stall notifications eat some performance from inserts
@ -777,8 +760,8 @@ else()
${THIRDPARTY_LIBS} ${SYSTEM_LIBS}) ${THIRDPARTY_LIBS} ${SYSTEM_LIBS})
set_target_properties(${ROCKSDB_SHARED_LIB} PROPERTIES set_target_properties(${ROCKSDB_SHARED_LIB} PROPERTIES
LINKER_LANGUAGE CXX LINKER_LANGUAGE CXX
VERSION ${ROCKSDB_VERSION} VERSION ${rocksdb_VERSION}
SOVERSION ${ROCKSDB_VERSION_MAJOR} SOVERSION ${rocksdb_VERSION_MAJOR}
CXX_STANDARD 11 CXX_STANDARD 11
OUTPUT_NAME "rocksdb") OUTPUT_NAME "rocksdb")
endif() endif()
@ -833,7 +816,7 @@ if(NOT WIN32 OR ROCKSDB_INSTALL_ON_WINDOWS)
write_basic_package_version_file( write_basic_package_version_file(
RocksDBConfigVersion.cmake RocksDBConfigVersion.cmake
VERSION ${ROCKSDB_VERSION} VERSION ${rocksdb_VERSION}
COMPATIBILITY SameMajorVersion COMPATIBILITY SameMajorVersion
) )

View File

@ -1,21 +1,29 @@
# - Find JeMalloc library # - Find JeMalloc library
# Find the native JeMalloc includes and library # Find the native JeMalloc includes and library
# #
# JEMALLOC_INCLUDE_DIR - where to find jemalloc.h, etc. # JeMalloc_INCLUDE_DIRS - where to find jemalloc.h, etc.
# JEMALLOC_LIBRARIES - List of libraries when using jemalloc. # JeMalloc_LIBRARIES - List of libraries when using jemalloc.
# JEMALLOC_FOUND - True if jemalloc found. # JeMalloc_FOUND - True if jemalloc found.
find_path(JEMALLOC_INCLUDE_DIR find_path(JeMalloc_INCLUDE_DIRS
NAMES jemalloc/jemalloc.h NAMES jemalloc/jemalloc.h
HINTS ${JEMALLOC_ROOT_DIR}/include) HINTS ${JEMALLOC_ROOT_DIR}/include)
find_library(JEMALLOC_LIBRARIES find_library(JeMalloc_LIBRARIES
NAMES jemalloc NAMES jemalloc
HINTS ${JEMALLOC_ROOT_DIR}/lib) HINTS ${JEMALLOC_ROOT_DIR}/lib)
include(FindPackageHandleStandardArgs) 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( mark_as_advanced(
JEMALLOC_LIBRARIES JeMalloc_LIBRARIES
JEMALLOC_INCLUDE_DIR) 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()

View File

@ -1,11 +1,11 @@
# - Find NUMA # - Find NUMA
# Find the NUMA library and includes # 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_LIBRARIES - List of libraries when using NUMA.
# NUMA_FOUND - True if NUMA found. # NUMA_FOUND - True if NUMA found.
find_path(NUMA_INCLUDE_DIR find_path(NUMA_INCLUDE_DIRS
NAMES numa.h numaif.h NAMES numa.h numaif.h
HINTS ${NUMA_ROOT_DIR}/include) HINTS ${NUMA_ROOT_DIR}/include)
@ -14,8 +14,16 @@ find_library(NUMA_LIBRARIES
HINTS ${NUMA_ROOT_DIR}/lib) HINTS ${NUMA_ROOT_DIR}/lib)
include(FindPackageHandleStandardArgs) 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( mark_as_advanced(
NUMA_LIBRARIES 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()

View File

@ -1,7 +1,7 @@
# - Find TBB # - Find TBB
# Find the Thread Building Blocks library and includes # 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_LIBRARIES - List of libraries when using TBB.
# TBB_FOUND - True if TBB found. # TBB_FOUND - True if TBB found.
@ -9,17 +9,25 @@ if(NOT DEFINED TBB_ROOT_DIR)
set(TBB_ROOT_DIR "$ENV{TBBROOT}") set(TBB_ROOT_DIR "$ENV{TBBROOT}")
endif() endif()
find_path(TBB_INCLUDE_DIR find_path(TBB_INCLUDE_DIRS
NAMES tbb/tbb.h NAMES tbb/tbb.h
HINTS ${TBB_ROOT_DIR}/include) HINTS ${TBB_ROOT_DIR}/include)
find_library(TBB_LIBRARIES find_library(TBB_LIBRARIES
NAMES tbb NAMES tbb
HINTS ${TBB_ROOT_DIR}/lib ENV LIBRARY_PATH) HINTS ${TBB_ROOT_DIR}/lib ENV LIBRARY_PATH)
include(FindPackageHandleStandardArgs) 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( mark_as_advanced(
TBB_LIBRARIES TBB_LIBRARIES
TBB_INCLUDE_DIR) 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()

View File

@ -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)

View File

@ -1,21 +1,29 @@
# - Find Lz4 # - Find Lz4
# Find the lz4 compression library and includes # Find the lz4 compression library and includes
# #
# LZ4_INCLUDE_DIR - where to find lz4.h, etc. # lz4_INCLUDE_DIRS - where to find lz4.h, etc.
# LZ4_LIBRARIES - List of libraries when using lz4. # lz4_LIBRARIES - List of libraries when using lz4.
# LZ4_FOUND - True if lz4 found. # lz4_FOUND - True if lz4 found.
find_path(LZ4_INCLUDE_DIR find_path(lz4_INCLUDE_DIRS
NAMES lz4.h NAMES lz4.h
HINTS ${LZ4_ROOT_DIR}/include) HINTS ${lz4_ROOT_DIR}/include)
find_library(LZ4_LIBRARIES find_library(lz4_LIBRARIES
NAMES lz4 NAMES lz4
HINTS ${LZ4_ROOT_DIR}/lib) HINTS ${lz4_ROOT_DIR}/lib)
include(FindPackageHandleStandardArgs) 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( mark_as_advanced(
LZ4_LIBRARIES lz4_LIBRARIES
LZ4_INCLUDE_DIR) 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()

View File

@ -1,21 +1,29 @@
# - Find Snappy # - Find Snappy
# Find the snappy compression library and includes # Find the snappy compression library and includes
# #
# SNAPPY_INCLUDE_DIR - where to find snappy.h, etc. # snappy_INCLUDE_DIRS - where to find snappy.h, etc.
# SNAPPY_LIBRARIES - List of libraries when using snappy. # snappy_LIBRARIES - List of libraries when using snappy.
# SNAPPY_FOUND - True if snappy found. # snappy_FOUND - True if snappy found.
find_path(SNAPPY_INCLUDE_DIR find_path(snappy_INCLUDE_DIRS
NAMES snappy.h NAMES snappy.h
HINTS ${SNAPPY_ROOT_DIR}/include) HINTS ${snappy_ROOT_DIR}/include)
find_library(SNAPPY_LIBRARIES find_library(SNAPPY_LIBRARIES
NAMES snappy NAMES snappy
HINTS ${SNAPPY_ROOT_DIR}/lib) HINTS ${snappy_ROOT_DIR}/lib)
include(FindPackageHandleStandardArgs) 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( mark_as_advanced(
SNAPPY_LIBRARIES snappy_LIBRARIES
SNAPPY_INCLUDE_DIR) 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()

View File

@ -1,21 +1,29 @@
# - Find zstd # - Find zstd
# Find the zstd compression library and includes # Find the zstd compression library and includes
# #
# ZSTD_INCLUDE_DIR - where to find zstd.h, etc. # zstd_INCLUDE_DIRS - where to find zstd.h, etc.
# ZSTD_LIBRARIES - List of libraries when using zstd. # zstd_LIBRARIES - List of libraries when using zstd.
# ZSTD_FOUND - True if zstd found. # zstd_FOUND - True if zstd found.
find_path(ZSTD_INCLUDE_DIR find_path(zstd_INCLUDE_DIRS
NAMES zstd.h NAMES zstd.h
HINTS ${ZSTD_ROOT_DIR}/include) HINTS ${zstd_ROOT_DIR}/include)
find_library(ZSTD_LIBRARIES find_library(zstd_LIBRARIES
NAMES zstd NAMES zstd
HINTS ${ZSTD_ROOT_DIR}/lib) HINTS ${zstd_ROOT_DIR}/lib)
include(FindPackageHandleStandardArgs) 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( mark_as_advanced(
ZSTD_LIBRARIES zstd_LIBRARIES
ZSTD_INCLUDE_DIR) 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()

View File

@ -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()