Home | History | Annotate | Download | only in benchmarks
      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     -I${BENCHMARK_LIBCXX_INSTALL}/include
     88     -I${LIBCXX_SOURCE_DIR}/test/support
     89 )
     90 set(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 )
     97 if (LIBCXX_NEEDS_SITE_CONFIG)
     98   list(APPEND BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS
     99       -include "${LIBCXX_BINARY_DIR}/__config_site")
    100 endif()
    101 
    102 set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS
    103     -nodefaultlibs
    104     -L${BENCHMARK_LIBCXX_INSTALL}/lib/
    105     ${SANITIZER_FLAGS}
    106 )
    107 set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS
    108   ${BENCHMARK_NATIVE_TARGET_FLAGS}
    109   ${BENCHMARK_TEST_COMPILE_FLAGS}
    110 )
    111 set(BENCHMARK_TEST_NATIVE_LINK_FLAGS
    112     ${BENCHMARK_NATIVE_TARGET_FLAGS}
    113     -L${BENCHMARK_NATIVE_INSTALL}/lib
    114 )
    115 split_list(BENCHMARK_TEST_COMPILE_FLAGS)
    116 split_list(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS)
    117 split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS)
    118 split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS)
    119 split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS)
    120 
    121 if (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()
    130 endif()
    131 
    132 set(libcxx_benchmark_targets)
    133 
    134 function(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()
    188 endfunction()
    189 
    190 
    191 #==============================================================================
    192 # Register Benchmark tests
    193 #==============================================================================
    194 file(GLOB BENCHMARK_TESTS "*.bench.cpp")
    195 foreach(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})
    204 endforeach()
    205 
    206 if (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})
    224 endif()
    225