@@ -6,6 +6,56 @@ project(hnswlib
6
6
include (GNUInstallDirs )
7
7
include (CheckCXXCompilerFlag )
8
8
9
+ # These example/test targets catch exceptions, so exceptions should always be
10
+ # enabled building these files even if they are disabled in other targets.
11
+ # We check that each target included in this list is a real target.
12
+ set (HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS
13
+ example_mt_filter
14
+ example_mt_replace_deleted
15
+ example_mt_search
16
+ multiThread_replace_test
17
+ test_updates )
18
+
19
+ # Adds an example or test target. The target name parameter is followed by
20
+ # the list of source files. Automatically links with the hnswlib library.
21
+ # Also decides whether to enable exceptions when building the target.
22
+ # If HNSWLIB_ENABLE_EXCEPTIONS is ON, exceptions are always enabled.
23
+ # If HNSWLIB_ENABLE_EXCEPTIONS is OFF, exceptions are only enabled for the
24
+ # specific targets listed in HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS.
25
+ function (add_example_or_test TARGET_NAME ... )
26
+ add_executable (${ARGV} )
27
+ target_link_libraries (${TARGET_NAME} hnswlib )
28
+ list (FIND HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS "${TARGET_NAME} " found_at_index )
29
+ if (found_at_index GREATER -1 )
30
+ if (NOT HNSWLIB_ENABLE_EXCEPTIONS )
31
+ message ("Enabling exceptions for target ${TARGET_NAME} as a special case" )
32
+ endif ()
33
+ set (should_enable_exceptions ON )
34
+ else ()
35
+ set (should_enable_exceptions "${HNSWLIB_ENABLE_EXCEPTIONS} " )
36
+ endif ()
37
+ if (should_enable_exceptions )
38
+ target_compile_options ("${TARGET_NAME} " PUBLIC -fexceptions )
39
+ else ()
40
+ target_compile_options ("${TARGET_NAME} " PUBLIC -fno-exceptions )
41
+ endif ()
42
+ if (NOT ${TARGET_NAME} MATCHES "^(main|test_updates)$" )
43
+ add_test (
44
+ NAME ${TARGET_NAME}
45
+ COMMAND ${TARGET_NAME}
46
+ )
47
+ endif ()
48
+ endfunction ()
49
+
50
+ option (HNSWLIB_ENABLE_EXCEPTIONS "Whether to enable exceptions in hnswlib" ON )
51
+ if (HNSWLIB_ENABLE_EXCEPTIONS )
52
+ message ("Exceptions are enabled using HNSWLIB_ENABLE_EXCEPTIONS=ON (default)" )
53
+ else ()
54
+ message ("Exceptions are disabled using HNSWLIB_ENABLE_EXCEPTIONS=OFF" )
55
+ endif ()
56
+
57
+ set (CMAKE_CXX_STANDARD 11 )
58
+
9
59
add_library (hnswlib INTERFACE )
10
60
add_library (hnswlib::hnswlib ALIAS hnswlib )
11
61
@@ -28,14 +78,14 @@ install(EXPORT hnswlibTargets
28
78
# Examples and tests
29
79
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME )
30
80
option (HNSWLIB_EXAMPLES "Build examples and tests." ON )
81
+ message ("Building examples and tests" )
31
82
else ()
32
83
option (HNSWLIB_EXAMPLES "Build examples and tests." OFF )
33
84
endif ()
34
85
if (HNSWLIB_EXAMPLES )
35
- set (CMAKE_CXX_STANDARD 11 )
36
-
86
+ enable_testing ()
37
87
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
38
- SET ( CMAKE_CXX_FLAGS "-Ofast -std=c++11 - DHAVE_CXX0X -openmp -fpic -ftree-vectorize" )
88
+ SET ( CMAKE_CXX_FLAGS "-Ofast -DHAVE_CXX0X -openmp -fpic -ftree-vectorize" )
39
89
check_cxx_compiler_flag ("-march=native" COMPILER_SUPPORT_NATIVE_FLAG )
40
90
if (COMPILER_SUPPORT_NATIVE_FLAG )
41
91
SET ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native" )
@@ -48,58 +98,48 @@ if(HNSWLIB_EXAMPLES)
48
98
endif ()
49
99
endif ()
50
100
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
51
- SET ( CMAKE_CXX_FLAGS "-Ofast -lrt -std=c++11 - DHAVE_CXX0X -march=native -fpic -w -fopenmp -ftree-vectorize -ftree-vectorizer-verbose=0" )
101
+ SET ( CMAKE_CXX_FLAGS "-Ofast -lrt -DHAVE_CXX0X -march=native -fpic -w -fopenmp -ftree-vectorize -ftree-vectorizer-verbose=0" )
52
102
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" )
53
103
SET ( CMAKE_CXX_FLAGS "/O2 -DHAVE_CXX0X /W1 /openmp /EHsc" )
54
104
endif ()
55
105
56
- # examples
57
- add_executable (example_search examples/cpp/example_search.cpp )
58
- target_link_libraries (example_search hnswlib )
59
-
60
- add_executable (example_epsilon_search examples/cpp/example_epsilon_search.cpp )
61
- target_link_libraries (example_epsilon_search hnswlib )
62
-
63
- add_executable (example_multivector_search examples/cpp/example_multivector_search.cpp )
64
- target_link_libraries (example_multivector_search hnswlib )
65
-
66
- add_executable (example_filter examples/cpp/example_filter.cpp )
67
- target_link_libraries (example_filter hnswlib )
68
-
69
- add_executable (example_replace_deleted examples/cpp/example_replace_deleted.cpp )
70
- target_link_libraries (example_replace_deleted hnswlib )
71
-
72
- add_executable (example_mt_search examples/cpp/example_mt_search.cpp )
73
- target_link_libraries (example_mt_search hnswlib )
74
-
75
- add_executable (example_mt_filter examples/cpp/example_mt_filter.cpp )
76
- target_link_libraries (example_mt_filter hnswlib )
77
-
78
- add_executable (example_mt_replace_deleted examples/cpp/example_mt_replace_deleted.cpp )
79
- target_link_libraries (example_mt_replace_deleted hnswlib )
80
-
81
- # tests
82
- add_executable (multivector_search_test tests/cpp/multivector_search_test.cpp )
83
- target_link_libraries (multivector_search_test hnswlib )
84
-
85
- add_executable (epsilon_search_test tests/cpp/epsilon_search_test.cpp )
86
- target_link_libraries (epsilon_search_test hnswlib )
87
-
88
- add_executable (test_updates tests/cpp/updates_test.cpp )
89
- target_link_libraries (test_updates hnswlib )
90
-
91
- add_executable (searchKnnCloserFirst_test tests/cpp/searchKnnCloserFirst_test.cpp )
92
- target_link_libraries (searchKnnCloserFirst_test hnswlib )
93
-
94
- add_executable (searchKnnWithFilter_test tests/cpp/searchKnnWithFilter_test.cpp )
95
- target_link_libraries (searchKnnWithFilter_test hnswlib )
96
-
97
- add_executable (multiThreadLoad_test tests/cpp/multiThreadLoad_test.cpp )
98
- target_link_libraries (multiThreadLoad_test hnswlib )
99
-
100
- add_executable (multiThread_replace_test tests/cpp/multiThread_replace_test.cpp )
101
- target_link_libraries (multiThread_replace_test hnswlib )
102
-
103
- add_executable (main tests/cpp/main.cpp tests/cpp/sift_1b.cpp )
104
- target_link_libraries (main hnswlib )
106
+ set (EXAMPLE_NAMES
107
+ example_epsilon_search
108
+ example_filter
109
+ example_mt_filter
110
+ example_mt_replace_deleted
111
+ example_mt_search
112
+ example_multivector_search
113
+ example_replace_deleted
114
+ example_search )
115
+
116
+ foreach (example_name IN LISTS EXAMPLE_NAMES )
117
+ add_example_or_test ("${example_name} " "examples/cpp/${example_name} .cpp" )
118
+ endforeach ()
119
+
120
+ set (TEST_NAMES
121
+ multivector_search_test
122
+ epsilon_search_test
123
+ searchKnnCloserFirst_test
124
+ searchKnnWithFilter_test
125
+ multiThreadLoad_test
126
+ multiThread_replace_test )
127
+ foreach (test_name IN LISTS TEST_NAMES )
128
+ add_example_or_test ("${test_name} " "tests/cpp/${test_name} .cpp" )
129
+ endforeach ()
130
+
131
+ # This test deviates from the above pattern of naming test executables.
132
+ add_example_or_test (test_updates tests/cpp/updates_test.cpp )
133
+
134
+ # For historical reasons, the "main" program links with sift_1b.cpp.
135
+ add_example_or_test (main tests/cpp/main.cpp tests/cpp/sift_1b.cpp )
136
+
137
+ foreach (target_name IN LISTS HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS )
138
+ if (NOT TARGET ${target_name} )
139
+ message (FATAL_ERROR
140
+ "Target '${target_name} ' included in "
141
+ "HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS does not exist. "
142
+ "Please check if this is a typo." )
143
+ endif ()
144
+ endforeach ()
105
145
endif ()
0 commit comments