From 1305bade56f77e0e29f2c8f2fc188e5f9da59756 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Fri, 10 Jun 2022 18:08:36 +1000 Subject: Help: Add missing version directive for find_package() GLOBAL keyword --- Help/command/find_package.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Help/command/find_package.rst b/Help/command/find_package.rst index e85892e..849052a 100644 --- a/Help/command/find_package.rst +++ b/Help/command/find_package.rst @@ -140,10 +140,11 @@ available components. ignored on all other ones. Formally, it is up to the target package how to interpret the registry view information given to it. -Specifying the ``GLOBAL`` keyword will promote all imported targets to -a global scope in the importing project. Alternatively this functionality -can be enabled by setting the variable -:variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` +.. versionadded:: 3.24 + Specifying the ``GLOBAL`` keyword will promote all imported targets to + a global scope in the importing project. Alternatively, this functionality + can be enabled by setting the :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` + variable. .. _FIND_PACKAGE_VERSION_FORMAT: -- cgit v0.12 From f19b48e0b89d5854e4a7a8fbbccadb38c02dcd76 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Fri, 10 Jun 2022 21:40:33 +1000 Subject: FetchContent: Honor CMAKE_FIND_PACKAGE_TARGETS_GLOBAL Fixes: #23606 --- Modules/FetchContent.cmake | 26 ++++++++++- .../ChildScope/CMakeLists.txt | 11 +++++ .../FetchContent_find_package/GLOBAL.cmake | 50 ++++++++++++++++++++++ .../PackageFindModules/FindEventuallyGlobal.cmake | 6 +++ .../FindGlobalWithArgsKeyword.cmake | 6 +++ .../FindGlobalWithoutArgsKeyword.cmake | 6 +++ .../FindLocalWithArgsKeyword.cmake | 6 +++ .../FindLocalWithoutArgsKeyword.cmake | 6 +++ .../FetchContent_find_package/RunCMakeTest.cmake | 1 + 9 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 Tests/RunCMake/FetchContent_find_package/ChildScope/CMakeLists.txt create mode 100644 Tests/RunCMake/FetchContent_find_package/GLOBAL.cmake create mode 100644 Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindEventuallyGlobal.cmake create mode 100644 Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithArgsKeyword.cmake create mode 100644 Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithoutArgsKeyword.cmake create mode 100644 Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithArgsKeyword.cmake create mode 100644 Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithoutArgsKeyword.cmake diff --git a/Modules/FetchContent.cmake b/Modules/FetchContent.cmake index 83aafa8..65a59c7 100644 --- a/Modules/FetchContent.cmake +++ b/Modules/FetchContent.cmake @@ -189,7 +189,14 @@ Commands Everything after the ``FIND_PACKAGE_ARGS`` keyword is appended to the :command:`find_package` call, so all other ```` must - come before the ``FIND_PACKAGE_ARGS`` keyword. + come before the ``FIND_PACKAGE_ARGS`` keyword. If the + :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to true + at the time ``FetchContent_Declare()`` is called, a ``GLOBAL`` keyword + will be appended to the :command:`find_package` arguments if it was + not already specified. It will also be appended if + ``FIND_PACKAGE_ARGS`` was not given, but + :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` was set to ``ALWAYS``. + ``OVERRIDE_FIND_PACKAGE`` cannot be used when ``FIND_PACKAGE_ARGS`` is given. @@ -254,6 +261,11 @@ Commands The value of the :variable:`FETCHCONTENT_TRY_FIND_PACKAGE_MODE` variable at the time :command:`FetchContent_Declare` was called determines whether ``FetchContent_MakeAvailable()`` can call :command:`find_package`. + If the :variable:`CMAKE_FIND_PACKAGE_TARGETS_GLOBAL` variable is set to + true when ``FetchContent_MakeAvailable()`` is called, it still affects + any imported targets created when that in turn calls + :command:`find_package`, even if that variable was false when the + corresponding details were declared. If the dependency was not satisfied by a provider or a :command:`find_package` call, ``FetchContent_MakeAvailable()`` then uses @@ -1072,10 +1084,17 @@ function(__FetchContent_declareDetails contentName) set(__cmdArgs) set(__findPackageArgs) + set(__sawQuietKeyword NO) + set(__sawGlobalKeyword NO) foreach(__item IN LISTS ARGN) if(DEFINED __findPackageArgs) # All remaining args are for find_package() string(APPEND __findPackageArgs " [==[${__item}]==]") + if(__item STREQUAL "QUIET") + set(__sawQuietKeyword YES) + elseif(__item STREQUAL "GLOBAL") + set(__sawGlobalKeyword YES) + endif() continue() endif() @@ -1114,9 +1133,12 @@ function(__FetchContent_declareDetails contentName) if(__tryFindPackage AND __tryFindPackageAllowed) set(propertyName "_FetchContent_${contentNameLower}_find_package_args") define_property(GLOBAL PROPERTY ${propertyName}) - if(NOT QUIET IN_LIST __findPackageArgs) + if(NOT __sawQuietKeyword) list(INSERT __findPackageArgs 0 QUIET) endif() + if(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL AND NOT __sawGlobalKeyword) + list(APPEND __findPackageArgs GLOBAL) + endif() cmake_language(EVAL CODE "set_property(GLOBAL PROPERTY ${propertyName} ${__findPackageArgs})" ) diff --git a/Tests/RunCMake/FetchContent_find_package/ChildScope/CMakeLists.txt b/Tests/RunCMake/FetchContent_find_package/ChildScope/CMakeLists.txt new file mode 100644 index 0000000..7e994d6 --- /dev/null +++ b/Tests/RunCMake/FetchContent_find_package/ChildScope/CMakeLists.txt @@ -0,0 +1,11 @@ +FetchContent_MakeAvailable( + GlobalWithArgsKeyword + GlobalWithoutArgsKeyword + LocalWithArgsKeyword + LocalWithoutArgsKeyword +) + +# This one was declared without GLOBAL, but should still become global when +# this variable is true at the time FetchContent_MakeAvailable() is called +set(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL TRUE) +FetchContent_MakeAvailable(EventuallyGlobal) diff --git a/Tests/RunCMake/FetchContent_find_package/GLOBAL.cmake b/Tests/RunCMake/FetchContent_find_package/GLOBAL.cmake new file mode 100644 index 0000000..0de65d5 --- /dev/null +++ b/Tests/RunCMake/FetchContent_find_package/GLOBAL.cmake @@ -0,0 +1,50 @@ +include(FetchContent) + +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/PackageFindModules) +set(FETCHCONTENT_TRY_FIND_PACKAGE_MODE ALWAYS) + +set(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL TRUE) +FetchContent_Declare( + GlobalWithArgsKeyword + SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/FatalIfAdded + FIND_PACKAGE_ARGS +) +FetchContent_Declare( + GlobalWithoutArgsKeyword + SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/FatalIfAdded +) + +set(CMAKE_FIND_PACKAGE_TARGETS_GLOBAL FALSE) +FetchContent_Declare( + LocalWithArgsKeyword + SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/FatalIfAdded + FIND_PACKAGE_ARGS +) +FetchContent_Declare( + LocalWithoutArgsKeyword + SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/FatalIfAdded +) +FetchContent_Declare( + EventuallyGlobal + SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/FatalIfAdded +) + +add_subdirectory(ChildScope) + +if(NOT TARGET GlobalWithArgsKeywordExe) + message(SEND_ERROR "GlobalWithArgsKeywordExe is not a global target") +endif() +if(NOT TARGET GlobalWithoutArgsKeywordExe) + message(SEND_ERROR "GlobalWithoutArgsKeywordExe is not a global target") +endif() + +if(TARGET LocalWithArgsKeywordExe) + message(SEND_ERROR "LocalWithArgsKeywordExe is unexpectedly a global target") +endif() +if(TARGET LocalWithoutArgsKeywordExe) + message(SEND_ERROR "LocalWithoutArgsKeywordExe is unexpectedly a global target") +endif() + +if(NOT TARGET EventuallyGlobalExe) + message(SEND_ERROR "EventuallyGlobalExe is not a global target") +endif() diff --git a/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindEventuallyGlobal.cmake b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindEventuallyGlobal.cmake new file mode 100644 index 0000000..2389cb1 --- /dev/null +++ b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindEventuallyGlobal.cmake @@ -0,0 +1,6 @@ +add_executable(EventuallyGlobalExe IMPORTED) +set_target_properties(EventuallyGlobalExe PROPERTIES + IMPORTED_LOCATION "${CMAKE_COMMAND}" +) + +set(EventuallyGlobal_FOUND TRUE) diff --git a/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithArgsKeyword.cmake b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithArgsKeyword.cmake new file mode 100644 index 0000000..55588b8 --- /dev/null +++ b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithArgsKeyword.cmake @@ -0,0 +1,6 @@ +add_executable(GlobalWithArgsKeywordExe IMPORTED) +set_target_properties(GlobalWithArgsKeywordExe PROPERTIES + IMPORTED_LOCATION "${CMAKE_COMMAND}" +) + +set(GlobalWithArgsKeyword_FOUND TRUE) diff --git a/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithoutArgsKeyword.cmake b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithoutArgsKeyword.cmake new file mode 100644 index 0000000..c3e6a6b --- /dev/null +++ b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindGlobalWithoutArgsKeyword.cmake @@ -0,0 +1,6 @@ +add_executable(GlobalWithoutArgsKeywordExe IMPORTED) +set_target_properties(GlobalWithoutArgsKeywordExe PROPERTIES + IMPORTED_LOCATION "${CMAKE_COMMAND}" +) + +set(GlobalWithoutArgsKeyword_FOUND TRUE) diff --git a/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithArgsKeyword.cmake b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithArgsKeyword.cmake new file mode 100644 index 0000000..381daa3 --- /dev/null +++ b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithArgsKeyword.cmake @@ -0,0 +1,6 @@ +add_executable(LocalWithArgsKeywordExe IMPORTED) +set_target_properties(LocalWithArgsKeywordExe PROPERTIES + IMPORTED_LOCATION "${CMAKE_COMMAND}" +) + +set(LocalWithArgsKeyword_FOUND TRUE) diff --git a/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithoutArgsKeyword.cmake b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithoutArgsKeyword.cmake new file mode 100644 index 0000000..523fecd --- /dev/null +++ b/Tests/RunCMake/FetchContent_find_package/PackageFindModules/FindLocalWithoutArgsKeyword.cmake @@ -0,0 +1,6 @@ +add_executable(LocalWithoutArgsKeywordExe IMPORTED) +set_target_properties(LocalWithoutArgsKeywordExe PROPERTIES + IMPORTED_LOCATION "${CMAKE_COMMAND}" +) + +set(LocalWithoutArgsKeyword_FOUND TRUE) diff --git a/Tests/RunCMake/FetchContent_find_package/RunCMakeTest.cmake b/Tests/RunCMake/FetchContent_find_package/RunCMakeTest.cmake index c139f57..83c0a9a 100644 --- a/Tests/RunCMake/FetchContent_find_package/RunCMakeTest.cmake +++ b/Tests/RunCMake/FetchContent_find_package/RunCMakeTest.cmake @@ -20,3 +20,4 @@ run_cmake(Try_find_package-NEVER) run_cmake(Try_find_package-OPT_IN) run_cmake(Try_find_package-BOGUS) run_cmake(Redirect_find_package_MODULE) +run_cmake(GLOBAL) -- cgit v0.12