Skip to content

Commit

Permalink
Make it possible to link against a system provided RocksDB
Browse files Browse the repository at this point in the history
Due to the custom build system integration for RocksDB we cannot
simply use FIND_PACKAGE_ARGS. Instead, we first do a `find_package`
and only if that fails do we fallback to the old rocksdb integration.

To better facilitate this, I took the liberty to also modernize
the CMake code a bit. Consumers of the lib just need to link
against the new `rocksdb::librocksdb` alias target to get the
required library and include directory.
  • Loading branch information
milianw committed Sep 29, 2023
1 parent c671e90 commit 5a6ef38
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
56 changes: 30 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,34 @@ FetchContent_Declare(
FetchContent_MakeAvailable(googletest)

### rocksdb
FetchContent_Declare(
rocksdb
SYSTEM
GIT_REPOSITORY https://github.com/facebook/rocksdb.git
GIT_TAG f32521662acf3352397d438b732144c7813bbbec # v8.5.3
GIT_PROGRESS TRUE
)
FetchContent_Populate(rocksdb)
add_custom_target(librocksdb ALL
WORKING_DIRECTORY ${rocksdb_SOURCE_DIR}
COMMAND cmake -G Ninja -B ${rocksdb_BINARY_DIR} -DCMAKE_BUILD_TYPE=Release -DWITH_GFLAGS=Off -DWITH_LIBURING=Off -DWITH_ZSTD=On -DFAIL_ON_WARNINGS=Off
COMMAND cmake --build ${rocksdb_BINARY_DIR} --target rocksdb
BYPRODUCTS ${rocksdb_BINARY_DIR}/librocksdb.a
COMMENT "Building RocksDB"
USES_TERMINAL
)
include_directories(SYSTEM "${rocksdb_SOURCE_DIR}/include")
find_package(RocksDB 8.5.3)
if (NOT RocksDB_FOUND)
FetchContent_Declare(
rocksdb
SYSTEM
GIT_REPOSITORY https://github.com/facebook/rocksdb.git
GIT_TAG f32521662acf3352397d438b732144c7813bbbec # v8.5.3
GIT_PROGRESS TRUE
)
FetchContent_Populate(rocksdb)
add_custom_target(librocksdb_build ALL
WORKING_DIRECTORY ${rocksdb_SOURCE_DIR}
COMMAND cmake -G Ninja -B ${rocksdb_BINARY_DIR} -DCMAKE_BUILD_TYPE=Release -DWITH_GFLAGS=Off -DWITH_LIBURING=Off -DWITH_ZSTD=On -DFAIL_ON_WARNINGS=Off
COMMAND cmake --build ${rocksdb_BINARY_DIR} --target rocksdb
BYPRODUCTS ${rocksdb_BINARY_DIR}/librocksdb.a
COMMENT "Building RocksDB"
USES_TERMINAL
)
add_library(librocksdb INTERFACE)
add_dependencies(librocksdb librocksdb_build)
target_include_directories(librocksdb INTERFACE SYSTEM "${rocksdb_SOURCE_DIR}/include")
add_library(rocksdb::librocksdb ALIAS librocksdb)

### zstd (for rocksdb)
find_package(zstd REQUIRED)

target_link_libraries(librocksdb INTERFACE zstd::zstd ${rocksdb_BINARY_DIR}/librocksdb.a)
endif()

### folly
### use folly as a header only library. some features won't be supported.
Expand Down Expand Up @@ -130,7 +141,6 @@ if (CODE_COVERAGE)
add_link_options(--coverage)
endif()


## System checks
## These checks are potentially fatal so perform them first.

Expand Down Expand Up @@ -199,9 +209,6 @@ find_package(Clang REQUIRED CONFIG)
message(STATUS "Found Clang ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using ClangConfig.cmake in: ${Clang_DIR}")

### zstd (for rocksdb)
find_package(zstd REQUIRED)

### drgn
# The setup.py script in drgn is really meant to build drgn (python
# debugger). It shoves the C headers/lib in a temporary directory (which
Expand Down Expand Up @@ -345,9 +352,8 @@ add_library(treebuilder
)
add_dependencies(treebuilder librocksdb)
target_link_libraries(treebuilder
${rocksdb_BINARY_DIR}/librocksdb.a
rocksdb::librocksdb
oicore # overkill but it does need a lot of stuff
zstd::zstd
)


Expand Down Expand Up @@ -383,10 +389,8 @@ target_link_libraries(oip oicore)

### Object Introspection RocksDB Printer (OIRP)
add_executable(oirp tools/OIRP.cpp)
add_dependencies(oirp librocksdb)
target_link_libraries(oirp
${rocksdb_BINARY_DIR}/librocksdb.a
zstd::zstd
rocksdb::librocksdb
msgpackc
)

Expand Down
4 changes: 4 additions & 0 deletions cmake/FindRocksDB.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ find_package_handle_standard_args(RocksDB DEFAULT_MSG RocksDB_INCLUDE_DIR RocksD
if(ROCKSDB_FOUND)
message(STATUS "Found RocksDB (include: ${RocksDB_INCLUDE_DIR}, library: ${RocksDB_LIBRARIES})")
mark_as_advanced(RocksDB_INCLUDE_DIR RocksDB_LIBRARIES)
add_library(librocksdb UNKNOWN IMPORTED)
set_target_properties(librocksdb PROPERTIES IMPORTED_LOCATION ${RocksDB_LIBRARIES})
target_include_directories(librocksdb INTERFACE ${RocksDB_INCLUDE_DIR})
add_library(rocksdb::librocksdb ALIAS librocksdb)
endif()

0 comments on commit 5a6ef38

Please sign in to comment.