blob: 4076589118e09aef0380906740d6d9a97563ba0f [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 "
58 "package of your distribution, e.g. librocksdb-dev or rocksdb-devel.")
Marc Kupietzf2aa92e2026-07-31 14:48:02 +090059endif ()
Marc Kupietz46cad402026-08-02 07:23:05 +020060message(STATUS "Using collocatordb ${COLLOCATORDB}")
61message(STATUS "Using RocksDB ${ROCKSDB}")
Marc Kupietz6afb3522021-03-22 17:22:55 +010062
63file(GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/src/*.c)
64foreach (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 Kupietz8b169772025-02-10 20:55:47 +010072 if(STATIC_DEREKO2VEC)
73 set_target_properties(${myname} PROPERTIES LINK_FLAGS "-static")
74 endif()
75 else ()
76 target_link_libraries(${myname} m pthread)
Marc Kupietz6afb3522021-03-22 17:22:55 +010077 endif ()
78endforeach (mysourcefile ${APP_SOURCES})
Marc Kupietz31e2e312021-03-22 17:22:57 +010079
Marc Kupietz46cad402026-08-02 07:23:05 +020080# 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 Kupietz4314a1c2026-08-02 07:32:43 +020084#
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.
89if (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 ()
95endif ()
Marc Kupietz46cad402026-08-02 07:23:05 +020096install(TARGETS dereko2vec vecs2mmap RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
97
Marc Kupietz31e2e312021-03-22 17:22:57 +010098enable_testing()
99find_program(BASH_PROGRAM bash)
100if (BASH_PROGRAM)
101 add_test(type-3-test ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/tests/test-type-3.sh)
102else ()
103 message(SEND_ERROR "Cannot find bash, needed for testing")
104endif ()