blob: 6fccb2685767d695f9d0a5027dbeb557d29ab0f0 [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 Kupietz57047c32026-07-31 09:17:57 +09006# RocksDB requires C++17 since version 7
7set(CMAKE_CXX_STANDARD 17)
8set(CMAKE_CXX_STANDARD_REQUIRED ON)
9
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010010include(GNUInstallDirs)
Marc Kupietz15f56e72021-03-15 14:58:13 +010011
12# the RPATH to be used when installing, but only if it's not a system directory
13list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
14if("${isSystemDir}" STREQUAL "-1")
15 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
16endif("${isSystemDir}" STREQUAL "-1")
17
Marc Kupietz6208fd72024-11-15 15:46:19 +010018configure_file(src/config.h.in ${CMAKE_CURRENT_BINARY_DIR}/config.h @ONLY)
Marc Kupietz57047c32026-07-31 09:17:57 +090019include_directories(/usr/local/include /opt/homebrew/include ${CMAKE_CURRENT_BINARY_DIR} build)
Marc Kupietz44229232024-08-05 15:00:20 +020020link_directories(/usr/local/lib64 /usr/local/lib /usr/lib64 /usr/lib /opt/homebrew/lib)
21if (1 AND APPLE)
22 message("MacOS deteted")
23 set(LIBRT "")
24 find_library(ROCKSDB NAMES librocksdb.a)
25 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flat_namespace")
26 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flat_namespace")
27else()
28 set(LIBRT "rt")
Marc Kupietz57047c32026-07-31 09:17:57 +090029 # No version pin, so that the rocksdb of the distribution can be used.
30 # Only shared libraries, the static one is picked up as ROCKSDB_STATIC
31 # below. Set -DROCKSDB=<path> to choose a specific one, and make sure the
32 # headers that are found belong to it, i.e. that there is no leftover
33 # rocksdb installation below /usr/local.
34 set(_collocatordb_lib_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
35 set(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
36 find_library(ROCKSDB NAMES rocksdb)
37 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_collocatordb_lib_suffixes})
Marc Kupietz44229232024-08-05 15:00:20 +020038endif()
Marc Kupietz57047c32026-07-31 09:17:57 +090039message(STATUS "Using RocksDB library ${ROCKSDB}")
Marc Kupietz1b09e4d2021-03-14 15:20:19 +010040find_library(ROCKSDB_STATIC librocksdb.a)
41
Marc Kupietz6208fd72024-11-15 15:46:19 +010042get_cmake_property(_variableNames VARIABLES)
43list (SORT _variableNames)
44foreach (_variableName ${_variableNames})
45 message(STATUS "${_variableName}=${${_variableName}}")
46endforeach()
47
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010048enable_testing()
49add_library(collocatordb SHARED src/collocatordb.cc)
Marc Kupietz44229232024-08-05 15:00:20 +020050
51target_link_libraries(collocatordb pthread ${LIBRT} snappy z bz2 lz4 zstd ${ROCKSDB} dl)
52
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010053set_target_properties(collocatordb PROPERTIES
54 VERSION ${PROJECT_VERSION}
55 SOVERSION 1
56 PUBLIC_HEADER src/collocatordb.h)
57
58add_library(collocatordb_static STATIC src/collocatordb.cc)
Marc Kupietz57047c32026-07-31 09:17:57 +090059# Distributions usually package rocksdb as a shared library only, in which case
60# the static collocatordb is linked against that one.
61if (ROCKSDB_STATIC)
62 set(ROCKSDB_FOR_STATIC ${ROCKSDB_STATIC})
63else()
64 set(ROCKSDB_FOR_STATIC ${ROCKSDB})
65 message(STATUS "No static rocksdb found, linking collocatordb_static against ${ROCKSDB}")
66endif()
67target_link_libraries(collocatordb_static ${ROCKSDB_FOR_STATIC} pthread ${LIBRT} snappy z bz2 lz4 zstd dl)
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010068
69add_executable(basic_test tests/basic_test.c tests/acutest.h)
70TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb)
71add_test(BASIC_TEST basic_test)
72
Marc Kupietz1b09e4d2021-03-14 15:20:19 +010073add_executable(static_library_test tests/basic_test.c tests/acutest.h)
74TARGET_LINK_LIBRARIES(static_library_test collocatordb_static)
75add_test(STATIC_LIBRARY_TEST static_library_test)
76
Marc Kupietzed0bfcf2021-03-14 14:56:15 +010077# Install library
Marc Kupietz8f977ca2021-03-22 17:52:52 +010078install(TARGETS ${PROJECT_NAME} collocatordb_static
Marc Kupietz15f56e72021-03-15 14:58:13 +010079 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietzd15845c2022-04-11 22:28:11 +020080 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Marc Kupietz15f56e72021-03-15 14:58:13 +010081 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Marc Kupietz44229232024-08-05 15:00:20 +020082
83if (1 AND APPLE)
84 add_custom_command(TARGET collocatordb POST_BUILD
85 COMMAND install_name_tool -id "/usr/local/lib/libcollocatordb.1.dylib" $<TARGET_FILE:collocatordb>)
86endif()
Marc Kupietzb17122d2024-11-21 14:32:29 +010087
Marc Kupietzb9912e32024-11-23 10:11:06 +010088file(GLOB TOOLS_SOURCES tools/*.c tools/*.cc)
89add_custom_target(tools)
90foreach(TOOL_SOURCE ${TOOLS_SOURCES})
91 get_filename_component(TOOL_NAME ${TOOL_SOURCE} NAME_WE)
92 add_executable(${TOOL_NAME} ${TOOL_SOURCE})
93 target_link_libraries(${TOOL_NAME} ${ROCKSDB} collocatordb)
94 set_target_properties(${TOOL_NAME} PROPERTIES
95 INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib64"
96 BUILD_WITH_INSTALL_RPATH TRUE)
97 add_dependencies(tools ${TOOL_NAME})
98 install(TARGETS ${TOOL_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
99endforeach()
100
Marc Kupietzb17122d2024-11-21 14:32:29 +0100101file(GLOB EXAMPLES_SOURCES examples/*.c examples/*.cc)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100102add_custom_target(examples)
Marc Kupietzb17122d2024-11-21 14:32:29 +0100103foreach(EXAMPLE_SOURCE ${EXAMPLES_SOURCES})
104 get_filename_component(EXAMPLE_NAME ${EXAMPLE_SOURCE} NAME_WE)
105 add_executable(${EXAMPLE_NAME} EXCLUDE_FROM_ALL ${EXAMPLE_SOURCE})
106 target_link_libraries(${EXAMPLE_NAME} ${ROCKSDB} collocatordb)
107 add_dependencies(examples ${EXAMPLE_NAME})
108endforeach()