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     )
     15 if (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})
     19 endif()
     20 split_list(BENCHMARK_LIBCXX_COMPILE_FLAGS)
     21 
     22 ExternalProject_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 #==============================================================================
     40 set(BENCHMARK_NATIVE_TARGET_FLAGS)
     41 if (LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN)
     42   set(BENCHMARK_NATIVE_TARGET_FLAGS
     43       -gcc-toolchain ${LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN})
     44 endif()
     45 split_list(BENCHMARK_NATIVE_TARGET_FLAGS)
     46 
     47 if (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)
     60 endif()
     61 
     62 
     63 #==============================================================================
     64 # Benchmark tests configuration
     65 #==============================================================================
     66 add_custom_target(cxx-benchmarks)
     67 set(BENCHMARK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
     68 set(BENCHMARK_LIBCXX_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-libcxx)
     69 set(BENCHMARK_NATIVE_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/benchmark-native)
     70 set(BENCHMARK_TEST_COMPILE_FLAGS
     71     -std=c++14 -O2
     72     -I${BENCHMARK_LIBCXX_INSTALL}/include
     73     -I${LIBCXX_SOURCE_DIR}/test/support
     74 )
     75 set(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS
     76     -nostdinc++
     77     -isystem ${LIBCXX_SOURCE_DIR}/include
     78     ${BENCHMARK_TEST_COMPILE_FLAGS}
     79     -Wno-user-defined-literals
     80 )
     81 set(BENCHMARK_TEST_LIBCXX_LINK_FLAGS
     82     -nodefaultlibs
     83     -L${BENCHMARK_LIBCXX_INSTALL}/lib/
     84 )
     85 set(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS
     86   ${BENCHMARK_NATIVE_TARGET_FLAGS}
     87   ${BENCHMARK_TEST_COMPILE_FLAGS}
     88 )
     89 set(BENCHMARK_TEST_NATIVE_LINK_FLAGS
     90     ${BENCHMARK_NATIVE_TARGET_FLAGS}
     91     -L${BENCHMARK_NATIVE_INSTALL}/lib
     92 )
     93 split_list(BENCHMARK_TEST_COMPILE_FLAGS)
     94 split_list(BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS)
     95 split_list(BENCHMARK_TEST_LIBCXX_LINK_FLAGS)
     96 split_list(BENCHMARK_TEST_NATIVE_COMPILE_FLAGS)
     97 split_list(BENCHMARK_TEST_NATIVE_LINK_FLAGS)
     98 macro(add_benchmark_test name source_file)
     99   set(libcxx_target ${name}_libcxx)
    100   add_executable(${libcxx_target} EXCLUDE_FROM_ALL ${source_file})
    101   add_dependencies(${libcxx_target} cxx google-benchmark-libcxx)
    102   add_dependencies(cxx-benchmarks ${libcxx_target})
    103   if (LIBCXX_ENABLE_SHARED)
    104     target_link_libraries(${libcxx_target} cxx_shared)
    105   else()
    106     target_link_libraries(${libcxx_target} cxx_static)
    107   endif()
    108   if (TARGET cxx_experimental)
    109     target_link_libraries(${libcxx_target} cxx_experimental)
    110   endif()
    111   target_link_libraries(${libcxx_target} -lbenchmark)
    112   set_target_properties(${libcxx_target}
    113     PROPERTIES
    114           OUTPUT_NAME "${name}.libcxx.out"
    115           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
    116           COMPILE_FLAGS "${BENCHMARK_TEST_LIBCXX_COMPILE_FLAGS}"
    117           LINK_FLAGS "${BENCHMARK_TEST_LIBCXX_LINK_FLAGS}")
    118   if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
    119     set(native_target ${name}_native)
    120     add_executable(${native_target} EXCLUDE_FROM_ALL ${source_file})
    121     add_dependencies(${native_target} google-benchmark-native
    122                                       google-benchmark-libcxx)
    123     target_link_libraries(${native_target} -lbenchmark)
    124     if (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")
    125       target_link_libraries(${native_target} -lstdc++fs)
    126     elseif (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++")
    127       target_link_libraries(${native_target} -lc++experimental)
    128     endif()
    129     if (LIBCXX_HAS_PTHREAD_LIB)
    130       target_link_libraries(${native_target} -pthread)
    131     endif()
    132     add_dependencies(cxx-benchmarks ${native_target})
    133     set_target_properties(${native_target}
    134       PROPERTIES
    135           OUTPUT_NAME "${name}.native.out"
    136           RUNTIME_OUTPUT_DIRECTORY "${BENCHMARK_OUTPUT_DIR}"
    137           INCLUDE_DIRECTORIES ""
    138           COMPILE_FLAGS "${BENCHMARK_TEST_NATIVE_COMPILE_FLAGS}"
    139           LINK_FLAGS "${BENCHMARK_TEST_NATIVE_LINK_FLAGS}")
    140   endif()
    141 endmacro()
    142 
    143 
    144 #==============================================================================
    145 # Register Benchmark tests
    146 #==============================================================================
    147 file(GLOB BENCHMARK_TESTS "*.bench.cpp")
    148 foreach(test_path ${BENCHMARK_TESTS})
    149   get_filename_component(test_file "${test_path}" NAME)
    150   string(REPLACE ".bench.cpp" "" test_name "${test_file}")
    151   if (NOT DEFINED ${test_name}_REPORTED)
    152     message(STATUS "Adding Benchmark: ${test_file}")
    153     # Only report the adding of the benchmark once.
    154     set(${test_name}_REPORTED ON CACHE INTERNAL "")
    155   endif()
    156   add_benchmark_test(${test_name} ${test_file})
    157 endforeach()
    158