[RISCV][NFC] precommit for D159399
[llvm-project.git] / libcxx / benchmarks / CMakeLists.txt
blob66cc2930257643b60833a58bff312b93461da141
1 include(ExternalProject)
2 include(CheckCXXCompilerFlag)
4 #==============================================================================
5 # Build Google Benchmark for libc++
6 #==============================================================================
8 set(CMAKE_FOLDER "${CMAKE_FOLDER}/Benchmarks")
10 set(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     )
18 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
19   list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS
20     -isystem "${LIBCXX_GENERATED_INCLUDE_TARGET_DIR}")
21 endif()
22 if (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})
26 endif()
27 split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS)
29 ExternalProject_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)
44 #==============================================================================
45 # Build Google Benchmark for the native stdlib
46 #==============================================================================
47 set(BENCHMARK_NATIVE_TARGET_FLAGS)
48 if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN)
49   set(BENCHMARK_NATIVE_TARGET_FLAGS
50       --gcc-toolchain=${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN})
51 endif()
52 split_list(BENCHMARK_NATIVE_TARGET_FLAGS)
54 if (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)
67 endif()
70 #==============================================================================
71 # Benchmark tests configuration
72 #==============================================================================
73 add_custom_target(cxx-benchmarks)
74 set(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
75 set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx)
76 set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native)
78 add_library(               cxx-benchmarks-flags INTERFACE)
79 #TODO(cmake): remove the `add_compile_options`. Currently we have to explicitly
80 # pass the `std:c++latest` flag on Windows to work around an issue where
81 # requesting `cxx_std_23` results in an error -- somehow CMake fails to
82 # translate the `c++23` flag into `c++latest`, and the highest numbered C++
83 # version that MSVC flags support is C++20.
84 if (MSVC)
85   add_compile_options(/std:c++latest)
86 else()
87   target_compile_features( cxx-benchmarks-flags INTERFACE cxx_std_23)
88 endif()
89 target_compile_options(    cxx-benchmarks-flags INTERFACE -fsized-deallocation -nostdinc++)
90 target_include_directories(cxx-benchmarks-flags INTERFACE "${LIBCXX_GENERATED_INCLUDE_DIR}"
91                                                 INTERFACE "${BENCHMARK_LIBCXX_INSTALL}/include"
92                                                 INTERFACE "${LIBCXX_SOURCE_DIR}/test/support")
94 add_library(           cxx-benchmarks-flags-native INTERFACE)
95 target_link_libraries( cxx-benchmarks-flags-native INTERFACE cxx-benchmarks-flags)
96 target_compile_options(cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS})
97 target_link_options(   cxx-benchmarks-flags-native INTERFACE ${BENCHMARK_NATIVE_TARGET_FLAGS} "-L${BENCHMARK_NATIVE_INSTALL}/lib")
98 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
99   find_library(LIBSTDCXX_FILESYSTEM_TEST stdc++fs
100         PATHS ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}
101         PATH_SUFFIXES lib lib64
102         DOC "The libstdc++ filesystem library used by the benchmarks"
103     )
104   if (LIBSTDCXX_FILESYSTEM_TEST)
105     target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lstdc++fs)
106   endif()
107 else()
108   target_link_libraries(cxx-benchmarks-flags-native INTERFACE -lc++fs -lc++experimental)
109 endif()
111 add_library(           cxx-benchmarks-flags-libcxx INTERFACE)
112 target_link_libraries( cxx-benchmarks-flags-libcxx INTERFACE cxx-benchmarks-flags)
113 target_compile_options(cxx-benchmarks-flags-libcxx INTERFACE ${SANITIZER_FLAGS} -Wno-user-defined-literals -Wno-suggest-override)
114 target_link_options(   cxx-benchmarks-flags-libcxx INTERFACE -nodefaultlibs "-L${BENCHMARK_LIBCXX_INSTALL}/lib" "-L${BENCHMARK_LIBCXX_INSTALL}/lib64" ${SANITIZER_FLAGS})
116 set(libcxx_benchmark_targets)
118 function(add_benchmark_test name source_file)
119   set(libcxx_target ${name}_libcxx)
120   list(APPEND libcxx_benchmark_targets ${libcxx_target})
121   add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
122   target_link_libraries(${libcxx_target} PRIVATE cxx-benchmarks-flags-libcxx)
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)
127   else()
128     target_link_libraries(${libcxx_target} PRIVATE cxx_static)
129   endif()
130   target_link_libraries(${libcxx_target} PRIVATE cxx_experimental benchmark)
131   if (LLVM_USE_SANITIZER)
132     target_link_libraries(${libcxx_target} PRIVATE -ldl)
133   endif()
134   set_target_properties(${libcxx_target}
135     PROPERTIES
136           OUTPUT_NAME "${name}.libcxx.out"
137           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
138           CXX_EXTENSIONS NO)
139   cxx_link_system_libraries(${libcxx_target})
140   if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
141     set(native_target ${name}_native)
142     add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file})
143     target_link_libraries(${native_target} PRIVATE cxx-benchmarks-flags-native)
144     add_dependencies(${native_target} google-benchmark-native
145                                       google-benchmark-libcxx)
146     target_link_libraries(${native_target} PRIVATE -lbenchmark)
147     if (LIBCXX_HAS_PTHREAD_LIB)
148       target_link_libraries(${native_target} PRIVATE -pthread)
149     endif()
150     add_dependencies(cxx-benchmarks ${native_target})
151     set_target_properties(${native_target}
152       PROPERTIES
153           OUTPUT_NAME "${name}.native.out"
154           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
155           CXX_EXTENSIONS NO)
156   endif()
157 endfunction()
160 #==============================================================================
161 # Register Benchmark tests
162 #==============================================================================
163 set(BENCHMARK_TESTS
164     algorithms.partition_point.bench.cpp
165     algorithms/equal.bench.cpp
166     algorithms/find.bench.cpp
167     algorithms/lower_bound.bench.cpp
168     algorithms/make_heap.bench.cpp
169     algorithms/make_heap_then_sort_heap.bench.cpp
170     algorithms/min.bench.cpp
171     algorithms/min_max_element.bench.cpp
172     algorithms/pop_heap.bench.cpp
173     algorithms/pstl.stable_sort.bench.cpp
174     algorithms/push_heap.bench.cpp
175     algorithms/ranges_make_heap.bench.cpp
176     algorithms/ranges_make_heap_then_sort_heap.bench.cpp
177     algorithms/ranges_pop_heap.bench.cpp
178     algorithms/ranges_push_heap.bench.cpp
179     algorithms/ranges_sort.bench.cpp
180     algorithms/ranges_sort_heap.bench.cpp
181     algorithms/ranges_stable_sort.bench.cpp
182     algorithms/sort.bench.cpp
183     algorithms/sort_heap.bench.cpp
184     algorithms/stable_sort.bench.cpp
185     libcxxabi/dynamic_cast.bench.cpp
186     libcxxabi/dynamic_cast_old_stress.bench.cpp
187     allocation.bench.cpp
188     deque.bench.cpp
189     deque_iterator.bench.cpp
190     filesystem.bench.cpp
191     format_to_n.bench.cpp
192     format_to.bench.cpp
193     format.bench.cpp
194     formatted_size.bench.cpp
195     formatter_float.bench.cpp
196     formatter_int.bench.cpp
197     function.bench.cpp
198     join_view.bench.cpp
199     lexicographical_compare_three_way.bench.cpp
200     map.bench.cpp
201     monotonic_buffer.bench.cpp
202     ordered_set.bench.cpp
203     std_format_spec_string_unicode.bench.cpp
204     string.bench.cpp
205     stringstream.bench.cpp
206     system_error.bench.cpp
207     to_chars.bench.cpp
208     unordered_set_operations.bench.cpp
209     util_smartptr.bench.cpp
210     variant_visit_1.bench.cpp
211     variant_visit_2.bench.cpp
212     variant_visit_3.bench.cpp
213     vector_operations.bench.cpp
214     )
216 foreach(test_path ${BENCHMARK_TESTS})
217   get_filename_component(test_file "${test_path}" NAME)
218   string(REPLACE ".bench.cpp" "" test_name "${test_file}")
219   if (NOT DEFINED ${test_name}_REPORTED)
220     message(STATUS "Adding Benchmark: ${test_file}")
221     # Only report the adding of the benchmark once.
222     set(${test_name}_REPORTED ON CACHE INTERNAL "")
223   endif()
224   add_benchmark_test(${test_name} ${test_path})
225 endforeach()
227 if (LIBCXX_INCLUDE_TESTS)
228   include(AddLLVM)
230   if (NOT DEFINED LIBCXX_TEST_DEPS)
231     message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined")
232   endif()
234   configure_lit_site_cfg(
235           ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
236           ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
238   set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}")
240   add_lit_target(check-cxx-benchmarks
241           "Running libcxx benchmarks tests"
242           ${CMAKE_CURRENT_BINARY_DIR}
243           DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS}
244           ARGS ${BENCHMARK_LIT_ARGS})
245 endif()