-
Notifications
You must be signed in to change notification settings - Fork 137
/
CMakeLists.txt
398 lines (348 loc) · 14 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# This CMake file is heavily inspired by following `usearch` CMake:
# https://github.com/nlohmann/json/blob/develop/CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(
usearch
VERSION 2.16.2
LANGUAGES C CXX
DESCRIPTION "Smaller & Faster Single-File Vector Search Engine from Unum"
HOMEPAGE_URL "https://github.com/unum-cloud/usearch"
)
# Determine if USearch is built as a subproject (using `add_subdirectory`) or if it is the main project
set(USEARCH_IS_MAIN_PROJECT OFF)
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(USEARCH_IS_MAIN_PROJECT ON)
endif ()
# Allow CMake 3.13+ to override options when using FetchContent / add_subdirectory
if (POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif ()
# Options
option(USEARCH_INSTALL "Install CMake targets" OFF)
option(USEARCH_USE_OPENMP "Use OpenMP for a thread pool" OFF)
option(USEARCH_USE_SIMSIMD "Use SimSIMD hardware-accelerated metrics" OFF)
option(USEARCH_USE_JEMALLOC "Use JeMalloc for faster memory allocations" OFF)
option(USEARCH_USE_FP16LIB "Use software emulation for half-precision types" ON)
option(USEARCH_BUILD_TEST_CPP "Compile a native unit test in C++" ${USEARCH_IS_MAIN_PROJECT})
option(USEARCH_BUILD_BENCH_CPP "Compile a native benchmark in C++" ${USEARCH_IS_MAIN_PROJECT})
option(USEARCH_BUILD_LIB_C "Compile a native library for the C 99 interface" OFF)
option(USEARCH_BUILD_TEST_C "Compile a test for the C 99 interface" OFF)
option(USEARCH_BUILD_WOLFRAM "Compile Wolfram Language bindings" OFF)
option(USEARCH_BUILD_SQLITE "Compile separate SQLite extension" OFF)
# Includes
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
include(ExternalProject)
# Configuration
include(GNUInstallDirs)
set(USEARCH_TARGET_NAME ${PROJECT_NAME})
set(USEARCH_CONFIG_INSTALL_DIR
"${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}"
CACHE INTERNAL ""
)
set(USEARCH_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
set(USEARCH_TARGETS_EXPORT_NAME "${PROJECT_NAME}Targets")
set(USEARCH_CMAKE_CONFIG_TEMPLATE "cmake/config.cmake.in")
set(USEARCH_CMAKE_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(USEARCH_CMAKE_VERSION_CONFIG_FILE "${USEARCH_CMAKE_CONFIG_DIR}/${PROJECT_NAME}ConfigVersion.cmake")
set(USEARCH_CMAKE_PROJECT_CONFIG_FILE "${USEARCH_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Config.cmake")
set(USEARCH_CMAKE_PROJECT_TARGETS_FILE "${USEARCH_CMAKE_CONFIG_DIR}/${PROJECT_NAME}Targets.cmake")
set(USEARCH_PKGCONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig")
include(CheckCSourceCompiles)
# Check for `__fp16` support
check_c_source_compiles(
[=[
int
main(int argc, char *argv)
{
__fp16 a = 1.0;
__fp16 b = a + a;
return 0;
}
]=]
USEARCH_CAN_COMPILE_FP16
)
# Check for `_Float16` support
check_c_source_compiles(
[=[
int
main(int argc, char *argv)
{
_Float16 a = 1.0;
_Float16 b = a + a;
return 0;
}
]=]
USEARCH_CAN_COMPILE_FLOAT16
)
# Check for `__bf16` support
check_c_source_compiles(
[=[
int
main(int argc, char *argv)
{
__bf16 a = 1.0;
__bf16 b = a + a;
return 0;
}
]=]
USEARCH_CAN_COMPILE_BF16
)
# Check for `_Bfloat16` support
check_c_source_compiles(
[=[
int
main(int argc, char *argv)
{
_Bfloat16 a = 1.0;
_Bfloat16 b = a + a;
return 0;
}
]=]
USEARCH_CAN_COMPILE_BFLOAT16
)
# Define our header-only library
add_library(${USEARCH_TARGET_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${USEARCH_TARGET_NAME} ALIAS ${USEARCH_TARGET_NAME})
set(USEARCH_INCLUDE_BUILD_DIR "${PROJECT_SOURCE_DIR}/include/")
if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
target_compile_features(${USEARCH_TARGET_NAME} INTERFACE cxx_range_for)
else ()
target_compile_features(${USEARCH_TARGET_NAME} INTERFACE cxx_std_11)
endif ()
# Core compilation settings affecting "index.hpp"
target_compile_definitions(${USEARCH_TARGET_NAME} INTERFACE "USEARCH_USE_OPENMP=$<BOOL:${USEARCH_USE_OPENMP}>")
# Supplementary compilation settings affecting "index_plugins.hpp"
target_compile_definitions(${USEARCH_TARGET_NAME} INTERFACE "USEARCH_USE_FP16LIB=$<BOOL:${USEARCH_USE_FP16LIB}>")
target_compile_definitions(${USEARCH_TARGET_NAME} INTERFACE "USEARCH_USE_SIMSIMD=$<BOOL:${USEARCH_USE_SIMSIMD}>")
# Define which types can be compiled
target_compile_definitions(
${USEARCH_TARGET_NAME} INTERFACE "USEARCH_CAN_COMPILE_FP16=$<BOOL:${USEARCH_CAN_COMPILE_FP16}>"
)
target_compile_definitions(
${USEARCH_TARGET_NAME} INTERFACE "USEARCH_CAN_COMPILE_FLOAT16=$<BOOL:${USEARCH_CAN_COMPILE_FLOAT16}>"
)
target_compile_definitions(
${USEARCH_TARGET_NAME} INTERFACE "USEARCH_CAN_COMPILE_BF16=$<BOOL:${USEARCH_CAN_COMPILE_BF16}>"
)
target_compile_definitions(
${USEARCH_TARGET_NAME} INTERFACE "USEARCH_CAN_COMPILE_BFLOAT16=$<BOOL:${USEARCH_CAN_COMPILE_BFLOAT16}>"
)
target_include_directories(
${USEARCH_TARGET_NAME} ${USEARCH_SYSTEM_INCLUDE} INTERFACE $<BUILD_INTERFACE:${USEARCH_INCLUDE_BUILD_DIR}>
$<INSTALL_INTERFACE:include>
)
# Install a pkg-config file, so other tools can find this
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc")
# Install a custom package version config file instead of write_basic_package_version_file to ensure that it's
# architecture-independent https://github.com/nlohmann/json/issues/1697
include(CMakePackageConfigHelpers)
configure_file("cmake/usearchConfigVersion.cmake.in" ${USEARCH_CMAKE_VERSION_CONFIG_FILE} @ONLY)
configure_file(${USEARCH_CMAKE_CONFIG_TEMPLATE} ${USEARCH_CMAKE_PROJECT_CONFIG_FILE} @ONLY)
if (USEARCH_INSTALL)
install(DIRECTORY ${USEARCH_INCLUDE_BUILD_DIR} DESTINATION ${USEARCH_INCLUDE_INSTALL_DIR})
install(FILES ${USEARCH_CMAKE_PROJECT_CONFIG_FILE} ${USEARCH_CMAKE_VERSION_CONFIG_FILE}
DESTINATION ${USEARCH_CONFIG_INSTALL_DIR}
)
export(
TARGETS ${USEARCH_TARGET_NAME}
NAMESPACE ${PROJECT_NAME}::
FILE ${USEARCH_CMAKE_PROJECT_TARGETS_FILE}
)
install(
TARGETS ${USEARCH_TARGET_NAME}
EXPORT ${USEARCH_TARGETS_EXPORT_NAME}
INCLUDES
DESTINATION ${USEARCH_INCLUDE_INSTALL_DIR}
)
install(
EXPORT ${USEARCH_TARGETS_EXPORT_NAME}
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${USEARCH_CONFIG_INSTALL_DIR}
)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION ${USEARCH_PKGCONFIG_INSTALL_DIR})
endif ()
# Find required packages
if (CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
set(CMAKE_THREAD_LIBS_INIT "-lpthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT 0)
set(CMAKE_USE_PTHREADS_INIT 1)
set(THREADS_PREFER_PTHREAD_FLAG ON)
endif ()
find_package(Threads REQUIRED)
if (USEARCH_USE_OPENMP)
find_package(OpenMP REQUIRED)
endif ()
if (USEARCH_USE_JEMALLOC)
include(jemalloc)
endif ()
# Set default build type to Release
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif ()
# Include directories
set(USEARCH_HEADER_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_SOURCE_DIR}/fp16/include"
"${CMAKE_CURRENT_SOURCE_DIR}/simsimd/include"
)
# Function to setup target
function (setup_target TARGET_NAME)
# Compiler-specific options List of all possible compiler IDs:
# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options(
${TARGET_NAME}
PRIVATE $<$<CONFIG:RELEASE>:-O3>
$<$<CONFIG:RELWITHDEBINFO>:-O3
-g>
$<$<CONFIG:DEBUG>:-O0
-g
-fsanitize=address
-fsanitize=leak
-fsanitize=alignment
-fsanitize=undefined
>
-ffast-math
-fPIC
-Wall
-Wextra
-Wno-conversion
-Wno-unknown-pragmas
-march=native
-fmax-errors=1
-pedantic
-fdiagnostics-color=always
)
target_link_options(
${TARGET_NAME}
PRIVATE
$<$<CONFIG:DEBUG>:-g
-fsanitize=address
-fsanitize=leak
-fsanitize=alignment
-fsanitize=undefined
>
-fPIC
)
if (USEARCH_USE_OPENMP)
target_link_libraries(${TARGET_NAME} PRIVATE OpenMP::OpenMP_CXX)
endif ()
target_link_libraries(${TARGET_NAME} PRIVATE Threads::Threads)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(
${TARGET_NAME}
PRIVATE $<$<CONFIG:RELEASE>:-O3>
$<$<CONFIG:RELWITHDEBINFO>:-O3
-g>
$<$<CONFIG:DEBUG>:-O0
-g
-fsanitize=address
-fsanitize=alignment
-fsanitize=undefined
>
-ffast-math
-fPIC
-pedantic
-Wfatal-errors
-fcolor-diagnostics
)
target_link_options(
${TARGET_NAME}
PRIVATE
$<$<CONFIG:DEBUG>:-g
-fsanitize=address
-fsanitize=alignment
-fsanitize=undefined
>
-fPIC
)
# Check if the compiler is AppleClang, and if not, add the leak sanitizer
if (CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
# It's likely AppleClang Adjust options as needed for AppleClang
else ()
# It's likely LLVM Clang
target_compile_options(${TARGET_NAME} PRIVATE $<$<CONFIG:DEBUG>:-fsanitize=leak>)
target_link_options(${TARGET_NAME} PRIVATE $<$<CONFIG:DEBUG>:-fsanitize=leak>)
endif ()
if (USEARCH_USE_OPENMP)
target_link_libraries(${TARGET_NAME} PRIVATE OpenMP::OpenMP_CXX)
endif ()
if (CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin")
target_link_libraries(
${TARGET_NAME} PRIVATE "-framework CoreFoundation" "-framework Security" Threads::Threads
)
else ()
target_link_libraries(${TARGET_NAME} PRIVATE Threads::Threads)
endif ()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "NVIDIA" OR CMAKE_CXX_COMPILER_ID STREQUAL "NVHPC")
target_compile_options(${TARGET_NAME} PRIVATE $<$<CONFIG:RELEASE>:-O3>)
target_compile_definitions(${TARGET_NAME} PRIVATE --expt-relaxed-constexpr --extended-lambda)
set_property(SOURCE bench.cpp PROPERTY LANGUAGE CUDA)
set_target_properties(${TARGET_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON)
set_target_properties(${TARGET_NAME} PROPERTIES CUDA_ARCHITECTURES "86")
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
target_compile_options(${TARGET_NAME} PRIVATE -w -ferror-limit=1)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_compile_options(${TARGET_NAME} PRIVATE /wd4068 /wd4996)
target_compile_options(
${TARGET_NAME} PRIVATE $<$<CONFIG:DEBUG>:/DEBUG> $<$<CONFIG:RELEASE>:/O3> $<$<CONFIG:RELWITHDEBINFO>:/O3
/DEBUG>
)
endif ()
target_include_directories(${TARGET_NAME} PRIVATE ${USEARCH_HEADER_INCLUDES})
set_target_properties(${TARGET_NAME} PROPERTIES CXX_STANDARD 17)
# On Windows, when using multi-configuration CMake builds, the paths will be nested, resulting in
# "build_debug\Debug" on Windows instead of "build_debug". That's why we override all the configs to use the same
# output directory.
set_target_properties(
${TARGET_NAME}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}"
)
set_target_properties(
${TARGET_NAME}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}"
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}"
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}"
)
set_target_properties(
${TARGET_NAME}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}"
)
set_target_properties(
${TARGET_NAME}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}"
ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}"
LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}"
)
# On Linux and MacOS our primary shared libraries for C are called `libusearch_c.so` and `libusearch_c.dylib`, while
# on Windows the "lib" prefix might be missing by default, so let's ensure it's always there
get_target_property(TARGET_TYPE ${TARGET_NAME} TYPE)
if (TARGET_TYPE STREQUAL "SHARED_LIBRARY" OR TARGET_TYPE STREQUAL "STATIC_LIBRARY")
set_target_properties(${TARGET_NAME} PROPERTIES PREFIX "lib")
endif ()
# Core compilation settings affecting "index.hpp"
target_compile_definitions(${TARGET_NAME} PRIVATE "USEARCH_USE_OPENMP=$<BOOL:${USEARCH_USE_OPENMP}>")
# Supplementary compilation settings affecting "index_plugins.hpp"
target_compile_definitions(${TARGET_NAME} PRIVATE "USEARCH_USE_FP16LIB=$<BOOL:${USEARCH_USE_FP16LIB}>")
target_compile_definitions(${TARGET_NAME} PRIVATE "USEARCH_USE_SIMSIMD=$<BOOL:${USEARCH_USE_SIMSIMD}>")
endfunction ()
# Must be called before "add_subdirectory()". See https://stackoverflow.com/questions/30250494/ctest-not-detecting-tests.
enable_testing()
if (${USEARCH_BUILD_TEST_CPP} OR ${USEARCH_BUILD_BENCH_CPP})
add_subdirectory(cpp)
endif ()
if (${USEARCH_BUILD_WOLFRAM})
add_subdirectory(wolfram)
endif ()
if (${USEARCH_BUILD_WASM})
add_subdirectory(wasm)
endif ()
if (${USEARCH_BUILD_LIB_C} OR ${USEARCH_BUILD_TEST_C})
add_subdirectory(c)
endif ()
if (${USEARCH_BUILD_SQLITE})
add_subdirectory(sqlite)
endif ()