2022-07-05 13:26:52 +02:00
|
|
|
# Copyright (C) 2022 The Qt Company Ltd.
|
2022-08-19 15:21:34 +02:00
|
|
|
# SPDX-License-Identifier: BSD-3-Clause
|
2022-07-05 13:26:52 +02:00
|
|
|
|
2021-04-29 12:47:52 +02:00
|
|
|
function(__qt_internal_strip_target_directory_scope_token target out_var)
|
|
|
|
# In CMake versions earlier than CMake 3.18, a subdirectory scope id is appended to the
|
|
|
|
# target name if the target is referenced in a target_link_libraries command from a
|
|
|
|
# different directory scope than where the target was created.
|
|
|
|
# Strip it.
|
|
|
|
#
|
|
|
|
# For informational purposes, in CMake 3.18, the target name looks as follows:
|
|
|
|
# ::@(0x5604cb3f6b50);Threads::Threads;::@
|
|
|
|
# This case doesn't have to be stripped (at least for now), because when we iterate over
|
|
|
|
# link libraries, the tokens appear as separate target names.
|
|
|
|
#
|
|
|
|
# Example: Threads::Threads::@<0x5604cb3f6b50>
|
|
|
|
# Output: Threads::Threads
|
|
|
|
string(REGEX REPLACE "::@<.+>$" "" target "${target}")
|
|
|
|
set("${out_var}" "${target}" PARENT_SCOPE)
|
|
|
|
endfunction()
|
2021-05-19 19:46:24 +02:00
|
|
|
|
2021-06-10 19:01:35 +02:00
|
|
|
# Tests if linker could resolve circular dependencies between object files and static libraries.
|
2021-06-16 19:24:59 +02:00
|
|
|
function(__qt_internal_static_link_order_public_test result)
|
2021-06-10 19:01:35 +02:00
|
|
|
# We could trust iOS linker
|
|
|
|
if(IOS)
|
2021-07-08 18:54:53 +02:00
|
|
|
set(QT_HAVE_LINK_ORDER_MATTERS "FALSE" CACHE INTERNAL "Link order matters")
|
2021-06-10 19:01:35 +02:00
|
|
|
endif()
|
|
|
|
|
2021-06-16 19:24:59 +02:00
|
|
|
if(DEFINED QT_HAVE_LINK_ORDER_MATTERS)
|
|
|
|
set(${result} "${QT_HAVE_LINK_ORDER_MATTERS}" PARENT_SCOPE)
|
2021-06-10 19:01:35 +02:00
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if(EXISTS "${QT_CMAKE_DIR}")
|
2021-06-16 19:24:59 +02:00
|
|
|
set(test_source_basedir "${QT_CMAKE_DIR}/..")
|
2021-06-10 19:01:35 +02:00
|
|
|
else()
|
|
|
|
set(test_source_basedir "${_qt_cmake_dir}/${QT_CMAKE_EXPORT_NAMESPACE}")
|
|
|
|
endif()
|
|
|
|
|
|
|
|
try_compile(${result}
|
2021-06-16 19:24:59 +02:00
|
|
|
"${CMAKE_CURRENT_BINARY_DIR}/config.tests/static_link_order"
|
2021-06-10 19:01:35 +02:00
|
|
|
"${test_source_basedir}/config.tests/static_link_order"
|
|
|
|
static_link_order_test
|
|
|
|
static_link_order_test
|
|
|
|
)
|
2021-06-16 19:24:59 +02:00
|
|
|
message(STATUS "Check if linker can resolve circular dependencies - ${${result}}")
|
2021-06-10 19:01:35 +02:00
|
|
|
|
|
|
|
# Invert the result
|
|
|
|
if(${result})
|
|
|
|
set(${result} FALSE)
|
|
|
|
else()
|
|
|
|
set(${result} TRUE)
|
|
|
|
endif()
|
|
|
|
|
2021-07-08 18:54:53 +02:00
|
|
|
set(QT_HAVE_LINK_ORDER_MATTERS "${${result}}" CACHE INTERNAL "Link order matters")
|
2021-06-10 19:01:35 +02:00
|
|
|
|
|
|
|
set(${result} "${${result}}" PARENT_SCOPE)
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# Sets _qt_link_order_matters flag for the target.
|
|
|
|
function(__qt_internal_set_link_order_matters target link_order_matters)
|
|
|
|
if(NOT TARGET ${target})
|
|
|
|
message(FATAL_ERROR "Unable to set _qt_link_order_matters flag. ${target} is not a target.")
|
|
|
|
endif()
|
|
|
|
|
2025-03-05 12:17:44 +01:00
|
|
|
_qt_internal_dealias_target(target)
|
2021-06-10 19:01:35 +02:00
|
|
|
|
|
|
|
if(link_order_matters)
|
|
|
|
set(link_order_matters TRUE)
|
|
|
|
else()
|
|
|
|
set(link_order_matters FALSE)
|
|
|
|
endif()
|
|
|
|
set_target_properties(${target} PROPERTIES _qt_link_order_matters "${link_order_matters}")
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# Function combines __qt_internal_static_link_order_public_test and
|
2021-06-16 19:24:59 +02:00
|
|
|
# __qt_internal_set_link_order_matters calls on Qt::Platform target.
|
|
|
|
function(__qt_internal_check_link_order_matters)
|
2021-06-10 19:01:35 +02:00
|
|
|
__qt_internal_static_link_order_public_test(
|
2021-06-16 19:24:59 +02:00
|
|
|
link_order_matters
|
2021-06-10 19:01:35 +02:00
|
|
|
)
|
|
|
|
__qt_internal_set_link_order_matters(
|
2021-06-16 19:24:59 +02:00
|
|
|
${QT_CMAKE_EXPORT_NAMESPACE}::Platform "${link_order_matters}"
|
2021-06-10 19:01:35 +02:00
|
|
|
)
|
2021-06-16 19:24:59 +02:00
|
|
|
|
|
|
|
if("${ARGC}" GREATER "0" AND NOT ARGV0 STREQUAL "")
|
|
|
|
set(${ARGV0} ${link_order_matters} PARENT_SCOPE)
|
|
|
|
endif()
|
2021-06-10 19:01:35 +02:00
|
|
|
endfunction()
|
|
|
|
|
2021-06-29 18:00:39 +02:00
|
|
|
# Constructs a TARGET_POLICY genex expression if the policy is available.
|
|
|
|
function(__qt_internal_get_cmp0099_genex_check result)
|
|
|
|
if(POLICY CMP0099)
|
|
|
|
set(${result} "$<BOOL:$<TARGET_POLICY:CMP0099>>" PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
set(${result} "$<BOOL:FALSE>" PARENT_SCOPE)
|
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
function(__qt_internal_check_cmp0099_available)
|
|
|
|
set(platform_target ${QT_CMAKE_EXPORT_NAMESPACE}::Platform)
|
2025-03-05 12:17:44 +01:00
|
|
|
_qt_internal_dealias_target(platform_target)
|
2021-06-29 18:00:39 +02:00
|
|
|
|
|
|
|
__qt_internal_get_cmp0099_genex_check(cmp0099_check)
|
|
|
|
set_target_properties(${platform_target} PROPERTIES
|
|
|
|
_qt_cmp0099_policy_check "${cmp0099_check}"
|
|
|
|
)
|
|
|
|
|
|
|
|
set(result TRUE)
|
|
|
|
if(NOT POLICY CMP0099)
|
|
|
|
set(result FALSE)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if("${ARGC}" GREATER "0" AND NOT ARGV0 STREQUAL "")
|
|
|
|
set(${ARGV0} ${result} PARENT_SCOPE)
|
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
function(__qt_internal_process_dependency_object_libraries target)
|
|
|
|
# The CMake versions greater than 3.21 take care about the order of object files in a
|
2021-06-10 19:01:35 +02:00
|
|
|
# linker line, it's expected that all object files are located at the beginning of the linker
|
|
|
|
# line.
|
|
|
|
# So circular dependencies between static libraries and object files are resolved and no need
|
|
|
|
# to call the finalizer code.
|
|
|
|
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.21)
|
|
|
|
return()
|
|
|
|
endif()
|
2021-06-15 13:09:57 +02:00
|
|
|
get_target_property(processed ${target} _qt_object_libraries_finalizer_processed)
|
2021-05-19 19:46:24 +02:00
|
|
|
if(processed)
|
|
|
|
return()
|
|
|
|
endif()
|
2021-06-15 13:09:57 +02:00
|
|
|
set_target_properties(${target} PROPERTIES _qt_object_libraries_finalizer_processed TRUE)
|
2021-05-19 19:46:24 +02:00
|
|
|
|
2021-06-17 17:55:42 +02:00
|
|
|
get_target_property(qt_link_order_matters
|
|
|
|
${QT_CMAKE_EXPORT_NAMESPACE}::Platform _qt_link_order_matters
|
|
|
|
)
|
2021-06-10 19:01:35 +02:00
|
|
|
__qt_internal_check_finalizer_mode(${target}
|
|
|
|
use_finalizer_mode
|
2021-06-15 13:09:57 +02:00
|
|
|
object_libraries
|
2021-06-17 17:55:42 +02:00
|
|
|
DEFAULT_VALUE "${qt_link_order_matters}"
|
2021-06-10 19:01:35 +02:00
|
|
|
)
|
|
|
|
|
2021-05-19 19:46:24 +02:00
|
|
|
if(NOT use_finalizer_mode)
|
|
|
|
return()
|
|
|
|
endif()
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
__qt_internal_collect_dependency_object_libraries(${target} objects)
|
|
|
|
target_sources(${target} PRIVATE "${objects}")
|
2021-05-19 19:46:24 +02:00
|
|
|
endfunction()
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
function(__qt_internal_collect_dependency_object_libraries target out_var)
|
|
|
|
set_property(GLOBAL PROPERTY _qt_processed_object_libraries "")
|
2021-06-02 15:02:03 +02:00
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
__qt_internal_collect_object_libraries_recursively(object_libraries ${target} ${target})
|
2021-06-02 15:02:03 +02:00
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
# Collect object libraries of plugins and plugin dependencies.
|
2021-06-02 15:02:03 +02:00
|
|
|
__qt_internal_collect_plugin_targets_from_dependencies(${target} plugin_targets)
|
2021-06-15 13:09:57 +02:00
|
|
|
__qt_internal_collect_dependency_plugin_object_libraries(${target}
|
2021-06-02 15:02:03 +02:00
|
|
|
"${plugin_targets}"
|
2021-06-15 13:09:57 +02:00
|
|
|
plugin_objects
|
2021-06-02 15:02:03 +02:00
|
|
|
)
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
set_property(GLOBAL PROPERTY _qt_processed_object_libraries "")
|
2021-06-29 18:00:39 +02:00
|
|
|
__qt_internal_get_cmp0099_genex_check(cmp0099_check)
|
2021-05-19 19:46:24 +02:00
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
list(REMOVE_DUPLICATES object_libraries)
|
|
|
|
set(objects "")
|
|
|
|
foreach(dep IN LISTS object_libraries)
|
2021-06-29 18:00:39 +02:00
|
|
|
list(PREPEND objects "$<$<NOT:${cmp0099_check}>:$<TARGET_OBJECTS:${dep}>>")
|
2021-05-19 19:46:24 +02:00
|
|
|
endforeach()
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
set(${out_var} "${plugin_objects};${objects}" PARENT_SCOPE)
|
2021-06-02 15:02:03 +02:00
|
|
|
endfunction()
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
function(__qt_internal_collect_dependency_plugin_object_libraries target plugin_targets out_var)
|
2021-06-29 18:00:39 +02:00
|
|
|
__qt_internal_get_cmp0099_genex_check(cmp0099_check)
|
2021-06-15 13:09:57 +02:00
|
|
|
set(plugin_objects "")
|
2021-06-02 15:02:03 +02:00
|
|
|
foreach(plugin_target IN LISTS plugin_targets)
|
2021-06-15 13:09:57 +02:00
|
|
|
__qt_internal_collect_object_libraries_recursively(plugin_object_libraries
|
2021-06-02 15:02:03 +02:00
|
|
|
"${QT_CMAKE_EXPORT_NAMESPACE}::${plugin_target}"
|
|
|
|
${target}
|
|
|
|
)
|
|
|
|
__qt_internal_get_static_plugin_condition_genex("${plugin_target}" plugin_condition)
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
foreach(plugin_object_library IN LISTS plugin_object_libraries)
|
2021-06-29 18:00:39 +02:00
|
|
|
string(JOIN "" plugin_objects_genex
|
|
|
|
"$<"
|
|
|
|
"$<AND:"
|
|
|
|
"$<NOT:${cmp0099_check}>,"
|
|
|
|
"${plugin_condition}"
|
|
|
|
">"
|
|
|
|
":$<TARGET_OBJECTS:${plugin_object_library}>"
|
|
|
|
">"
|
2021-06-02 15:02:03 +02:00
|
|
|
)
|
2021-06-29 18:00:39 +02:00
|
|
|
list(APPEND plugin_objects "${plugin_objects_genex}")
|
2021-06-02 15:02:03 +02:00
|
|
|
endforeach()
|
|
|
|
endforeach()
|
2021-06-15 13:09:57 +02:00
|
|
|
set(${out_var} "${plugin_objects}" PARENT_SCOPE)
|
2021-05-19 19:46:24 +02:00
|
|
|
endfunction()
|
2021-05-28 14:02:29 +02:00
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
function(__qt_internal_collect_object_libraries_recursively out_var target initial_target)
|
|
|
|
get_property(processed_object_libraries GLOBAL PROPERTY _qt_processed_object_libraries)
|
2021-06-01 12:41:55 +02:00
|
|
|
|
|
|
|
set(interface_libs "")
|
|
|
|
set(libs "")
|
2021-05-28 14:02:29 +02:00
|
|
|
if(NOT "${target}" STREQUAL "${initial_target}")
|
|
|
|
get_target_property(interface_libs ${target} INTERFACE_LINK_LIBRARIES)
|
|
|
|
endif()
|
2021-06-01 12:41:55 +02:00
|
|
|
get_target_property(type ${target} TYPE)
|
|
|
|
if(NOT type STREQUAL "INTERFACE_LIBRARY")
|
|
|
|
get_target_property(libs ${target} LINK_LIBRARIES)
|
|
|
|
endif()
|
2021-05-28 14:02:29 +02:00
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
set(object_libraries "")
|
2021-05-28 14:02:29 +02:00
|
|
|
foreach(lib IN LISTS libs interface_libs)
|
2021-06-25 15:16:19 +02:00
|
|
|
# Extract possible target from exported LINK_ONLY dependencies.
|
|
|
|
# This is super important for traversing backing library dependencies of qml plugins.
|
|
|
|
if(lib MATCHES "^\\$<LINK_ONLY:(.*)>$")
|
|
|
|
set(lib "${CMAKE_MATCH_1}")
|
|
|
|
endif()
|
2021-05-28 14:02:29 +02:00
|
|
|
if(TARGET ${lib})
|
2025-03-05 12:17:44 +01:00
|
|
|
_qt_internal_dealias_target(lib)
|
2021-05-28 14:02:29 +02:00
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
if(${lib} IN_LIST processed_object_libraries)
|
2021-05-28 14:02:29 +02:00
|
|
|
continue()
|
|
|
|
else()
|
2021-06-15 13:09:57 +02:00
|
|
|
list(APPEND processed_object_libraries ${lib})
|
|
|
|
set_property(GLOBAL APPEND PROPERTY _qt_processed_object_libraries ${lib})
|
2021-05-28 14:02:29 +02:00
|
|
|
endif()
|
|
|
|
|
2021-06-15 13:09:57 +02:00
|
|
|
get_target_property(is_qt_propagated_object_library ${lib}
|
|
|
|
_is_qt_propagated_object_library
|
|
|
|
)
|
|
|
|
if(is_qt_propagated_object_library)
|
|
|
|
list(APPEND object_libraries ${lib})
|
2021-05-28 14:02:29 +02:00
|
|
|
else()
|
2021-06-15 13:09:57 +02:00
|
|
|
__qt_internal_collect_object_libraries_recursively(next_level_object_libraries
|
2021-06-02 15:02:03 +02:00
|
|
|
${lib}
|
|
|
|
${initial_target}
|
|
|
|
)
|
2021-06-15 13:09:57 +02:00
|
|
|
list(APPEND object_libraries ${next_level_object_libraries})
|
2021-05-28 14:02:29 +02:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endforeach()
|
2021-06-15 13:09:57 +02:00
|
|
|
set(${out_var} "${object_libraries}" PARENT_SCOPE)
|
2021-05-28 14:02:29 +02:00
|
|
|
endfunction()
|
2021-05-20 13:38:30 +02:00
|
|
|
|
2024-07-08 17:50:48 +02:00
|
|
|
# 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()
|
|
|
|
|
|
|
|
# This function recursively walks transitive link libraries of the given target
|
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>
2024-07-01 19:10:26 +02:00
|
|
|
# and promotes encountered 3rd party targets to be IMPORTED_GLOBAL if they are not.
|
2024-07-08 17:50:48 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
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>
2024-07-01 19:10:26 +02:00
|
|
|
# The promotion needs to happen in the same directory scope where the imported target is
|
|
|
|
# first created.
|
2024-07-08 17:50:48 +02:00
|
|
|
#
|
|
|
|
# Uses __qt_internal_walk_libs.
|
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>
2024-07-01 19:10:26 +02:00
|
|
|
function(_qt_internal_promote_3rd_party_link_targets_to_global target)
|
2024-07-08 17:50:48 +02:00
|
|
|
__qt_internal_walk_libs("${target}" _discarded_out_var _discarded_out_var_2
|
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>
2024-07-01 19:10:26 +02:00
|
|
|
"qt_find_package_targets_dict" "promote_3rd_party_global")
|
2024-07-08 17:50:48 +02:00
|
|
|
endfunction()
|
|
|
|
|
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>
2024-07-01 19:10:26 +02:00
|
|
|
# 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.
|
2021-05-20 13:38:30 +02:00
|
|
|
function(__qt_internal_promote_target_to_global target)
|
|
|
|
get_property(is_global TARGET ${target} PROPERTY IMPORTED_GLOBAL)
|
|
|
|
if(NOT is_global)
|
|
|
|
message(DEBUG "Promoting target to global: '${target}'")
|
|
|
|
set_property(TARGET ${target} PROPERTY IMPORTED_GLOBAL TRUE)
|
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
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>
2024-07-01 19:10:26 +02:00
|
|
|
# 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()
|
|
|
|
|
2021-05-20 13:38:30 +02:00
|
|
|
function(__qt_internal_promote_target_to_global_checked target)
|
2021-06-11 09:54:47 +02:00
|
|
|
# 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)
|
2021-05-20 13:38:30 +02:00
|
|
|
__qt_internal_promote_target_to_global(${target})
|
|
|
|
endif()
|
|
|
|
endfunction()
|
2021-06-11 09:54:47 +02:00
|
|
|
|
|
|
|
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()
|
2021-07-08 14:01:59 +02:00
|
|
|
|
|
|
|
function(_qt_internal_set_up_static_runtime_library target)
|
|
|
|
if(QT_FEATURE_static_runtime)
|
|
|
|
if(MSVC)
|
|
|
|
set_property(TARGET ${target} PROPERTY
|
|
|
|
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
|
|
|
|
elseif(MINGW)
|
2023-08-19 16:27:58 +08:00
|
|
|
get_target_property(target_type ${target} TYPE)
|
|
|
|
if(target_type STREQUAL "EXECUTABLE")
|
|
|
|
set(link_option PRIVATE)
|
|
|
|
else()
|
|
|
|
set(link_option INTERFACE)
|
|
|
|
endif()
|
|
|
|
if(CLANG)
|
|
|
|
target_link_options(${target} ${link_option} "LINKER:-Bstatic")
|
|
|
|
else()
|
|
|
|
target_link_options(${target} ${link_option} "-static")
|
|
|
|
endif()
|
2021-07-08 14:01:59 +02:00
|
|
|
endif()
|
|
|
|
endif()
|
|
|
|
endfunction()
|
2024-03-07 15:32:17 +01:00
|
|
|
|
|
|
|
function(_qt_internal_warn_about_example_add_subdirectory)
|
|
|
|
# This is set by qt_build_repo_impl_examples() in QtBuildRepoHelpers.cmake, only for developer
|
|
|
|
# builds, to catch examples that are added via add_subdirectory instead of via
|
|
|
|
# qt_internal_add_example.
|
|
|
|
if(QT_WARN_ABOUT_EXAMPLE_ADD_SUBDIRECTORY)
|
|
|
|
get_filename_component(dir_name "${PROJECT_SOURCE_DIR}" NAME)
|
|
|
|
message(AUTHOR_WARNING
|
|
|
|
"It looks like this example project was added via add_subdirectory instead of via "
|
|
|
|
"qt_internal_add_example. This causes issues in certain build configurations. Please "
|
|
|
|
"change the code to use\n qt_internal_add_example(${dir_name})\ninstead."
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
endfunction()
|
2025-01-06 17:14:40 +01:00
|
|
|
|
|
|
|
# Mark source files as generated.
|
|
|
|
#
|
|
|
|
# This sets `GENERATED` property to TRUE, along with other Qt relevant properties,
|
|
|
|
# e.g. `SKIP_LINTING`.
|
|
|
|
#
|
|
|
|
# Synopsis
|
|
|
|
#
|
|
|
|
# _qt_internal_set_source_file_generated(SOURCE <src1> ...
|
|
|
|
# [CONFIGURE_GENERATED]
|
|
|
|
# [SKIP_AUTOGEN]
|
|
|
|
# [DIRECTORY <dirs> ...]
|
|
|
|
# [TARGET_DIRECTORY <targets> ...]
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# Arguments
|
|
|
|
#
|
|
|
|
# `SOURCES`
|
|
|
|
# Source files that are generated.
|
|
|
|
#
|
|
|
|
# Equivalent to `set_source_files_properties(<files>)`.
|
|
|
|
#
|
|
|
|
# `DIRECTORY`
|
|
|
|
# Equivalent to `set_source_files_properties(DIRECTORY)`.
|
|
|
|
#
|
|
|
|
# `TARGET_DIRECTORY`
|
|
|
|
# Equivalent to `set_source_files_properties(TARGET_DIRECTORY)`.
|
|
|
|
#
|
|
|
|
# `SKIP_AUTOGEN`
|
|
|
|
# Set SKIP_AUTOGEN property to True as well.
|
|
|
|
#
|
|
|
|
# `CONFIGURE_GENERATED`
|
|
|
|
# Files are generated with `configure_file`.
|
|
|
|
# Does not set `GENERATED TRUE` property. This is needed to avoid removing the file when
|
|
|
|
# running the clean target.
|
|
|
|
function(_qt_internal_set_source_file_generated)
|
|
|
|
set(option_args
|
|
|
|
SKIP_AUTOGEN
|
|
|
|
CONFIGURE_GENERATED
|
|
|
|
)
|
|
|
|
set(single_args "")
|
|
|
|
set(multi_args
|
|
|
|
SOURCES
|
|
|
|
DIRECTORY
|
|
|
|
TARGET_DIRECTORY
|
|
|
|
)
|
|
|
|
|
|
|
|
cmake_parse_arguments(PARSE_ARGV 0 arg
|
|
|
|
"${option_args}" "${single_args}" "${multi_args}"
|
|
|
|
)
|
|
|
|
# Parse required variables
|
|
|
|
if(NOT arg_SOURCES AND QT_FEATURE_developer_build)
|
|
|
|
message(WARNING
|
|
|
|
"Unexpected call _qt_internal_set_source_file_generated with empty `SOURCES`."
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
# Prepend again the appropriate keywords to pass to `set_source_files_properties`
|
|
|
|
if(arg_DIRECTORY)
|
|
|
|
list(PREPEND arg_DIRECTORY DIRECTORY)
|
|
|
|
endif()
|
|
|
|
if(arg_TARGET_DIRECTORY)
|
|
|
|
list(PREPEND arg_TARGET_DIRECTORY TARGET_DIRECTORY)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Construct the properties list
|
|
|
|
set(properties "")
|
|
|
|
if(NOT arg_CONFIGURE_GENERATED)
|
|
|
|
list(APPEND properties
|
|
|
|
GENERATED TRUE
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
if(arg_SKIP_AUTOGEN)
|
|
|
|
list(APPEND properties
|
|
|
|
SKIP_AUTOGEN TRUE
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
# Add SKIP_LINTING if possible. We do not add it unconditionally here to avoid
|
|
|
|
# confusion when CMake ignores this variable.
|
|
|
|
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.27" AND NOT QT_FEATURE_lint_generated_code)
|
|
|
|
list(APPEND properties
|
|
|
|
SKIP_LINTING TRUE
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
set_source_files_properties(${arg_SOURCES}
|
|
|
|
${arg_DIRECTORY}
|
|
|
|
${arg_TARGET_DIRECTORY}
|
|
|
|
PROPERTIES ${properties}
|
|
|
|
)
|
|
|
|
endfunction()
|
2025-03-05 12:04:30 +01:00
|
|
|
|
|
|
|
# Get the real target checking for ALIASED_TARGET
|
|
|
|
function(_qt_internal_get_real_target out_var target)
|
|
|
|
get_target_property(aliased_target "${target}" ALIASED_TARGET)
|
|
|
|
if(aliased_target)
|
|
|
|
set(${out_var} "${aliased_target}" PARENT_SCOPE)
|
|
|
|
else()
|
|
|
|
set(${out_var} "${target}" PARENT_SCOPE)
|
|
|
|
endif()
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# Helpful shortcut to `_qt_internal_get_real_target` if we just need to dealias
|
|
|
|
function(_qt_internal_dealias_target target_var)
|
|
|
|
_qt_internal_get_real_target(${target_var} ${${target_var}})
|
|
|
|
set(${target_var} "${${target_var}}" PARENT_SCOPE)
|
|
|
|
endfunction()
|