ceph/cmake/modules/BuildRocksDB.cmake
Kefu Chai af6bb91505 cmake/modules/BuildRocksDB.cmake: inherit CMAKE_C_COMPILER from parent
if we set the CFLAGS globally, and the CFLAGS contains options
only acceptable by a certain C compiler, RocksDB could fail to
configure. for instance, if we set CXXFLAGS so it contains
`--config /usr/lib/rpm/redhat/redhat-hardened-clang.cfg` and use clang++
as the CMAKE_CXX_COMPILER, while keep CMAKE_C_COMPILER unchanged.
RocksDB would fail to configure like:

```-- Check for working C compiler: /usr/bin/cc - broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:67 (message):
  The C compiler

    "/usr/bin/cc"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/jenkins-build/build/workspace/ceph-dev-new-build/ARCH/x86_64/AVAILABLE_ARCH/x86_64/AVAILABLE_DIST/centos9/DIST/centos9/MACHINE_SIZE/gigantic/release/19.0.0-1717-g0f726187/rpm/el9/BUILD/ceph-19.0.0-1717-g0f726187/redhat-linux-build/src/rocksdb/CMakeFiles/CMakeScratch/TryCompile-RU5UFV

    Run Build Command(s):/usr/bin/cmake -E env VERBOSE=1 /usr/bin/gmake -f Makefile cmTC_65e36/fast && gmake[3]: Entering directory '/home/jenkins-build/build/workspace/ceph-dev-new-build/ARCH/x86_64/AVAILABLE_ARCH/x86_64/AVAILABLE_DIST/centos9/DIST/centos9/MACHINE_SIZE/gigantic/release/19.0.0-1717-g0f726187/rpm/el9/BUILD/ceph-19.0.0-1717-g0f726187/redhat-linux-build/src/rocksdb/CMakeFiles/CMakeScratch/TryCompile-RU5UFV'
    /usr/bin/gmake  -f CMakeFiles/cmTC_65e36.dir/build.make CMakeFiles/cmTC_65e36.dir/build
    gmake[4]: Entering directory '/home/jenkins-build/build/workspace/ceph-dev-new-build/ARCH/x86_64/AVAILABLE_ARCH/x86_64/AVAILABLE_DIST/centos9/DIST/centos9/MACHINE_SIZE/gigantic/release/19.0.0-1717-g0f726187/rpm/el9/BUILD/ceph-19.0.0-1717-g0f726187/redhat-linux-build/src/rocksdb/CMakeFiles/CMakeScratch/TryCompile-RU5UFV'
    Building C object CMakeFiles/cmTC_65e36.dir/testCCompiler.c.o
    /usr/bin/cc   -O2 -flto=thin -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security  -Wp,-D_GLIBCXX_ASSERTIONS --config /usr/lib/rpm/redhat/redhat-hardened-clang.cfg -fstack-protector-strong -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection  -fPIE -o CMakeFiles/cmTC_65e36.dir/testCCompiler.c.o -c /home/jenkins-build/build/workspace/ceph-dev-new-build/ARCH/x86_64/AVAILABLE_ARCH/x86_64/AVAILABLE_DIST/centos9/DIST/centos9/MACHINE_SIZE/gigantic/release/19.0.0-1717-g0f726187/rpm/el9/BUILD/ceph-19.0.0-1717-g0f726187/redhat-linux-build/src/rocksdb/CMakeFiles/CMakeScratch/TryCompile-RU5UFV/testCCompiler.c
    cc: error: unrecognized command-line option ‘--config’; did you mean ‘-mpconfig’?
    gmake[4]: *** [CMakeFiles/cmTC_65e36.dir/build.make:78: CMakeFiles/cmTC_65e36.dir/testCCompiler.c.o] Error 1
```

where RocksDB tries to check C compiler -- /usr/bin/cc along with
the said CFLAGS, and fails to compile the test C program, because
GCC does not support this option.

so, in this change, let's pass the CMAKE_C_COMPILER as well.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
2024-03-02 18:08:10 +08:00

118 lines
4.9 KiB
CMake

