| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.9) |
| Marc Kupietz | 28f05d1 | 2026-09-04 16:46:24 +0200 | [diff] [blame] | 2 | project(collocatordb VERSION 1.8.0 DESCRIPTION "Storing and retrieving collocation counts based on RocksDB") |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 3 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fno-rtti") |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 4 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") |
| 5 | |
| Marc Kupietz | 65d4479 | 2026-07-31 09:46:34 +0900 | [diff] [blame] | 6 | # RocksDB requires C++17 since version 7 and C++20 since version 11, so use the |
| 7 | # newest standard the compiler knows. Nothing here depends on it, this only has |
| 8 | # to satisfy the rocksdb headers. |
| 9 | if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES) |
| 10 | set(CMAKE_CXX_STANDARD 20) |
| 11 | else() |
| 12 | set(CMAKE_CXX_STANDARD 17) |
| 13 | endif() |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 14 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 15 | |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 16 | include(GNUInstallDirs) |
| Marc Kupietz | 15f56e7 | 2021-03-15 14:58:13 +0100 | [diff] [blame] | 17 | |
| Marc Kupietz | 8fd499e | 2026-08-02 07:32:43 +0200 | [diff] [blame] | 18 | # The RPATH to be used when installing, but only if it is not a system |
| 19 | # directory. CMAKE_INSTALL_FULL_LIBDIR rather than a hard coded lib: the library |
| 20 | # goes to lib64 on Fedora, Rocky Linux and RHEL, and to lib/<triplet> on Debian |
| 21 | # and Ubuntu, and an installation below /usr/local is in the search path of the |
| 22 | # loader on none of them by default. |
| 23 | list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_FULL_LIBDIR}" isSystemDir) |
| Marc Kupietz | 15f56e7 | 2021-03-15 14:58:13 +0100 | [diff] [blame] | 24 | if("${isSystemDir}" STREQUAL "-1") |
| Marc Kupietz | 8fd499e | 2026-08-02 07:32:43 +0200 | [diff] [blame] | 25 | set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}") |
| Marc Kupietz | 15f56e7 | 2021-03-15 14:58:13 +0100 | [diff] [blame] | 26 | endif("${isSystemDir}" STREQUAL "-1") |
| 27 | |
| Marc Kupietz | 6208fd7 | 2024-11-15 15:46:19 +0100 | [diff] [blame] | 28 | configure_file(src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY) |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 29 | include_directories(/usr/local/include /opt/homebrew/include ${CMAKE_CURRENT_BINARY_DIR} build) |
| Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 30 | link_directories(/usr/local/lib64 /usr/local/lib /usr/lib64 /usr/lib /opt/homebrew/lib) |
| 31 | if (1 AND APPLE) |
| 32 | message("MacOS deteted") |
| 33 | set(LIBRT "") |
| 34 | find_library(ROCKSDB NAMES librocksdb.a) |
| 35 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flat_namespace") |
| 36 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flat_namespace") |
| 37 | else() |
| 38 | set(LIBRT "rt") |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 39 | # No version pin, so that the rocksdb of the distribution can be used. |
| 40 | # Only shared libraries, the static one is picked up as ROCKSDB_STATIC |
| 41 | # below. Set -DROCKSDB=<path> to choose a specific one, and make sure the |
| 42 | # headers that are found belong to it, i.e. that there is no leftover |
| 43 | # rocksdb installation below /usr/local. |
| 44 | set(_collocatordb_lib_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES}) |
| Marc Kupietz | 89c7b9f | 2026-08-02 07:38:44 +0200 | [diff] [blame] | 45 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 46 | find_library(ROCKSDB NAMES rocksdb) |
| 47 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${_collocatordb_lib_suffixes}) |
| Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 48 | endif() |
| Marc Kupietz | d885c24 | 2026-08-02 15:56:24 +0200 | [diff] [blame] | 49 | if (NOT ROCKSDB) |
| 50 | message(FATAL_ERROR |
| 51 | "No rocksdb found. Install the development package of the " |
| 52 | "distribution, e.g. librocksdb-dev or rocksdb-devel. Where there is " |
| 53 | "none, build rocksdb and say where it is: " |
| 54 | "-DCMAKE_PREFIX_PATH=<prefix it was installed to>. Without this the " |
| 55 | "shared library would be built with unresolved rocksdb symbols, " |
| 56 | "which only shows up when something links it.") |
| 57 | endif() |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 58 | message(STATUS "Using RocksDB library ${ROCKSDB}") |
| Marc Kupietz | cd489a9 | 2026-07-31 15:33:16 +0900 | [diff] [blame] | 59 | |
| Marc Kupietz | d885c24 | 2026-08-02 15:56:24 +0200 | [diff] [blame] | 60 | # The headers have to belong to that library. Looked up rather than assumed, so |
| 61 | # that CMAKE_PREFIX_PATH also works for a rocksdb that was built by hand, and |
| 62 | # put before the rest, so that a leftover rocksdb below /usr/local cannot |
| 63 | # shadow it - the compiler looks into /usr/local/include before /usr/include. |
| Marc Kupietz | cd489a9 | 2026-07-31 15:33:16 +0900 | [diff] [blame] | 64 | find_path(ROCKSDB_INCLUDE_DIR rocksdb/version.h) |
| Marc Kupietz | d885c24 | 2026-08-02 15:56:24 +0200 | [diff] [blame] | 65 | if (ROCKSDB_INCLUDE_DIR) |
| 66 | include_directories(BEFORE ${ROCKSDB_INCLUDE_DIR}) |
| 67 | endif() |
| Marc Kupietz | cd489a9 | 2026-07-31 15:33:16 +0900 | [diff] [blame] | 68 | if (ROCKSDB_INCLUDE_DIR AND ROCKSDB) |
| 69 | file(STRINGS "${ROCKSDB_INCLUDE_DIR}/rocksdb/version.h" _rocksdb_major_line |
| 70 | REGEX "^#define ROCKSDB_MAJOR ") |
| 71 | string(REGEX MATCH "[0-9]+" ROCKSDB_HEADER_MAJOR "${_rocksdb_major_line}") |
| 72 | get_filename_component(_rocksdb_real "${ROCKSDB}" REALPATH) |
| 73 | if (_rocksdb_real MATCHES "librocksdb\\.so\\.([0-9]+)") |
| 74 | set(ROCKSDB_LIB_MAJOR ${CMAKE_MATCH_1}) |
| 75 | message(STATUS "Using RocksDB headers ${ROCKSDB_INCLUDE_DIR} (version ${ROCKSDB_HEADER_MAJOR})") |
| 76 | if (NOT ROCKSDB_HEADER_MAJOR STREQUAL ROCKSDB_LIB_MAJOR) |
| 77 | message(FATAL_ERROR |
| 78 | "The rocksdb headers in ${ROCKSDB_INCLUDE_DIR} are version " |
| 79 | "${ROCKSDB_HEADER_MAJOR}, but ${ROCKSDB} is version " |
| 80 | "${ROCKSDB_LIB_MAJOR}. Most likely a leftover rocksdb below " |
| 81 | "/usr/local shadows the one of the distribution - remove it, " |
| 82 | "or point -DROCKSDB= at the library belonging to the headers.") |
| 83 | endif() |
| 84 | endif() |
| 85 | endif() |
| 86 | |
| Marc Kupietz | 1b09e4d | 2021-03-14 15:20:19 +0100 | [diff] [blame] | 87 | find_library(ROCKSDB_STATIC librocksdb.a) |
| 88 | |
| Marc Kupietz | 6208fd7 | 2024-11-15 15:46:19 +0100 | [diff] [blame] | 89 | get_cmake_property(_variableNames VARIABLES) |
| 90 | list (SORT _variableNames) |
| 91 | foreach (_variableName ${_variableNames}) |
| 92 | message(STATUS "${_variableName}=${${_variableName}}") |
| 93 | endforeach() |
| 94 | |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 95 | enable_testing() |
| 96 | add_library(collocatordb SHARED src/collocatordb.cc) |
| Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 97 | |
| 98 | target_link_libraries(collocatordb pthread ${LIBRT} snappy z bz2 lz4 zstd ${ROCKSDB} dl) |
| 99 | |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 100 | set_target_properties(collocatordb PROPERTIES |
| 101 | VERSION ${PROJECT_VERSION} |
| 102 | SOVERSION 1 |
| 103 | PUBLIC_HEADER src/collocatordb.h) |
| 104 | |
| 105 | add_library(collocatordb_static STATIC src/collocatordb.cc) |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 106 | # Distributions usually package rocksdb as a shared library only, in which case |
| 107 | # the static collocatordb is linked against that one. |
| 108 | if (ROCKSDB_STATIC) |
| 109 | set(ROCKSDB_FOR_STATIC ${ROCKSDB_STATIC}) |
| 110 | else() |
| 111 | set(ROCKSDB_FOR_STATIC ${ROCKSDB}) |
| 112 | message(STATUS "No static rocksdb found, linking collocatordb_static against ${ROCKSDB}") |
| 113 | endif() |
| 114 | target_link_libraries(collocatordb_static ${ROCKSDB_FOR_STATIC} pthread ${LIBRT} snappy z bz2 lz4 zstd dl) |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 115 | |
| 116 | add_executable(basic_test tests/basic_test.c tests/acutest.h) |
| 117 | TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb) |
| 118 | add_test(BASIC_TEST basic_test) |
| 119 | |
| Marc Kupietz | 1b09e4d | 2021-03-14 15:20:19 +0100 | [diff] [blame] | 120 | add_executable(static_library_test tests/basic_test.c tests/acutest.h) |
| 121 | TARGET_LINK_LIBRARIES(static_library_test collocatordb_static) |
| 122 | add_test(STATIC_LIBRARY_TEST static_library_test) |
| 123 | |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 124 | # Install library |
| Marc Kupietz | 8f977ca | 2021-03-22 17:52:52 +0100 | [diff] [blame] | 125 | install(TARGETS ${PROJECT_NAME} collocatordb_static |
| Marc Kupietz | 15f56e7 | 2021-03-15 14:58:13 +0100 | [diff] [blame] | 126 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| Marc Kupietz | d15845c | 2022-04-11 22:28:11 +0200 | [diff] [blame] | 127 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| Marc Kupietz | 15f56e7 | 2021-03-15 14:58:13 +0100 | [diff] [blame] | 128 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 129 | |
| 130 | if (1 AND APPLE) |
| 131 | add_custom_command(TARGET collocatordb POST_BUILD |
| 132 | COMMAND install_name_tool -id "/usr/local/lib/libcollocatordb.1.dylib" $<TARGET_FILE:collocatordb>) |
| 133 | endif() |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 134 | |
| Marc Kupietz | b9912e3 | 2024-11-23 10:11:06 +0100 | [diff] [blame] | 135 | file(GLOB TOOLS_SOURCES tools/*.c tools/*.cc) |
| 136 | add_custom_target(tools) |
| 137 | foreach(TOOL_SOURCE ${TOOLS_SOURCES}) |
| 138 | get_filename_component(TOOL_NAME ${TOOL_SOURCE} NAME_WE) |
| 139 | add_executable(${TOOL_NAME} ${TOOL_SOURCE}) |
| 140 | target_link_libraries(${TOOL_NAME} ${ROCKSDB} collocatordb) |
| 141 | set_target_properties(${TOOL_NAME} PROPERTIES |
| Marc Kupietz | 8fd499e | 2026-08-02 07:32:43 +0200 | [diff] [blame] | 142 | INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}" |
| Marc Kupietz | b9912e3 | 2024-11-23 10:11:06 +0100 | [diff] [blame] | 143 | BUILD_WITH_INSTALL_RPATH TRUE) |
| 144 | add_dependencies(tools ${TOOL_NAME}) |
| 145 | install(TARGETS ${TOOL_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) |
| 146 | endforeach() |
| 147 | |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 148 | file(GLOB EXAMPLES_SOURCES examples/*.c examples/*.cc) |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 149 | add_custom_target(examples) |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 150 | foreach(EXAMPLE_SOURCE ${EXAMPLES_SOURCES}) |
| 151 | get_filename_component(EXAMPLE_NAME ${EXAMPLE_SOURCE} NAME_WE) |
| 152 | add_executable(${EXAMPLE_NAME} EXCLUDE_FROM_ALL ${EXAMPLE_SOURCE}) |
| 153 | target_link_libraries(${EXAMPLE_NAME} ${ROCKSDB} collocatordb) |
| 154 | add_dependencies(examples ${EXAMPLE_NAME}) |
| 155 | endforeach() |