Bug 1728059 [wpt PR 30223] - Add Subresource Web Bundle tests for non utf-8 encoding...
[gecko.git] / third_party / highway / CMakeLists.txt
blobc6ec4012b1dd771efff79138abc2b20dd477da0e
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 project(hwy VERSION 0.12.1)  # Keep in sync with highway.h version
24 set(CMAKE_CXX_STANDARD 11)
25 set(CMAKE_CXX_EXTENSIONS OFF)
26 set(CMAKE_CXX_STANDARD_REQUIRED YES)
28 # Enabled PIE binaries by default if supported.
29 include(CheckPIESupported OPTIONAL RESULT_VARIABLE CHECK_PIE_SUPPORTED)
30 if(CHECK_PIE_SUPPORTED)
31   check_pie_supported(LANGUAGES CXX)
32   if(CMAKE_CXX_LINK_PIE_SUPPORTED)
33     set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
34   endif()
35 endif()
37 include(GNUInstallDirs)
39 if (NOT CMAKE_BUILD_TYPE)
40   set(CMAKE_BUILD_TYPE RelWithDebInfo)
41 endif()
43 set(HWY_CMAKE_ARM7 OFF CACHE BOOL "Set copts for ARMv7 with NEON?")
45 include(CheckCXXSourceCompiles)
46 check_cxx_source_compiles(
47    "int main() {
48       #if !defined(__EMSCRIPTEN__)
49       static_assert(false, \"__EMSCRIPTEN__ is not defined\");
50       #endif
51       return 0;
52     }"
53   HWY_EMSCRIPTEN
56 set(HWY_CONTRIB_SOURCES
57     hwy/contrib/image/image.cc
58     hwy/contrib/image/image.h
59     hwy/contrib/math/math-inl.h
62 set(HWY_SOURCES
63     hwy/aligned_allocator.cc
64     hwy/aligned_allocator.h
65     hwy/base.h
66     hwy/cache_control.h
67     hwy/foreach_target.h
68     hwy/highway.h
69     hwy/nanobenchmark.cc
70     hwy/nanobenchmark.h
71     hwy/ops/arm_neon-inl.h
72     hwy/ops/arm_sve-inl.h
73     hwy/ops/scalar-inl.h
74     hwy/ops/set_macros-inl.h
75     hwy/ops/shared-inl.h
76     hwy/ops/wasm_128-inl.h
77     hwy/ops/x86_128-inl.h
78     hwy/ops/x86_256-inl.h
79     hwy/ops/x86_512-inl.h
80     hwy/targets.cc
81     hwy/targets.h
82     hwy/tests/test_util-inl.h
85 if (MSVC)
86   # TODO(janwas): add flags
87 else()
88   set(HWY_FLAGS
89     # Avoid changing binaries based on the current time and date.
90     -Wno-builtin-macro-redefined
91     -D__DATE__="redacted"
92     -D__TIMESTAMP__="redacted"
93     -D__TIME__="redacted"
95     # Optimizations
96     -fmerge-all-constants
98     # Warnings
99     -Wall
100     -Wextra
101     -Wformat-security
102     -Wno-unused-function
103     -Wnon-virtual-dtor
104     -Woverloaded-virtual
105     -Wvla
106   )
108   if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
109     list(APPEND HWY_FLAGS
110       -Wc++2a-extensions
111       -Wfloat-overflow-conversion
112       -Wfloat-zero-conversion
113       -Wfor-loop-analysis
114       -Wgnu-redeclared-enum
115       -Winfinite-recursion
116       -Wself-assign
117       -Wstring-conversion
118       -Wtautological-overlap-compare
119       -Wthread-safety-analysis
120       -Wundefined-func-template
122       -fno-cxx-exceptions
123       -fno-slp-vectorize
124       -fno-vectorize
126       # Use color in messages
127       -fdiagnostics-show-option -fcolor-diagnostics
128     )
129   endif()
131   if (WIN32)
132     list(APPEND HWY_FLAGS
133       -Wno-c++98-compat-pedantic
134       -Wno-cast-align
135       -Wno-double-promotion
136       -Wno-float-equal
137       -Wno-format-nonliteral
138       -Wno-global-constructors
139       -Wno-language-extension-token
140       -Wno-missing-prototypes
141       -Wno-shadow
142       -Wno-shadow-field-in-constructor
143       -Wno-sign-conversion
144       -Wno-unused-member-function
145       -Wno-unused-template
146       -Wno-used-but-marked-unused
147       -Wno-zero-as-null-pointer-constant
148     )
149   else()
150     list(APPEND HWY_FLAGS
151       -fmath-errno
152       -fno-exceptions
153     )
154   endif()
156   if (HWY_CMAKE_ARM7)
157     list(APPEND HWY_FLAGS
158       -march=armv7-a
159       -mfpu=neon-vfpv4
160       -mfloat-abi=hard  # must match the toolchain specified as CXX=
161       -mfp16-format=ieee  # required for vcvt_f32_f16
162     )
163   endif()  # HWY_CMAKE_ARM7
165 endif()  # !MSVC
167 add_library(hwy STATIC ${HWY_SOURCES})
168 target_compile_options(hwy PRIVATE ${HWY_FLAGS})
169 set_property(TARGET hwy PROPERTY POSITION_INDEPENDENT_CODE ON)
170 target_include_directories(hwy PUBLIC ${CMAKE_CURRENT_LIST_DIR})
172 add_library(hwy_contrib STATIC ${HWY_CONTRIB_SOURCES})
173 target_compile_options(hwy_contrib PRIVATE ${HWY_FLAGS})
174 set_property(TARGET hwy_contrib PROPERTY POSITION_INDEPENDENT_CODE ON)
175 target_include_directories(hwy_contrib PUBLIC ${CMAKE_CURRENT_LIST_DIR})
177 # -------------------------------------------------------- install library
178 install(TARGETS hwy
179   DESTINATION "${CMAKE_INSTALL_LIBDIR}")
180 # Install all the headers keeping the relative path to the current directory
181 # when installing them.
182 foreach (source ${HWY_SOURCES})
183   if ("${source}" MATCHES "\.h$")
184     get_filename_component(dirname "${source}" DIRECTORY)
185     install(FILES "${source}"
186         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${dirname}")
187   endif()
188 endforeach()
190 install(TARGETS hwy_contrib
191   DESTINATION "${CMAKE_INSTALL_LIBDIR}")
192 # Install all the headers keeping the relative path to the current directory
193 # when installing them.
194 foreach (source ${HWY_CONTRIB_SOURCES})
195   if ("${source}" MATCHES "\.h$")
196     get_filename_component(dirname "${source}" DIRECTORY)
197     install(FILES "${source}"
198         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${dirname}")
199   endif()
200 endforeach()
202 # Add a pkg-config file for libhwy and the contrib/test libraries.
203 set(HWY_LIBRARY_VERSION "${CMAKE_PROJECT_VERSION}")
204 foreach (pc libhwy.pc libhwy-contrib.pc libhwy-test.pc)
205   configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${pc}.in" "${pc}" @ONLY)
206   install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${pc}"
207       DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
208 endforeach()
210 # -------------------------------------------------------- hwy_list_targets
211 # Generate a tool to print the compiled-in targets as defined by the current
212 # flags. This tool will print to stderr at build time, after building hwy.
213 add_executable(hwy_list_targets hwy/tests/list_targets.cc)
214 target_compile_options(hwy_list_targets PRIVATE ${HWY_FLAGS})
215 target_include_directories(hwy_list_targets PRIVATE
216   $<TARGET_PROPERTY:hwy,INCLUDE_DIRECTORIES>)
217 # TARGET_FILE always returns the path to executable
218 # Naked target also not always could be run (due to the lack of '.\' prefix)
219 # Thus effective command to run should contain the full path
220 # and emulator prefix (if any).
221 add_custom_command(TARGET hwy POST_BUILD
222     COMMAND ${CMAKE_CROSSCOMPILING_EMULATOR} $<TARGET_FILE:hwy_list_targets> || (exit 0))
224 # -------------------------------------------------------- Examples
226 # Avoids mismatch between GTest's static CRT and our dynamic.
227 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
229 # Programming exercise with integrated benchmark
230 add_executable(hwy_benchmark hwy/examples/benchmark.cc)
231 target_sources(hwy_benchmark PRIVATE
232     hwy/nanobenchmark.cc
233     hwy/nanobenchmark.h)
234 # Try adding either -DHWY_COMPILE_ONLY_SCALAR or -DHWY_COMPILE_ONLY_STATIC to
235 # observe the difference in targets printed.
236 target_compile_options(hwy_benchmark PRIVATE ${HWY_FLAGS})
237 target_link_libraries(hwy_benchmark hwy)
238 set_target_properties(hwy_benchmark
239     PROPERTIES RUNTIME_OUTPUT_DIRECTORY "examples/")
241 # -------------------------------------------------------- Tests
243 include(CTest)
245 if(BUILD_TESTING)
246 enable_testing()
247 include(GoogleTest)
249 set(HWY_SYSTEM_GTEST OFF CACHE BOOL "Use pre-installed googletest?")
250 if(HWY_SYSTEM_GTEST)
251 find_package(GTest REQUIRED)
252 else()
253 # Download and unpack googletest at configure time
254 configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
255 execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
256   RESULT_VARIABLE result
257   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
258 if(result)
259   message(FATAL_ERROR "CMake step for googletest failed: ${result}")
260 endif()
261 execute_process(COMMAND ${CMAKE_COMMAND} --build .
262   RESULT_VARIABLE result
263   WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
264 if(result)
265   message(FATAL_ERROR "Build step for googletest failed: ${result}")
266 endif()
268 # Prevent overriding the parent project's compiler/linker
269 # settings on Windows
270 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
272 # Add googletest directly to our build. This defines
273 # the gtest and gtest_main targets.
274 add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
275                  ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
276                  EXCLUDE_FROM_ALL)
278 # The gtest/gtest_main targets carry header search path
279 # dependencies automatically when using CMake 2.8.11 or
280 # later. Otherwise we have to add them here ourselves.
281 if (CMAKE_VERSION VERSION_LESS 2.8.11)
282   include_directories("${gtest_SOURCE_DIR}/include")
283 endif()
284 endif() # HWY_SYSTEM_GTEST
286 set(HWY_TEST_FILES
287   hwy/contrib/image/image_test.cc
288   # hwy/contrib/math/math_test.cc
289   hwy/aligned_allocator_test.cc
290   hwy/base_test.cc
291   hwy/highway_test.cc
292   hwy/targets_test.cc
293   hwy/examples/skeleton_test.cc
294   hwy/tests/arithmetic_test.cc
295   hwy/tests/combine_test.cc
296   hwy/tests/compare_test.cc
297   hwy/tests/convert_test.cc
298   hwy/tests/logical_test.cc
299   hwy/tests/memory_test.cc
300   hwy/tests/swizzle_test.cc
301   hwy/tests/test_util_test.cc
304 file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests)
305 foreach (TESTFILE IN LISTS HWY_TEST_FILES)
306   # The TESTNAME is the name without the extension or directory.
307   get_filename_component(TESTNAME ${TESTFILE} NAME_WE)
308   add_executable(${TESTNAME} ${TESTFILE})
309   target_compile_options(${TESTNAME} PRIVATE ${HWY_FLAGS})
310   # Test all targets, not just the best/baseline.
311   target_compile_options(${TESTNAME} PRIVATE -DHWY_COMPILE_ALL_ATTAINABLE=1)
313   if(HWY_SYSTEM_GTEST)
314     target_link_libraries(${TESTNAME} hwy hwy_contrib GTest::GTest GTest::Main)
315   else()
316     target_link_libraries(${TESTNAME} hwy hwy_contrib gtest gtest_main)
317   endif()
318   # Output test targets in the test directory.
319   set_target_properties(${TESTNAME} PROPERTIES PREFIX "tests/")
321   if (HWY_EMSCRIPTEN)
322     set_target_properties(${TESTNAME} PROPERTIES LINK_FLAGS "-s SINGLE_FILE=1")
323   endif()
325   if(${CMAKE_VERSION} VERSION_LESS "3.10.3")
326     gtest_discover_tests(${TESTNAME} TIMEOUT 60)
327   else ()
328     gtest_discover_tests(${TESTNAME} DISCOVERY_TIMEOUT 60)
329   endif ()
330 endforeach ()
332 # The skeleton test uses the skeleton library code.
333 target_sources(skeleton_test PRIVATE hwy/examples/skeleton.cc)
335 endif() # BUILD_TESTING