Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to non-deprecated FetchContent_Populate syntax (research PR) #578

Closed
wants to merge 9 commits into from
41 changes: 36 additions & 5 deletions cmake/CPM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ macro(cpm_set_policies)
cmake_policy(SET CMP0150 NEW)
set(CMAKE_POLICY_DEFAULT_CMP0150 NEW)
endif()

# don't use subdir directories for cloning
if(POLICY CMP0168)
cmake_policy(SET CMP0168 NEW)
endif()
endmacro()
cpm_set_policies()

Expand Down Expand Up @@ -865,7 +870,7 @@ function(CPMAddPackage)
cpm_declare_fetch(
"${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}"
)
cpm_fetch_package("${CPM_ARGS_NAME}" populated)
cpm_fetch_package("${CPM_ARGS_NAME}" populated "${CPM_ARGS_UNPARSED_ARGUMENTS}")
if(CPM_SOURCE_CACHE AND download_directory)
file(LOCK ${download_directory}/../cmake.lock RELEASE)
endif()
Expand Down Expand Up @@ -1056,7 +1061,7 @@ endfunction()

# downloads a previously declared package via FetchContent and exports the variables
# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope
function(cpm_fetch_package PACKAGE populated)
function(cpm_fetch_package PACKAGE populated fetch_args)
set(${populated}
FALSE
PARENT_SCOPE
Expand All @@ -1067,11 +1072,37 @@ function(cpm_fetch_package PACKAGE populated)
endif()

FetchContent_GetProperties(${PACKAGE})

string(TOLOWER "${PACKAGE}" lower_case_name)

if(NOT ${lower_case_name}_POPULATED)
FetchContent_Populate(${PACKAGE})
# in case `FetchContent_GetProperties` does not retrieve the SOURCE_DIR and BINARY_DIR we need to
# parse them and their defaults manually.
cmake_parse_arguments(${lower_case_name} "" "SOURCE_DIR;BINARY_DIR;SUBBUILD_DIR" "" ${fetch_args})

if(NOT ${lower_case_name}_SOURCE_DIR)
set(${lower_case_name}_SOURCE_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-src")
endif()
if(NOT ${lower_case_name}_BINARY_DIR)
set(${lower_case_name}_BINARY_DIR "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build")
endif()

if(NOT "${${lower_case_name}_POPULATED}")
# only exectute `FetchContent_Populate` if we specified a way to download the source
if(NOT "${${lower_case_name}_UNPARSED_ARGUMENTS}" STREQUAL "")
FetchContent_Populate(
${PACKAGE}
SOURCE_DIR ${${lower_case_name}_SOURCE_DIR}
BINARY_DIR ${${lower_case_name}_BINARY_DIR}
"${${lower_case_name}_UNPARSED_ARGUMENTS}"
)
endif()
if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24.0")
# ensure FetchContent knows the project is populated, this doesn't seem to be the case when
# just calling `FetchContent_Populate`.
fetchcontent_setpopulated(
${PACKAGE} SOURCE_DIR ${${lower_case_name}_SOURCE_DIR} BINARY_DIR
${${lower_case_name}_BINARY_DIR}
)
endif()
set(${populated}
TRUE
PARENT_SCOPE
Expand Down
Loading