blob: ff66b8e04aabdc5821c28e5cbb903f9eff4c296c [file] [log] [blame]
Marc Kupietzed0bfcf2021-03-14 14:56:15 +01001cmake_minimum_required(VERSION 3.9)
Marc Kupietz28f05d12026-09-04 16:46:24 +02002project(collocatordb VERSION 1.8.0 DESCRIPTION "Storing and retrieving collocation counts based on RocksDB")
Marc Kupietz57047c32026-07-31 09:17:57 +09003set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fno-rtti")
Marc Kupietzed0bfcf2021-03-14 14:56:15 +01004set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
5
Marc Kupietz65d44792026-07-31 09:46:34 +09006# 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.
9if ("cxx_std_20" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
10 set(CMAKE_CXX_STANDARD 20)
11else()
12 set(CMAKE_CXX_STANDARD 17)
13endif()
Marc Kupietz57047c32026-07-31 09:17:57 +090014set(CMAKE_CXX_STANDARD_REQUIRED ON)
15
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010016include(GNUInstallDirs)
Marc Kupietz15f56e72021-03-15 14:58:13 +010017
Marc Kupietz8fd499e2026-08-02 07:32:43 +020018# 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.
23list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_FULL_LIBDIR}" isSystemDir)
Marc Kupietz15f56e72021-03-15 14:58:13 +010024if("${isSystemDir}" STREQUAL "-1")
Marc Kupietz8fd499e2026-08-02 07:32:43 +020025 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
Marc Kupietz15f56e72021-03-15 14:58:13 +010026endif("${isSystemDir}" STREQUAL "-1")
27
Marc Kupietz6208fd72024-11-15 15:46:19 +010028configure_file(src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY)
Marc Kupietz57047c32026-07-31 09:17:57 +090029include_directories(/usr/local/include /opt/homebrew/include ${CMAKE_CURRENT_BINARY_DIR} build)
Marc Kupietz44229232024-08-05 15:00:20 +020030link_directories(/usr/local/lib64 /usr/local/lib /usr/lib64 /usr/lib /opt/homebrew/lib)
31if (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")
37else()
38 set(LIBRT "rt")
Marc Kupietz57047c32026-07-31 09:17:57 +090039 # 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 Kupietz89c7b9f2026-08-02 07:38:44 +020045 set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
Marc Kupietz57047c32026-07-31 09:17:57 +090046 find_library(ROCKSDB NAMES rocksdb)
47 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_collocatordb_lib_suffixes})
Marc Kupietz44229232024-08-05 15:00:20 +020048endif()
Marc Kupietzd885c242026-08-02 15:56:24 +020049if (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.")
57endif()
Marc Kupietz57047c32026-07-31 09:17:57 +090058message(STATUS "Using RocksDB library ${ROCKSDB}")
Marc Kupietzcd489a92026-07-31 15:33:16 +090059
Marc Kupietzd885c242026-08-02 15:56:24 +020060# 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 Kupietzcd489a92026-07-31 15:33:16 +090064find_path(ROCKSDB_INCLUDE_DIR rocksdb/version.h)
Marc Kupietzd885c242026-08-02 15:56:24 +020065if (ROCKSDB_INCLUDE_DIR)
66 include_directories(BEFORE ${ROCKSDB_INCLUDE_DIR})
67endif()
Marc Kupietzcd489a92026-07-31 15:33:16 +090068if (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()
85endif()
86
Marc Kupietz1b09e4d2021-03-14 15:20:19 +010087find_library(ROCKSDB_STATIC librocksdb.a)
88
Marc Kupietz6208fd72024-11-15 15:46:19 +010089get_cmake_property(_variableNames VARIABLES)
90list (SORT _variableNames)
91foreach (_variableName ${_variableNames})
92 message(STATUS "${_variableName}=${${_variableName}}")
93endforeach()
94
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010095enable_testing()
96add_library(collocatordb SHARED src/collocatordb.cc)
Marc Kupietz44229232024-08-05 15:00:20 +020097
98target_link_libraries(collocatordb pthread ${LIBRT} snappy z bz2 lz4 zstd ${ROCKSDB} dl)
99
Marc Kupietzed0bfcf2021-03-14 14:56:15 +0100100set_target_properties(collocatordb PROPERTIES
101 VERSION ${PROJECT_VERSION}
102 SOVERSION 1
103 PUBLIC_HEADER src/collocatordb.h)
104
105add_library(collocatordb_static STATIC src/collocatordb.cc)
Marc Kupietz57047c32026-07-31 09:17:57 +0900106# Distributions usually package rocksdb as a shared library only, in which case
107# the static collocatordb is linked against that one.
108if (ROCKSDB_STATIC)
109 set(ROCKSDB_FOR_STATIC ${ROCKSDB_STATIC})
110else()
111 set(ROCKSDB_FOR_STATIC ${ROCKSDB})
112 message(STATUS "No static rocksdb found, linking collocatordb_static against ${ROCKSDB}")
113endif()
114target_link_libraries(collocatordb_static ${ROCKSDB_FOR_STATIC} pthread ${LIBRT} snappy z bz2 lz4 zstd dl)
Marc Kupietzed0bfcf2021-03-14 14:56:15 +0100115
116add_executable(basic_test tests/basic_test.c tests/acutest.h)
117TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb)
118add_test(BASIC_TEST basic_test)
119
Marc Kupietz1b09e4d2021-03-14 15:20:19 +0100120add_executable(static_library_test tests/basic_test.c tests/acutest.h)
121TARGET_LINK_LIBRARIES(static_library_test collocatordb_static)
122add_test(STATIC_LIBRARY_TEST static_library_test)
123
Marc Kupietzed0bfcf2021-03-14 14:56:15 +0100124# Install library
Marc Kupietz8f977ca2021-03-22 17:52:52 +0100125install(TARGETS ${PROJECT_NAME} collocatordb_static
Marc Kupietz15f56e72021-03-15 14:58:13 +0100126 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietzd15845c2022-04-11 22:28:11 +0200127 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietz15f56e72021-03-15 14:58:13 +0100128 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Marc Kupietz44229232024-08-05 15:00:20 +0200129
130if (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>)
133endif()
Marc Kupietzb17122d2024-11-21 14:32:29 +0100134
Marc Kupietzb9912e32024-11-23 10:11:06 +0100135file(GLOB TOOLS_SOURCES tools/*.c tools/*.cc)
136add_custom_target(tools)
137foreach(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 Kupietz8fd499e2026-08-02 07:32:43 +0200142 INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}"
Marc Kupietzb9912e32024-11-23 10:11:06 +0100143 BUILD_WITH_INSTALL_RPATH TRUE)
144 add_dependencies(tools ${TOOL_NAME})
145 install(TARGETS ${TOOL_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
146endforeach()
147
Marc Kupietzb17122d2024-11-21 14:32:29 +0100148file(GLOB EXAMPLES_SOURCES examples/*.c examples/*.cc)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100149add_custom_target(examples)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100150foreach(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})
155endforeach()