blob: 8abe34544bc8d316a2db63ebbf5d72b0c7566e4e [file] [log] [blame]
Marc Kupietz6afb3522021-03-22 17:22:55 +01001cmake_minimum_required(VERSION 3.9)
Marc Kupietzd724abd2024-10-21 18:41:25 +02002project(dereko2vec VERSION 0.9.1 DESCRIPTION "Fork from wang2vec with extensions for re-training and count based models")
Marc Kupietz8b169772025-02-10 20:55:47 +01003option(STATIC_DEREKO2VEC "Build dereko2vec as a static binary" OFF)
Marc Kupietza0904742026-08-09 14:29:23 +02004option(STATIC_ROCKSDB "Link rocksdb and collocatordb statically, everything else shared" OFF)
Marc Kupietz6d9b00e2026-08-02 07:54:24 +02005if (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.")
8endif ()
Marc Kupietz8b169772025-02-10 20:55:47 +01009
Marc Kupietz6afb3522021-03-22 17:22:55 +010010set(CMAKE_VERBOSE_MAKEFILE on)
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020011
12if (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 "")
18else ()
19 set(LIBRT "rt")
20 set(TUNE_FLAGS "-march=native -mtune=native")
21endif ()
22
Marc Kupietz61485ad2023-12-22 16:16:59 +010023if (CMAKE_BUILD_TYPE MATCHES Debug)
Marc Kupietz8b169772025-02-10 20:55:47 +010024 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 Kupietz61485ad2023-12-22 16:16:59 +010025else ()
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020026 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 ${TUNE_FLAGS} -Wall -funroll-loops")
Marc Kupietz61485ad2023-12-22 16:16:59 +010027endif ()
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020028set(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} m pthread ${LIBRT} snappy z bz2 lz4 zstd stdc++ dl)
Marc Kupietz6afb3522021-03-22 17:22:55 +010029include(GNUInstallDirs)
30
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020031# /opt/homebrew is where homebrew installs on Apple Silicon
32include_directories(/usr/local/include /usr/local/kl/include /opt/homebrew/include)
33link_directories(/usr/local/kl/lib /usr/local/lib /usr/local/lib64 /opt/homebrew/lib)
Marc Kupietz6afb3522021-03-22 17:22:55 +010034
Marc Kupietza0904742026-08-09 14:29:23 +020035# 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.
42if (STATIC_DEREKO2VEC OR STATIC_ROCKSDB)
Marc Kupietz46cad402026-08-02 07:23:05 +020043 find_library(COLLOCATORDB libcollocatordb_static.a)
44 find_library(ROCKSDB librocksdb.a)
45else ()
46 set(_dereko2vec_lib_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
Marc Kupietz2072f292026-08-02 07:38:44 +020047 set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
Marc Kupietz46cad402026-08-02 07:23:05 +020048 find_library(COLLOCATORDB NAMES collocatordb)
49 find_library(ROCKSDB NAMES rocksdb)
50 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_dereko2vec_lib_suffixes})
Marc Kupietz6afb3522021-03-22 17:22:55 +010051endif ()
52
Marc Kupietz46cad402026-08-02 07:23:05 +020053if (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 Kupietza0904742026-08-09 14:29:23 +020056 " - a static build needs libcollocatordb_static.a, otherwise "
Marc Kupietz46cad402026-08-02 07:23:05 +020057 "libcollocatordb.so is enough.")
Marc Kupietz6afb3522021-03-22 17:22:55 +010058endif ()
Marc Kupietzf2aa92e2026-07-31 14:48:02 +090059if (NOT ROCKSDB)
Marc Kupietz46cad402026-08-02 07:23:05 +020060 message(FATAL_ERROR "No rocksdb found. Install the rocksdb development "
Marc Kupietzb421c802026-08-02 16:21:31 +020061 "package of your distribution, e.g. librocksdb-dev or "
Marc Kupietza0904742026-08-09 14:29:23 +020062 "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 Kupietzf2aa92e2026-07-31 14:48:02 +090067endif ()
Marc Kupietz46cad402026-08-02 07:23:05 +020068message(STATUS "Using collocatordb ${COLLOCATORDB}")
69message(STATUS "Using RocksDB ${ROCKSDB}")
Marc Kupietz6afb3522021-03-22 17:22:55 +010070
Marc Kupietzb421c802026-08-02 16:21:31 +020071# 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.
75find_path(COLLOCATORDB_INCLUDE_DIR collocatordb.h)
76if (COLLOCATORDB_INCLUDE_DIR)
77 include_directories(BEFORE ${COLLOCATORDB_INCLUDE_DIR})
78 message(STATUS "Using collocatordb header in ${COLLOCATORDB_INCLUDE_DIR}")
79endif ()
80
Marc Kupietz6afb3522021-03-22 17:22:55 +010081file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/src/*.c)
82foreach (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 Kupietz8b169772025-02-10 20:55:47 +010090 if(STATIC_DEREKO2VEC)
91 set_target_properties(${myname} PROPERTIES LINK_FLAGS "-static")
92 endif()
93 else ()
94 target_link_libraries(${myname} m pthread)
Marc Kupietz6afb3522021-03-22 17:22:55 +010095 endif ()
96endforeach (mysourcefile ${APP_SOURCES})
Marc Kupietz31e2e312021-03-22 17:22:57 +010097
Marc Kupietz46cad402026-08-02 07:23:05 +020098# 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 Kupietz4314a1c2026-08-02 07:32:43 +0200102#
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.
107if (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 ()
113endif ()
Marc Kupietz46cad402026-08-02 07:23:05 +0200114install(TARGETS dereko2vec vecs2mmap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
115
Marc Kupietz31e2e312021-03-22 17:22:57 +0100116enable_testing()
117find_program(BASH_PROGRAM bash)
118if (BASH_PROGRAM)
119 add_test(type-3-test ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test-type-3.sh)
120else ()
121 message(SEND_ERROR "Cannot find bash, needed for testing")
122endif ()