blob: f391940e3627fbd5cbbed04d6d40f04f6b6e84b2 [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 Kupietz6d9b00e2026-08-02 07:54:24 +02004if (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.")
7endif ()
Marc Kupietz8b169772025-02-10 20:55:47 +01008
Marc Kupietz6afb3522021-03-22 17:22:55 +01009set(CMAKE_VERBOSE_MAKEFILE on)
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020010
11if (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 "")
17else ()
18 set(LIBRT "rt")
19 set(TUNE_FLAGS "-march=native -mtune=native")
20endif ()
21
Marc Kupietz61485ad2023-12-22 16:16:59 +010022if (CMAKE_BUILD_TYPE MATCHES Debug)
Marc Kupietz8b169772025-02-10 20:55:47 +010023 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 +010024else ()
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020025 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 ${TUNE_FLAGS} -Wall -funroll-loops")
Marc Kupietz61485ad2023-12-22 16:16:59 +010026endif ()
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020027set(ADDITIONAL_LIBS ${ADDITIONAL_LIBS} m pthread ${LIBRT} snappy z bz2 lz4 zstd stdc++ dl)
Marc Kupietz6afb3522021-03-22 17:22:55 +010028include(GNUInstallDirs)
29
Marc Kupietz6d9b00e2026-08-02 07:54:24 +020030# /opt/homebrew is where homebrew installs on Apple Silicon
31include_directories(/usr/local/include /usr/local/kl/include /opt/homebrew/include)
32link_directories(/usr/local/kl/lib /usr/local/lib /usr/local/lib64 /opt/homebrew/lib)
Marc Kupietz6afb3522021-03-22 17:22:55 +010033
Marc Kupietz46cad402026-08-02 07:23:05 +020034# 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.
39if (STATIC_DEREKO2VEC)
40 find_library(COLLOCATORDB libcollocatordb_static.a)
41 find_library(ROCKSDB librocksdb.a)
42else ()
43 set(_dereko2vec_lib_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
Marc Kupietz2072f292026-08-02 07:38:44 +020044 set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX})
Marc Kupietz46cad402026-08-02 07:23:05 +020045 find_library(COLLOCATORDB NAMES collocatordb)
46 find_library(ROCKSDB NAMES rocksdb)
47 set(CMAKE_FIND_LIBRARY_SUFFIXES ${_dereko2vec_lib_suffixes})
Marc Kupietz6afb3522021-03-22 17:22:55 +010048endif ()
49
Marc Kupietz46cad402026-08-02 07:23:05 +020050if (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 Kupietz6afb3522021-03-22 17:22:55 +010055endif ()
Marc Kupietzf2aa92e2026-07-31 14:48:02 +090056if (NOT ROCKSDB)
Marc Kupietz46cad402026-08-02 07:23:05 +020057 message(FATAL_ERROR "No rocksdb found. Install the rocksdb development "
Marc Kupietzb421c802026-08-02 16:21:31 +020058 "package of your distribution, e.g. librocksdb-dev or "
59 "rocksdb-devel. Where there is none, or where it is older than the "
60 "one collocatordb was built against, build rocksdb and say where "
61 "it is: -DCMAKE_PREFIX_PATH=<prefix it was installed to>. It has "
62 "to be the same rocksdb that collocatordb was built against.")
Marc Kupietzf2aa92e2026-07-31 14:48:02 +090063endif ()
Marc Kupietz46cad402026-08-02 07:23:05 +020064message(STATUS "Using collocatordb ${COLLOCATORDB}")
65message(STATUS "Using RocksDB ${ROCKSDB}")
Marc Kupietz6afb3522021-03-22 17:22:55 +010066
Marc Kupietzb421c802026-08-02 16:21:31 +020067# collocatordb.h has to belong to that library. Looked up rather than assumed,
68# so that CMAKE_PREFIX_PATH works for a collocatordb in a prefix of its own,
69# and put before the rest, so that an older one below /usr/local cannot shadow
70# it - the compiler looks into /usr/local/include before /usr/include.
71find_path(COLLOCATORDB_INCLUDE_DIR collocatordb.h)
72if (COLLOCATORDB_INCLUDE_DIR)
73 include_directories(BEFORE ${COLLOCATORDB_INCLUDE_DIR})
74 message(STATUS "Using collocatordb header in ${COLLOCATORDB_INCLUDE_DIR}")
75endif ()
76
Marc Kupietz6afb3522021-03-22 17:22:55 +010077file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/src/*.c)
78foreach (mysourcefile ${APP_SOURCES})
79 # I used a simple string replace, to cut off .cpp.
80 string(REPLACE ".c" "" myname ${mysourcefile})
81 string(REGEX REPLACE ".*/" "" myname ${myname})
82 add_executable(${myname} ${mysourcefile})
83 # Make sure YourLib is linked to each app
84 if (${myname} MATCHES "derek")
85 target_link_libraries(${myname} ${COLLOCATORDB} ${ROCKSDB} ${ADDITIONAL_LIBS})
Marc Kupietz8b169772025-02-10 20:55:47 +010086 if(STATIC_DEREKO2VEC)
87 set_target_properties(${myname} PROPERTIES LINK_FLAGS "-static")
88 endif()
89 else ()
90 target_link_libraries(${myname} m pthread)
Marc Kupietz6afb3522021-03-22 17:22:55 +010091 endif ()
92endforeach (mysourcefile ${APP_SOURCES})
Marc Kupietz31e2e312021-03-22 17:22:57 +010093
Marc Kupietz46cad402026-08-02 07:23:05 +020094# Only the two that are used outside of a build directory: the trainer and the
95# converter that writes the memory mappable form derekovecs maps. The remaining
96# programs keep the generic names they inherited from word2vec, like distance
97# or kmeans_txt, which have no business in a directory on everybody's PATH.
Marc Kupietz4314a1c2026-08-02 07:32:43 +020098#
99# The installed dereko2vec gets the directory of the collocatordb it was linked
100# against as its rpath. An installation below /usr/local is not in the search
101# path of the loader by default, and without this it only starts with
102# LD_LIBRARY_PATH set.
103if (NOT STATIC_DEREKO2VEC)
104 get_filename_component(COLLOCATORDB_LIBDIR "${COLLOCATORDB}" DIRECTORY)
105 list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${COLLOCATORDB_LIBDIR}" isSystemLibDir)
106 if ("${isSystemLibDir}" STREQUAL "-1")
107 set_target_properties(dereko2vec PROPERTIES INSTALL_RPATH "${COLLOCATORDB_LIBDIR}")
108 endif ()
109endif ()
Marc Kupietz46cad402026-08-02 07:23:05 +0200110install(TARGETS dereko2vec vecs2mmap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
111
Marc Kupietz31e2e312021-03-22 17:22:57 +0100112enable_testing()
113find_program(BASH_PROGRAM bash)
114if (BASH_PROGRAM)
115 add_test(type-3-test ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test-type-3.sh)
116else ()
117 message(SEND_ERROR "Cannot find bash, needed for testing")
118endif ()