@@ -9,19 +9,25 @@ include(FetchContent)
99
1010# These example/test targets catch exceptions, so exceptions should always be
1111# enabled building these files even if they are disabled in other targets.
12+ # We check that each target included in this list is a real target.
1213set (HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS
1314 example_mt_filter
1415 example_mt_replace_deleted
1516 example_mt_search
1617 multiThread_replace_test
1718 test_updates)
1819
19-
20+ # Adds an example or test target. The target name parameter is followed by
21+ # the list of source files. Automatically links with the hnswlib library.
22+ # Also decides whether to enable exceptions when building the target.
23+ # If HNSWLIB_ENABLE_EXCEPTIONS is ON, exceptions are always enabled.
24+ # If HNSWLIB_ENABLE_EXCEPTIONS is OFF, exceptions are only enabled for the
25+ # specific targets listed in HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS.
2026function (add_example_or_test TARGET_NAME ...)
2127 add_executable (${ARGV} )
22- target_link_libraries (" ${TARGET_NAME} " ${common_link_libraries} )
23- list (FIND HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS "${TARGET_NAME} " _index )
24- if (_index GREATER -1)
28+ target_link_libraries (${TARGET_NAME} hnswlib )
29+ list (FIND HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS "${TARGET_NAME} " found_at_index )
30+ if (found_at_index GREATER -1)
2531 if (NOT HNSWLIB_ENABLE_EXCEPTIONS)
2632 message ("Enabling exceptions for target ${TARGET_NAME} as a special case" )
2733 endif ()
@@ -34,9 +40,20 @@ function(add_example_or_test TARGET_NAME ...)
3440 else ()
3541 target_compile_options ("${TARGET_NAME} " PUBLIC -fno-exceptions)
3642 endif ()
43+ if (NOT ${TARGET_NAME} STREQUAL "main" )
44+ add_test (
45+ NAME ${TARGET_NAME}
46+ COMMAND ${TARGET_NAME}
47+ )
48+ endif ()
3749endfunction ()
3850
3951option (HNSWLIB_ENABLE_EXCEPTIONS "Whether to enable exceptions in hnswlib" ON )
52+ if (HNSWLIB_ENABLE_EXCEPTIONS)
53+ message ("Exceptions are enabled using HNSWLIB_ENABLE_EXCEPTIONS=ON (default)" )
54+ else ()
55+ message ("Exceptions are disabled using HNSWLIB_ENABLE_EXCEPTIONS=OFF" )
56+ endif ()
4057
4158set (CMAKE_CXX_STANDARD 11)
4259
@@ -62,10 +79,12 @@ install(EXPORT hnswlibTargets
6279# Examples and tests
6380if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME )
6481 option (HNSWLIB_EXAMPLES "Build examples and tests." ON )
82+ message ("Building examples and tests" )
6583else ()
6684 option (HNSWLIB_EXAMPLES "Build examples and tests." OFF )
6785endif ()
6886if (HNSWLIB_EXAMPLES)
87+ enable_testing ()
6988 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
7089 SET ( CMAKE_CXX_FLAGS "-Ofast -DHAVE_CXX0X -openmp -fpic -ftree-vectorize" )
7190 check_cxx_compiler_flag("-march=native" COMPILER_SUPPORT_NATIVE_FLAG)
@@ -85,9 +104,6 @@ if(HNSWLIB_EXAMPLES)
85104 SET ( CMAKE_CXX_FLAGS "/O2 -DHAVE_CXX0X /W1 /openmp /EHsc" )
86105 endif ()
87106
88- set (common_link_libraries
89- hnswlib)
90-
91107 set (EXAMPLE_NAMES
92108 example_epsilon_search
93109 example_filter
@@ -103,7 +119,7 @@ if(HNSWLIB_EXAMPLES)
103119 endforeach ()
104120
105121 set (TEST_NAMES
106- multivector_search_test
122+ multivector_search_test
107123 epsilon_search_test
108124 searchKnnCloserFirst_test
109125 searchKnnWithFilter_test
@@ -116,5 +132,15 @@ if(HNSWLIB_EXAMPLES)
116132 # This test deviates from the above pattern of naming test executables.
117133 add_example_or_test(test_updates tests/cpp/updates_test.cpp)
118134
135+ # For historical reasons, the "main" program links with sift_1b.cpp.
119136 add_example_or_test(main tests/cpp/main.cpp tests/cpp/sift_1b.cpp)
137+
138+ foreach (target_name IN LISTS HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS)
139+ if (NOT TARGET ${target_name} )
140+ message (FATAL_ERROR
141+ "Target '${target_name} ' included in "
142+ "HNSWLIB_TARGETS_REQUIRING_EXCEPTIONS does not exist. "
143+ "Please check if this is a typo." )
144+ endif ()
145+ endforeach ()
120146endif ()
0 commit comments