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() 21if (LIBCXX_NEEDS_SITE_CONFIG) 22 list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS -include "${LIBCXX_BINARY_DIR}/__config_site") 23endif() 24split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS) 25 26ExternalProject_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#============================================================================== 44set(BENCHMARK_NATIVE_TARGET_FLAGS) 45if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN) 46 set(BENCHMARK_NATIVE_TARGET_FLAGS 47 -gcc-toolchain ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}) 48endif() 49split_list(BENCHMARK_NATIVE_TARGET_FLAGS) 50 51if (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) 64endif() 65 66 67#============================================================================== 68# Benchmark tests configuration 69#============================================================================== 70add_custom_target(cxx-benchmarks) 71set(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) 72set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx) 73set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native) 74 75check_flag_supported("-std=c++17") 76mangle_name("LIBCXX_SUPPORTS_STD_EQ_c++17_FLAG" BENCHMARK_SUPPORTS_STD_CXX17_FLAG) 77if (${BENCHMARK_SUPPORTS_STD_CXX17_FLAG}) 78 set(BENCHMARK_DIALECT_FLAG "-std=c++17") 79else() 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") 83endif() 84 85set(BENCHMARK_TEST_COMPILE_FLAGS 86 ${BENCHMARK_DIALECT_FLAG} -O2 87 -I${BENCHMARK_LIBCXX_INSTALL}/include 88 -I${LIBCXX_SOURCE_DIR}/test/support 89) 90set(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS 91 -nostdinc++ 92 -isystem ${LIBCXX_SOURCE_DIR}/include 93 ${BENCHMARK_TEST_COMPILE_FLAGS} 94 ${SANITIZER_FLAGS} 95 -Wno-user-defined-literals 96) 97if (LIBCXX_NEEDS_SITE_CONFIG) 98 list(APPEND BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS 99 -include "${LIBCXX_BINARY_DIR}/__config_site") 100endif() 101 102set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS 103 -nodefaultlibs 104 -L${BENCHMARK_LIBCXX_INSTALL}/lib/ 105 ${SANITIZER_FLAGS} 106) 107set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS 108 ${BENCHMARK_NATIVE_TARGET_FLAGS} 109 ${BENCHMARK_TEST_COMPILE_FLAGS} 110) 111set(BENCHMARK_TEST_NATIVE_LINK_FLAGS 112 ${BENCHMARK_NATIVE_TARGET_FLAGS} 113 -L${BENCHMARK_NATIVE_INSTALL}/lib 114) 115split_list(BENCHMARK_TEST_COMPILE_FLAGS) 116split_list(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS) 117split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS) 118split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS) 119split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS) 120 121if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++") 122 find_library(LIBSTDCXX_FILESYSTEM_TEST stdc++fs 123 PATHS ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN} 124 PATH_SUFFIXES lib lib64 125 DOC "The libstdc++ filesystem library used by the benchmarks" 126 ) 127 if (NOT "${LIBSTDCXX_FILESYSTEM_TEST}" STREQUAL "LIBSTDCXX_FILESYSTEM_TEST-NOTFOUND") 128 set(LIBSTDCXX_FILESYSTEM_LIB "stdc++fs") 129 endif() 130endif() 131 132set(libcxx_benchmark_targets) 133 134function(add_benchmark_test name source_file) 135 set(libcxx_target ${name}_libcxx) 136 list(APPEND libcxx_benchmark_targets ${libcxx_target}) 137 add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file}) 138 add_dependencies(${libcxx_target} cxx cxx-headers google-benchmark-libcxx) 139 add_dependencies(cxx-benchmarks ${libcxx_target}) 140 if (LIBCXX_ENABLE_SHARED) 141 target_link_libraries(${libcxx_target} cxx_shared) 142 else() 143 target_link_libraries(${libcxx_target} cxx_static) 144 endif() 145 if (TARGET cxx_experimental) 146 target_link_libraries(${libcxx_target} cxx_experimental) 147 endif() 148 if (TARGET cxx_filesystem) 149 target_link_libraries(${libcxx_target} cxx_filesystem) 150 endif() 151 target_link_libraries(${libcxx_target} -lbenchmark) 152 if (LLVM_USE_SANITIZER) 153 target_link_libraries(${libcxx_target} -ldl) 154 endif() 155 set_target_properties(${libcxx_target} 156 PROPERTIES 157 OUTPUT_NAME "${name}.libcxx.out" 158 RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}" 159 COMPILE_FLAGS "${BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS}" 160 LINK_FLAGS "${BENCHMARK_TEST_LIBCXX_LINK_FLAGS}") 161 if (LIBCXX_BENCHMARK_NATIVE_STDLIB) 162 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++" AND NOT DEFINED LIBSTDCXX_FILESYSTEM_LIB 163 AND "${name}" STREQUAL "filesystem") 164 return() 165 endif() 166 set(native_target ${name}_native) 167 add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file}) 168 add_dependencies(${native_target} google-benchmark-native 169 google-benchmark-libcxx) 170 target_link_libraries(${native_target} -lbenchmark) 171 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++") 172 target_link_libraries(${native_target} ${LIBSTDCXX_FILESYSTEM_LIB}) 173 elseif (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++") 174 target_link_libraries(${native_target} -lc++fs -lc++experimental) 175 endif() 176 if (LIBCXX_HAS_PTHREAD_LIB) 177 target_link_libraries(${native_target} -pthread) 178 endif() 179 add_dependencies(cxx-benchmarks ${native_target}) 180 set_target_properties(${native_target} 181 PROPERTIES 182 OUTPUT_NAME "${name}.native.out" 183 RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}" 184 INCLUDE_DIRECTORIES "" 185 COMPILE_FLAGS "${BENCHMARK_TEST_NATIVE_COMPILE_FLAGS}" 186 LINK_FLAGS "${BENCHMARK_TEST_NATIVE_LINK_FLAGS}") 187 endif() 188endfunction() 189 190 191#============================================================================== 192# Register Benchmark tests 193#============================================================================== 194file(GLOB BENCHMARK_TESTS "*.bench.cpp") 195foreach(test_path ${BENCHMARK_TESTS}) 196 get_filename_component(test_file "${test_path}" NAME) 197 string(REPLACE ".bench.cpp" "" test_name "${test_file}") 198 if (NOT DEFINED ${test_name}_REPORTED) 199 message(STATUS "Adding Benchmark: ${test_file}") 200 # Only report the adding of the benchmark once. 201 set(${test_name}_REPORTED ON CACHE INTERNAL "") 202 endif() 203 add_benchmark_test(${test_name} ${test_file}) 204endforeach() 205 206if (LIBCXX_INCLUDE_TESTS) 207 include(AddLLVM) 208 209 if (NOT DEFINED LIBCXX_TEST_DEPS) 210 message(FATAL_ERROR "Expected LIBCXX_TEST_DEPS to be defined") 211 endif() 212 213 configure_lit_site_cfg( 214 ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in 215 ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py) 216 217 set(BENCHMARK_LIT_ARGS "--show-all --show-xfail --show-unsupported ${LIT_ARGS_DEFAULT}") 218 219 add_lit_target(check-cxx-benchmarks 220 "Running libcxx benchmarks tests" 221 ${CMAKE_CURRENT_BINARY_DIR} 222 DEPENDS cxx-benchmarks ${LIBCXX_TEST_DEPS} 223 ARGS ${BENCHMARK_LIT_ARGS}) 224endif() 225