1 include(ExternalProject)
2 include(CheckCXXCompilerFlag)
4 #==============================================================================
5 # Build Google Benchmark for libc++
6 #==============================================================================
8 set(BENCHMARK_LIBCXX_COMPILE_FLAGS
9 -Wno-unused-command-line-argument
11 -isystem ${LIBCXX_SOURCE_DIR}/include
12 -L${LIBCXX_LIBRARY_DIR}
13 -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
16 if (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH)
17 list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
18 -L${LIBCXX_CXX_ABI_LIBRARY_PATH}
19 -Wl,-rpath,${LIBCXX_CXX_ABI_LIBRARY_PATH})
21 list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS -include "${LIBCXX_BINARY_DIR}/__config_site")
22 split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS)
24 ExternalProject_Add(google-benchmark-libcxx
26 DEPENDS cxx cxx-headers
27 PREFIX benchmark-libcxx
28 SOURCE_DIR ${LIBCXX_SOURCE_DIR}/utils/google-benchmark
29 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx
31 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
32 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
33 -DCMAKE_BUILD_TYPE:STRING=RELEASE
34 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
35 -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_LIBCXX_COMPILE_FLAGS}
36 -DBENCHMARK_USE_LIBCXX:BOOL=ON
37 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
39 #==============================================================================
40 # Build Google Benchmark for the native stdlib
41 #==============================================================================
42 set(BENCHMARK_NATIVE_TARGET_FLAGS)
43 if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN)
44 set(BENCHMARK_NATIVE_TARGET_FLAGS
45 -gcc-toolchain ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN})
47 split_list(BENCHMARK_NATIVE_TARGET_FLAGS)
49 if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
50 ExternalProject_Add(google-benchmark-native
52 PREFIX benchmark-native
53 SOURCE_DIR ${LIBCXX_SOURCE_DIR}/utils/google-benchmark
54 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native
56 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
57 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
58 -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_NATIVE_TARGET_FLAGS}
59 -DCMAKE_BUILD_TYPE:STRING=RELEASE
60 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
61 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
65 #==============================================================================
66 # Benchmark tests configuration
67 #==============================================================================
68 add_custom_target(cxx-benchmarks)
69 set(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
70 set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx)
71 set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native)
74 set(BENCHMARK_TEST_COMPILE_FLAGS
77 -I${BENCHMARK_LIBCXX_INSTALL}/include
78 -I${LIBCXX_SOURCE_DIR}/test/support
80 set(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS
81 ${BENCHMARK_TEST_COMPILE_FLAGS}
83 -Wno-user-defined-literals
87 set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS
89 -L${BENCHMARK_LIBCXX_INSTALL}/lib/
92 set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS
93 ${BENCHMARK_NATIVE_TARGET_FLAGS}
94 ${BENCHMARK_TEST_COMPILE_FLAGS}
96 set(BENCHMARK_TEST_NATIVE_LINK_FLAGS
97 ${BENCHMARK_NATIVE_TARGET_FLAGS}
98 -L${BENCHMARK_NATIVE_INSTALL}/lib
100 split_list(BENCHMARK_TEST_COMPILE_FLAGS)
101 split_list(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS)
102 split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS)
103 split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS)
104 split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS)
106 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
107 find_library(LIBSTDCXX_FILESYSTEM_TEST stdc++fs
108 PATHS ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}
109 PATH_SUFFIXES lib lib64
110 DOC "The libstdc++ filesystem library used by the benchmarks"
112 if (NOT "${LIBSTDCXX_FILESYSTEM_TEST}" STREQUAL "LIBSTDCXX_FILESYSTEM_TEST-NOTFOUND")
113 set(LIBSTDCXX_FILESYSTEM_LIB "stdc++fs")
117 set(libcxx_benchmark_targets)
119 function(add_benchmark_test name source_file)
120 set(libcxx_target ${name}_libcxx)
121 list(APPEND libcxx_benchmark_targets ${libcxx_target})
122 add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
123 add_dependencies(${libcxx_target} cxx google-benchmark-libcxx)
124 add_dependencies(cxx-benchmarks ${libcxx_target})
125 if (LIBCXX_ENABLE_SHARED)
126 target_link_libraries(${libcxx_target} PRIVATE cxx_shared)
128 target_link_libraries(${libcxx_target} PRIVATE cxx_static)
130 if (TARGET cxx_experimental)
131 target_link_libraries(${libcxx_target} PRIVATE cxx_experimental)
133 target_link_libraries(${libcxx_target} PRIVATE -lbenchmark)
134 if (LLVM_USE_SANITIZER)
135 target_link_libraries(${libcxx_target} PRIVATE -ldl)
137 set_target_properties(${libcxx_target}
139 OUTPUT_NAME "${name}.libcxx.out"
140 RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
141 COMPILE_FLAGS "${BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS}"
142 LINK_FLAGS "${BENCHMARK_TEST_LIBCXX_LINK_FLAGS}"
144 CXX_STANDARD_REQUIRED YES
146 cxx_link_system_libraries(${libcxx_target})
147 if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
148 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++" AND NOT DEFINED LIBSTDCXX_FILESYSTEM_LIB
149 AND "${name}" STREQUAL "filesystem")
152 set(native_target ${name}_native)
153 add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file})
154 add_dependencies(${native_target} google-benchmark-native
155 google-benchmark-libcxx)
156 target_link_libraries(${native_target} PRIVATE -lbenchmark)
157 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
158 target_link_libraries(${native_target} PRIVATE ${LIBSTDCXX_FILESYSTEM_LIB})
159 elseif (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++")
160 target_link_libraries(${native_target} PRIVATE -lc++fs -lc++experimental)
162 if (LIBCXX_HAS_PTHREAD_LIB)
163 target_link_libraries(${native_target} PRIVATE -pthread)
165 add_dependencies(cxx-benchmarks ${native_target})
166 set_target_properties(${native_target}
168 OUTPUT_NAME "${name}.native.out"
169 RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
170 INCLUDE_DIRECTORIES ""
171 COMPILE_FLAGS "${BENCHMARK_TEST_NATIVE_COMPILE_FLAGS}"
172 LINK_FLAGS "${BENCHMARK_TEST_NATIVE_LINK_FLAGS}"
174 CXX_STANDARD_REQUIRED YES
180 #==============================================================================
181 # Register Benchmark tests
182 #==============================================================================
183 file(GLOB BENCHMARK_TESTS "*.bench.cpp")
184 foreach(test_path ${BENCHMARK_TESTS})
185 get_filename_component(test_file "${test_path}" NAME)
186 string(REPLACE ".bench.cpp" "" test_name "${test_file}")
187 if (NOT DEFINED ${test_name}_REPORTED)
188 message(STATUS "Adding Benchmark: ${test_file}")
189 # Only report the adding of the benchmark once.
190 set(${test_name}_REPORTED ON CACHE INTERNAL "")
192 add_benchmark_test(${test_name} ${test_file})
195 if (LIBCXX_INCLUDE_TESTS)
198 if (NOT DEFINED LIBCXX_TEST_DEPS)
199 message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined")
202 configure_lit_site_cfg(
203 ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
204 ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
206 set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}")
208 add_lit_target(check-cxx-benchmarks
209 "Running libcxx benchmarks tests"
210 ${CMAKE_CURRENT_BINARY_DIR}
211 DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS}
212 ARGS ${BENCHMARK_LIT_ARGS})