blob: 912a49b28c209bfc8f99333fc30e9ca183925a7c [file] [log] [blame]
Marc Kupietzed0bfcf2021-03-14 14:56:15 +01001cmake_minimum_required(VERSION 3.9)
Marc Kupietz2f65bcf2026-08-02 14:04:37 +02002project(collocatordb VERSION 1.6.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 Kupietz57047c32026-07-31 09:17:57 +090049message(STATUS "Using RocksDB library ${ROCKSDB}")
Marc Kupietzcd489a92026-07-31 15:33:16 +090050
51# The compiler looks into /usr/local/include before /usr/include, so a leftover
52# rocksdb installation there is used for the headers while the library of the
53# distribution is linked. That ends in a wall of undefined references, so say
54# what is wrong while it is still obvious.
55find_path(ROCKSDB_INCLUDE_DIR rocksdb/version.h)
56if (ROCKSDB_INCLUDE_DIR AND ROCKSDB)
57 file(STRINGS "${ROCKSDB_INCLUDE_DIR}/rocksdb/version.h" _rocksdb_major_line
58 REGEX "^#define ROCKSDB_MAJOR ")
59 string(REGEX MATCH "[0-9]+" ROCKSDB_HEADER_MAJOR "${_rocksdb_major_line}")
60 get_filename_component(_rocksdb_real "${ROCKSDB}" REALPATH)
61 if (_rocksdb_real MATCHES "librocksdb\\.so\\.([0-9]+)")
62 set(ROCKSDB_LIB_MAJOR ${CMAKE_MATCH_1})
63 message(STATUS "Using RocksDB headers ${ROCKSDB_INCLUDE_DIR} (version ${ROCKSDB_HEADER_MAJOR})")
64 if (NOT ROCKSDB_HEADER_MAJOR STREQUAL ROCKSDB_LIB_MAJOR)
65 message(FATAL_ERROR
66 "The rocksdb headers in ${ROCKSDB_INCLUDE_DIR} are version "
67 "${ROCKSDB_HEADER_MAJOR}, but ${ROCKSDB} is version "
68 "${ROCKSDB_LIB_MAJOR}. Most likely a leftover rocksdb below "
69 "/usr/local shadows the one of the distribution - remove it, "
70 "or point -DROCKSDB= at the library belonging to the headers.")
71 endif()
72 endif()
73endif()
74
Marc Kupietz1b09e4d2021-03-14 15:20:19 +010075find_library(ROCKSDB_STATIC librocksdb.a)
76
Marc Kupietz6208fd72024-11-15 15:46:19 +010077get_cmake_property(_variableNames VARIABLES)
78list (SORT _variableNames)
79foreach (_variableName ${_variableNames})
80 message(STATUS "${_variableName}=${${_variableName}}")
81endforeach()
82
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010083enable_testing()
84add_library(collocatordb SHARED src/collocatordb.cc)
Marc Kupietz44229232024-08-05 15:00:20 +020085
86target_link_libraries(collocatordb pthread ${LIBRT} snappy z bz2 lz4 zstd ${ROCKSDB} dl)
87
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010088set_target_properties(collocatordb PROPERTIES
89 VERSION ${PROJECT_VERSION}
90 SOVERSION 1
91 PUBLIC_HEADER src/collocatordb.h)
92
93add_library(collocatordb_static STATIC src/collocatordb.cc)
Marc Kupietz57047c32026-07-31 09:17:57 +090094# Distributions usually package rocksdb as a shared library only, in which case
95# the static collocatordb is linked against that one.
96if (ROCKSDB_STATIC)
97 set(ROCKSDB_FOR_STATIC ${ROCKSDB_STATIC})
98else()
99 set(ROCKSDB_FOR_STATIC ${ROCKSDB})
100 message(STATUS "No static rocksdb found, linking collocatordb_static against ${ROCKSDB}")
101endif()
102target_link_libraries(collocatordb_static ${ROCKSDB_FOR_STATIC} pthread ${LIBRT} snappy z bz2 lz4 zstd dl)
Marc Kupietzed0bfcf2021-03-14 14:56:15 +0100103
104add_executable(basic_test tests/basic_test.c tests/acutest.h)
105TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb)
106add_test(BASIC_TEST basic_test)
107
Marc Kupietz1b09e4d2021-03-14 15:20:19 +0100108add_executable(static_library_test tests/basic_test.c tests/acutest.h)
109TARGET_LINK_LIBRARIES(static_library_test collocatordb_static)
110add_test(STATIC_LIBRARY_TEST static_library_test)
111
Marc Kupietzed0bfcf2021-03-14 14:56:15 +0100112# Install library
Marc Kupietz8f977ca2021-03-22 17:52:52 +0100113install(TARGETS ${PROJECT_NAME} collocatordb_static
Marc Kupietz15f56e72021-03-15 14:58:13 +0100114 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietzd15845c2022-04-11 22:28:11 +0200115 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietz15f56e72021-03-15 14:58:13 +0100116 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Marc Kupietz44229232024-08-05 15:00:20 +0200117
118if (1 AND APPLE)
119 add_custom_command(TARGET collocatordb POST_BUILD
120 COMMAND install_name_tool -id "/usr/local/lib/libcollocatordb.1.dylib" $<TARGET_FILE:collocatordb>)
121endif()
Marc Kupietzb17122d2024-11-21 14:32:29 +0100122
Marc Kupietzb9912e32024-11-23 10:11:06 +0100123file(GLOB TOOLS_SOURCES tools/*.c tools/*.cc)
124add_custom_target(tools)
125foreach(TOOL_SOURCE ${TOOLS_SOURCES})
126 get_filename_component(TOOL_NAME ${TOOL_SOURCE} NAME_WE)
127 add_executable(${TOOL_NAME} ${TOOL_SOURCE})
128 target_link_libraries(${TOOL_NAME} ${ROCKSDB} collocatordb)
129 set_target_properties(${TOOL_NAME} PROPERTIES
Marc Kupietz8fd499e2026-08-02 07:32:43 +0200130 INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}"
Marc Kupietzb9912e32024-11-23 10:11:06 +0100131 BUILD_WITH_INSTALL_RPATH TRUE)
132 add_dependencies(tools ${TOOL_NAME})
133 install(TARGETS ${TOOL_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
134endforeach()
135
Marc Kupietzb17122d2024-11-21 14:32:29 +0100136file(GLOB EXAMPLES_SOURCES examples/*.c examples/*.cc)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100137add_custom_target(examples)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100138foreach(EXAMPLE_SOURCE ${EXAMPLES_SOURCES})
139 get_filename_component(EXAMPLE_NAME ${EXAMPLE_SOURCE} NAME_WE)
140 add_executable(${EXAMPLE_NAME} EXCLUDE_FROM_ALL ${EXAMPLE_SOURCE})
141 target_link_libraries(${EXAMPLE_NAME} ${ROCKSDB} collocatordb)
142 add_dependencies(examples ${EXAMPLE_NAME})
143endforeach()