Skip to content

Commit 8b67fe2

Browse files
authored
replace deprecated calls to FetchContent_Populate (#570)
* replace deprecated calls to FetchContent_Populate The single argument signature for FetchContent_Populate is deprecated with CMake 3.30. It was used, in order to call add_subdirectory manually with the EXCLUDE_FROM_ALL and SYSTEM flags. These have been added to FetchContent_Declare with 3.25 and 3.28. Calling FetchContent_MakeAvailable will internally call add_subdirectory with EXCLUDE_FROM_ALL and SYSTEM. There is therefore no need to call this manually. * fix: OPTIONS passed to CPMAddPackage not set where previously parsed in cpm_add_subdirectory which is not called on the new code path. * refactor: remove an unnecessary else branch * ci: include cmake 3.30 in test matrix * fix: forward SOURCE_SUBDIR to FetchContent_Declare For CMake version <3.28 this is done by calling add_subdirectory manually. For newer version FetchContent_Declare/MakeAvailable handles this for us. * fix: only set options if download_only is false this replicates the old behaviour * fix: DOWNLOAD_ONLY test * refactor: always use *_Populate to reduce code paths * Revert "refactor: always use *_Populate to reduce code paths" This reverts commit 0e8ca2a. --------- Co-authored-by: Avus <[email protected]>
1 parent d416d9b commit 8b67fe2

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

.github/workflows/test.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
os: [ubuntu-latest, windows-2022, macos-latest]
2020
# we want to ensure compatibility with a recent CMake version as well as the lowest officially supported
2121
# legacy version that we define as the default version of the second-latest Ubuntu LTS release currently available
22-
cmake_version: ['3.16.3', '3.27.5']
22+
cmake_version: ['3.16.3', '3.27.5', '3.30.0']
2323
exclude:
2424
# there seems to be an issue with CMake 3.16 not finding a C++ compiler on windows-2022
2525
- os: windows-2022

cmake/CPM.cmake

+47-6
Original file line numberDiff line numberDiff line change
@@ -862,14 +862,38 @@ function(CPMAddPackage)
862862
)
863863

864864
if(NOT CPM_SKIP_FETCH)
865+
# CMake 3.28 added EXCLUDE, SYSTEM (3.25), and SOURCE_SUBDIR (3.18) to FetchContent_Declare.
866+
# Calling FetchContent_MakeAvailable will then internally forward these options to
867+
# add_subdirectory. Up until these changes, we had to call FetchContent_Populate and
868+
# add_subdirectory separately, which is no longer necessary and has been deprecated as of 3.30.
869+
set(fetchContentDeclareExtraArgs "")
870+
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.28.0")
871+
if(${CPM_ARGS_EXCLUDE_FROM_ALL})
872+
list(APPEND fetchContentDeclareExtraArgs EXCLUDE_FROM_ALL)
873+
endif()
874+
if(${CPM_ARGS_SYSTEM})
875+
list(APPEND fetchContentDeclareExtraArgs SYSTEM)
876+
endif()
877+
if(DEFINED CPM_ARGS_SOURCE_SUBDIR)
878+
list(APPEND fetchContentDeclareExtraArgs SOURCE_SUBDIR ${CPM_ARGS_SOURCE_SUBDIR})
879+
endif()
880+
# For CMake version <3.28 OPTIONS are parsed in cpm_add_subdirectory
881+
if(CPM_ARGS_OPTIONS AND NOT DOWNLOAD_ONLY)
882+
foreach(OPTION ${CPM_ARGS_OPTIONS})
883+
cpm_parse_option("${OPTION}")
884+
set(${OPTION_KEY} "${OPTION_VALUE}")
885+
endforeach()
886+
endif()
887+
endif()
865888
cpm_declare_fetch(
866-
"${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}"
889+
"${CPM_ARGS_NAME}" ${fetchContentDeclareExtraArgs} "${CPM_ARGS_UNPARSED_ARGUMENTS}"
867890
)
868-
cpm_fetch_package("${CPM_ARGS_NAME}" populated)
891+
892+
cpm_fetch_package("${CPM_ARGS_NAME}" ${DOWNLOAD_ONLY} populated ${CPM_ARGS_UNPARSED_ARGUMENTS})
869893
if(CPM_SOURCE_CACHE AND download_directory)
870894
file(LOCK ${download_directory}/../cmake.lock RELEASE)
871895
endif()
872-
if(${populated})
896+
if(${populated} AND ${CMAKE_VERSION} VERSION_LESS "3.28.0")
873897
cpm_add_subdirectory(
874898
"${CPM_ARGS_NAME}"
875899
"${DOWNLOAD_ONLY}"
@@ -980,7 +1004,7 @@ function(CPMGetPackageVersion PACKAGE OUTPUT)
9801004
endfunction()
9811005

9821006
# declares a package in FetchContent_Declare
983-
function(cpm_declare_fetch PACKAGE VERSION INFO)
1007+
function(cpm_declare_fetch PACKAGE)
9841008
if(${CPM_DRY_RUN})
9851009
cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)")
9861010
return()
@@ -1056,7 +1080,7 @@ endfunction()
10561080

10571081
# downloads a previously declared package via FetchContent and exports the variables
10581082
# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope
1059-
function(cpm_fetch_package PACKAGE populated)
1083+
function(cpm_fetch_package PACKAGE DOWNLOAD_ONLY populated)
10601084
set(${populated}
10611085
FALSE
10621086
PARENT_SCOPE
@@ -1071,7 +1095,24 @@ function(cpm_fetch_package PACKAGE populated)
10711095
string(TOLOWER "${PACKAGE}" lower_case_name)
10721096

10731097
if(NOT ${lower_case_name}_POPULATED)
1074-
FetchContent_Populate(${PACKAGE})
1098+
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.28.0")
1099+
if(DOWNLOAD_ONLY)
1100+
# MakeAvailable will call add_subdirectory internally which is not what we want when
1101+
# DOWNLOAD_ONLY is set. Populate will only download the dependency without adding it to the
1102+
# build
1103+
FetchContent_Populate(
1104+
${PACKAGE}
1105+
SOURCE_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-src"
1106+
BINARY_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build"
1107+
SUBBUILD_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild"
1108+
${ARGN}
1109+
)
1110+
else()
1111+
FetchContent_MakeAvailable(${PACKAGE})
1112+
endif()
1113+
else()
1114+
FetchContent_Populate(${PACKAGE})
1115+
endif()
10751116
set(${populated}
10761117
TRUE
10771118
PARENT_SCOPE

0 commit comments

Comments
 (0)