Skip to content

Commit

Permalink
Migrate from CMake FetchContent to CMake Package Manager (CPM)
Browse files Browse the repository at this point in the history
  • Loading branch information
perhapsmaple committed Jul 22, 2023
1 parent 0ae7045 commit 583c09f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ if(DEBUG)
)
endif(DEBUG)

# set up CPM.cmake
if(CPM_SOURCE_CACHE)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM.cmake")
else()
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM.cmake")
endif()

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
file(DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake
${CPM_DOWNLOAD_LOCATION}
)
endif()

add_subdirectory(test)
add_subdirectory(benchmark)
add_subdirectory(examples)
Expand Down
35 changes: 13 additions & 22 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,28 @@ option(BENCHMARK "Enable Benchmark" OFF)
option(CMAKE_USE_WIN32_THREADS_INIT "using WIN32 threads" ON)

if(BENCHMARK)
include(${CPM_DOWNLOAD_LOCATION})

include(FetchContent)

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
CPMAddPackage(
NAME googletest
GITHUB_REPOSITORY google/googletest
GIT_TAG origin/main
#GIT_TAG v1.13.0 # release-1.13.0
OPTIONS "gtest_forced_shared_crt ON"
)

FetchContent_Declare(
googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
CPMAddPackage(
NAME googlebenchmark
GITHUB_REPOSITORY google/benchmark
GIT_TAG origin/main
)

FetchContent_Declare(
zlib
GIT_REPOSITORY https://github.com/madler/zlib.git
GIT_TAG v1.2.13
CPMAddPackage(
NAME zlib
GITHUB_REPOSITORY madler/zlib
VERSION 1.2.13
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE True"
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
FetchContent_MakeAvailable(googlebenchmark)

set(SAVE_CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE})
set(CMAKE_POSITION_INDEPENDENT_CODE True)
FetchContent_MakeAvailable(zlib)
set(CMAKE_POSITION_INDEPENDENT_CODE ${SAVE_CMAKE_POSITION_INDEPENDENT_CODE})

file (GLOB BENCHMARK_FILES "*.cpp" "*.hpp")
add_executable(benchmark_exe ${BENCHMARK_FILES})

Expand Down
27 changes: 10 additions & 17 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,22 @@ endif(CODE_COVERAGE)

option(TEST "Enable Test" OFF)
if(TEST)
include(FetchContent)
include(${CPM_DOWNLOAD_LOCATION})

FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
CPMAddPackage(
NAME googletest
GITHUB_REPOSITORY google/googletest
GIT_TAG origin/main
#GIT_TAG v1.13.0 # release-1.13.0
OPTIONS "gtest_forced_shared_crt ON"
)

FetchContent_Declare(
zlib
GIT_REPOSITORY https://github.com/madler/zlib.git
GIT_TAG v1.2.13
CPMAddPackage(
NAME zlib
GITHUB_REPOSITORY madler/zlib
VERSION 1.2.13
OPTIONS "CMAKE_POSITION_INDEPENDENT_CODE True"
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

set(SAVE_CMAKE_POSITION_INDEPENDENT_CODE ${CMAKE_POSITION_INDEPENDENT_CODE})
set(CMAKE_POSITION_INDEPENDENT_CODE True)
FetchContent_MakeAvailable(zlib)
set(CMAKE_POSITION_INDEPENDENT_CODE ${SAVE_CMAKE_POSITION_INDEPENDENT_CODE})

file (GLOB TEST_FILES "*.cpp" "*.hpp")

add_executable(test_exe ${TEST_FILES})
Expand Down

0 comments on commit 583c09f

Please sign in to comment.