|
7 | 7 |
|
8 | 8 | cmake_minimum_required (VERSION 3.16) |
9 | 9 |
|
10 | | -# Exposes the google-benchmark target `gbenchmark`. |
| 10 | +# Exposes the google-benchmark target `benchmark` and `benchmark_main`. |
| 11 | +# CMake 3.24: https://cmake.org/cmake/help/latest/module/FetchContent.html#variable:FETCHCONTENT_TRY_FIND_PACKAGE_MODE |
11 | 12 | macro (sharg_require_benchmark) |
12 | 13 | enable_testing () |
13 | 14 |
|
14 | | - set (gbenchmark_git_tag "v1.7.0") |
| 15 | + set (benchmark_version "1.7.0") |
| 16 | + set (gbenchmark_git_tag "v${benchmark_version}") |
15 | 17 |
|
16 | | - message (STATUS "Fetch Google Benchmark:") |
| 18 | + find_package (benchmark ${benchmark_version} EXACT QUIET) |
17 | 19 |
|
18 | | - include (FetchContent) |
19 | | - FetchContent_Declare ( |
20 | | - gbenchmark_fetch_content |
21 | | - GIT_REPOSITORY "https://github.com/google/benchmark.git" |
22 | | - GIT_TAG "${gbenchmark_git_tag}") |
23 | | - option (BENCHMARK_ENABLE_TESTING "" OFF) |
24 | | - option (BENCHMARK_ENABLE_WERROR "" OFF) # Does not apply to Debug builds. |
25 | | - FetchContent_MakeAvailable (gbenchmark_fetch_content) |
| 20 | + if (NOT benchmark_FOUND) |
| 21 | + message (STATUS "Fetching Google Benchmark ${benchmark_version}") |
26 | 22 |
|
27 | | - # NOTE: google benchmark's CMakeLists.txt already defines Shlwapi |
28 | | - add_library (gbenchmark ALIAS benchmark_main) |
| 23 | + include (FetchContent) |
| 24 | + FetchContent_Declare ( |
| 25 | + gbenchmark_fetch_content |
| 26 | + GIT_REPOSITORY "https://github.com/google/benchmark.git" |
| 27 | + GIT_TAG "${gbenchmark_git_tag}") |
| 28 | + option (BENCHMARK_ENABLE_TESTING "" OFF) |
| 29 | + option (BENCHMARK_ENABLE_WERROR "" OFF) # Does not apply to Debug builds. |
| 30 | + option (BENCHMARK_ENABLE_INSTALL "" OFF) |
| 31 | + FetchContent_MakeAvailable (gbenchmark_fetch_content) |
| 32 | + else () |
| 33 | + message (STATUS "Found Google Benchmark ${benchmark_version}") |
| 34 | + endif () |
29 | 35 |
|
| 36 | + # NOTE: google benchmark's CMakeLists.txt already defines Shlwapi |
30 | 37 | if (NOT TARGET gbenchmark_build) |
31 | | - add_custom_target (gbenchmark_build DEPENDS gbenchmark) |
32 | | - target_compile_options ("benchmark_main" PUBLIC "-w") |
| 38 | + add_custom_target (gbenchmark_build DEPENDS benchmark_main benchmark) |
33 | 39 | endif () |
| 40 | + |
34 | 41 | endmacro () |
0 commit comments