Bug 1867925 - Mark some storage-access-api tests as intermittent after wpt-sync....
[gecko.git] / third_party / highway / CMakeLists.txt
blob8685c04e7f8cd26e85f1072a019a33d39f71cbce
1 # Copyright 2019 Google LLC
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 #      http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 cmake_minimum_required(VERSION 3.10)
17 # Set PIE flags for POSITION_INDEPENDENT_CODE targets, added in 3.14.
18 if(POLICY CMP0083)
19   cmake_policy(SET CMP0083 NEW)
20 endif()
22 # Workaround for 3.19 raising error 'IMPORTED_LOCATION not set for imported
23 # target "GTest::gtest_main"'.
24 if(POLICY CMP0111)
25   cmake_policy(SET CMP0111 OLD)
26 endif()
28 project(hwy VERSION 1.0.4)  # Keep in sync with highway.h version
29 # `hwy` is lowercase to handle find_package() in Config mode:
30 set(namespace "${PROJECT_NAME}::")
32 # Directly define the ABI version from the cmake project() version values:
33 set(LIBRARY_VERSION "${hwy_VERSION}")
34 set(LIBRARY_SOVERSION ${hwy_VERSION_MAJOR})
36 set(CMAKE_CXX_EXTENSIONS OFF)
38 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
39 # Search for Atomics implementation:
40 find_package(Atomics REQUIRED)
42 # Enabled PIE binaries by default if supported.
43 include(CheckPIESupported OPTIONAL RESULT_VARIABLE CHECK_PIE_SUPPORTED)
44 if(CHECK_PIE_SUPPORTED)
45   check_pie_supported(LANGUAGES CXX)
46   if(CMAKE_CXX_LINK_PIE_SUPPORTED)
47     set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
48   endif()
49 endif()
51 include(GNUInstallDirs)
53 if (NOT CMAKE_BUILD_TYPE)
54   set(CMAKE_BUILD_TYPE RelWithDebInfo)
55 endif()
57 # The following is only required with GCC < 6.1.0 or CLANG < 16.0
58 set(HWY_CMAKE_ARM7 OFF CACHE BOOL "Set copts for Armv7 with NEON (requires vfpv4)?")
60 # Unconditionally adding -Werror risks breaking the build when new warnings
61 # arise due to compiler/platform changes. Enable this in CI/tests.
62 set(HWY_WARNINGS_ARE_ERRORS OFF CACHE BOOL "Add -Werror flag?")
64 set(HWY_ENABLE_CONTRIB ON CACHE BOOL "Include contrib/")
65 set(HWY_ENABLE_EXAMPLES ON CACHE BOOL "Build examples")
66 set(HWY_ENABLE_INSTALL ON CACHE BOOL "Install library")
67 set(HWY_ENABLE_TESTS ON CACHE BOOL "Enable HWY tests")
69 include(CheckCXXSourceCompiles)
70 check_cxx_source_compiles(
71    "int main() {
72       #if !defined(__EMSCRIPTEN__)
73       static_assert(false, \"__EMSCRIPTEN__ is not defined\");
74       #endif
75       return 0;
76     }"
77   HWY_EMSCRIPTEN
80 check_cxx_source_compiles(
81    "int main() {
82       #if !defined(__riscv)
83       static_assert(false, \"__riscv is not defined\");
84       #endif
85       return 0;
86     }"
87   HWY_RISCV
90 if (HWY_ENABLE_CONTRIB)
91 # Glob all the traits so we don't need to modify this file when adding
92 # additional special cases.
93 file(GLOB HWY_CONTRIB_SOURCES "hwy/contrib/sort/vqsort_*.cc")
94 list(APPEND HWY_CONTRIB_SOURCES
95     hwy/contrib/dot/dot-inl.h
96     hwy/contrib/image/image.cc
97     hwy/contrib/image/image.h
98     hwy/contrib/math/math-inl.h
99     hwy/contrib/sort/shared-inl.h
100     hwy/contrib/sort/sorting_networks-inl.h
101     hwy/contrib/sort/traits-inl.h
102     hwy/contrib/sort/traits128-inl.h
103     hwy/contrib/sort/vqsort-inl.h
104     hwy/contrib/sort/vqsort.cc
105     hwy/contrib/sort/vqsort.h
106     hwy/contrib/algo/copy-inl.h
107     hwy/contrib/algo/find-inl.h
108     hwy/contrib/algo/transform-inl.h
110 endif()  # HWY_ENABLE_CONTRIB
112 set(HWY_SOURCES
113     hwy/aligned_allocator.cc
114     hwy/aligned_allocator.h
115     hwy/base.h
116     hwy/cache_control.h
117     hwy/detect_compiler_arch.h  # private
118     hwy/detect_targets.h  # private
119     hwy/foreach_target.h
120     hwy/highway.h
121     hwy/highway_export.h
122     hwy/nanobenchmark.cc
123     hwy/nanobenchmark.h
124     hwy/ops/arm_neon-inl.h
125     hwy/ops/arm_sve-inl.h
126     hwy/ops/emu128-inl.h
127     hwy/ops/generic_ops-inl.h
128     hwy/ops/ppc_vsx-inl.h
129     hwy/ops/rvv-inl.h
130     hwy/ops/scalar-inl.h
131     hwy/ops/set_macros-inl.h
132     hwy/ops/shared-inl.h
133     hwy/ops/wasm_128-inl.h
134     hwy/ops/tuple-inl.h
135     hwy/ops/x86_128-inl.h
136     hwy/ops/x86_256-inl.h
137     hwy/ops/x86_512-inl.h
138     hwy/per_target.cc
139     hwy/per_target.h
140     hwy/print-inl.h
141     hwy/print.cc
142     hwy/print.h
143     hwy/targets.cc
144     hwy/targets.h
147 set(HWY_TEST_SOURCES
148     hwy/tests/hwy_gtest.h
149     hwy/tests/test_util-inl.h
150     hwy/tests/test_util.cc
151     hwy/tests/test_util.h
154 if (MSVC)
155   set(HWY_FLAGS
156     # fix build error C1128 in blockwise*_test & arithmetic_test
157     /bigobj
158   )
159 else()
160   set(HWY_FLAGS
161     # Avoid changing binaries based on the current time and date.
162     -Wno-builtin-macro-redefined
163     -D__DATE__="redacted"
164     -D__TIMESTAMP__="redacted"
165     -D__TIME__="redacted"
167     # Optimizations
168     -fmerge-all-constants
170     # Warnings
171     -Wall
172     -Wextra
173     # These are not included in Wall nor Wextra:
174     -Wconversion
175     -Wsign-conversion
176     -Wvla
177     -Wnon-virtual-dtor
178   )
180   if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
181     list(APPEND HWY_FLAGS
182       -Wfloat-overflow-conversion
183       -Wfloat-zero-conversion
184       -Wfor-loop-analysis
185       -Wgnu-redeclared-enum
186       -Winfinite-recursion
187       -Wself-assign
188       -Wstring-conversion
189       -Wtautological-overlap-compare
190       -Wthread-safety-analysis
191       -Wundefined-func-template
193       -fno-cxx-exceptions
194       -fno-slp-vectorize
195       -fno-vectorize
197       # Use color in messages
198       -fdiagnostics-show-option -fcolor-diagnostics
199     )
200     if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6.0)
201       list(APPEND HWY_FLAGS -Wc++2a-extensions)
202     endif()
203   endif()
205   if (WIN32)
206     if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
207       list(APPEND HWY_FLAGS
208         -Wno-global-constructors
209         -Wno-language-extension-token
210         -Wno-used-but-marked-unused
211         -Wno-shadow-field-in-constructor
212         -Wno-unused-member-function
213         -Wno-unused-template
214         -Wno-c++98-compat-pedantic
215         -Wno-used-but-marked-unused
216         -Wno-zero-as-null-pointer-constant
217       )
218     endif()
220     list(APPEND HWY_FLAGS
221       -Wno-cast-align
222       -Wno-double-promotion
223       -Wno-float-equal
224       -Wno-format-nonliteral
225       -Wno-shadow
226       -Wno-sign-conversion
227     )
228   else()
229     list(APPEND HWY_FLAGS
230       -fmath-errno
231       -fno-exceptions
232     )
233   endif()  # WIN32
235   if (HWY_CMAKE_ARM7)
236     list(APPEND HWY_FLAGS
237       -march=armv7-a
238       -mfpu=neon-vfpv4
239       -mfloat-abi=hard  # must match the toolchain specified as CXX=
240       -mfp16-format=ieee  # required for vcvt_f32_f16
241     )
242   endif()  # HWY_CMAKE_ARM7
244   if(HWY_RISCV)
245     # gcc(13) and recent clang both support V, but not yet runtime dispatch, so
246     # we add the gcv compiler flag, which then requires the CPU (now when using
247     # either compiler) to support V.
248     list(APPEND HWY_FLAGS -march=rv64gcv1p0)
249     if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
250       list(APPEND HWY_FLAGS -menable-experimental-extensions)
251     endif()
252   endif()
254   if (HWY_WARNINGS_ARE_ERRORS)
255     list(APPEND HWY_FLAGS -Werror)
256   endif()
258   # Prevent "wasm-ld: error: --shared-memory is disallowed by targets.cc.o
259   # because it was not compiled with 'atomics' or 'bulk-memory' features."
260   if (HWY_EMSCRIPTEN)
261     list(APPEND HWY_FLAGS -matomics)
262   endif()
264 endif()  # !MSVC
266 include(CheckIncludeFile)
267 check_include_file(sys/auxv.h  HAVE_SYS_AUXV_H)
268 check_include_file(asm/hwcap.h HAVE_ASM_HWCAP_H)
269 if (NOT HAVE_ASM_HWCAP_H)
270   list(APPEND HWY_FLAGS -DTOOLCHAIN_MISS_ASM_HWCAP_H)
271 endif()
273 # By default prefer STATIC build (legacy behavior)
274 option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
275 option(HWY_FORCE_STATIC_LIBS "Ignore BUILD_SHARED_LIBS" OFF)
276 # only expose shared/static options to advanced users:
277 mark_as_advanced(BUILD_SHARED_LIBS)
278 mark_as_advanced(HWY_FORCE_STATIC_LIBS)
279 # Define visibility settings globally:
280 set(CMAKE_CXX_VISIBILITY_PRESET hidden)
281 set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
283 # Copy-cat "add_library" logic + add override.
284 set(HWY_LIBRARY_TYPE "SHARED")
285 if (NOT BUILD_SHARED_LIBS OR HWY_FORCE_STATIC_LIBS)
286   set(HWY_LIBRARY_TYPE "STATIC")
287 endif()
289 # This preprocessor define will drive the build, also used in the *.pc files:
290 if("${HWY_LIBRARY_TYPE}" STREQUAL "SHARED")
291   set(DLLEXPORT_TO_DEFINE "HWY_SHARED_DEFINE")
292 else()
293   set(DLLEXPORT_TO_DEFINE "HWY_STATIC_DEFINE")
294 endif()
296 add_library(hwy ${HWY_LIBRARY_TYPE} ${HWY_SOURCES})
297 if(NOT HAVE_SYS_AUXV_H)
298   target_compile_definitions(hwy PUBLIC TOOLCHAIN_MISS_SYS_AUXV_H)
299 endif()
300 target_compile_definitions(hwy PUBLIC "${DLLEXPORT_TO_DEFINE}")
301 target_compile_options(hwy PRIVATE ${HWY_FLAGS})
302 set_property(TARGET hwy PROPERTY POSITION_INDEPENDENT_CODE ON)
303 set_target_properties(hwy PROPERTIES VERSION ${LIBRARY_VERSION} SOVERSION ${LIBRARY_SOVERSION})
304 target_include_directories(hwy PUBLIC
305     $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
306     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
307 target_compile_features(hwy PUBLIC cxx_std_11)
308 set_target_properties(hwy PROPERTIES
309   LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/hwy/hwy.version)
310 # For GCC __atomic_store_8, see #887
311 target_link_libraries(hwy PRIVATE ${ATOMICS_LIBRARIES})
312 # not supported by MSVC/Clang, safe to skip (we use DLLEXPORT annotations)
313 if(UNIX AND NOT APPLE)
314   set_property(TARGET hwy APPEND_STRING PROPERTY
315     LINK_FLAGS " -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/hwy/hwy.version")
316 endif()
318 if (HWY_ENABLE_CONTRIB)
319 add_library(hwy_contrib ${HWY_LIBRARY_TYPE} ${HWY_CONTRIB_SOURCES})
320 target_link_libraries(hwy_contrib hwy)
321 target_compile_options(hwy_contrib PRIVATE ${HWY_FLAGS})
322 set_property(TARGET hwy_contrib PROPERTY POSITION_INDEPENDENT_CODE ON)
323 set_target_properties(hwy_contrib PROPERTIES VERSION ${LIBRARY_VERSION} SOVERSION ${LIBRARY_SOVERSION})
324 target_include_directories(hwy_contrib PUBLIC
325     $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
326     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
327 target_compile_features(hwy_contrib PUBLIC cxx_std_11)
328 set_target_properties(hwy_contrib PROPERTIES
329   LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/hwy/hwy.version)
330 # not supported by MSVC/Clang, safe to skip (we use DLLEXPORT annotations)
331 if(UNIX AND NOT APPLE)
332   set_property(TARGET hwy_contrib APPEND_STRING PROPERTY
333     LINK_FLAGS " -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/hwy/hwy.version")
334 endif()
335 endif()  # HWY_ENABLE_CONTRIB
337 add_library(hwy_test ${HWY_LIBRARY_TYPE} ${HWY_TEST_SOURCES})
338 target_link_libraries(hwy_test hwy)
339 target_compile_options(hwy_test PRIVATE ${HWY_FLAGS})
340 set_property(TARGET hwy_test PROPERTY POSITION_INDEPENDENT_CODE ON)
341 set_target_properties(hwy_test PROPERTIES VERSION ${LIBRARY_VERSION} SOVERSION ${LIBRARY_SOVERSION})
342 target_include_directories(hwy_test PUBLIC
343     $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
344     $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
345 target_compile_features(hwy_test PUBLIC cxx_std_11)
346 set_target_properties(hwy_test PROPERTIES
347   LINK_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/hwy/hwy.version)
348 # not supported by MSVC/Clang, safe to skip (we use DLLEXPORT annotations)
349 if(UNIX AND NOT APPLE)
350   set_property(TARGET hwy_test APPEND_STRING PROPERTY
351     LINK_FLAGS " -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/hwy/hwy.version")
352 endif()
354 # -------------------------------------------------------- hwy_list_targets
355 # Generate a tool to print the compiled-in targets as defined by the current
356 # flags. This tool will print to stderr at build time, after building hwy.
357 add_executable(hwy_list_targets hwy/tests/list_targets.cc)
358 target_compile_options(hwy_list_targets PRIVATE ${HWY_FLAGS})
359 target_link_libraries(hwy_list_targets hwy)
360 target_include_directories(hwy_list_targets PRIVATE
361   $<TARGET_PROPERTY:hwy,INCLUDE_DIRECTORIES>)
362 # TARGET_FILE always returns the path to executable
363 # Naked target also not always could be run (due to the lack of '.\' prefix)
364 # Thus effective command to run should contain the full path
365 # and emulator prefix (if any).
366 if (NOT CMAKE_CROSSCOMPILING OR CMAKE_CROSSCOMPILING_EMULATOR)
367 add_custom_command(TARGET hwy_list_targets POST_BUILD
368     COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:hwy_list_targets> || (exit 0))
369 endif()
371 # --------------------------------------------------------
372 # Allow skipping the following sections for projects that do not need them:
373 # tests, examples, benchmarks and installation.
375 # -------------------------------------------------------- install library
376 if (HWY_ENABLE_INSTALL)
378 install(TARGETS hwy EXPORT hwy_targets
379   LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
380   ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
381   RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
382 # Install all the headers keeping the relative path to the current directory
383 # when installing them.
384 foreach (source ${HWY_SOURCES})
385   if ("${source}" MATCHES "\.h$")
386     get_filename_component(dirname "${source}" DIRECTORY)
387     install(FILES "${source}"
388         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${dirname}")
389   endif()
390 endforeach()
392 if (HWY_ENABLE_CONTRIB)
393 install(TARGETS hwy_contrib EXPORT hwy_targets
394   LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
395   ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
396   RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
397 # Install all the headers keeping the relative path to the current directory
398 # when installing them.
399 foreach (source ${HWY_CONTRIB_SOURCES})
400   if ("${source}" MATCHES "\.h$")
401     get_filename_component(dirname "${source}" DIRECTORY)
402     install(FILES "${source}"
403         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${dirname}")
404   endif()
405 endforeach()
406 endif()  # HWY_ENABLE_CONTRIB
408 install(TARGETS hwy_test EXPORT hwy_targets
409   LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
410   ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
411   RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
412 # Install all the headers keeping the relative path to the current directory
413 # when installing them.
414 foreach (source ${HWY_TEST_SOURCES})
415   if ("${source}" MATCHES "\.h$")
416     get_filename_component(dirname "${source}" DIRECTORY)
417     install(FILES "${source}"
418         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${dirname}")
419   endif()
420 endforeach()
422 # Add a pkg-config file for libhwy and the contrib/test libraries.
423 set(HWY_LIBRARY_VERSION "${CMAKE_PROJECT_VERSION}")
424 set(HWY_PC_FILES libhwy.pc libhwy-test.pc)
425 if (HWY_ENABLE_CONTRIB)
426 list(APPEND HWY_PC_FILES libhwy-contrib.pc)
427 endif()  # HWY_ENABLE_CONTRIB
428 foreach (pc ${HWY_PC_FILES})
429   configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${pc}.in" "${pc}" @ONLY)
430   install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${pc}"
431       DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
432 endforeach()
434 endif()  # HWY_ENABLE_INSTALL
435 # -------------------------------------------------------- Examples
436 if (HWY_ENABLE_EXAMPLES)
438 # Avoids mismatch between GTest's static CRT and our dynamic.
439 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
441 # Programming exercise with integrated benchmark
442 add_executable(hwy_benchmark hwy/examples/benchmark.cc)
443 target_sources(hwy_benchmark PRIVATE
444     hwy/nanobenchmark.h)
445 # Try adding one of -DHWY_COMPILE_ONLY_SCALAR, -DHWY_COMPILE_ONLY_EMU128 or
446 # -DHWY_COMPILE_ONLY_STATIC to observe the difference in targets printed.
447 target_compile_options(hwy_benchmark PRIVATE ${HWY_FLAGS})
448 target_link_libraries(hwy_benchmark PRIVATE hwy)
449 target_link_libraries(hwy_benchmark PRIVATE ${ATOMICS_LIBRARIES})
450 set_target_properties(hwy_benchmark
451     PROPERTIES RUNTIME_OUTPUT_DIRECTORY "examples/")
453 endif()  # HWY_ENABLE_EXAMPLES
454 # -------------------------------------------------------- Tests
456 include(CTest)
458 if(BUILD_TESTING AND HWY_ENABLE_TESTS)
459 enable_testing()
460 include(GoogleTest)
462 set(HWY_SYSTEM_GTEST OFF CACHE BOOL "Use pre-installed googletest?")
463 if(HWY_SYSTEM_GTEST)
464 find_package(GTest REQUIRED)
465 else()
466 # Download and unpack googletest at configure time
467 configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
468 execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
469   RESULT_VARIABLE result
470   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
471 if(result)
472   message(FATAL_ERROR "CMake step for googletest failed: ${result}")
473 endif()
474 execute_process(COMMAND ${CMAKE_COMMAND} --build .
475   RESULT_VARIABLE result
476   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
477 if(result)
478   message(FATAL_ERROR "Build step for googletest failed: ${result}")
479 endif()
481 # Prevent overriding the parent project's compiler/linker
482 # settings on Windows
483 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
485 # Add googletest directly to our build. This defines
486 # the gtest and gtest_main targets.
487 add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
488                  ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
489                  EXCLUDE_FROM_ALL)
490 endif()  # HWY_SYSTEM_GTEST
492 set(HWY_TEST_FILES
493   hwy/contrib/algo/copy_test.cc
494   hwy/contrib/algo/find_test.cc
495   hwy/contrib/algo/transform_test.cc
496   hwy/aligned_allocator_test.cc
497   hwy/base_test.cc
498   hwy/highway_test.cc
499   hwy/nanobenchmark_test.cc
500   hwy/targets_test.cc
501   hwy/examples/skeleton_test.cc
502   hwy/tests/arithmetic_test.cc
503   hwy/tests/blockwise_shift_test.cc
504   hwy/tests/blockwise_test.cc
505   hwy/tests/combine_test.cc
506   hwy/tests/compare_test.cc
507   hwy/tests/compress_test.cc
508   hwy/tests/convert_test.cc
509   hwy/tests/count_test.cc
510   hwy/tests/crypto_test.cc
511   hwy/tests/demote_test.cc
512   hwy/tests/expand_test.cc
513   hwy/tests/float_test.cc
514   hwy/tests/if_test.cc
515   hwy/tests/interleaved_test.cc
516   hwy/tests/logical_test.cc
517   hwy/tests/mask_mem_test.cc
518   hwy/tests/mask_test.cc
519   hwy/tests/memory_test.cc
520   hwy/tests/mul_test.cc
521   hwy/tests/reduction_test.cc
522   hwy/tests/reverse_test.cc
523   hwy/tests/shift_test.cc
524   hwy/tests/swizzle_test.cc
525   hwy/tests/test_util_test.cc
526   hwy/tests/tuple_test.cc
529 set(HWY_TEST_LIBS hwy hwy_test)
531 if (HWY_ENABLE_CONTRIB)
532 list(APPEND HWY_TEST_LIBS hwy_contrib)
534 list(APPEND HWY_TEST_FILES
535   hwy/contrib/dot/dot_test.cc
536   hwy/contrib/image/image_test.cc
537   # Disabled due to SIGILL in clang7 debug build during gtest discovery phase,
538   # not reproducible locally. Still tested via bazel build.
539   # hwy/contrib/math/math_test.cc
540   hwy/contrib/sort/sort_test.cc
541   hwy/contrib/sort/bench_sort.cc
543 endif()  # HWY_ENABLE_CONTRIB
545 if(HWY_SYSTEM_GTEST)
546   if (CMAKE_VERSION VERSION_LESS 3.20)
547     set(HWY_GTEST_LIBS GTest::GTest GTest::Main)
548   else()
549     set(HWY_GTEST_LIBS GTest::gtest GTest::gtest_main)
550   endif()
551 else()
552   set(HWY_GTEST_LIBS gtest gtest_main)
553 endif()
555 file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests)
556 foreach (TESTFILE IN LISTS HWY_TEST_FILES)
557   # The TESTNAME is the name without the extension or directory.
558   get_filename_component(TESTNAME ${TESTFILE} NAME_WE)
559   add_executable(${TESTNAME} ${TESTFILE})
560   target_compile_options(${TESTNAME} PRIVATE ${HWY_FLAGS})
561   # Test all targets, not just the best/baseline. This changes the default
562   # policy to all-attainable; note that setting -DHWY_COMPILE_* directly can
563   # cause compile errors because only one may be set, and other CMakeLists.txt
564   # that include us may set them.
565   target_compile_options(${TESTNAME} PRIVATE -DHWY_IS_TEST=1)
567   target_link_libraries(${TESTNAME} PRIVATE ${HWY_TEST_LIBS} ${HWY_GTEST_LIBS})
568   # For GCC __atomic_store_8, see #887
569   target_link_libraries(${TESTNAME} PRIVATE ${ATOMICS_LIBRARIES})
570   # Output test targets in the test directory.
571   set_target_properties(${TESTNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "tests")
573   if (HWY_EMSCRIPTEN)
574     set_target_properties(${TESTNAME} PROPERTIES LINK_FLAGS "-s SINGLE_FILE=1")
575   endif()
577   if(${CMAKE_VERSION} VERSION_LESS "3.10.3")
578     gtest_discover_tests(${TESTNAME} TIMEOUT 60)
579   else ()
580     gtest_discover_tests(${TESTNAME} DISCOVERY_TIMEOUT 60)
581   endif ()
582 endforeach ()
584 # The skeleton test uses the skeleton library code.
585 target_sources(skeleton_test PRIVATE hwy/examples/skeleton.cc)
587 endif()  # BUILD_TESTING
589 if (HWY_ENABLE_INSTALL)
590   # write hwy-config file to handle `Config` mode
591   include(CMakePackageConfigHelpers)
592   write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/hwy-config-version.cmake" COMPATIBILITY SameMajorVersion)
593   install(FILES "${CMAKE_CURRENT_BINARY_DIR}/hwy-config-version.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/hwy")
594   install(EXPORT hwy_targets NAMESPACE "${namespace}" FILE hwy-config.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/hwy")
595 endif()