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