summaryrefslogtreecommitdiffstats
path: root/tests/auto/cmake
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2021-05-20 13:38:30 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2021-06-16 13:22:17 +0200
commit561fc8107f38b93808343e35c62d7d06704f8eb6 (patch)
tree552b0084d0fbc20c9e19dbd6bd244e93b28a590f /tests/auto/cmake
parentd829d54a42393d797c5f6ab3b80e88df35fad1e4 (diff)
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 <alexey.edelev@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'tests/auto/cmake')
-rw-r--r--tests/auto/cmake/CMakeLists.txt4
-rw-r--r--tests/auto/cmake/test_global_promotion/CMakeLists.txt13
-rw-r--r--tests/auto/cmake/test_global_promotion/subdir_with_global_qt/CMakeLists.txt14
-rw-r--r--tests/auto/cmake/test_global_promotion/subdir_with_local_qt/CMakeLists.txt12
4 files changed, 43 insertions, 0 deletions
diff --git a/tests/auto/cmake/CMakeLists.txt b/tests/auto/cmake/CMakeLists.txt
index b95c530640..8852c7e68b 100644
--- a/tests/auto/cmake/CMakeLists.txt
+++ b/tests/auto/cmake/CMakeLists.txt
@@ -247,6 +247,10 @@ set_tests_properties(test_import_plugins PROPERTIES FIXTURES_REQUIRED build_mock
_qt_internal_test_expect_pass(test_versionless_targets)
+if(NOT NO_GUI)
+ _qt_internal_test_expect_pass(test_global_promotion)
+endif()
+
_qt_internal_test_expect_pass(test_add_resources_binary_generated
BINARY test_add_resources_binary_generated)
diff --git a/tests/auto/cmake/test_global_promotion/CMakeLists.txt b/tests/auto/cmake/test_global_promotion/CMakeLists.txt
new file mode 100644
index 0000000000..c59dece81e
--- /dev/null
+++ b/tests/auto/cmake/test_global_promotion/CMakeLists.txt
@@ -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)
diff --git a/tests/auto/cmake/test_global_promotion/subdir_with_global_qt/CMakeLists.txt b/tests/auto/cmake/test_global_promotion/subdir_with_global_qt/CMakeLists.txt
new file mode 100644
index 0000000000..a8808a8c83
--- /dev/null
+++ b/tests/auto/cmake/test_global_promotion/subdir_with_global_qt/CMakeLists.txt
@@ -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")
diff --git a/tests/auto/cmake/test_global_promotion/subdir_with_local_qt/CMakeLists.txt b/tests/auto/cmake/test_global_promotion/subdir_with_local_qt/CMakeLists.txt
new file mode 100644
index 0000000000..1bed5a2f99
--- /dev/null
+++ b/tests/auto/cmake/test_global_promotion/subdir_with_local_qt/CMakeLists.txt
@@ -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")