| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.9) |
| Marc Kupietz | d724abd | 2024-10-21 18:41:25 +0200 | [diff] [blame] | 2 | project(dereko2vec VERSION 0.9.1 DESCRIPTION "Fork from wang2vec with extensions for re-training and count based models") |
| Marc Kupietz | 8b16977 | 2025-02-10 20:55:47 +0100 | [diff] [blame] | 3 | option(STATIC_DEREKO2VEC "Build dereko2vec as a static binary" OFF) |
| Marc Kupietz | a090474 | 2026-08-09 14:29:23 +0200 | [diff] [blame] | 4 | option(STATIC_ROCKSDB "Link rocksdb and collocatordb statically, everything else shared" OFF) |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 5 | if (STATIC_DEREKO2VEC AND APPLE) |
| 6 | message(FATAL_ERROR "STATIC_DEREKO2VEC does not work on MacOS, which ships " |
| 7 | "no static system libraries. Build without it.") |
| 8 | endif () |
| Marc Kupietz | 8b16977 | 2025-02-10 20:55:47 +0100 | [diff] [blame] | 9 | |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 10 | set(CMAKE_VERBOSE_MAKEFILE on) |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 11 | |
| 12 | if (APPLE) |
| 13 | # No librt, its functions are in the system library. -march=native is not |
| 14 | # accepted by the compilers on Apple Silicon, and the speed of the training |
| 15 | # is not what MacOS is used for here. |
| 16 | set(LIBRT "") |
| 17 | set(TUNE_FLAGS "") |
| 18 | else () |
| 19 | set(LIBRT "rt") |
| 20 | set(TUNE_FLAGS "-march=native -mtune=native") |
| 21 | endif () |
| 22 | |
| Marc Kupietz | 61485ad | 2023-12-22 16:16:59 +0100 | [diff] [blame] | 23 | if (CMAKE_BUILD_TYPE MATCHES Debug) |
| Marc Kupietz | 8b16977 | 2025-02-10 20:55:47 +0100 | [diff] [blame] | 24 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -Wall -Wextra -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-but-set-parameter -Wno-unused-value -Wno-unused-result") |
| Marc Kupietz | 61485ad | 2023-12-22 16:16:59 +0100 | [diff] [blame] | 25 | else () |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 26 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 ${TUNE_FLAGS} -Wall -funroll-loops") |
| Marc Kupietz | 61485ad | 2023-12-22 16:16:59 +0100 | [diff] [blame] | 27 | endif () |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 28 | set(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} m pthread ${LIBRT} snappy z bz2 lz4 zstd stdc++ dl) |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 29 | include(GNUInstallDirs) |
| 30 | |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 31 | # /opt/homebrew is where homebrew installs on Apple Silicon |
| 32 | include_directories(/usr/local/include /usr/local/kl/include /opt/homebrew/include) |
| 33 | link_directories(/usr/local/kl/lib /usr/local/lib /usr/local/lib64 /opt/homebrew/lib) |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 34 | |
| Marc Kupietz | a090474 | 2026-08-09 14:29:23 +0200 | [diff] [blame] | 35 | # A static dereko2vec needs everything as a static library. With STATIC_ROCKSDB |
| 36 | # only rocksdb and collocatordb are static and the compression libraries stay |
| 37 | # shared, which is the fast indexing build the README describes. An ordinary |
| 38 | # build takes the shared ones, and it has to take them explicitly: a leftover |
| 39 | # static library below /usr/local would otherwise win over the shared library |
| 40 | # of the distribution, because cmake weighs the search path higher than the |
| 41 | # suffix. Set -DCOLLOCATORDB=<path> or -DROCKSDB=<path> to pick specific ones. |
| 42 | if (STATIC_DEREKO2VEC OR STATIC_ROCKSDB) |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 43 | find_library(COLLOCATORDB libcollocatordb_static.a) |
| 44 | find_library(ROCKSDB librocksdb.a) |
| 45 | else () |
| 46 | set(_dereko2vec_lib_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES}) |
| Marc Kupietz | 2072f29 | 2026-08-02 07:38:44 +0200 | [diff] [blame] | 47 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 48 | find_library(COLLOCATORDB NAMES collocatordb) |
| 49 | find_library(ROCKSDB NAMES rocksdb) |
| 50 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${_dereko2vec_lib_suffixes}) |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 51 | endif () |
| 52 | |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 53 | if (NOT COLLOCATORDB) |
| 54 | message(FATAL_ERROR "No collocatordb found. Please install it from " |
| 55 | "https://korap.ids-mannheim.de/gerrit/plugins/gitiles/ids-kl/collocatordb" |
| Marc Kupietz | a090474 | 2026-08-09 14:29:23 +0200 | [diff] [blame] | 56 | " - a static build needs libcollocatordb_static.a, otherwise " |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 57 | "libcollocatordb.so is enough.") |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 58 | endif () |
| Marc Kupietz | f2aa92e | 2026-07-31 14:48:02 +0900 | [diff] [blame] | 59 | if (NOT ROCKSDB) |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 60 | message(FATAL_ERROR "No rocksdb found. Install the rocksdb development " |
| Marc Kupietz | b421c80 | 2026-08-02 16:21:31 +0200 | [diff] [blame] | 61 | "package of your distribution, e.g. librocksdb-dev or " |
| Marc Kupietz | a090474 | 2026-08-09 14:29:23 +0200 | [diff] [blame] | 62 | "rocksdb-devel. Where there is none, or where a static one is " |
| 63 | "needed, scripts/build-rocksdb.sh builds one into a prefix of its " |
| 64 | "own; then say where it is: -DCMAKE_PREFIX_PATH=<prefix>. It has " |
| 65 | "to be the same rocksdb that collocatordb was built against, " |
| 66 | "which the script takes care of on its own.") |
| Marc Kupietz | f2aa92e | 2026-07-31 14:48:02 +0900 | [diff] [blame] | 67 | endif () |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 68 | message(STATUS "Using collocatordb ${COLLOCATORDB}") |
| 69 | message(STATUS "Using RocksDB ${ROCKSDB}") |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 70 | |
| Marc Kupietz | b421c80 | 2026-08-02 16:21:31 +0200 | [diff] [blame] | 71 | # collocatordb.h has to belong to that library. Looked up rather than assumed, |
| 72 | # so that CMAKE_PREFIX_PATH works for a collocatordb in a prefix of its own, |
| 73 | # and put before the rest, so that an older one below /usr/local cannot shadow |
| 74 | # it - the compiler looks into /usr/local/include before /usr/include. |
| 75 | find_path(COLLOCATORDB_INCLUDE_DIR collocatordb.h) |
| 76 | if (COLLOCATORDB_INCLUDE_DIR) |
| 77 | include_directories(BEFORE ${COLLOCATORDB_INCLUDE_DIR}) |
| 78 | message(STATUS "Using collocatordb header in ${COLLOCATORDB_INCLUDE_DIR}") |
| 79 | endif () |
| 80 | |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 81 | file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/src/*.c) |
| 82 | foreach (mysourcefile ${APP_SOURCES}) |
| 83 | # I used a simple string replace, to cut off .cpp. |
| 84 | string(REPLACE ".c" "" myname ${mysourcefile}) |
| 85 | string(REGEX REPLACE ".*/" "" myname ${myname}) |
| 86 | add_executable(${myname} ${mysourcefile}) |
| 87 | # Make sure YourLib is linked to each app |
| 88 | if (${myname} MATCHES "derek") |
| 89 | target_link_libraries(${myname} ${COLLOCATORDB} ${ROCKSDB} ${ADDITIONAL_LIBS}) |
| Marc Kupietz | 8b16977 | 2025-02-10 20:55:47 +0100 | [diff] [blame] | 90 | if(STATIC_DEREKO2VEC) |
| 91 | set_target_properties(${myname} PROPERTIES LINK_FLAGS "-static") |
| 92 | endif() |
| 93 | else () |
| 94 | target_link_libraries(${myname} m pthread) |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 95 | endif () |
| 96 | endforeach (mysourcefile ${APP_SOURCES}) |
| Marc Kupietz | 31e2e31 | 2021-03-22 17:22:57 +0100 | [diff] [blame] | 97 | |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 98 | # Only the two that are used outside of a build directory: the trainer and the |
| 99 | # converter that writes the memory mappable form derekovecs maps. The remaining |
| 100 | # programs keep the generic names they inherited from word2vec, like distance |
| 101 | # or kmeans_txt, which have no business in a directory on everybody's PATH. |
| Marc Kupietz | 4314a1c | 2026-08-02 07:32:43 +0200 | [diff] [blame] | 102 | # |
| 103 | # The installed dereko2vec gets the directory of the collocatordb it was linked |
| 104 | # against as its rpath. An installation below /usr/local is not in the search |
| 105 | # path of the loader by default, and without this it only starts with |
| 106 | # LD_LIBRARY_PATH set. |
| 107 | if (NOT STATIC_DEREKO2VEC) |
| 108 | get_filename_component(COLLOCATORDB_LIBDIR "${COLLOCATORDB}" DIRECTORY) |
| 109 | list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${COLLOCATORDB_LIBDIR}" isSystemLibDir) |
| 110 | if ("${isSystemLibDir}" STREQUAL "-1") |
| 111 | set_target_properties(dereko2vec PROPERTIES INSTALL_RPATH "${COLLOCATORDB_LIBDIR}") |
| 112 | endif () |
| 113 | endif () |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 114 | install(TARGETS dereko2vec vecs2mmap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) |
| 115 | |
| Marc Kupietz | 31e2e31 | 2021-03-22 17:22:57 +0100 | [diff] [blame] | 116 | enable_testing() |
| 117 | find_program(BASH_PROGRAM bash) |
| 118 | if (BASH_PROGRAM) |
| 119 | add_test(type-3-test ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test-type-3.sh) |
| 120 | else () |
| 121 | message(SEND_ERROR "Cannot find bash, needed for testing") |
| 122 | endif () |