[libc++] Add missing link-time dependencies to the benchmarks
[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   if (LIBCXX_HAS_C_LIB)
150     target_link_libraries(${libcxx_target} PRIVATE -lc)
151   endif()
152   if (LIBCXX_HAS_M_LIB)
153     target_link_libraries(${libcxx_target} PRIVATE -lm)
154   endif()
155   if (LIBCXX_HAS_PTHREAD_LIB)
156     target_link_libraries(${libcxx_target} PRIVATE -lpthread)
157   endif()
158   set_target_properties(${libcxx_target}
159     PROPERTIES
160           OUTPUT_NAME "${name}.libcxx.out"
161           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
162           COMPILE_FLAGS "${BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS}"
163           LINK_FLAGS "${BENCHMARK_TEST_LIBCXX_LINK_FLAGS}")
164   if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
165     if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++" AND NOT DEFINED LIBSTDCXX_FILESYSTEM_LIB
166         AND "${name}" STREQUAL "filesystem")
167       return()
168     endif()
169     set(native_target ${name}_native)
170     add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file})
171     add_dependencies(${native_target} google-benchmark-native
172                                       google-benchmark-libcxx)
173     target_link_libraries(${native_target} PRIVATE -lbenchmark)
174     if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
175       target_link_libraries(${native_target} PRIVATE ${LIBSTDCXX_FILESYSTEM_LIB})
176     elseif (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++")
177       target_link_libraries(${native_target} PRIVATE -lc++fs -lc++experimental)
178     endif()
179     if (LIBCXX_HAS_PTHREAD_LIB)
180       target_link_libraries(${native_target} PRIVATE -pthread)
181     endif()
182     add_dependencies(cxx-benchmarks ${native_target})
183     set_target_properties(${native_target}
184       PROPERTIES
185           OUTPUT_NAME "${name}.native.out"
186           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
187           INCLUDE_DIRECTORIES ""
188           COMPILE_FLAGS "${BENCHMARK_TEST_NATIVE_COMPILE_FLAGS}"
189           LINK_FLAGS "${BENCHMARK_TEST_NATIVE_LINK_FLAGS}")
190   endif()
191 endfunction()
192
193
194 #==============================================================================
195 # Register Benchmark tests
196 #==============================================================================
197 file(GLOB BENCHMARK_TESTS "*.bench.cpp")
198 foreach(test_path ${BENCHMARK_TESTS})
199   get_filename_component(test_file "${test_path}" NAME)
200   string(REPLACE ".bench.cpp" "" test_name "${test_file}")
201   if (NOT DEFINED ${test_name}_REPORTED)
202     message(STATUS "Adding Benchmark: ${test_file}")
203     # Only report the adding of the benchmark once.
204     set(${test_name}_REPORTED ON CACHE INTERNAL "")
205   endif()
206   add_benchmark_test(${test_name} ${test_file})
207 endforeach()
208
209 if (LIBCXX_INCLUDE_TESTS)
210   include(AddLLVM)
211
212   if (NOT DEFINED LIBCXX_TEST_DEPS)
213     message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined")
214   endif()
215
216   configure_lit_site_cfg(
217           ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
218           ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py)
219
220   set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}")
221
222   add_lit_target(check-cxx-benchmarks
223           "Running libcxx benchmarks tests"
224           ${CMAKE_CURRENT_BINARY_DIR}
225           DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS}
226           ARGS ${BENCHMARK_LIT_ARGS})
227 endif()
228