summaryrefslogtreecommitdiffstats
path: root/cmake/QtPublicTargetHelpers.cmake
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2021-06-11 09:54:47 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2021-06-16 13:22:17 +0200
commit7f0f44f014c3ae211516fb2736401b8497dd426a (patch)
tree7ad621e3305bae46f95951a986a70de7fc362c29 /cmake/QtPublicTargetHelpers.cmake
parent561fc8107f38b93808343e35c62d7d06704f8eb6 (diff)
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 <alexey.edelev@qt.io> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'cmake/QtPublicTargetHelpers.cmake')
-rw-r--r--cmake/QtPublicTargetHelpers.cmake36
1 files changed, 35 insertions, 1 deletions
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()