From 7f0f44f014c3ae211516fb2736401b8497dd426a Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Fri, 11 Jun 2021 09:54:47 +0200 Subject: [PATCH] CMake: Promote all targets to global within a scope when possible CMake 3.21 introduced a new IMPORTED_TARGETS directory property which we can use to promote all imported targets within a scope to be global. This would cover transitive non-Qt imported targets which the Qt build system does not know about and is thus a more complete solution compared to promoting only Qt targets. Run a finalizer at the end of the directory scope where find_package(Qt6) is called to promote all imported targets within that scope to global (when requested). The old promotion method is disabled when the CMake version is new enough. Pick-to: 6.2 Task-number: QTBUG-92878 Task-number: QTBUG-94528 Change-Id: I533a3bd4186eba652f878ddd72c76118c2fd8bae Reviewed-by: Alexey Edelev Reviewed-by: Joerg Bornemann --- cmake/QtConfig.cmake.in | 2 ++ cmake/QtPublicTargetHelpers.cmake | 36 ++++++++++++++++++++++++++++++- cmake/QtTargetHelpers.cmake | 4 ++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/cmake/QtConfig.cmake.in b/cmake/QtConfig.cmake.in index 19d619e7f1..98c06a46f2 100644 --- a/cmake/QtConfig.cmake.in +++ b/cmake/QtConfig.cmake.in @@ -105,3 +105,5 @@ if (_Qt_NOTFOUND_MESSAGE) set(@INSTALL_CMAKE_NAMESPACE@_NOT_FOUND_MESSAGE "${_Qt_NOTFOUND_MESSAGE}") set(@INSTALL_CMAKE_NAMESPACE@_FOUND False) endif() + +__qt_internal_defer_promote_targets_in_dir_scope_to_global() diff --git a/cmake/QtPublicTargetHelpers.cmake b/cmake/QtPublicTargetHelpers.cmake index 70e6c85b1a..25e8616ac4 100644 --- a/cmake/QtPublicTargetHelpers.cmake +++ b/cmake/QtPublicTargetHelpers.cmake @@ -124,7 +124,41 @@ function(__qt_internal_promote_target_to_global target) endfunction() function(__qt_internal_promote_target_to_global_checked target) - if(QT_PROMOTE_TO_GLOBAL_TARGETS) + # With CMake version 3.21 we use a different mechanism that allows us to promote all targets + # within a scope. + if(QT_PROMOTE_TO_GLOBAL_TARGETS AND CMAKE_VERSION VERSION_LESS 3.21) __qt_internal_promote_target_to_global(${target}) endif() endfunction() + +function(__qt_internal_promote_targets_in_dir_scope_to_global) + # IMPORTED_TARGETS got added in 3.21. + if(CMAKE_VERSION VERSION_LESS 3.21) + return() + endif() + + get_directory_property(targets IMPORTED_TARGETS) + foreach(target IN LISTS targets) + __qt_internal_promote_target_to_global(${target}) + endforeach() +endfunction() + +function(__qt_internal_promote_targets_in_dir_scope_to_global_checked) + if(QT_PROMOTE_TO_GLOBAL_TARGETS) + __qt_internal_promote_targets_in_dir_scope_to_global() + endif() +endfunction() + +# This function ends up being called multiple times as part of a find_package(Qt6Foo) call, +# due sub-packages depending on the Qt6 package. Ensure the finalizer is ran only once per +# directory scope. +function(__qt_internal_defer_promote_targets_in_dir_scope_to_global) + get_directory_property(is_deferred _qt_promote_targets_is_deferred) + if(NOT is_deferred) + set_property(DIRECTORY PROPERTY _qt_promote_targets_is_deferred TRUE) + + if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.19) + cmake_language(DEFER CALL __qt_internal_promote_targets_in_dir_scope_to_global_checked) + endif() + endif() +endfunction() diff --git a/cmake/QtTargetHelpers.cmake b/cmake/QtTargetHelpers.cmake index be1cf5e2da..00c4482c75 100644 --- a/cmake/QtTargetHelpers.cmake +++ b/cmake/QtTargetHelpers.cmake @@ -281,8 +281,8 @@ endmacro() # # The file also makes the targets global if the QT_PROMOTE_TO_GLOBAL_TARGETS property is set in the # consuming project. -# Only the specified TARGETS are made global. Transitive 3rd party targets are not made global, due -# to limitations in CMake. See https://gitlab.kitware.com/cmake/cmake/-/issues/22291 +# When using a CMake version lower than 3.21, only the specified TARGETS are made global. +# E.g. transitive non-Qt 3rd party targets of the specified targets are not made global. # # EXPORT_NAME_PREFIX: # The portion of the file name before AdditionalTargetInfo.cmake