| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.9) |
| Marc Kupietz | 18b230d | 2026-07-31 09:28:23 +0900 | [diff] [blame] | 2 | project(collocatordb VERSION 1.5.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 | |
| 18 | # the RPATH to be used when installing, but only if it's not a system directory |
| 19 | list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) |
| 20 | if("${isSystemDir}" STREQUAL "-1") |
| 21 | set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") |
| 22 | endif("${isSystemDir}" STREQUAL "-1") |
| 23 | |
| Marc Kupietz | 6208fd7 | 2024-11-15 15:46:19 +0100 | [diff] [blame] | 24 | 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] | 25 | 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] | 26 | link_directories(/usr/local/lib64 /usr/local/lib /usr/lib64 /usr/lib /opt/homebrew/lib) |
| 27 | if (1 AND APPLE) |
| 28 | message("MacOS deteted") |
| 29 | set(LIBRT "") |
| 30 | find_library(ROCKSDB NAMES librocksdb.a) |
| 31 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flat_namespace") |
| 32 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flat_namespace") |
| 33 | else() |
| 34 | set(LIBRT "rt") |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 35 | # No version pin, so that the rocksdb of the distribution can be used. |
| 36 | # Only shared libraries, the static one is picked up as ROCKSDB_STATIC |
| 37 | # below. Set -DROCKSDB=<path> to choose a specific one, and make sure the |
| 38 | # headers that are found belong to it, i.e. that there is no leftover |
| 39 | # rocksdb installation below /usr/local. |
| 40 | set(_collocatordb_lib_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES}) |
| 41 | set(CMAKE_FIND_LIBRARY_SUFFIXES ".so") |
| 42 | find_library(ROCKSDB NAMES rocksdb) |
| 43 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${_collocatordb_lib_suffixes}) |
| Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 44 | endif() |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 45 | message(STATUS "Using RocksDB library ${ROCKSDB}") |
| Marc Kupietz | cd489a9 | 2026-07-31 15:33:16 +0900 | [diff] [blame^] | 46 | |
| 47 | # The compiler looks into /usr/local/include before /usr/include, so a leftover |
| 48 | # rocksdb installation there is used for the headers while the library of the |
| 49 | # distribution is linked. That ends in a wall of undefined references, so say |
| 50 | # what is wrong while it is still obvious. |
| 51 | find_path(ROCKSDB_INCLUDE_DIR rocksdb/version.h) |
| 52 | if (ROCKSDB_INCLUDE_DIR AND ROCKSDB) |
| 53 | file(STRINGS "${ROCKSDB_INCLUDE_DIR}/rocksdb/version.h" _rocksdb_major_line |
| 54 | REGEX "^#define ROCKSDB_MAJOR ") |
| 55 | string(REGEX MATCH "[0-9]+" ROCKSDB_HEADER_MAJOR "${_rocksdb_major_line}") |
| 56 | get_filename_component(_rocksdb_real "${ROCKSDB}" REALPATH) |
| 57 | if (_rocksdb_real MATCHES "librocksdb\\.so\\.([0-9]+)") |
| 58 | set(ROCKSDB_LIB_MAJOR ${CMAKE_MATCH_1}) |
| 59 | message(STATUS "Using RocksDB headers ${ROCKSDB_INCLUDE_DIR} (version ${ROCKSDB_HEADER_MAJOR})") |
| 60 | if (NOT ROCKSDB_HEADER_MAJOR STREQUAL ROCKSDB_LIB_MAJOR) |
| 61 | message(FATAL_ERROR |
| 62 | "The rocksdb headers in ${ROCKSDB_INCLUDE_DIR} are version " |
| 63 | "${ROCKSDB_HEADER_MAJOR}, but ${ROCKSDB} is version " |
| 64 | "${ROCKSDB_LIB_MAJOR}. Most likely a leftover rocksdb below " |
| 65 | "/usr/local shadows the one of the distribution - remove it, " |
| 66 | "or point -DROCKSDB= at the library belonging to the headers.") |
| 67 | endif() |
| 68 | endif() |
| 69 | endif() |
| 70 | |
| Marc Kupietz | 1b09e4d | 2021-03-14 15:20:19 +0100 | [diff] [blame] | 71 | find_library(ROCKSDB_STATIC librocksdb.a) |
| 72 | |
| Marc Kupietz | 6208fd7 | 2024-11-15 15:46:19 +0100 | [diff] [blame] | 73 | get_cmake_property(_variableNames VARIABLES) |
| 74 | list (SORT _variableNames) |
| 75 | foreach (_variableName ${_variableNames}) |
| 76 | message(STATUS "${_variableName}=${${_variableName}}") |
| 77 | endforeach() |
| 78 | |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 79 | enable_testing() |
| 80 | add_library(collocatordb SHARED src/collocatordb.cc) |
| Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 81 | |
| 82 | target_link_libraries(collocatordb pthread ${LIBRT} snappy z bz2 lz4 zstd ${ROCKSDB} dl) |
| 83 | |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 84 | set_target_properties(collocatordb PROPERTIES |
| 85 | VERSION ${PROJECT_VERSION} |
| 86 | SOVERSION 1 |
| 87 | PUBLIC_HEADER src/collocatordb.h) |
| 88 | |
| 89 | add_library(collocatordb_static STATIC src/collocatordb.cc) |
| Marc Kupietz | 57047c3 | 2026-07-31 09:17:57 +0900 | [diff] [blame] | 90 | # Distributions usually package rocksdb as a shared library only, in which case |
| 91 | # the static collocatordb is linked against that one. |
| 92 | if (ROCKSDB_STATIC) |
| 93 | set(ROCKSDB_FOR_STATIC ${ROCKSDB_STATIC}) |
| 94 | else() |
| 95 | set(ROCKSDB_FOR_STATIC ${ROCKSDB}) |
| 96 | message(STATUS "No static rocksdb found, linking collocatordb_static against ${ROCKSDB}") |
| 97 | endif() |
| 98 | 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] | 99 | |
| 100 | add_executable(basic_test tests/basic_test.c tests/acutest.h) |
| 101 | TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb) |
| 102 | add_test(BASIC_TEST basic_test) |
| 103 | |
| Marc Kupietz | 1b09e4d | 2021-03-14 15:20:19 +0100 | [diff] [blame] | 104 | add_executable(static_library_test tests/basic_test.c tests/acutest.h) |
| 105 | TARGET_LINK_LIBRARIES(static_library_test collocatordb_static) |
| 106 | add_test(STATIC_LIBRARY_TEST static_library_test) |
| 107 | |
| Marc Kupietz | ed0bfcf | 2021-03-14 14:56:15 +0100 | [diff] [blame] | 108 | # Install library |
| Marc Kupietz | 8f977ca | 2021-03-22 17:52:52 +0100 | [diff] [blame] | 109 | install(TARGETS ${PROJECT_NAME} collocatordb_static |
| Marc Kupietz | 15f56e7 | 2021-03-15 14:58:13 +0100 | [diff] [blame] | 110 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| Marc Kupietz | d15845c | 2022-04-11 22:28:11 +0200 | [diff] [blame] | 111 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| Marc Kupietz | 15f56e7 | 2021-03-15 14:58:13 +0100 | [diff] [blame] | 112 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 113 | |
| 114 | if (1 AND APPLE) |
| 115 | add_custom_command(TARGET collocatordb POST_BUILD |
| 116 | COMMAND install_name_tool -id "/usr/local/lib/libcollocatordb.1.dylib" $<TARGET_FILE:collocatordb>) |
| 117 | endif() |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 118 | |
| Marc Kupietz | b9912e3 | 2024-11-23 10:11:06 +0100 | [diff] [blame] | 119 | file(GLOB TOOLS_SOURCES tools/*.c tools/*.cc) |
| 120 | add_custom_target(tools) |
| 121 | foreach(TOOL_SOURCE ${TOOLS_SOURCES}) |
| 122 | get_filename_component(TOOL_NAME ${TOOL_SOURCE} NAME_WE) |
| 123 | add_executable(${TOOL_NAME} ${TOOL_SOURCE}) |
| 124 | target_link_libraries(${TOOL_NAME} ${ROCKSDB} collocatordb) |
| 125 | set_target_properties(${TOOL_NAME} PROPERTIES |
| 126 | INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib64" |
| 127 | BUILD_WITH_INSTALL_RPATH TRUE) |
| 128 | add_dependencies(tools ${TOOL_NAME}) |
| 129 | install(TARGETS ${TOOL_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR}) |
| 130 | endforeach() |
| 131 | |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 132 | file(GLOB EXAMPLES_SOURCES examples/*.c examples/*.cc) |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 133 | add_custom_target(examples) |
| Marc Kupietz | b17122d | 2024-11-21 14:32:29 +0100 | [diff] [blame] | 134 | foreach(EXAMPLE_SOURCE ${EXAMPLES_SOURCES}) |
| 135 | get_filename_component(EXAMPLE_NAME ${EXAMPLE_SOURCE} NAME_WE) |
| 136 | add_executable(${EXAMPLE_NAME} EXCLUDE_FROM_ALL ${EXAMPLE_SOURCE}) |
| 137 | target_link_libraries(${EXAMPLE_NAME} ${ROCKSDB} collocatordb) |
| 138 | add_dependencies(examples ${EXAMPLE_NAME}) |
| 139 | endforeach() |