Skip to content

Commit

Permalink
Use FetchContent instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Flast committed May 30, 2024
1 parent efed455 commit da21e55
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.11)

project(flat_map LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
Expand Down
6 changes: 3 additions & 3 deletions bench/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
include(${PROJECT_SOURCE_DIR}/cmake/benchmark.cmake)

add_custom_target(benchmark)
set_target_properties(benchmark PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
add_custom_target(bench)
set_target_properties(bench PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)

macro(add_bench benchname)
add_benchmark(${benchname} ${ARGN})
set_target_properties(${benchname} PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
target_link_libraries(${benchname} flat_map)
add_dependencies(benchmark ${benchname})
add_dependencies(bench ${benchname})
endmacro()

add_bench(map_construction map_construction.cpp)
Expand Down
26 changes: 8 additions & 18 deletions cmake/benchmark.cmake
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
find_package(benchmark QUIET)

if(benchmark_FOUND)
add_library(benchmarkLib ALIAS benchmark::benchmark)
add_library(benchmarkInc ALIAS benchmark::benchmark)
else()
include(ExternalProject)
if(NOT benchmark_FOUND)
include(FetchContent)

ExternalProject_Add(
benchmarkBuild
set(BENCHMARK_ENABLE_TESTING Off)
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG v1.5.5
PREFIX external
CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/external
-DCMAKE_BUILD_TYPE=Release
-DBENCHMARK_ENABLE_TESTING=Off
GIT_TAG v1.8.4
)

add_library(benchmarkLib STATIC IMPORTED)
set_target_properties(benchmarkLib PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/external/lib/libbenchmark.a)
add_library(benchmarkInc INTERFACE)
target_include_directories(benchmarkInc INTERFACE ${CMAKE_BINARY_DIR}/external/include)
add_dependencies(benchmarkInc benchmarkBuild)
FetchContent_MakeAvailable(benchmark)
endif()

function(add_benchmark benchname)
add_executable(${benchname} ${ARGN})
target_link_libraries(${benchname} benchmarkInc benchmarkLib)
target_link_libraries(${benchname} benchmark::benchmark)
endfunction()

0 comments on commit da21e55

Please sign in to comment.