Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions cpp/cmake/benchmark.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,35 @@ endif()
if(BENCHMARKS)
include(FetchContent)

# 🚀 UPGRADE: Updated to v1.8.3 for better compiler support.
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG v1.7.1
GIT_TAG v1.8.3
# 🛡️ SAFETY: 'SYSTEM' suppresses compiler warnings from this 3rd party lib.
# (Requires CMake 3.25+. If older, remove this line).
SYSTEM
FIND_PACKAGE_ARGS
)

set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Benchmark tests off")
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "Benchmark installation off")
# ⚡ CONFIGURATION: Set options BEFORE populating to ensure they are respected.
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Benchmark tests off" FORCE)
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "Benchmark installation off" FORCE)
set(BENCHMARK_DOWNLOAD_DEPENDENCIES ON CACHE BOOL "Download dependencies" FORCE)

FetchContent_MakeAvailable(benchmark)
if(NOT benchmark_FOUND)
# FetchContent_MakeAvailable calls FetchContent_Populate if `find_package` is unsuccessful
# so these variables will be available if we reach this case
set_property(DIRECTORY ${benchmark_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL)
set_property(DIRECTORY ${benchmark_BINARY_DIR} PROPERTY EXCLUDE_FROM_ALL)
# 🧠 SMART PATTERN: "Populate & Add" instead of "MakeAvailable".
# "FetchContent_MakeAvailable" is convenient but lacks granular control over
# arguments passed to 'add_subdirectory' (like EXCLUDE_FROM_ALL) in older CMake.

FetchContent_GetProperties(benchmark)
if(NOT benchmark_POPULATED)
FetchContent_Populate(benchmark)

# 🛡️ ISOLATION: 'EXCLUDE_FROM_ALL' ensures that 'make all' strictly builds
# YOUR project, not the benchmark library objects (unless your tests link to them).
add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

# Note: If you are on CMake 3.28+, you can revert to MakeAvailable:
# FetchContent_MakeAvailable(benchmark EXCLUDE_FROM_ALL)
endif()