FetchContent: Set CMAKE_EXPORT_FIND_PACKAGE_NAME

This commit is contained in:
Kyle Edwards
2023-11-07 14:53:28 -05:00
parent c6e6861e63
commit 0ad8fc5a63
8 changed files with 135 additions and 0 deletions

View File

@@ -12,3 +12,5 @@ export-find_dependency-calls
allow targets to specify what package name to pass when exporting
:command:`find_dependency` calls. This property is initialized with a new
:variable:`CMAKE_EXPORT_FIND_PACKAGE_NAME` variable.
* :command:`FetchContent_MakeAvailable` now sets the
:variable:`CMAKE_EXPORT_FIND_PACKAGE_NAME` variable for CMake projects.

View File

@@ -373,6 +373,10 @@ Commands
:command:`FetchContent_Declare`, the ``EXCLUDE_FROM_ALL`` keyword will
be added to the :command:`add_subdirectory` command as well.
.. versionadded:: 3.29
:variable:`CMAKE_EXPORT_FIND_PACKAGE_NAME` is set to the dependency name
before calling :command:`add_subdirectory`.
Projects should aim to declare the details of all dependencies they might
use before they call ``FetchContent_MakeAvailable()`` for any of them.
This ensures that if any of the dependencies are also sub-dependencies of
@@ -1944,6 +1948,10 @@ macro(FetchContent_MakeAvailable)
"Trying FETCHCONTENT_MAKEAVAILABLE_SERIAL dependency provider for "
"${__cmake_contentName}"
)
list(APPEND __cmake_fcCurrentVarsStack "__fcprefix__${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
set(CMAKE_EXPORT_FIND_PACKAGE_NAME "${__cmake_contentName}")
# It's still valid if there are no saved details. The project may have
# been written to assume a dependency provider is always set and will
# provide dependencies without having any declared details for them.
@@ -1998,6 +2006,12 @@ macro(FetchContent_MakeAvailable)
unset(__cmake_item)
unset(__cmake_contentDetails)
list(POP_BACK __cmake_fcCurrentVarsStack __cmake_original_export_find_package_name)
string(SUBSTRING "${__cmake_original_export_find_package_name}"
12 -1 __cmake_original_export_find_package_name
)
set(CMAKE_EXPORT_FIND_PACKAGE_NAME ${__cmake_original_export_find_package_name})
FetchContent_GetProperties(${__cmake_contentName})
if(${__cmake_contentNameLower}_POPULATED)
continue()
@@ -2067,6 +2081,9 @@ macro(FetchContent_MakeAvailable)
endif()
if(EXISTS ${__cmake_srcdir}/CMakeLists.txt)
list(APPEND __cmake_fcCurrentVarsStack "__fcprefix__${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
set(CMAKE_EXPORT_FIND_PACKAGE_NAME "${__cmake_contentName}")
set(__cmake_add_subdirectory_args ${__cmake_srcdir} ${${__cmake_contentNameLower}_BINARY_DIR})
if(__cmake_arg_EXCLUDE_FROM_ALL)
list(APPEND __cmake_add_subdirectory_args EXCLUDE_FROM_ALL)
@@ -2075,6 +2092,12 @@ macro(FetchContent_MakeAvailable)
list(APPEND __cmake_add_subdirectory_args SYSTEM)
endif()
add_subdirectory(${__cmake_add_subdirectory_args})
list(POP_BACK __cmake_fcCurrentVarsStack __cmake_original_export_find_package_name)
string(SUBSTRING "${__cmake_original_export_find_package_name}"
12 -1 __cmake_original_export_find_package_name
)
set(CMAKE_EXPORT_FIND_PACKAGE_NAME ${__cmake_original_export_find_package_name})
endif()
unset(__cmake_srcdir)

View File

@@ -0,0 +1,6 @@
if(NOT CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL "SomeOtherValue")
message(FATAL_ERROR "Expected value of CMAKE_EXPORT_FIND_PACKAGE_NAME:\n SomeOtherValue\nActual value:\n ${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
endif()
set(fp_called TRUE)
set(FDE-S_FOUND TRUE)

View File

@@ -0,0 +1,6 @@
if(DEFINED CMAKE_EXPORT_FIND_PACKAGE_NAME)
message(FATAL_ERROR "CMAKE_EXPORT_FIND_PACKAGE_NAME should not be defined")
endif()
set(fp_called TRUE)
set(FDE-U_FOUND TRUE)

View File

@@ -0,0 +1,78 @@
include(FetchContent)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMake)
unset(dp_called)
unset(fp_called)
set(_expected_export_find_package_name_dp FDE-U)
FetchContent_Declare(
FDE-U
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(FDE-U)
if(NOT dp_called)
message(FATAL_ERROR "FetchContent_MakeAvailable did not call dependency provider")
endif()
if(NOT fp_called)
message(FATAL_ERROR "FetchContent_MakeAvailable did not call find_package()")
endif()
if(DEFINED CMAKE_EXPORT_FIND_PACKAGE_NAME)
message(FATAL_ERROR "CMAKE_EXPORT_FIND_PACKAGE_NAME should have been unset after FetchContent_MakeAvailable().\nActual value:\n ${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
endif()
unset(sub_called)
set(_expected_export_find_package_name_dp FDE-U-Sub)
set(_expected_export_find_package_name_sub FDE-U-Sub)
FetchContent_Declare(
FDE-U-Sub
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/FindDependencyExport
)
FetchContent_MakeAvailable(FDE-U-Sub)
if(NOT sub_called)
message(FATAL_ERROR "FetchContent_MakeAvailable did not call add_subdirectory()")
endif()
if(DEFINED CMAKE_EXPORT_FIND_PACKAGE_NAME)
message(FATAL_ERROR "CMAKE_EXPORT_FIND_PACKAGE_NAME should have been unset after FetchContent_MakeAvailable()")
endif()
unset(dp_called)
unset(fp_called)
set(CMAKE_EXPORT_FIND_PACKAGE_NAME SomeOtherValue)
set(_expected_export_find_package_name_dp FDE-S)
FetchContent_Declare(
FDE-S
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(FDE-S)
if(NOT dp_called)
message(FATAL_ERROR "FetchContent_MakeAvailable did not call dependency provider")
endif()
if(NOT fp_called)
message(FATAL_ERROR "FetchContent_MakeAvailable did not call find_package()")
endif()
if(NOT CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL "SomeOtherValue")
message(FATAL_ERROR "Expected value of CMAKE_EXPORT_FIND_PACKAGE_NAME:\n SomeOtherValue\nActual value:\n ${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
endif()
unset(sub_called)
set(_expected_export_find_package_name_dp FDE-S-Sub)
set(_expected_export_find_package_name_sub FDE-S-Sub)
FetchContent_Declare(
FDE-S-Sub
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/FindDependencyExport
)
FetchContent_MakeAvailable(FDE-S-Sub)
if(NOT sub_called)
message(FATAL_ERROR "FetchContent_MakeAvailable did not call add_subdirectory()")
endif()
if(NOT CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL "SomeOtherValue")
message(FATAL_ERROR "Expected value of CMAKE_EXPORT_FIND_PACKAGE_NAME:\n SomeOtherValue\nActual value:\n ${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
endif()

View File

@@ -0,0 +1,5 @@
if(NOT CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL _expected_export_find_package_name_sub)
message(FATAL_ERROR "Expected value of CMAKE_EXPORT_FIND_PACKAGE_NAME:\n ${_expected_export_find_package_name_sub}\nActual value:\n ${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
endif()
set(sub_called TRUE PARENT_SCOPE)

View File

@@ -0,0 +1,11 @@
function(fde_provide_dependency method name)
if(NOT CMAKE_EXPORT_FIND_PACKAGE_NAME STREQUAL _expected_export_find_package_name_dp)
message(FATAL_ERROR "Expected value of CMAKE_EXPORT_FIND_PACKAGE_NAME:\n ${_expected_export_find_package_name_dp}\nActual value:\n ${CMAKE_EXPORT_FIND_PACKAGE_NAME}")
endif()
set(dp_called TRUE PARENT_SCOPE)
endfunction()
cmake_language(SET_DEPENDENCY_PROVIDER fde_provide_dependency
SUPPORTED_METHODS FETCHCONTENT_MAKEAVAILABLE_SERIAL
)

View File

@@ -19,6 +19,10 @@ run_cmake(MakeAvailableTwice)
run_cmake(MakeAvailableUndeclared)
run_cmake(VerifyHeaderSet)
run_cmake_with_options(FindDependencyExport
-D "CMAKE_PROJECT_TOP_LEVEL_INCLUDES=${CMAKE_CURRENT_LIST_DIR}/FindDependencyExportDP.cmake"
)
run_cmake_with_options(ManualSourceDirectory
-D "FETCHCONTENT_SOURCE_DIR_WITHPROJECT=${CMAKE_CURRENT_LIST_DIR}/WithProject"
)