Skip to content

Instantly share code, notes, and snippets.

@jpcofr
Created August 11, 2023 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpcofr/ea690052eeabfca30b26d66954881082 to your computer and use it in GitHub Desktop.
Save jpcofr/ea690052eeabfca30b26d66954881082 to your computer and use it in GitHub Desktop.
Build config for a very simple C/C++ project with tests and benchmark support
# Description: Build configuration for a simple project ready for benchmarking and testing.
# New source files can be added on demand as the build system recurses the repository.
# Compatibility: macOS 13.3
# Dependencies: gtest, Google Benchmark
cmake_minimum_required(VERSION 3.25)
set(CMAKE_CXX_STANDARD 17)
project(challenge_project)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Find ccache
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "${CCACHE_PROGRAM}")
endif()
## Configure testing
include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)
# Download and configure googletest
FetchContent_Declare(
gtest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.13.0
GIT_SHALLOW ON
)
FetchContent_GetProperties(gtest)
if(NOT gtest_POPULATED)
FetchContent_Populate(gtest)
add_subdirectory(${gtest_SOURCE_DIR} ${CMAKE_BINARY_DIR}/_deps/gtest-build)
endif()
# Download and configure google benchmark
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.7.1
GIT_SHALLOW ON
)
FetchContent_GetProperties(benchmark)
if(NOT benchmark_POPULATED)
FetchContent_Populate(benchmark)
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
add_subdirectory(${benchmark_SOURCE_DIR} ${CMAKE_BINARY_DIR}/_deps/benchmark-build)
endif()
include_directories(${gtest_SOURCE_DIR}/googletest/include)
include_directories(${benchmark_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/include)
## Configure executables
add_executable(test main.cpp)
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp")
target_sources(test PRIVATE ${SOURCE_FILES})
target_link_libraries(test gtest gtest_main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment