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 ) 15if (DEFINED LIBCXX_CXX_ABI_LIBRARY_PATH) 16 list(APPEND BENCHMARK_LIBCXX_COMPILE_FLAGS 17 -L${LIBCXX_CXX_ABI_LIBRARY_PATH} 18 -Wl,-rpath,${LIBCXX_CXX_ABI_LIBRARY_PATH}) 19endif() 20split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS) 21 22ExternalProject_Add(google-benchmark-libcxx 23 EXCLUDE_FROM_ALL ON 24 DEPENDS cxx 25 PREFIX benchmark-libcxx 26 SOURCE_DIR ${LIBCXX_SOURCE_DIR}/utils/google-benchmark 27 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx 28 CMAKE_CACHE_ARGS 29 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} 30 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} 31 -DCMAKE_BUILD_TYPE:STRING=RELEASE 32 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> 33 -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_LIBCXX_COMPILE_FLAGS} 34 -DBENCHMARK_USE_LIBCXX:BOOL=ON 35 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF) 36 37#============================================================================== 38# Build Google Benchmark for the native stdlib 39#============================================================================== 40set(BENCHMARK_NATIVE_TARGET_FLAGS) 41if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN) 42 set(BENCHMARK_NATIVE_TARGET_FLAGS 43 -gcc-toolchain ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN}) 44endif() 45split_list(BENCHMARK_NATIVE_TARGET_FLAGS) 46 47if (LIBCXX_BENCHMARK_NATIVE_STDLIB) 48 ExternalProject_Add(google-benchmark-native 49 EXCLUDE_FROM_ALL ON 50 PREFIX benchmark-native 51 SOURCE_DIR ${LIBCXX_SOURCE_DIR}/utils/google-benchmark 52 INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native 53 CMAKE_CACHE_ARGS 54 -DCMAKE_C_COMPILER:STRING=${CMAKE_C_COMPILER} 55 -DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER} 56 -DCMAKE_CXX_FLAGS:STRING=${BENCHMARK_NATIVE_TARGET_FLAGS} 57 -DCMAKE_BUILD_TYPE:STRING=RELEASE 58 -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> 59 -DBENCHMARK_ENABLE_TESTING:BOOL=OFF) 60endif() 61 62#============================================================================== 63# Benchmark tests configuration 64#============================================================================== 65add_custom_target(cxx-benchmarks) 66 67set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx) 68set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native) 69set(BENCHMARK_TEST_COMPILE_FLAGS 70 -std=c++14 -O2 71 -I${BENCHMARK_LIBCXX_INSTALL}/include 72 -I${LIBCXX_SOURCE_DIR}/test/support 73) 74set(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS 75 -nostdinc++ 76 -isystem ${LIBCXX_SOURCE_DIR}/include 77 ${BENCHMARK_TEST_COMPILE_FLAGS} 78 -Wno-user-defined-literals 79) 80set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS 81 -nodefaultlibs 82 -L${BENCHMARK_LIBCXX_INSTALL}/lib/ 83) 84set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS 85 ${BENCHMARK_NATIVE_TARGET_FLAGS} 86 ${BENCHMARK_TEST_COMPILE_FLAGS} 87) 88set(BENCHMARK_TEST_NATIVE_LINK_FLAGS 89 ${BENCHMARK_NATIVE_TARGET_FLAGS} 90 -L${BENCHMARK_NATIVE_INSTALL}/lib 91) 92split_list(BENCHMARK_TEST_COMPILE_FLAGS) 93split_list(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS) 94split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS) 95split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS) 96split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS) 97macro(add_benchmark_test name source_file) 98 set(libcxx_target ${name}_libcxx) 99 add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file}) 100 add_dependencies(${libcxx_target} cxx google-benchmark-libcxx) 101 add_dependencies(cxx-benchmarks ${libcxx_target}) 102 if (LIBCXX_ENABLE_SHARED) 103 target_link_libraries(${libcxx_target} cxx_shared) 104 else() 105 target_link_libraries(${libcxx_target} cxx_static) 106 endif() 107 if (TARGET cxx_experimental) 108 target_link_libraries(${libcxx_target} cxx_experimental) 109 endif() 110 target_link_libraries(${libcxx_target} -lbenchmark) 111 set_target_properties(${libcxx_target} 112 PROPERTIES 113 OUTPUT_NAME "${name}.libcxx.out" 114 COMPILE_FLAGS "${BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS}" 115 LINK_FLAGS "${BENCHMARK_TEST_LIBCXX_LINK_FLAGS}") 116 if (LIBCXX_BENCHMARK_NATIVE_STDLIB) 117 set(native_target ${name}_native) 118 add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file}) 119 add_dependencies(${native_target} google-benchmark-native 120 google-benchmark-libcxx) 121 target_link_libraries(${native_target} -lbenchmark) 122 if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++") 123 target_link_libraries(${native_target} -lstdc++fs) 124 elseif (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++") 125 target_link_libraries(${native_target} -lc++experimental) 126 endif() 127 if (LIBCXX_HAS_PTHREAD_LIB) 128 target_link_libraries(${native_target} -pthread) 129 endif() 130 add_dependencies(cxx-benchmarks ${native_target}) 131 set_target_properties(${native_target} 132 PROPERTIES 133 OUTPUT_NAME "${name}.native.out" 134 INCLUDE_DIRECTORIES "" 135 COMPILE_FLAGS "${BENCHMARK_TEST_NATIVE_COMPILE_FLAGS}" 136 LINK_FLAGS "${BENCHMARK_TEST_NATIVE_LINK_FLAGS}") 137 endif() 138endmacro() 139 140 141#============================================================================== 142# Register Benchmark tests 143#============================================================================== 144file(GLOB BENCHMARK_TESTS "*.bench.cpp") 145foreach(test_path ${BENCHMARK_TESTS}) 146 get_filename_component(test_file "${test_path}" NAME) 147 string(REPLACE ".bench.cpp" "" test_name "${test_file}") 148 if (NOT DEFINED ${test_name}_REPORTED) 149 message(STATUS "Adding Benchmark: ${test_file}") 150 # Only report the adding of the benchmark once. 151 set(${test_name}_REPORTED ON CACHE INTERNAL "") 152 endif() 153 add_benchmark_test(${test_name} ${test_file}) 154endforeach() 155