1include(ExternalProject) 2include(CheckCXXCompilerFlag) 3 4#============================================================================== 5# Build Google Benchmark for libc++ 6#============================================================================== 7 8set(CMAKE_FOLDER "${CMAKE_FOLDER}/Benchmarks") 9 10set(BENCHMARK_LIBCXX_COMPILE_FLAGS 11 -Wno-unused-command-line-argument 12 -nostdinc++ 13 -isystem "${LIBCXX_GENERATED_INCLUDE_DIR}" 14 -L${LIBCXX_LIBRARY_DIR} 15 -Wl,-rpath,${LIBCXX_LIBRARY_DIR} 16 ${SANITIZER_FLAGS} 17 ) 18if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE) 19 list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS 20 -isystem "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}") 21endif() 22if (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH) 23 list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS 24 -L${LIBCXX_CXX_ABI_LIBRARY_PATH} 25 -Wl,-rpath,${LIBCXX_CXX_ABI_LIBRARY_PATH}) 26endif() 27split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS) 28 29ExternalProject_Add(google-benchmark-libcxx 30 EXCLUDE_FROM_ALL ON 31 DEPENDS cxx cxx-headers 32 PREFIX benchmark-libcxx 33 SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark 34 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx 35 CMAKE_CACHE_ARGS 36 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} 37 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} 38 -DCMAKE_BUILD_TYPE:STRING=RELEASE 39 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> 40 -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_LIBCXX_COMPILE_FLAGS} 41 -DBENCHMARK_USE_LIBCXX:BOOL=ON 42 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF) 43 44#============================================================================== 45# Build Google Benchmark for the native stdlib 46#============================================================================== 47set(BENCHMARK_NATIVE_TARGET_FLAGS) 48if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN) 49 set(BENCHMARK_NATIVE_TARGET_FLAGS 50 --gcc-toolchain=${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}) 51endif() 52split_list(BENCHMARK_NATIVE_TARGET_FLAGS) 53 54if (LIBCXX_BENCHMARK_NATIVE_STDLIB) 55 ExternalProject_Add(google-benchmark-native 56 EXCLUDE_FROM_ALL ON 57 PREFIX benchmark-native 58 SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark 59 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native 60 CMAKE_CACHE_ARGS 61 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} 62 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} 63 -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_NATIVE_TARGET_FLAGS} 64 -DCMAKE_BUILD_TYPE:STRING=RELEASE 65 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> 66 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF) 67endif() 68 69 70#============================================================================== 71# Benchmark tests configuration 72#============================================================================== 73add_custom_target(cxx-benchmarks) 74set(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) 75set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx) 76set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native) 77 78add_library( cxx-benchmarks-flags INTERFACE) 79 80# TODO(cmake): remove. This is a workaround to prevent older versions of GCC 81# from failing the configure step because they don't support C++23. 82if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "13.0") 83 return() 84endif() 85#TODO(cmake): remove the `add_compile_options`. Currently we have to explicitly 86# pass the `std:c++latest` flag on Windows to work around an issue where 87# requesting `cxx_std_23` results in an error -- somehow CMake fails to 88# translate the `c++23` flag into `c++latest`, and the highest numbered C++ 89# version that MSVC flags support is C++20. 90if (MSVC) 91 add_compile_options(/std:c++latest) 92# ibm-clang does not recognize the cxx_std_23 flag, so use this as a temporary 93# workaround on AIX as well. 94elseif (${CMAKE_SYSTEM_NAME} MATCHES "AIX") 95 add_compile_options(-std=c++23) 96else() 97 target_compile_features( cxx-benchmarks-flags INTERFACE cxx_std_23) 98endif() 99 100target_compile_options( cxx-benchmarks-flags INTERFACE -fsized-deallocation -nostdinc++) 101target_include_directories(cxx-benchmarks-flags INTERFACE "${LIBCXX_GENERATED_INCLUDE_DIR}" 102 INTERFACE "${BENCHMARK_LIBCXX_INSTALL}/include" 103 INTERFACE "${LIBCXX_SOURCE_DIR}/test/support") 104 105add_library( cxx-benchmarks-flags-native INTERFACE) 106target_link_libraries( cxx-benchmarks-flags-native INTERFACE cxx-benchmarks-flags) 107target_compile_options(cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS}) 108target_link_options( cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS} "-L${BENCHMARK_NATIVE_INSTALL}/lib") 109if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++") 110 find_library(LIBSTDCXX_FILESYSTEM_TEST stdc++fs 111 PATHS ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN} 112 PATH_SUFFIXES lib lib64 113 DOC "The libstdc++ filesystem library used by the benchmarks" 114 ) 115 if (LIBSTDCXX_FILESYSTEM_TEST) 116 target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lstdc++fs) 117 endif() 118else() 119 target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lc++fs -lc++experimental) 120endif() 121 122add_library( cxx-benchmarks-flags-libcxx INTERFACE) 123target_link_libraries( cxx-benchmarks-flags-libcxx INTERFACE cxx-benchmarks-flags) 124target_compile_options(cxx-benchmarks-flags-libcxx INTERFACE ${SANITIZER_FLAGS} -Wno-user-defined-literals -Wno-suggest-override) 125target_link_options( cxx-benchmarks-flags-libcxx INTERFACE -nostdlib++ "-L${BENCHMARK_LIBCXX_INSTALL}/lib" "-L${BENCHMARK_LIBCXX_INSTALL}/lib64" ${SANITIZER_FLAGS}) 126 127set(libcxx_benchmark_targets) 128 129function(add_benchmark_test name source_file) 130 set(libcxx_target ${name}_libcxx) 131 list(APPEND libcxx_benchmark_targets ${libcxx_target}) 132 add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file}) 133 target_link_libraries(${libcxx_target} PRIVATE cxx-benchmarks-flags-libcxx) 134 add_dependencies(${libcxx_target} cxx google-benchmark-libcxx) 135 add_dependencies(cxx-benchmarks ${libcxx_target}) 136 if (LIBCXX_ENABLE_SHARED) 137 target_link_libraries(${libcxx_target} PRIVATE cxx_shared) 138 else() 139 target_link_libraries(${libcxx_target} PRIVATE cxx_static) 140 endif() 141 target_link_libraries(${libcxx_target} PRIVATE cxx_experimental benchmark) 142 if (LLVM_USE_SANITIZER) 143 target_link_libraries(${libcxx_target} PRIVATE -ldl) 144 endif() 145 set_target_properties(${libcxx_target} 146 PROPERTIES 147 OUTPUT_NAME "${name}.libcxx.out" 148 RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}" 149 CXX_EXTENSIONS NO) 150 cxx_link_system_libraries(${libcxx_target}) 151 if (LIBCXX_BENCHMARK_NATIVE_STDLIB) 152 set(native_target ${name}_native) 153 add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file}) 154 target_link_libraries(${native_target} PRIVATE cxx-benchmarks-flags-native) 155 add_dependencies(${native_target} google-benchmark-native 156 google-benchmark-libcxx) 157 target_link_libraries(${native_target} PRIVATE -lbenchmark) 158 if (LIBCXX_HAS_PTHREAD_LIB) 159 target_link_libraries(${native_target} PRIVATE -pthread) 160 endif() 161 add_dependencies(cxx-benchmarks ${native_target}) 162 set_target_properties(${native_target} 163 PROPERTIES 164 OUTPUT_NAME "${name}.native.out" 165 RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}" 166 CXX_EXTENSIONS NO) 167 endif() 168endfunction() 169 170 171#============================================================================== 172# Register Benchmark tests 173#============================================================================== 174set(BENCHMARK_TESTS 175 algorithms.partition_point.bench.cpp 176 algorithms/count.bench.cpp 177 algorithms/equal.bench.cpp 178 algorithms/find.bench.cpp 179 algorithms/for_each.bench.cpp 180 algorithms/lower_bound.bench.cpp 181 algorithms/make_heap.bench.cpp 182 algorithms/make_heap_then_sort_heap.bench.cpp 183 algorithms/min.bench.cpp 184 algorithms/min_max_element.bench.cpp 185 algorithms/pop_heap.bench.cpp 186 algorithms/pstl.stable_sort.bench.cpp 187 algorithms/push_heap.bench.cpp 188 algorithms/ranges_ends_with.bench.cpp 189 algorithms/ranges_make_heap.bench.cpp 190 algorithms/ranges_make_heap_then_sort_heap.bench.cpp 191 algorithms/ranges_pop_heap.bench.cpp 192 algorithms/ranges_push_heap.bench.cpp 193 algorithms/ranges_sort.bench.cpp 194 algorithms/ranges_sort_heap.bench.cpp 195 algorithms/ranges_stable_sort.bench.cpp 196 algorithms/sort.bench.cpp 197 algorithms/sort_heap.bench.cpp 198 algorithms/stable_sort.bench.cpp 199 libcxxabi/dynamic_cast.bench.cpp 200 libcxxabi/dynamic_cast_old_stress.bench.cpp 201 allocation.bench.cpp 202 deque.bench.cpp 203 deque_iterator.bench.cpp 204 filesystem.bench.cpp 205 format_to_n.bench.cpp 206 format_to.bench.cpp 207 format.bench.cpp 208 formatted_size.bench.cpp 209 formatter_float.bench.cpp 210 formatter_int.bench.cpp 211 function.bench.cpp 212 join_view.bench.cpp 213 lexicographical_compare_three_way.bench.cpp 214 map.bench.cpp 215 monotonic_buffer.bench.cpp 216 ordered_set.bench.cpp 217 stop_token.bench.cpp 218 std_format_spec_string_unicode.bench.cpp 219 string.bench.cpp 220 stringstream.bench.cpp 221 system_error.bench.cpp 222 to_chars.bench.cpp 223 unordered_set_operations.bench.cpp 224 util_smartptr.bench.cpp 225 variant_visit_1.bench.cpp 226 variant_visit_2.bench.cpp 227 variant_visit_3.bench.cpp 228 vector_operations.bench.cpp 229 ) 230 231foreach(test_path ${BENCHMARK_TESTS}) 232 get_filename_component(test_file "${test_path}" NAME) 233 string(REPLACE ".bench.cpp" "" test_name "${test_file}") 234 if (NOT DEFINED ${test_name}_REPORTED) 235 message(STATUS "Adding Benchmark: ${test_file}") 236 # Only report the adding of the benchmark once. 237 set(${test_name}_REPORTED ON CACHE INTERNAL "") 238 endif() 239 add_benchmark_test(${test_name} ${test_path}) 240endforeach() 241 242if (LIBCXX_INCLUDE_TESTS) 243 include(AddLLVM) 244 245 if (NOT DEFINED LIBCXX_TEST_DEPS) 246 message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined") 247 endif() 248 249 configure_lit_site_cfg( 250 ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in 251 ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py) 252 253 set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}") 254 255 add_lit_target(check-cxx-benchmarks 256 "Running libcxx benchmarks tests" 257 ${CMAKE_CURRENT_BINARY_DIR} 258 DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS} 259 ARGS ${BENCHMARK_LIT_ARGS}) 260endif() 261