-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CMake: Allow promoting the Qt libraries to be global targets
User projects can set the QT_PROMOTE_TO_GLOBAL_TARGETS variable to true so that the various imported targets created by find_package(Qt6) are promoted to global targets. This would allow a project to find Qt packages in a subdirectory scope while using those Qt targets from a different scope. E.g. it fixes errors like CMake Error at CMakeLists.txt:5 (target_link_libraries): Error evaluating generator expression: $<TARGET_OBJECTS:Qt6::Widgets_resources_1> Objects of target "Qt6::Widgets_resources_1" referenced but no such target exists. when trying to use a static Qt from a sibling scope. Various 3rd party dependency targets (like Atomic or ZLIB) are not made global due to limitations in CMake, but as long as those targets are not mentioned directly, it shouldn't cause issues. The targets are made global in the generated QtFooAdditionalTargetInfo.cmake file. To ensure that resource object libraries promoted, the generation of the file has to be done at the end of the defining scope where qt_internal_export_additional_targets_file is called, which is achieved with a deferred finalizer. Replaced all occurrences of target promotion with a helper function which allows tracing of all promoted targets by specifying --log-level=debug to CMake. Pick-to: 6.2 Fixes: QTBUG-92878 Change-Id: Ic4ec03b0bc383d7e591a58c520c3974fbea746d2 Reviewed-by: Alexey Edelev <[email protected]> Reviewed-by: Qt CI Bot <[email protected]> Reviewed-by: Joerg Bornemann <[email protected]>
- Loading branch information
Showing
12 changed files
with
197 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
|
||
project(global_promotion) | ||
|
||
add_subdirectory(subdir_with_local_qt) | ||
add_subdirectory(subdir_with_global_qt) | ||
|
||
set(file_path "${CMAKE_CURRENT_BINARY_DIR}/main.cpp") | ||
file(GENERATE OUTPUT "${file_path}" CONTENT "int main() { return 0; }") | ||
add_executable(exe main.cpp) | ||
|
||
# The Qt targets found in the 2nd child directory scope should be available in this scope. | ||
target_link_libraries(exe PRIVATE lib_global_qt) |
14 changes: 14 additions & 0 deletions
14
tests/auto/cmake/test_global_promotion/subdir_with_global_qt/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
message(STATUS "Entered subdir_with_global_qt subdirectory") | ||
|
||
set(file_path "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp") | ||
file(GENERATE OUTPUT "${file_path}" CONTENT "int foo() { return 42; }") | ||
add_library(lib_global_qt STATIC "${file_path}") | ||
|
||
# These Qt targets will be available in all scopes of the project. | ||
# The previous local targets are simply shadowed. | ||
set(QT_PROMOTE_TO_GLOBAL_TARGETS ON) | ||
find_package(Qt6 REQUIRED COMPONENTS Gui) | ||
|
||
target_link_libraries(lib_global_qt PRIVATE Qt6::Gui) | ||
|
||
message(STATUS "Exiting subdir_with_global_qt subdirectory") |
12 changes: 12 additions & 0 deletions
12
tests/auto/cmake/test_global_promotion/subdir_with_local_qt/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
message(STATUS "Entered subdir_with_local_qt subdirectory") | ||
|
||
set(file_path "${CMAKE_CURRENT_BINARY_DIR}/lib.cpp") | ||
file(GENERATE OUTPUT "${file_path}" CONTENT "int foo() { return 42; }") | ||
add_library(lib_local_qt STATIC "${file_path}") | ||
|
||
# These Qt targets will be local to this directory scope. | ||
find_package(Qt6 REQUIRED COMPONENTS Gui) | ||
|
||
target_link_libraries(lib_local_qt PRIVATE Qt6::Gui) | ||
|
||
message(STATUS "Exiting subdir_with_local_qt subdirectory") |