CMake: Prevent most global promotion errors when building Qt
Backstory. The main reason why we keep getting "unable to promote 3rd party 'X' target to global scope" errors when building Qt repositories, is because we try to promote 3rd party imported targets in a different scope than where the imported targets were created. What were the main motivations for promoting 3rd party targets to global? 1) imported targets are by default local to the directory scope they were created in 2) we want 3rd party targets to be accessible across subdirectory scopes, but looked up once, e.g. qt_find_package(JPEG) looked up in src/gui/CMakeLists.txt, but the target should also be usable in the sibling scope src/plugins/imageformats/CMakeLists.txt Having the package lookup close to the consuming qt module is easier to maintain, because all the other 3rd party dependency lookups are in the same file. This goes against the conventional CMake advice where each subdirectory should look for its own dependencies, or the dependency should be available directly in the root project scope. 3) to make the 3rd party targets available in the root project scope as part of the following flow: QtPostProcess.cmake -> qt_internal_create_module_depends_file() -> qt_collect_third_party_deps() -> get_property(INTERFACE_QT_PACKAGE_NAME) -> write 3rd party Dependencies.cmake file for each qt module. Properties can only be queried from an imported target if it's in the same scope or was promoted to global, otherwise you get 'non-existent target' errors. 4) for prl and pri file generation, where we need the targets to be available during generator expression evaluation within the relevant qt module directory scope Here is a list of approaches I came up with on how to improve the situation. 1) Make all imported targets global during the Qt build, by iterating over the directory property IMPORTED_TARGETS and making each one global. Requires CMake 3.21. Status: Already implemented for a long time, but is opt-in. Pros: Relatively robust Cons: Minimum CMake version for building Qt is 3.16. 2) Make all imported targets global during the Qt build using the CMAKE_FIND_PACKAGE_TARGETS_GLOBAL variable. Requires CMake 3.24. Status: Not implemented, but can be set by Qt builders directly on the command line. Pros: Should be robust Cons: Minimum CMake version for building Qt is 3.16. 3) Abandon the desire to have a single qt_find_package in a single directory scope, and embrace the CMake-way of repeating the dependency in each subdirectory that requires it. Status: Not implemented. Pros: Should be robust Cons: A lot of qt_find_package duplication, will require rewriting various code paths, QtPostProcess would have to be done at directory scope, unclear if dependency tracking will still work work reliably when there might be multiple same-named directory-scoped targets, other unknown unknowns 4) Move all qt_find_package calls into a $repo_name/dependencies.cmake file which would be read at project root scope. This would potentially avoid all scoping issues, because all dependencies will have to be specified at root scope. Status: Not implemented. Pros: No duplication Cons: Dependencies are not scoped anymore to module directories, won't be able to conditionally look for dependencies based on module feature evaluation, not clear yet how this will tie into standalone tests which are in tests/ subdir, other unknown unknowns 5) Try to promote as many 3rd party libraries at project root scope as possible. Currently we have 2 general locations where we look up dependencies. One is each qt_find_package call. The other is Qt6FooDependencies.cmake -> _qt_internal_find_third_party_dependencies(). Many 3rd party targets are created by _qt_internal_find_third_party_dependencies() in the root scope, but not promoted, and then we try to promote them in child scopes using qt_find_package, which causes the promotion errors. Starting with 58eefbd0b6169d0749b312268c1ae1e594e04362 and 37a5e001277db9e1392a242171ab2b88cb6c3049 we now record the provided targets of previous qt_find_package calls. So instead of waiting to try and promote targets later during the configuration process, we can make sure we promote the targets at _qt_internal_find_third_party_dependencies() call time, right when we lookup the Qt dependencies of the qt repo, in the root scope. Status: Implemented in this change Notably, we only promote 3rd party targets to global for qt builds, and not user projects, to not accidentally break user project behaviors. Also, we only promote 3rd party targets, and not Qt internal targets like Qt6::Core, Qt6::Platform, Qt6::PlatformCommonInternal, Qt6::GlobalConfig, etc, for a few reasons: - the code that requires targets to be global only cares about 3rd party targets - promoting the internal targets is more prone to breaking, because there is more than one place where find_package(Qt6Foo) might be called, and if that ends up being in a different directory scope, we encounter the same global promotion errors. Some notable cases where this happens: - tests/CMakeLists.txt brings in extra Qt packages via StandaloneTestsConfig.cmake files - qtbase standalone tests qt_internal_qtbase_pre_project_setup() calls find_package(Qt6 COMPONENTS BuildInternals) which ends up creating the Platform target in the root scope instead of the tests/ scope - Qt6::BundledLibpng links against Core, which ends up trying to promote Core's internal dependencies Platform and GlobalConfig To only promote 3rd party targets, we walk the dependencies of an initial target recursively, and skip promoting targets that have the _qt_is_internal_target or _qt_should_skip_global_promotion_always properties set. Pros: Improves the situation compared to the status quo Cons: Still not ideal due to the various filtering of internal targets and having to mark them as such. 6) Avoid promoting targets to global if we can detect that the target was created in a different scope than where we are trying to promote it. We can do that by comparing the target's BINARY_DIR to the CMAKE_CURRENT_BINARY_DIR and skip promotion if they are not equal. Status: Not implemented, but we can consider it because it's quick to do. Pros: More robust than newly implemented approach (5) Cons: Requires CMake 3.18, because trying to read the BINARY_DIR property on an INTERFACE_LIBRARY would error out. Also, if we implement it and make it the default when using 3.18+, we might 'collect' a lot more hidden promotion errors that will only be revealed later once someone uses CMake 3.16 or 3.17, because most will probably use newer CMake versions. Perhaps the trade-off is worth it? Pick-to: 6.8 Fixes: QTBUG-89204 Fixes: QTBUG-94356 Fixes: QTBUG-95052 Fixes: QTBUG-98807 Fixes: QTBUG-125371 Change-Id: I088a17a98ef35aa69537a3ad208c61de40def581 Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io> Reviewed-by: Alexey Edelev <alexey.edelev@qt.io>
This commit is contained in:
parent
dad49f5a1e
commit
d2e85cede0
@ -11,7 +11,6 @@ macro(qt_internal_project_setup)
|
||||
# Check for the minimum CMake version.
|
||||
qt_internal_require_suitable_cmake_version()
|
||||
qt_internal_upgrade_cmake_policies()
|
||||
qt_internal_promote_platform_targets_to_global()
|
||||
endmacro()
|
||||
|
||||
macro(qt_build_internals_set_up_private_api)
|
||||
|
@ -209,15 +209,8 @@ macro(qt_find_package)
|
||||
)
|
||||
endif()
|
||||
|
||||
get_property(is_global TARGET ${qt_find_package_target_name} PROPERTY
|
||||
IMPORTED_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)
|
||||
__qt_internal_promote_target_to_global(${qt_find_package_target_name})
|
||||
_qt_find_package_promote_targets_to_global_scope(
|
||||
"${qt_find_package_target_name}")
|
||||
endif()
|
||||
_qt_internal_promote_3rd_party_provided_target_and_3rd_party_deps_to_global(
|
||||
"${qt_find_package_target_name}")
|
||||
|
||||
set(_qt_find_package_sbom_args "")
|
||||
|
||||
|
@ -52,19 +52,20 @@ macro(_qt_internal_find_third_party_dependencies target target_dep_list)
|
||||
)
|
||||
endif()
|
||||
|
||||
# Work around: QTBUG-125371
|
||||
if(NOT "${ARGV0}" STREQUAL "Qt6")
|
||||
foreach(__qt_${target}_provided_target
|
||||
IN LISTS
|
||||
__qt_${target}_third_party_package_${__qt_${target}_package_components_id}_provided_targets)
|
||||
_qt_internal_sbom_record_system_library_usage(
|
||||
"${__qt_${target}_provided_target}"
|
||||
TYPE SYSTEM_LIBRARY
|
||||
FRIENDLY_PACKAGE_NAME "${__qt_${target}_pkg}"
|
||||
${__qt_${target}_sbom_args}
|
||||
)
|
||||
endforeach()
|
||||
endif()
|
||||
foreach(__qt_${target}_provided_target
|
||||
IN LISTS
|
||||
__qt_${target}_third_party_package_${__qt_${target}_package_components_id}_provided_targets)
|
||||
|
||||
_qt_internal_promote_3rd_party_provided_target_and_3rd_party_deps_to_global(
|
||||
"${__qt_${target}_provided_target}")
|
||||
|
||||
_qt_internal_sbom_record_system_library_usage(
|
||||
"${__qt_${target}_provided_target}"
|
||||
TYPE SYSTEM_LIBRARY
|
||||
FRIENDLY_PACKAGE_NAME "${__qt_${target}_pkg}"
|
||||
${__qt_${target}_sbom_args}
|
||||
)
|
||||
endforeach()
|
||||
endif()
|
||||
endforeach()
|
||||
endmacro()
|
||||
|
@ -260,20 +260,46 @@ function(_qt_internal_should_not_promote_package_target_to_global target out_var
|
||||
endfunction()
|
||||
|
||||
# This function recursively walks transitive link libraries of the given target
|
||||
# and promotes those targets to be IMPORTED_GLOBAL if they are not.
|
||||
# and promotes encountered 3rd party targets to be IMPORTED_GLOBAL if they are not.
|
||||
#
|
||||
# This is required for .prl file generation in top-level builds, to make sure that imported 3rd
|
||||
# party library targets in any repo are made global, so there are no scoping issues.
|
||||
#
|
||||
# Only works if called from qt_find_package(), because the promotion needs to happen in the same
|
||||
# directory scope where the imported target is first created.
|
||||
# The promotion needs to happen in the same directory scope where the imported target is
|
||||
# first created.
|
||||
#
|
||||
# Uses __qt_internal_walk_libs.
|
||||
function(_qt_find_package_promote_targets_to_global_scope target)
|
||||
function(_qt_internal_promote_3rd_party_link_targets_to_global target)
|
||||
__qt_internal_walk_libs("${target}" _discarded_out_var _discarded_out_var_2
|
||||
"qt_find_package_targets_dict" "promote_global")
|
||||
"qt_find_package_targets_dict" "promote_3rd_party_global")
|
||||
endfunction()
|
||||
|
||||
# Check if a target is an internal target (one added by qt_internal_* API, executables, libraries,
|
||||
# etc).
|
||||
function(_qt_internal_is_internal_target target out_var)
|
||||
get_target_property(is_internal ${target} _qt_is_internal_target)
|
||||
if(is_internal)
|
||||
set(value TRUE)
|
||||
else()
|
||||
set(value FALSE)
|
||||
endif()
|
||||
set(${out_var} "${value}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# Check if a target should never be promoted to global.
|
||||
# Some targets like the Platform target is public, and can't have _qt_is_internal_target set.
|
||||
# But we still want to avoid promoting it to global. Setting this property achieves that.
|
||||
function(_qt_internal_should_skip_3rd_party_global_promotion target out_var)
|
||||
get_target_property(should_skip ${target} _qt_should_skip_3rd_party_global_promotion)
|
||||
if(should_skip)
|
||||
set(value TRUE)
|
||||
else()
|
||||
set(value FALSE)
|
||||
endif()
|
||||
set(${out_var} "${value}" PARENT_SCOPE)
|
||||
endfunction()
|
||||
|
||||
# Tries to promote any non-global imported target to global scope.
|
||||
function(__qt_internal_promote_target_to_global target)
|
||||
get_property(is_global TARGET ${target} PROPERTY IMPORTED_GLOBAL)
|
||||
if(NOT is_global)
|
||||
@ -282,6 +308,48 @@ function(__qt_internal_promote_target_to_global target)
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Promotes a 3rd party provided target to global, which was found by qt_find_package or
|
||||
# _qt_internal_find_third_party_dependencies.
|
||||
# Only does it when building Qt, but not when building user projects.
|
||||
function(_qt_internal_promote_3rd_party_provided_target_and_3rd_party_deps_to_global target)
|
||||
# Return early if building a user project, and not Qt.
|
||||
# QT_BUILDING_QT is set when building a qt repo, but we also check for QT_REPO_MODULE_VERSION,
|
||||
# which is set in .cmake.conf, because _qt_internal_find_third_party_dependencies is called
|
||||
# before QT_BUILDING_QT is set.
|
||||
if(NOT (QT_BUILDING_QT OR QT_REPO_MODULE_VERSION))
|
||||
return()
|
||||
endif()
|
||||
|
||||
# Return early if the provided target does not exist, which can happen in the case of zstd,
|
||||
# where we list multiple possible target names, but only some will be available.
|
||||
if(NOT TARGET "${target}")
|
||||
return()
|
||||
endif()
|
||||
|
||||
get_property(is_global TARGET "${target}" PROPERTY IMPORTED_GLOBAL)
|
||||
_qt_internal_should_not_promote_package_target_to_global("${target}" should_not_promote)
|
||||
if(NOT is_global AND NOT should_not_promote)
|
||||
_qt_internal_promote_3rd_party_target_to_global(${target})
|
||||
_qt_internal_promote_3rd_party_link_targets_to_global("${target}")
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
# Tries to promote a non-global imported 3rd party target to global scope.
|
||||
# 3rd party targets are usually system library targets.
|
||||
# - targets that were not created by qt_internal_add_foo commands
|
||||
# - targets that don't have the should_skip_global_promotion property
|
||||
function(_qt_internal_promote_3rd_party_target_to_global target)
|
||||
get_property(is_global TARGET ${target} PROPERTY IMPORTED_GLOBAL)
|
||||
|
||||
if(NOT is_global)
|
||||
_qt_internal_is_internal_target("${target}" is_internal)
|
||||
_qt_internal_should_skip_3rd_party_global_promotion("${target}" should_skip)
|
||||
if(NOT is_internal AND NOT should_skip)
|
||||
__qt_internal_promote_target_to_global("${target}")
|
||||
endif()
|
||||
endif()
|
||||
endfunction()
|
||||
|
||||
function(__qt_internal_promote_target_to_global_checked target)
|
||||
# With CMake version 3.21 we use a different mechanism that allows us to promote all targets
|
||||
# within a scope.
|
||||
|
@ -74,7 +74,7 @@ endfunction()
|
||||
# operation: a string to tell the function what additional behaviors to execute.
|
||||
# 'collect_libs' (default) operation is to collect linker file paths and flags.
|
||||
# Used for prl file generation.
|
||||
# 'promote_global' promotes walked imported targets to global scope.
|
||||
# 'promote_3rd_party_global' promotes walked 3rd party imported targets to global scope.
|
||||
# 'collect_targets' collects all target names (discards framework or link flags)
|
||||
# 'direct_targets' collects only the direct target names (discards framework or link
|
||||
# flags)
|
||||
@ -238,7 +238,7 @@ function(__qt_internal_walk_libs
|
||||
__qt_internal_merge_libs(rcc_objects ${lib_rcc_objects_${target}})
|
||||
endif()
|
||||
endif()
|
||||
if(operation STREQUAL "promote_global")
|
||||
if(operation STREQUAL "promote_3rd_party_global")
|
||||
set(lib_target_unaliased "${lib_target}")
|
||||
get_target_property(aliased_target ${lib_target} ALIASED_TARGET)
|
||||
if(aliased_target)
|
||||
@ -252,7 +252,8 @@ function(__qt_internal_walk_libs
|
||||
_qt_internal_should_not_promote_package_target_to_global(
|
||||
"${lib_target_unaliased}" should_not_promote)
|
||||
if(is_imported AND NOT should_not_promote)
|
||||
__qt_internal_promote_target_to_global(${lib_target_unaliased})
|
||||
_qt_internal_promote_3rd_party_target_to_global(
|
||||
${lib_target_unaliased})
|
||||
endif()
|
||||
endif()
|
||||
elseif("${lib_target}" MATCHES "^(Qt|${QT_CMAKE_EXPORT_NAMESPACE})::(.*)")
|
||||
|
@ -1737,6 +1737,12 @@ endfunction()
|
||||
function(qt_internal_add_platform_target target)
|
||||
_qt_internal_add_library("${target}" INTERFACE)
|
||||
qt_internal_add_target_aliases("${target}")
|
||||
|
||||
# The platform targets should never be promoted to global via the
|
||||
# _qt_internal_promote_3rd_party_target_to_global command.
|
||||
set_property(TARGET "${target}" PROPERTY _qt_should_skip_3rd_party_global_promotion TRUE)
|
||||
set_property(TARGET "${target}" APPEND PROPERTY EXPORT_PROPERTIES
|
||||
"_qt_should_skip_3rd_party_global_promotion")
|
||||
endfunction()
|
||||
|
||||
# A small wrapper for adding the PlatformXInternal and GlobalConfig INTERFACE targets to apply
|
||||
@ -1752,32 +1758,3 @@ function(qt_internal_add_platform_internal_target target)
|
||||
IMMEDIATE_FINALIZATION
|
||||
)
|
||||
endfunction()
|
||||
|
||||
# The macro promotes the Qt platform targets and their dependencies to global. The macro shouldn't
|
||||
# be called explicitly in regular cases. It's called right after the first find_package(Qt ...)
|
||||
# call in the qt_internal_project_setup macro.
|
||||
# This allows using the qt_find_package(Wrap<3rdparty> PROVIDED_TARGETS ...) function,
|
||||
# without the risk of having duplicated global promotion of Qt internals. This is especially
|
||||
# sensitive for the bundled 3rdparty libraries.
|
||||
macro(qt_internal_promote_platform_targets_to_global)
|
||||
if(TARGET Qt6::Platform)
|
||||
get_target_property(is_imported Qt6::Platform IMPORTED)
|
||||
if(is_imported)
|
||||
set(known_platform_targets
|
||||
Platform
|
||||
PlatformCommonInternal
|
||||
PlatformModuleInternal
|
||||
PlatformPluginInternal
|
||||
PlatformAppInternal
|
||||
PlatformToolInternal
|
||||
)
|
||||
set(versionless_platform_targets ${known_platform_targets})
|
||||
|
||||
list(TRANSFORM known_platform_targets PREPEND Qt6::)
|
||||
list(TRANSFORM versionless_platform_targets PREPEND Qt::)
|
||||
qt_find_package(Qt6 PROVIDED_TARGETS
|
||||
${known_platform_targets}
|
||||
${versionless_platform_targets})
|
||||
endif()
|
||||
endif()
|
||||
endmacro()
|
||||
|
Loading…
x
Reference in New Issue
Block a user