efe7914985382227b1ad86d24829a4cc8ece8f97
[lldb.git] / libcxx / benchmarks / CMakeLists.txt
1 include(ExternalProject)
2 include(CheckCXXCompilerFlag)
3
4 #==============================================================================
5 # Build Google Benchmark for libc++
6 #==============================================================================
7
8 set(BENCHMARK_LIBCXX_COMPILE_FLAGS
9     -Wno-unused-command-line-argument
10     -nostdinc++
11     -isystem ${LIBCXX_SOURCE_DIR}/include
12     -L${LIBCXX_LIBRARY_DIR}
13     -Wl,-rpath,${LIBCXX_LIBRARY_DIR}
14     ${SANITIZER_FLAGS}
15     )
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})
20 endif()
21 if (LIBCXX_NEEDS_SITE_CONFIG)
22   list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS -include "${LIBCXX_BINARY_DIR}/__config_site")
23 endif()
24 split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS)
25
26 ExternalProject_Add(google-benchmark-libcxx
27         EXCLUDE_FROM_ALL ON
28         DEPENDS cxx cxx-headers
29         PREFIX benchmark-libcxx
30         SOURCE_DIR ${LIBCXX_SOURCE_DIR}/utils/google-benchmark
31         INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx
32         CMAKE_CACHE_ARGS
33           -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
34           -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
35           -DCMAKE_BUILD_TYPE:STRING=RELEASE
36           -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
37           -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_LIBCXX_COMPILE_FLAGS}
38           -DBENCHMARK_USE_LIBCXX:BOOL=ON
39           -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
40
41 #==============================================================================
42 # Build Google Benchmark for the native stdlib
43 #==============================================================================
44 set(BENCHMARK_NATIVE_TARGET_FLAGS)
45 if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN)
46   set(BENCHMARK_NATIVE_TARGET_FLAGS
47       -gcc-toolchain ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN})
48 endif()
49 split_list(BENCHMARK_NATIVE_TARGET_FLAGS)
50
51 if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
52   ExternalProject_Add(google-benchmark-native
53         EXCLUDE_FROM_ALL ON
54         PREFIX benchmark-native
55         SOURCE_DIR ${LIBCXX_SOURCE_DIR}/utils/google-benchmark
56         INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native
57         CMAKE_CACHE_ARGS
58           -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER}
59           -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}
60           -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_NATIVE_TARGET_FLAGS}
61           -DCMAKE_BUILD_TYPE:STRING=RELEASE
62           -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
63           -DBENCHMARK_ENABLE_TESTING:BOOL=OFF)
64 endif()
65
66
67 #==============================================================================
68 # Benchmark tests configuration
69 #==============================================================================
70 add_custom_target(cxx-benchmarks)
71 set(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
72 set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx)
73 set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native)
74
75 check_flag_supported("-std=c++17")
76 mangle_name("LIBCXX_SUPPORTS_STD_EQ_c++17_FLAG" BENCHMARK_SUPPORTS_STD_CXX17_FLAG)
77 if (${BENCHMARK_SUPPORTS_STD_CXX17_FLAG})
78   set(BENCHMARK_DIALECT_FLAG "-std=c++17")
79 else()
80   # If the compiler doesn't support -std=c++17, attempt to fall back to -std=c++1z while still
81   # requiring C++17 language features.
82   set(BENCHMARK_DIALECT_FLAG "-std=c++1z")
83 endif()
84
85 set(BENCHMARK_TEST_COMPILE_FLAGS
86     ${BENCHMARK_DIALECT_FLAG} -O2
87     -fsized-deallocation
88     -I${BENCHMARK_LIBCXX_INSTALL}/include
89     -I${LIBCXX_SOURCE_DIR}/test/support
90 )
91 set(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS
92     -nostdinc++
93     -isystem ${LIBCXX_SOURCE_DIR}/include
94     ${BENCHMARK_TEST_COMPILE_FLAGS}
95     ${SANITIZER_FLAGS}
96     -Wno-user-defined-literals
97 )
98
99 set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS
100     -nodefaultlibs
101     -L${BENCHMARK_LIBCXX_INSTALL}/lib/
102     ${SANITIZER_FLAGS}
103 )
104 set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS
105   ${BENCHMARK_NATIVE_TARGET_FLAGS}
106   ${BENCHMARK_TEST_COMPILE_FLAGS}
107 )
108 set(BENCHMARK_TEST_NATIVE_LINK_FLAGS
109     ${BENCHMARK_NATIVE_TARGET_FLAGS}
110     -L${BENCHMARK_NATIVE_INSTALL}/lib
111 )
112 split_list(BENCHMARK_TEST_COMPILE_FLAGS)
113 split_list(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS)
114 split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS)
115 split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS)
116 split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS)
117
118 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
119   find_library(LIBSTDCXX_FILESYSTEM_TEST stdc++fs
120         PATHS ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}
121         PATH_SUFFIXES lib lib64
122         DOC "The libstdc++ filesystem library used by the benchmarks"
123     )
124     if (NOT "${LIBSTDCXX_FILESYSTEM_TEST}" STREQUAL "LIBSTDCXX_FILESYSTEM_TEST-NOTFOUND")
125       set(LIBSTDCXX_FILESYSTEM_LIB "stdc++fs")
126     endif()
127 endif()
128
129 set(libcxx_benchmark_targets)
130
131 function(add_benchmark_test name source_file)
132   set(libcxx_target ${name}_libcxx)
133   list(APPEND libcxx_benchmark_targets ${libcxx_target})
134   add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
135   add_dependencies(${libcxx_target} cxx cxx-headers google-benchmark-libcxx)
136   add_dependencies(cxx-benchmarks ${libcxx_target})
137   if (LIBCXX_ENABLE_SHARED)
138     target_link_libraries(${libcxx_target} PRIVATE cxx_shared)
139   else()
140     target_link_libraries(${libcxx_target} PRIVATE cxx_static)
141   endif()
142   if (TARGET cxx_experimental)
143     target_link_libraries(${libcxx_target} PRIVATE cxx_experimental)
144   endif()
145   target_link_libraries(${libcxx_target} PRIVATE -lbenchmark)
146   if (LLVM_USE_SANITIZER)
147     target_link_libraries(${libcxx_target} PRIVATE -ldl)
148   endif()
149   set_target_properties(${libcxx_target}
150     PROPERTIES
151           OUTPUT_NAME "${name}.libcxx.out"
152           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
153           COMPILE_FLAGS "${BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS}"
154           LINK_FLAGS "${BENCHMARK_TEST_LIBCXX_LINK_FLAGS}")
155   if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
156     if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++" AND NOT DEFINED LIBSTDCXX_FILESYSTEM_LIB
157         AND "${name}" STREQUAL "filesystem")
158       return()
159     endif()
160     set(native_target ${name}_native)
161     add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file})
162     add_dependencies(${native_target} google-benchmark-native
163                                       google-benchmark-libcxx)
164     target_link_libraries(${native_target} PRIVATE -lbenchmark)
165     if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
166       target_link_libraries(${native_target} PRIVATE ${LIBSTDCXX_FILESYSTEM_LIB})
167     elseif (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++")
168       target_link_libraries(${native_target} PRIVATE -lc++fs -lc++experimental)
169     endif()
170     if (LIBCXX_HAS_PTHREAD_LIB)
171       target_link_libraries(${native_target} PRIVATE -pthread)
172     endif()
173     add_dependencies(cxx-benchmarks ${native_target})
174     set_target_properties(${native_target}
175       PROPERTIES
176           OUTPUT_NAME "${name}.native.out"
177           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
178           INCLUDE_DIRECTORIES ""
179           COMPILE_FLAGS "${BENCHMARK_TEST_NATIVE_COMPILE_FLAGS}"
180           LINK_FLAGS "${BENCHMARK_TEST_NATIVE_LINK_FLAGS}")
181   endif()
182 endfunction()
183
184
185 #==============================================================================
186 # Register Benchmark tests
187 #==============================================================================
188 file(GLOB BENCHMARK_TESTS "*.bench.cpp")
189 foreach(test_path ${BENCHMARK_TESTS})
190   get_filename_component(test_file "${test_path}" NAME)
191   string(REPLACE ".bench.cpp" "" test_name "${test_file}")
192   if (NOT DEFINED ${test_name}_REPORTED)
193     message(STATUS "Adding Benchmark: ${test_file}")
194     # Only report the adding of the benchmark once.
195     set(${test_name}_REPORTED ON CACHE INTERNAL "")
196   endif()
197   add_benchmark_test(${test_name} ${test_file})
198 endforeach()
199
200 if (LIBCXX_INCLUDE_TESTS)
201   include(AddLLVM)
202
203   if (NOT DEFINED LIBCXX_TEST_DEPS)
204     message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined")
205   endif()
206
207   configure_lit_site_cfg(
208           ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
209           ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
210
211   set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}")
212
213   add_lit_target(check-cxx-benchmarks
214           "Running libcxx benchmarks tests"
215           ${CMAKE_CURRENT_BINARY_DIR}
216           DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS}
217           ARGS ${BENCHMARK_LIT_ARGS})
218 endif()
219