blob: 84a14c40ecf8bc9ee4322350cf4184f2c44b8c63 [file] [log] [blame]
Marc Kupietzed0bfcf2021-03-14 14:56:15 +01001cmake_minimum_required(VERSION 3.9)
Marc Kupietz18b230d2026-07-31 09:28:23 +09002project(collocatordb VERSION 1.5.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
18# the RPATH to be used when installing, but only if it's not a system directory
19list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
20if("${isSystemDir}" STREQUAL "-1")
21 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
22endif("${isSystemDir}" STREQUAL "-1")
23
Marc Kupietz6208fd72024-11-15 15:46:19 +010024configure_file(src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY)
Marc Kupietz57047c32026-07-31 09:17:57 +090025include_directories(/usr/local/include /opt/homebrew/include ${CMAKE_CURRENT_BINARY_DIR} build)
Marc Kupietz44229232024-08-05 15:00:20 +020026link_directories(/usr/local/lib64 /usr/local/lib /usr/lib64 /usr/lib /opt/homebrew/lib)
27if (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")
33else()
34 set(LIBRT "rt")
Marc Kupietz57047c32026-07-31 09:17:57 +090035 # 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 Kupietz44229232024-08-05 15:00:20 +020044endif()
Marc Kupietz57047c32026-07-31 09:17:57 +090045message(STATUS "Using RocksDB library ${ROCKSDB}")
Marc Kupietz1b09e4d2021-03-14 15:20:19 +010046find_library(ROCKSDB_STATIC librocksdb.a)
47
Marc Kupietz6208fd72024-11-15 15:46:19 +010048get_cmake_property(_variableNames VARIABLES)
49list (SORT _variableNames)
50foreach (_variableName ${_variableNames})
51 message(STATUS "${_variableName}=${${_variableName}}")
52endforeach()
53
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010054enable_testing()
55add_library(collocatordb SHARED src/collocatordb.cc)
Marc Kupietz44229232024-08-05 15:00:20 +020056
57target_link_libraries(collocatordb pthread ${LIBRT} snappy z bz2 lz4 zstd ${ROCKSDB} dl)
58
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010059set_target_properties(collocatordb PROPERTIES
60 VERSION ${PROJECT_VERSION}
61 SOVERSION 1
62 PUBLIC_HEADER src/collocatordb.h)
63
64add_library(collocatordb_static STATIC src/collocatordb.cc)
Marc Kupietz57047c32026-07-31 09:17:57 +090065# Distributions usually package rocksdb as a shared library only, in which case
66# the static collocatordb is linked against that one.
67if (ROCKSDB_STATIC)
68 set(ROCKSDB_FOR_STATIC ${ROCKSDB_STATIC})
69else()
70 set(ROCKSDB_FOR_STATIC ${ROCKSDB})
71 message(STATUS "No static rocksdb found, linking collocatordb_static against ${ROCKSDB}")
72endif()
73target_link_libraries(collocatordb_static ${ROCKSDB_FOR_STATIC} pthread ${LIBRT} snappy z bz2 lz4 zstd dl)
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010074
75add_executable(basic_test tests/basic_test.c tests/acutest.h)
76TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb)
77add_test(BASIC_TEST basic_test)
78
Marc Kupietz1b09e4d2021-03-14 15:20:19 +010079add_executable(static_library_test tests/basic_test.c tests/acutest.h)
80TARGET_LINK_LIBRARIES(static_library_test collocatordb_static)
81add_test(STATIC_LIBRARY_TEST static_library_test)
82
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010083# Install library
Marc Kupietz8f977ca2021-03-22 17:52:52 +010084install(TARGETS ${PROJECT_NAME} collocatordb_static
Marc Kupietz15f56e72021-03-15 14:58:13 +010085 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietzd15845c2022-04-11 22:28:11 +020086 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietz15f56e72021-03-15 14:58:13 +010087 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Marc Kupietz44229232024-08-05 15:00:20 +020088
89if (1 AND APPLE)
90 add_custom_command(TARGET collocatordb POST_BUILD
91 COMMAND install_name_tool -id "/usr/local/lib/libcollocatordb.1.dylib" $<TARGET_FILE:collocatordb>)
92endif()
Marc Kupietzb17122d2024-11-21 14:32:29 +010093
Marc Kupietzb9912e32024-11-23 10:11:06 +010094file(GLOB TOOLS_SOURCES tools/*.c tools/*.cc)
95add_custom_target(tools)
96foreach(TOOL_SOURCE ${TOOLS_SOURCES})
97 get_filename_component(TOOL_NAME ${TOOL_SOURCE} NAME_WE)
98 add_executable(${TOOL_NAME} ${TOOL_SOURCE})
99 target_link_libraries(${TOOL_NAME} ${ROCKSDB} collocatordb)
100 set_target_properties(${TOOL_NAME} PROPERTIES
101 INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib64"
102 BUILD_WITH_INSTALL_RPATH TRUE)
103 add_dependencies(tools ${TOOL_NAME})
104 install(TARGETS ${TOOL_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
105endforeach()
106
Marc Kupietzb17122d2024-11-21 14:32:29 +0100107file(GLOB EXAMPLES_SOURCES examples/*.c examples/*.cc)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100108add_custom_target(examples)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100109foreach(EXAMPLE_SOURCE ${EXAMPLES_SOURCES})
110 get_filename_component(EXAMPLE_NAME ${EXAMPLE_SOURCE} NAME_WE)
111 add_executable(${EXAMPLE_NAME} EXCLUDE_FROM_ALL ${EXAMPLE_SOURCE})
112 target_link_libraries(${EXAMPLE_NAME} ${ROCKSDB} collocatordb)
113 add_dependencies(examples ${EXAMPLE_NAME})
114endforeach()