| 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 | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 4 | if (STATIC_DEREKO2VEC AND APPLE) |
| 5 | message(FATAL_ERROR "STATIC_DEREKO2VEC does not work on MacOS, which ships " |
| 6 | "no static system libraries. Build without it.") |
| 7 | endif () |
| Marc Kupietz | 8b16977 | 2025-02-10 20:55:47 +0100 | [diff] [blame] | 8 | |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 9 | set(CMAKE_VERBOSE_MAKEFILE on) |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 10 | |
| 11 | if (APPLE) |
| 12 | # No librt, its functions are in the system library. -march=native is not |
| 13 | # accepted by the compilers on Apple Silicon, and the speed of the training |
| 14 | # is not what MacOS is used for here. |
| 15 | set(LIBRT "") |
| 16 | set(TUNE_FLAGS "") |
| 17 | else () |
| 18 | set(LIBRT "rt") |
| 19 | set(TUNE_FLAGS "-march=native -mtune=native") |
| 20 | endif () |
| 21 | |
| Marc Kupietz | 61485ad | 2023-12-22 16:16:59 +0100 | [diff] [blame] | 22 | if (CMAKE_BUILD_TYPE MATCHES Debug) |
| Marc Kupietz | 8b16977 | 2025-02-10 20:55:47 +0100 | [diff] [blame] | 23 | 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] | 24 | else () |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 25 | 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] | 26 | endif () |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 27 | 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] | 28 | include(GNUInstallDirs) |
| 29 | |
| Marc Kupietz | 6d9b00e | 2026-08-02 07:54:24 +0200 | [diff] [blame] | 30 | # /opt/homebrew is where homebrew installs on Apple Silicon |
| 31 | include_directories(/usr/local/include /usr/local/kl/include /opt/homebrew/include) |
| 32 | 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] | 33 | |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 34 | # A static dereko2vec needs everything as a static library. An ordinary build |
| 35 | # takes the shared ones, and it has to take them explicitly: a leftover static |
| 36 | # library below /usr/local would otherwise win over the shared library of the |
| 37 | # distribution, because cmake weighs the search path higher than the suffix. |
| 38 | # Set -DCOLLOCATORDB=<path> or -DROCKSDB=<path> to pick specific ones. |
| 39 | if (STATIC_DEREKO2VEC) |
| 40 | find_library(COLLOCATORDB libcollocatordb_static.a) |
| 41 | find_library(ROCKSDB librocksdb.a) |
| 42 | else () |
| 43 | set(_dereko2vec_lib_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES}) |
| Marc Kupietz | 2072f29 | 2026-08-02 07:38:44 +0200 | [diff] [blame] | 44 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX}) |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 45 | find_library(COLLOCATORDB NAMES collocatordb) |
| 46 | find_library(ROCKSDB NAMES rocksdb) |
| 47 | set(CMAKE_FIND_LIBRARY_SUFFIXES ${_dereko2vec_lib_suffixes}) |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 48 | endif () |
| 49 | |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 50 | if (NOT COLLOCATORDB) |
| 51 | message(FATAL_ERROR "No collocatordb found. Please install it from " |
| 52 | "https://korap.ids-mannheim.de/gerrit/plugins/gitiles/ids-kl/collocatordb" |
| 53 | " - a static dereko2vec needs libcollocatordb_static.a, otherwise " |
| 54 | "libcollocatordb.so is enough.") |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 55 | endif () |
| Marc Kupietz | f2aa92e | 2026-07-31 14:48:02 +0900 | [diff] [blame] | 56 | if (NOT ROCKSDB) |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 57 | message(FATAL_ERROR "No rocksdb found. Install the rocksdb development " |
| 58 | "package of your distribution, e.g. librocksdb-dev or rocksdb-devel.") |
| Marc Kupietz | f2aa92e | 2026-07-31 14:48:02 +0900 | [diff] [blame] | 59 | endif () |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 60 | message(STATUS "Using collocatordb ${COLLOCATORDB}") |
| 61 | message(STATUS "Using RocksDB ${ROCKSDB}") |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 62 | |
| 63 | file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/src/*.c) |
| 64 | foreach (mysourcefile ${APP_SOURCES}) |
| 65 | # I used a simple string replace, to cut off .cpp. |
| 66 | string(REPLACE ".c" "" myname ${mysourcefile}) |
| 67 | string(REGEX REPLACE ".*/" "" myname ${myname}) |
| 68 | add_executable(${myname} ${mysourcefile}) |
| 69 | # Make sure YourLib is linked to each app |
| 70 | if (${myname} MATCHES "derek") |
| 71 | target_link_libraries(${myname} ${COLLOCATORDB} ${ROCKSDB} ${ADDITIONAL_LIBS}) |
| Marc Kupietz | 8b16977 | 2025-02-10 20:55:47 +0100 | [diff] [blame] | 72 | if(STATIC_DEREKO2VEC) |
| 73 | set_target_properties(${myname} PROPERTIES LINK_FLAGS "-static") |
| 74 | endif() |
| 75 | else () |
| 76 | target_link_libraries(${myname} m pthread) |
| Marc Kupietz | 6afb352 | 2021-03-22 17:22:55 +0100 | [diff] [blame] | 77 | endif () |
| 78 | endforeach (mysourcefile ${APP_SOURCES}) |
| Marc Kupietz | 31e2e31 | 2021-03-22 17:22:57 +0100 | [diff] [blame] | 79 | |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 80 | # Only the two that are used outside of a build directory: the trainer and the |
| 81 | # converter that writes the memory mappable form derekovecs maps. The remaining |
| 82 | # programs keep the generic names they inherited from word2vec, like distance |
| 83 | # 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] | 84 | # |
| 85 | # The installed dereko2vec gets the directory of the collocatordb it was linked |
| 86 | # against as its rpath. An installation below /usr/local is not in the search |
| 87 | # path of the loader by default, and without this it only starts with |
| 88 | # LD_LIBRARY_PATH set. |
| 89 | if (NOT STATIC_DEREKO2VEC) |
| 90 | get_filename_component(COLLOCATORDB_LIBDIR "${COLLOCATORDB}" DIRECTORY) |
| 91 | list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${COLLOCATORDB_LIBDIR}" isSystemLibDir) |
| 92 | if ("${isSystemLibDir}" STREQUAL "-1") |
| 93 | set_target_properties(dereko2vec PROPERTIES INSTALL_RPATH "${COLLOCATORDB_LIBDIR}") |
| 94 | endif () |
| 95 | endif () |
| Marc Kupietz | 46cad40 | 2026-08-02 07:23:05 +0200 | [diff] [blame] | 96 | install(TARGETS dereko2vec vecs2mmap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) |
| 97 | |
| Marc Kupietz | 31e2e31 | 2021-03-22 17:22:57 +0100 | [diff] [blame] | 98 | enable_testing() |
| 99 | find_program(BASH_PROGRAM bash) |
| 100 | if (BASH_PROGRAM) |
| 101 | add_test(type-3-test ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test-type-3.sh) |
| 102 | else () |
| 103 | message(SEND_ERROR "Cannot find bash, needed for testing") |
| 104 | endif () |