function(build_rocksdb)
set(rocksdb_CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON)
list(APPEND rocksdb_CMAKE_ARGS -DWITH_GFLAGS=OFF)
# cmake doesn't properly handle arguments containing ";", such as
# CMAKE_PREFIX_PATH, for which reason we'll have to use some other separator.
string(REPLACE ";" "!" CMAKE_PREFIX_PATH_ALT_SEP "${CMAKE_PREFIX_PATH}")
list(APPEND rocksdb_CMAKE_ARGS -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH_ALT_SEP})
if(CMAKE_TOOLCHAIN_FILE)
list(APPEND rocksdb_CMAKE_ARGS
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE})
endif()
list(APPEND rocksdb_CMAKE_ARGS -DWITH_LIBURING=${WITH_LIBURING})
if(WITH_LIBURING)
list(APPEND rocksdb_CMAKE_ARGS -During_INCLUDE_DIR=${URING_INCLUDE_DIR})
list(APPEND rocksdb_CMAKE_ARGS -During_LIBRARIES=${URING_LIBRARY_DIR})
list(APPEND rocksdb_INTERFACE_LINK_LIBRARIES uring::uring)
endif()
if(ALLOCATOR STREQUAL "jemalloc")
list(APPEND rocksdb_CMAKE_ARGS -DWITH_JEMALLOC=ON)
list(APPEND rocksdb_INTERFACE_LINK_LIBRARIES JeMalloc::JeMalloc)
endif()
list(APPEND rocksdb_CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER})
list(APPEND rocksdb_CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER})
list(APPEND rocksdb_CMAKE_ARGS -DWITH_SNAPPY=${SNAPPY_FOUND})
if(SNAPPY_FOUND)
list(APPEND rocksdb_INTERFACE_LINK_LIBRARIES snappy::snappy)
endif()
# libsnappy is a C++ library, we need to force rocksdb to link against
# libsnappy statically.
if(SNAPPY_FOUND AND WITH_STATIC_LIBSTDCXX)
list(APPEND rocksdb_CMAKE_ARGS -DWITH_SNAPPY_STATIC_LIB=ON)
endif()
list(APPEND rocksdb_CMAKE_ARGS -DWITH_LZ4=${LZ4_FOUND})
if(LZ4_FOUND)
list(APPEND rocksdb_INTERFACE_LINK_LIBRARIES LZ4::LZ4)
# When cross compiling, cmake may fail to locate lz4.
list(APPEND rocksdb_CMAKE_ARGS -Dlz4_INCLUDE_DIRS=${LZ4_INCLUDE_DIR})
list(APPEND rocksdb_CMAKE_ARGS -Dlz4_LIBRARIES=${LZ4_LIBRARY})
endif()
list(APPEND rocksdb_CMAKE_ARGS -DWITH_ZLIB=${ZLIB_FOUND})
if(ZLIB_FOUND)
list(APPEND rocksdb_INTERFACE_LINK_LIBRARIES ZLIB::ZLIB)
endif()
list(APPEND rocksdb_CMAKE_ARGS -DPORTABLE=ON)
list(APPEND rocksdb_CMAKE_ARGS -DCMAKE_AR=${CMAKE_AR})
list(APPEND rocksdb_CMAKE_ARGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE})
list(APPEND rocksdb_CMAKE_ARGS -DFAIL_ON_WARNINGS=OFF)
list(APPEND rocksdb_CMAKE_ARGS -DUSE_RTTI=1)
CHECK_C_COMPILER_FLAG("-Wno-stringop-truncation" HAS_WARNING_STRINGOP_TRUNCATION)
if(HAS_WARNING_STRINGOP_TRUNCATION)
list(APPEND rocksdb_CMAKE_ARGS -DCMAKE_C_FLAGS=-Wno-stringop-truncation)
endif()
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-Wno-deprecated-copy" HAS_WARNING_DEPRECATED_COPY)
set(rocksdb_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
if(HAS_WARNING_DEPRECATED_COPY)
string(APPEND rocksdb_CXX_FLAGS " -Wno-deprecated-copy")
endif()
check_cxx_compiler_flag("-Wno-pessimizing-move" HAS_WARNING_PESSIMIZING_MOVE)
if(HAS_WARNING_PESSIMIZING_MOVE)
string(APPEND rocksdb_CXX_FLAGS " -Wno-pessimizing-move")
endif()
if(rocksdb_CXX_FLAGS)
list(APPEND rocksdb_CMAKE_ARGS -DCMAKE_CXX_FLAGS='${rocksdb_CXX_FLAGS}')
endif()
# we use an external project and copy the sources to bin directory to ensure
# that object files are built outside of the source tree.
include(ExternalProject)
set(rocksdb_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/rocksdb")
set(rocksdb_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/rocksdb")
set(rocksdb_LIBRARY "${rocksdb_BINARY_DIR}/librocksdb.a")
if(CMAKE_MAKE_PROGRAM MATCHES "make")
# try to inherit command line arguments passed by parent "make" job
set(make_cmd $(MAKE) rocksdb)
else()
set(make_cmd ${CMAKE_COMMAND} --build <BINARY_DIR> --target rocksdb)
endif()
ExternalProject_Add(rocksdb_ext
SOURCE_DIR "${rocksdb_SOURCE_DIR}"
CMAKE_ARGS ${rocksdb_CMAKE_ARGS}
BINARY_DIR "${rocksdb_BINARY_DIR}"
BUILD_COMMAND "${make_cmd}"
BUILD_BYPRODUCTS "${rocksdb_LIBRARY}"
INSTALL_COMMAND ""
LIST_SEPARATOR !)
# make sure all the link libraries are built first
add_dependencies(rocksdb_ext ${rocksdb_INTERFACE_LINK_LIBRARIES})
add_library(RocksDB::RocksDB STATIC IMPORTED)
add_dependencies(RocksDB::RocksDB rocksdb_ext)
set(rocksdb_INCLUDE_DIR "${rocksdb_SOURCE_DIR}/include")
foreach(ver "MAJOR" "MINOR" "PATCH")
file(STRINGS "${rocksdb_INCLUDE_DIR}/rocksdb/version.h" ROCKSDB_VER_${ver}_LINE
REGEX "^#define[ \t]+ROCKSDB_${ver}[ \t]+[0-9]+$")
string(REGEX REPLACE "^#define[ \t]+ROCKSDB_${ver}[ \t]+([0-9]+)$"
"\\1" ROCKSDB_VERSION_${ver} "${ROCKSDB_VER_${ver}_LINE}")
unset(ROCKDB_VER_${ver}_LINE)
endforeach()
set(rocksdb_VERSION_STRING
"${ROCKSDB_VERSION_MAJOR}.${ROCKSDB_VERSION_MINOR}.${ROCKSDB_VERSION_PATCH}")
set_target_properties(RocksDB::RocksDB PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${rocksdb_INCLUDE_DIR}"
INTERFACE_LINK_LIBRARIES "${rocksdb_INTERFACE_LINK_LIBRARIES}"
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
IMPORTED_LOCATION "${rocksdb_LIBRARY}"
VERSION "${rocksdb_VERSION_STRING}")
endfunction()