You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
12 KiB

2 months ago
  1. # Enable the tests
  2. set(THREADS_PREFER_PTHREAD_FLAG ON)
  3. find_package(Threads REQUIRED)
  4. include(CheckCXXCompilerFlag)
  5. # NOTE: Some tests use `<cassert>` to perform the test. Therefore we must
  6. # strip -DNDEBUG from the default CMake flags in DEBUG mode.
  7. string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
  8. if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
  9. add_definitions( -UNDEBUG )
  10. add_definitions(-DTEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS)
  11. # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
  12. foreach (flags_var_to_scrub
  13. CMAKE_CXX_FLAGS_RELEASE
  14. CMAKE_CXX_FLAGS_RELWITHDEBINFO
  15. CMAKE_CXX_FLAGS_MINSIZEREL
  16. CMAKE_C_FLAGS_RELEASE
  17. CMAKE_C_FLAGS_RELWITHDEBINFO
  18. CMAKE_C_FLAGS_MINSIZEREL)
  19. string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
  20. "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
  21. endforeach()
  22. endif()
  23. if (NOT BUILD_SHARED_LIBS)
  24. add_definitions(-DBENCHMARK_STATIC_DEFINE)
  25. endif()
  26. check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
  27. set(BENCHMARK_O3_FLAG "")
  28. if (BENCHMARK_HAS_O3_FLAG)
  29. set(BENCHMARK_O3_FLAG "-O3")
  30. endif()
  31. # NOTE: These flags must be added after find_package(Threads REQUIRED) otherwise
  32. # they will break the configuration check.
  33. if (DEFINED BENCHMARK_CXX_LINKER_FLAGS)
  34. list(APPEND CMAKE_EXE_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS})
  35. endif()
  36. add_library(output_test_helper STATIC output_test_helper.cc output_test.h)
  37. target_link_libraries(output_test_helper PRIVATE benchmark::benchmark)
  38. macro(compile_benchmark_test name)
  39. add_executable(${name} "${name}.cc")
  40. target_link_libraries(${name} benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT})
  41. if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "NVHPC")
  42. target_compile_options( ${name} PRIVATE --diag_suppress partial_override )
  43. endif()
  44. endmacro(compile_benchmark_test)
  45. macro(compile_benchmark_test_with_main name)
  46. add_executable(${name} "${name}.cc")
  47. target_link_libraries(${name} benchmark::benchmark_main)
  48. endmacro(compile_benchmark_test_with_main)
  49. macro(compile_output_test name)
  50. add_executable(${name} "${name}.cc" output_test.h)
  51. target_link_libraries(${name} output_test_helper benchmark::benchmark_main
  52. ${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
  53. endmacro(compile_output_test)
  54. # Demonstration executable
  55. compile_benchmark_test(benchmark_test)
  56. add_test(NAME benchmark COMMAND benchmark_test --benchmark_min_time=0.01s)
  57. compile_benchmark_test(spec_arg_test)
  58. add_test(NAME spec_arg COMMAND spec_arg_test --benchmark_filter=BM_NotChosen)
  59. compile_benchmark_test(spec_arg_verbosity_test)
  60. add_test(NAME spec_arg_verbosity COMMAND spec_arg_verbosity_test --v=42)
  61. compile_benchmark_test(benchmark_setup_teardown_test)
  62. add_test(NAME benchmark_setup_teardown COMMAND benchmark_setup_teardown_test)
  63. compile_benchmark_test(filter_test)
  64. macro(add_filter_test name filter expect)
  65. add_test(NAME ${name} COMMAND filter_test --benchmark_min_time=0.01s --benchmark_filter=${filter} ${expect})
  66. add_test(NAME ${name}_list_only COMMAND filter_test --benchmark_list_tests --benchmark_filter=${filter} ${expect})
  67. endmacro(add_filter_test)
  68. compile_benchmark_test(benchmark_min_time_flag_time_test)
  69. add_test(NAME min_time_flag_time COMMAND benchmark_min_time_flag_time_test)
  70. compile_benchmark_test(benchmark_min_time_flag_iters_test)
  71. add_test(NAME min_time_flag_iters COMMAND benchmark_min_time_flag_iters_test)
  72. add_filter_test(filter_simple "Foo" 3)
  73. add_filter_test(filter_simple_negative "-Foo" 2)
  74. add_filter_test(filter_suffix "BM_.*" 4)
  75. add_filter_test(filter_suffix_negative "-BM_.*" 1)
  76. add_filter_test(filter_regex_all ".*" 5)
  77. add_filter_test(filter_regex_all_negative "-.*" 0)
  78. add_filter_test(filter_regex_blank "" 5)
  79. add_filter_test(filter_regex_blank_negative "-" 0)
  80. add_filter_test(filter_regex_none "monkey" 0)
  81. add_filter_test(filter_regex_none_negative "-monkey" 5)
  82. add_filter_test(filter_regex_wildcard ".*Foo.*" 3)
  83. add_filter_test(filter_regex_wildcard_negative "-.*Foo.*" 2)
  84. add_filter_test(filter_regex_begin "^BM_.*" 4)
  85. add_filter_test(filter_regex_begin_negative "-^BM_.*" 1)
  86. add_filter_test(filter_regex_begin2 "^N" 1)
  87. add_filter_test(filter_regex_begin2_negative "-^N" 4)
  88. add_filter_test(filter_regex_end ".*Ba$" 1)
  89. add_filter_test(filter_regex_end_negative "-.*Ba$" 4)
  90. compile_benchmark_test(options_test)
  91. add_test(NAME options_benchmarks COMMAND options_test --benchmark_min_time=0.01s)
  92. compile_benchmark_test(basic_test)
  93. add_test(NAME basic_benchmark COMMAND basic_test --benchmark_min_time=0.01s)
  94. compile_output_test(repetitions_test)
  95. add_test(NAME repetitions_benchmark COMMAND repetitions_test --benchmark_min_time=0.01s --benchmark_repetitions=3)
  96. compile_benchmark_test(diagnostics_test)
  97. add_test(NAME diagnostics_test COMMAND diagnostics_test --benchmark_min_time=0.01s)
  98. compile_benchmark_test(skip_with_error_test)
  99. add_test(NAME skip_with_error_test COMMAND skip_with_error_test --benchmark_min_time=0.01s)
  100. compile_benchmark_test(donotoptimize_test)
  101. # Some of the issues with DoNotOptimize only occur when optimization is enabled
  102. check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
  103. if (BENCHMARK_HAS_O3_FLAG)
  104. set_target_properties(donotoptimize_test PROPERTIES COMPILE_FLAGS "-O3")
  105. endif()
  106. add_test(NAME donotoptimize_test COMMAND donotoptimize_test --benchmark_min_time=0.01s)
  107. compile_benchmark_test(fixture_test)
  108. add_test(NAME fixture_test COMMAND fixture_test --benchmark_min_time=0.01s)
  109. compile_benchmark_test(register_benchmark_test)
  110. add_test(NAME register_benchmark_test COMMAND register_benchmark_test --benchmark_min_time=0.01s)
  111. compile_benchmark_test(map_test)
  112. add_test(NAME map_test COMMAND map_test --benchmark_min_time=0.01s)
  113. compile_benchmark_test(multiple_ranges_test)
  114. add_test(NAME multiple_ranges_test COMMAND multiple_ranges_test --benchmark_min_time=0.01s)
  115. compile_benchmark_test(args_product_test)
  116. add_test(NAME args_product_test COMMAND args_product_test --benchmark_min_time=0.01s)
  117. compile_benchmark_test_with_main(link_main_test)
  118. add_test(NAME link_main_test COMMAND link_main_test --benchmark_min_time=0.01s)
  119. compile_output_test(reporter_output_test)
  120. add_test(NAME reporter_output_test COMMAND reporter_output_test --benchmark_min_time=0.01s)
  121. compile_output_test(templated_fixture_test)
  122. add_test(NAME templated_fixture_test COMMAND templated_fixture_test --benchmark_min_time=0.01s)
  123. compile_output_test(user_counters_test)
  124. add_test(NAME user_counters_test COMMAND user_counters_test --benchmark_min_time=0.01s)
  125. compile_output_test(perf_counters_test)
  126. add_test(NAME perf_counters_test COMMAND perf_counters_test --benchmark_min_time=0.01s --benchmark_perf_counters=CYCLES,BRANCHES)
  127. compile_output_test(internal_threading_test)
  128. add_test(NAME internal_threading_test COMMAND internal_threading_test --benchmark_min_time=0.01s)
  129. compile_output_test(report_aggregates_only_test)
  130. add_test(NAME report_aggregates_only_test COMMAND report_aggregates_only_test --benchmark_min_time=0.01s)
  131. compile_output_test(display_aggregates_only_test)
  132. add_test(NAME display_aggregates_only_test COMMAND display_aggregates_only_test --benchmark_min_time=0.01s)
  133. compile_output_test(user_counters_tabular_test)
  134. add_test(NAME user_counters_tabular_test COMMAND user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01s)
  135. compile_output_test(user_counters_thousands_test)
  136. add_test(NAME user_counters_thousands_test COMMAND user_counters_thousands_test --benchmark_min_time=0.01s)
  137. compile_output_test(memory_manager_test)
  138. add_test(NAME memory_manager_test COMMAND memory_manager_test --benchmark_min_time=0.01s)
  139. # MSVC does not allow to set the language standard to C++98/03.
  140. if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
  141. compile_benchmark_test(cxx03_test)
  142. set_target_properties(cxx03_test
  143. PROPERTIES
  144. CXX_STANDARD 98
  145. CXX_STANDARD_REQUIRED YES)
  146. # libstdc++ provides different definitions within <map> between dialects. When
  147. # LTO is enabled and -Werror is specified GCC diagnoses this ODR violation
  148. # causing the test to fail to compile. To prevent this we explicitly disable
  149. # the warning.
  150. check_cxx_compiler_flag(-Wno-odr BENCHMARK_HAS_WNO_ODR)
  151. check_cxx_compiler_flag(-Wno-lto-type-mismatch BENCHMARK_HAS_WNO_LTO_TYPE_MISMATCH)
  152. # Cannot set_target_properties multiple times here because the warnings will
  153. # be overwritten on each call
  154. set (DISABLE_LTO_WARNINGS "")
  155. if (BENCHMARK_HAS_WNO_ODR)
  156. set(DISABLE_LTO_WARNINGS "${DISABLE_LTO_WARNINGS} -Wno-odr")
  157. endif()
  158. if (BENCHMARK_HAS_WNO_LTO_TYPE_MISMATCH)
  159. set(DISABLE_LTO_WARNINGS "${DISABLE_LTO_WARNINGS} -Wno-lto-type-mismatch")
  160. endif()
  161. set_target_properties(cxx03_test PROPERTIES LINK_FLAGS "${DISABLE_LTO_WARNINGS}")
  162. add_test(NAME cxx03 COMMAND cxx03_test --benchmark_min_time=0.01s)
  163. endif()
  164. # Attempt to work around flaky test failures when running on Appveyor servers.
  165. if (DEFINED ENV{APPVEYOR})
  166. set(COMPLEXITY_MIN_TIME "0.5s")
  167. else()
  168. set(COMPLEXITY_MIN_TIME "0.01s")
  169. endif()
  170. compile_output_test(complexity_test)
  171. add_test(NAME complexity_benchmark COMMAND complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME})
  172. ###############################################################################
  173. # GoogleTest Unit Tests
  174. ###############################################################################
  175. if (BENCHMARK_ENABLE_GTEST_TESTS)
  176. macro(compile_gtest name)
  177. add_executable(${name} "${name}.cc")
  178. target_link_libraries(${name} benchmark::benchmark
  179. gmock_main ${CMAKE_THREAD_LIBS_INIT})
  180. endmacro(compile_gtest)
  181. macro(add_gtest name)
  182. compile_gtest(${name})
  183. add_test(NAME ${name} COMMAND ${name})
  184. endmacro()
  185. add_gtest(benchmark_gtest)
  186. add_gtest(benchmark_name_gtest)
  187. add_gtest(benchmark_random_interleaving_gtest)
  188. add_gtest(commandlineflags_gtest)
  189. add_gtest(statistics_gtest)
  190. add_gtest(string_util_gtest)
  191. add_gtest(perf_counters_gtest)
  192. add_gtest(time_unit_gtest)
  193. add_gtest(min_time_parse_gtest)
  194. endif(BENCHMARK_ENABLE_GTEST_TESTS)
  195. ###############################################################################
  196. # Assembly Unit Tests
  197. ###############################################################################
  198. if (BENCHMARK_ENABLE_ASSEMBLY_TESTS)
  199. if (NOT LLVM_FILECHECK_EXE)
  200. message(FATAL_ERROR "LLVM FileCheck is required when including this file")
  201. endif()
  202. include(AssemblyTests.cmake)
  203. add_filecheck_test(donotoptimize_assembly_test)
  204. add_filecheck_test(state_assembly_test)
  205. add_filecheck_test(clobber_memory_assembly_test)
  206. endif()
  207. ###############################################################################
  208. # Code Coverage Configuration
  209. ###############################################################################
  210. # Add the coverage command(s)
  211. if(CMAKE_BUILD_TYPE)
  212. string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
  213. endif()
  214. if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
  215. find_program(GCOV gcov)
  216. find_program(LCOV lcov)
  217. find_program(GENHTML genhtml)
  218. find_program(CTEST ctest)
  219. if (GCOV AND LCOV AND GENHTML AND CTEST AND HAVE_CXX_FLAG_COVERAGE)
  220. add_custom_command(
  221. OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html
  222. COMMAND ${LCOV} -q -z -d .
  223. COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i
  224. COMMAND ${CTEST} --force-new-ctest-process
  225. COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov
  226. COMMAND ${LCOV} -q -a before.lcov -a after.lcov --output-file final.lcov
  227. COMMAND ${LCOV} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov
  228. COMMAND ${GENHTML} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_BINARY_DIR}" -t benchmark
  229. DEPENDS filter_test benchmark_test options_test basic_test fixture_test cxx03_test complexity_test
  230. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  231. COMMENT "Running LCOV"
  232. )
  233. add_custom_target(coverage
  234. DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html
  235. COMMENT "LCOV report at lcov/index.html"
  236. )
  237. message(STATUS "Coverage command added")
  238. else()
  239. if (HAVE_CXX_FLAG_COVERAGE)
  240. set(CXX_FLAG_COVERAGE_MESSAGE supported)
  241. else()
  242. set(CXX_FLAG_COVERAGE_MESSAGE unavailable)
  243. endif()
  244. message(WARNING
  245. "Coverage not available:\n"
  246. " gcov: ${GCOV}\n"
  247. " lcov: ${LCOV}\n"
  248. " genhtml: ${GENHTML}\n"
  249. " ctest: ${CTEST}\n"
  250. " --coverage flag: ${CXX_FLAG_COVERAGE_MESSAGE}")
  251. endif()
  252. endif()