1*4bdff4beSrobertif (CMAKE_VERSION VERSION_LESS 3.17)
2*4bdff4beSrobert  message(WARNING "The libc++ benchmarks won't be available because the version of CMake is too old to support them.")
3*4bdff4beSrobert  return()
4*4bdff4beSrobertendif()
5*4bdff4beSrobert
646035553Spatrickinclude(ExternalProject)
746035553Spatrickinclude(CheckCXXCompilerFlag)
846035553Spatrick
946035553Spatrick#==============================================================================
1046035553Spatrick# Build Google Benchmark for libc++
1146035553Spatrick#==============================================================================
1246035553Spatrick
1376d0caaeSpatrickset(CMAKE_FOLDER "${CMAKE_FOLDER}/Benchmarks")
1476d0caaeSpatrick
1546035553Spatrickset(BENCHMARK_LIBCXX_COMPILE_FLAGS
1646035553Spatrick    -Wno-unused-command-line-argument
1746035553Spatrick    -nostdinc++
1876d0caaeSpatrick    -isystem "${LIBCXX_GENERATED_INCLUDE_DIR}"
1946035553Spatrick    -L${LIBCXX_LIBRARY_DIR}
2046035553Spatrick    -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
2146035553Spatrick    ${SANITIZER_FLAGS}
2246035553Spatrick    )
2376d0caaeSpatrickif(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
2476d0caaeSpatrick  list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
2576d0caaeSpatrick    -isystem "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}")
2676d0caaeSpatrickendif()
2746035553Spatrickif (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH)
2846035553Spatrick  list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
2946035553Spatrick          -L${LIBCXX_CXX_ABI_LIBRARY_PATH}
3046035553Spatrick          -Wl,-rpath,${LIBCXX_CXX_ABI_LIBRARY_PATH})
3146035553Spatrickendif()
3246035553Spatricksplit_list(BENCHMARK_LIBCXX_COMPILE_FLAGS)
3346035553Spatrick
3446035553SpatrickExternalProject_Add(google-benchmark-libcxx
3546035553Spatrick        EXCLUDE_FROM_ALL ON
3646035553Spatrick        DEPENDS cxx cxx-headers
3746035553Spatrick        PREFIX benchmark-libcxx
38*4bdff4beSrobert        SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark
3946035553Spatrick        INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx
4046035553Spatrick        CMAKE_CACHE_ARGS
4146035553Spatrick          -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
4246035553Spatrick          -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
4346035553Spatrick          -DCMAKE_BUILD_TYPE:STRING=RELEASE
4446035553Spatrick          -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
4546035553Spatrick          -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_LIBCXX_COMPILE_FLAGS}
4646035553Spatrick          -DBENCHMARK_USE_LIBCXX:BOOL=ON
4746035553Spatrick          -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
4846035553Spatrick
4946035553Spatrick#==============================================================================
5046035553Spatrick# Build Google Benchmark for the native stdlib
5146035553Spatrick#==============================================================================
5246035553Spatrickset(BENCHMARK_NATIVE_TARGET_FLAGS)
5346035553Spatrickif (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN)
5446035553Spatrick  set(BENCHMARK_NATIVE_TARGET_FLAGS
55*4bdff4beSrobert      --gcc-toolchain=${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN})
5646035553Spatrickendif()
5746035553Spatricksplit_list(BENCHMARK_NATIVE_TARGET_FLAGS)
5846035553Spatrick
5946035553Spatrickif (LIBCXX_BENCHMARK_NATIVE_STDLIB)
6046035553Spatrick  ExternalProject_Add(google-benchmark-native
6146035553Spatrick        EXCLUDE_FROM_ALL ON
6246035553Spatrick        PREFIX benchmark-native
63*4bdff4beSrobert        SOURCE_DIR ${LLVM_THIRD_PARTY_DIR}/benchmark
6446035553Spatrick        INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native
6546035553Spatrick        CMAKE_CACHE_ARGS
6646035553Spatrick          -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
6746035553Spatrick          -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
6846035553Spatrick          -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_NATIVE_TARGET_FLAGS}
6946035553Spatrick          -DCMAKE_BUILD_TYPE:STRING=RELEASE
7046035553Spatrick          -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
7146035553Spatrick          -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
7246035553Spatrickendif()
7346035553Spatrick
7446035553Spatrick
7546035553Spatrick#==============================================================================
7646035553Spatrick# Benchmark tests configuration
7746035553Spatrick#==============================================================================
7846035553Spatrickadd_custom_target(cxx-benchmarks)
7946035553Spatrickset(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
8046035553Spatrickset(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx)
8146035553Spatrickset(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native)
8246035553Spatrick
83*4bdff4beSrobertadd_library(               cxx-benchmarks-flags INTERFACE)
84*4bdff4beSroberttarget_compile_features(   cxx-benchmarks-flags INTERFACE cxx_std_20)
85*4bdff4beSroberttarget_compile_options(    cxx-benchmarks-flags INTERFACE -fsized-deallocation -nostdinc++)
86*4bdff4beSroberttarget_include_directories(cxx-benchmarks-flags INTERFACE "${LIBCXX_GENERATED_INCLUDE_DIR}"
87*4bdff4beSrobert                                                INTERFACE "${BENCHMARK_LIBCXX_INSTALL}/include"
88*4bdff4beSrobert                                                INTERFACE "${LIBCXX_SOURCE_DIR}/test/support")
8946035553Spatrick
90*4bdff4beSrobertadd_library(           cxx-benchmarks-flags-native INTERFACE)
91*4bdff4beSroberttarget_link_libraries( cxx-benchmarks-flags-native INTERFACE cxx-benchmarks-flags)
92*4bdff4beSroberttarget_compile_options(cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS})
93*4bdff4beSroberttarget_link_options(   cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS} "-L${BENCHMARK_NATIVE_INSTALL}/lib")
9446035553Spatrickif (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
9546035553Spatrick  find_library(LIBSTDCXX_FILESYSTEM_TEST stdc++fs
9646035553Spatrick        PATHS ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}
9746035553Spatrick        PATH_SUFFIXES lib lib64
9846035553Spatrick        DOC "The libstdc++ filesystem library used by the benchmarks"
9946035553Spatrick    )
100*4bdff4beSrobert  if (LIBSTDCXX_FILESYSTEM_TEST)
101*4bdff4beSrobert    target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lstdc++fs)
10246035553Spatrick  endif()
103*4bdff4beSrobertelse()
104*4bdff4beSrobert  target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lc++fs -lc++experimental)
10546035553Spatrickendif()
10646035553Spatrick
107*4bdff4beSrobertadd_library(           cxx-benchmarks-flags-libcxx INTERFACE)
108*4bdff4beSroberttarget_link_libraries( cxx-benchmarks-flags-libcxx INTERFACE cxx-benchmarks-flags)
109*4bdff4beSroberttarget_compile_options(cxx-benchmarks-flags-libcxx INTERFACE ${SANITIZER_FLAGS} -Wno-user-defined-literals -Wno-suggest-override)
110*4bdff4beSroberttarget_link_options(   cxx-benchmarks-flags-libcxx INTERFACE -nodefaultlibs "-L${BENCHMARK_LIBCXX_INSTALL}/lib" ${SANITIZER_FLAGS})
111*4bdff4beSrobert
11246035553Spatrickset(libcxx_benchmark_targets)
11346035553Spatrick
11446035553Spatrickfunction(add_benchmark_test name source_file)
11546035553Spatrick  set(libcxx_target ${name}_libcxx)
11646035553Spatrick  list(APPEND libcxx_benchmark_targets ${libcxx_target})
11746035553Spatrick  add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
118*4bdff4beSrobert  target_link_libraries(${libcxx_target} PRIVATE cxx-benchmarks-flags-libcxx)
119037e7968Spatrick  add_dependencies(${libcxx_target} cxx google-benchmark-libcxx)
12046035553Spatrick  add_dependencies(cxx-benchmarks ${libcxx_target})
12146035553Spatrick  if (LIBCXX_ENABLE_SHARED)
12246035553Spatrick    target_link_libraries(${libcxx_target} PRIVATE cxx_shared)
12346035553Spatrick  else()
12446035553Spatrick    target_link_libraries(${libcxx_target} PRIVATE cxx_static)
12546035553Spatrick  endif()
126*4bdff4beSrobert  target_link_libraries(${libcxx_target} PRIVATE cxx_experimental benchmark)
12746035553Spatrick  if (LLVM_USE_SANITIZER)
12846035553Spatrick    target_link_libraries(${libcxx_target} PRIVATE -ldl)
12946035553Spatrick  endif()
13046035553Spatrick  set_target_properties(${libcxx_target}
13146035553Spatrick    PROPERTIES
13246035553Spatrick          OUTPUT_NAME "${name}.libcxx.out"
13346035553Spatrick          RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
13476d0caaeSpatrick          CXX_EXTENSIONS NO)
13546035553Spatrick  cxx_link_system_libraries(${libcxx_target})
13646035553Spatrick  if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
13746035553Spatrick    set(native_target ${name}_native)
13846035553Spatrick    add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file})
139*4bdff4beSrobert    target_link_libraries(${native_target} PRIVATE cxx-benchmarks-flags-native)
14046035553Spatrick    add_dependencies(${native_target} google-benchmark-native
14146035553Spatrick                                      google-benchmark-libcxx)
14246035553Spatrick    target_link_libraries(${native_target} PRIVATE -lbenchmark)
14346035553Spatrick    if (LIBCXX_HAS_PTHREAD_LIB)
14446035553Spatrick      target_link_libraries(${native_target} PRIVATE -pthread)
14546035553Spatrick    endif()
14646035553Spatrick    add_dependencies(cxx-benchmarks ${native_target})
14746035553Spatrick    set_target_properties(${native_target}
14846035553Spatrick      PROPERTIES
14946035553Spatrick          OUTPUT_NAME "${name}.native.out"
15046035553Spatrick          RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
15176d0caaeSpatrick          CXX_EXTENSIONS NO)
15246035553Spatrick  endif()
15346035553Spatrickendfunction()
15446035553Spatrick
15546035553Spatrick
15646035553Spatrick#==============================================================================
15746035553Spatrick# Register Benchmark tests
15846035553Spatrick#==============================================================================
159*4bdff4beSrobertset(BENCHMARK_TESTS
160*4bdff4beSrobert    algorithms.partition_point.bench.cpp
161*4bdff4beSrobert    algorithms/lower_bound.bench.cpp
162*4bdff4beSrobert    algorithms/make_heap.bench.cpp
163*4bdff4beSrobert    algorithms/make_heap_then_sort_heap.bench.cpp
164*4bdff4beSrobert    algorithms/min_max_element.bench.cpp
165*4bdff4beSrobert    algorithms/pop_heap.bench.cpp
166*4bdff4beSrobert    algorithms/push_heap.bench.cpp
167*4bdff4beSrobert    algorithms/ranges_make_heap.bench.cpp
168*4bdff4beSrobert    algorithms/ranges_make_heap_then_sort_heap.bench.cpp
169*4bdff4beSrobert    algorithms/ranges_pop_heap.bench.cpp
170*4bdff4beSrobert    algorithms/ranges_push_heap.bench.cpp
171*4bdff4beSrobert    algorithms/ranges_sort.bench.cpp
172*4bdff4beSrobert    algorithms/ranges_sort_heap.bench.cpp
173*4bdff4beSrobert    algorithms/ranges_stable_sort.bench.cpp
174*4bdff4beSrobert    algorithms/sort.bench.cpp
175*4bdff4beSrobert    algorithms/sort_heap.bench.cpp
176*4bdff4beSrobert    algorithms/stable_sort.bench.cpp
177*4bdff4beSrobert    allocation.bench.cpp
178*4bdff4beSrobert    deque.bench.cpp
179*4bdff4beSrobert    deque_iterator.bench.cpp
180*4bdff4beSrobert    filesystem.bench.cpp
181*4bdff4beSrobert    format_to_n.bench.cpp
182*4bdff4beSrobert    format_to.bench.cpp
183*4bdff4beSrobert    format.bench.cpp
184*4bdff4beSrobert    formatted_size.bench.cpp
185*4bdff4beSrobert    formatter_float.bench.cpp
186*4bdff4beSrobert    formatter_int.bench.cpp
187*4bdff4beSrobert    function.bench.cpp
188*4bdff4beSrobert    join_view.bench.cpp
189*4bdff4beSrobert    map.bench.cpp
190*4bdff4beSrobert    monotonic_buffer.bench.cpp
191*4bdff4beSrobert    ordered_set.bench.cpp
192*4bdff4beSrobert    std_format_spec_string_unicode.bench.cpp
193*4bdff4beSrobert    string.bench.cpp
194*4bdff4beSrobert    stringstream.bench.cpp
195*4bdff4beSrobert    to_chars.bench.cpp
196*4bdff4beSrobert    unordered_set_operations.bench.cpp
197*4bdff4beSrobert    util_smartptr.bench.cpp
198*4bdff4beSrobert    variant_visit_1.bench.cpp
199*4bdff4beSrobert    variant_visit_2.bench.cpp
200*4bdff4beSrobert    variant_visit_3.bench.cpp
201*4bdff4beSrobert    vector_operations.bench.cpp
202*4bdff4beSrobert    )
203*4bdff4beSrobert
20446035553Spatrickforeach(test_path ${BENCHMARK_TESTS})
20546035553Spatrick  get_filename_component(test_file "${test_path}" NAME)
20646035553Spatrick  string(REPLACE ".bench.cpp" "" test_name "${test_file}")
20746035553Spatrick  if (NOT DEFINED ${test_name}_REPORTED)
20846035553Spatrick    message(STATUS "Adding Benchmark: ${test_file}")
20946035553Spatrick    # Only report the adding of the benchmark once.
21046035553Spatrick    set(${test_name}_REPORTED ON CACHE INTERNAL "")
21146035553Spatrick  endif()
212*4bdff4beSrobert  add_benchmark_test(${test_name} ${test_path})
21346035553Spatrickendforeach()
21446035553Spatrick
21546035553Spatrickif (LIBCXX_INCLUDE_TESTS)
21646035553Spatrick  include(AddLLVM)
21746035553Spatrick
21846035553Spatrick  if (NOT DEFINED LIBCXX_TEST_DEPS)
21946035553Spatrick    message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined")
22046035553Spatrick  endif()
22146035553Spatrick
22246035553Spatrick  configure_lit_site_cfg(
22346035553Spatrick          ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
22446035553Spatrick          ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
22546035553Spatrick
22646035553Spatrick  set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}")
22746035553Spatrick
22846035553Spatrick  add_lit_target(check-cxx-benchmarks
22946035553Spatrick          "Running libcxx benchmarks tests"
23046035553Spatrick          ${CMAKE_CURRENT_BINARY_DIR}
23146035553Spatrick          DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS}
23246035553Spatrick          ARGS ${BENCHMARK_LIT_ARGS})
23346035553Spatrickendif()
234