From 57b94b58df474ebc268993291a669dbad21a1300 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Thu, 13 Aug 2020 17:16:24 +0200 Subject: [PATCH] CMake: Allow opting out of global target promotion The endless saga of fighting with qt_find_package and global target promotion. In certain scenarios we want to opt out of target promotion to global scope. One such case is in qttools with WrapLibClang and Threads::Threads. Threads::Threads will be found in the top-level scope via Qt6Dependencies. WrapLibClang is declared in src/ directory scope, and then we try to promote it ands its dependencies to the global scope, via qt_find_package() -> qt_find_package_promote_targets_to_global_scope(). This fails because we can't promote Threads::Threads due to it being added in a different subdirectory scope. Introduce 2 new functions. qt_internal_should_not_promote_package_target_to_global and qt_internal_disable_find_package_global_promotion. The first one is used to disable promotion of targets to global scope in qt_find_package. To mark a target not to be promoted, the second function is used. It will be used by qttools for the WrapLibClang case. Task-number: QTBUG-85877 Change-Id: If6caf10a94999402026517a623ae29e3ab1eeb7f Reviewed-by: Joerg Bornemann --- cmake/QtBuild.cmake | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cmake/QtBuild.cmake b/cmake/QtBuild.cmake index 0ab5dc2ff4..4921391ebf 100644 --- a/cmake/QtBuild.cmake +++ b/cmake/QtBuild.cmake @@ -3318,7 +3318,12 @@ function(qt_internal_walk_libs target out_var dict_name operation) get_property(is_imported TARGET ${lib_target_unaliased} PROPERTY IMPORTED) get_property(is_global TARGET ${lib_target_unaliased} PROPERTY IMPORTED_GLOBAL) - if(NOT is_global AND is_imported) + + # Allow opting out of promotion. This is useful in certain corner cases + # like with WrapLibClang and Threads in qttools. + qt_internal_should_not_promote_package_target_to_global( + "${lib_target_unaliased}" should_not_promote) + if(NOT is_global AND is_imported AND NOT should_not_promote) set_property(TARGET ${lib_target_unaliased} PROPERTY IMPORTED_GLOBAL TRUE) endif() endif() @@ -5649,7 +5654,9 @@ macro(qt_find_package) get_property(is_global TARGET ${qt_find_package_target_name} PROPERTY IMPORTED_GLOBAL) - if(NOT is_global) + qt_internal_should_not_promote_package_target_to_global( + "${qt_find_package_target_name}" should_not_promote) + if(NOT is_global AND NOT should_not_promote) set_property(TARGET ${qt_find_package_target_name} PROPERTY IMPORTED_GLOBAL TRUE) qt_find_package_promote_targets_to_global_scope( @@ -6149,6 +6156,16 @@ function(qt_internal_strip_target_directory_scope_token target out_var) set("${out_var}" "${target}" PARENT_SCOPE) endfunction() +# Sets out_var to to TRUE if the target was marked to not be promoted to global scope. +function(qt_internal_should_not_promote_package_target_to_global target out_var) + get_property(should_not_promote TARGET "${target}" PROPERTY _qt_no_promote_global) + set("${out_var}" "${should_not_promote}" PARENT_SCOPE) +endfunction() + +function(qt_internal_disable_find_package_global_promotion target) + set_target_properties("${target}" PROPERTIES _qt_no_promote_global TRUE) +endfunction() + include(QtApp) # Compatibility macros that should be removed once all their usages are removed.