diff options
author | Joerg Bornemann <joerg.bornemann@qt.io> | 2023-12-05 17:08:33 +0100 |
---|---|---|
committer | Joerg Bornemann <joerg.bornemann@qt.io> | 2023-12-12 20:06:01 +0100 |
commit | 96289e100d623245d244eeed2299f0e680454c10 (patch) | |
tree | 06d757a2749264d25a23c57775bbf1c84e50398d | |
parent | 13bd64e58ecde3f6ad530fac5c0ab7cc89e68f5c (diff) |
CMake: Fix qt_add_translations in different subdirs for VS generators
Commit 0338a2f7523e1fe21be325e76f6aba6b0c9e4467, was supposed to fix
QTBUG-115166: XXX_lupdate targets were always run when using a VS
generator.
The fix introduced the regression QTBUG-119555 if
qt_add_translations (or qt_add_lupdate) was run in different
sub-directories. We were hitting yet another restriction in the VS
project generator and project configuration errored out with:
TARGET 'update_translations' was not created in this directory.
This happened, because qt_add_lupdate added a PRE_BUILD step to
update_translations, but for the VS generator, this must happen in the
same subdirectory.
We now work around this problem by building foo_lupdate with "cmake
--build" in the COMMANDs of update_translations. As a consequence, we
must create update_translations at the end of the project directory
scope.
Since we now require cmake_language(DEFER CALL), users who generate VS
projects with CMake versions older than 3.19 will see a warning that the
update_translations target cannot be created. The VS installation comes
with a recent enough CMake version (on my machine it's 3.26.4), so this
should not be a problem.
[ChangeLog][CMake] When using CMake's Visual Studio project generator,
the creation of the update_translations target requires now CMake 3.19
or newer. A warning is printed for older CMake versions. This warning
can be disabled by setting QT_NO_GLOBAL_LUPDATE_TARGET_CREATION_WARNING
to ON.
Fixes: QTBUG-119555
Task-number: QTBUG-115166
Change-Id: I16b6ebd467544caf852b8791f4db8699f8d4c483
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
(cherry picked from commit 66aebd35ab37cbe93ad728412116177b4e182b80)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit aef0c3cf32885699fa2c6985136814db17d90ec6)
-rw-r--r-- | src/linguist/Qt6LinguistToolsMacros.cmake | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/src/linguist/Qt6LinguistToolsMacros.cmake b/src/linguist/Qt6LinguistToolsMacros.cmake index e9eb6153b..aca25ea89 100644 --- a/src/linguist/Qt6LinguistToolsMacros.cmake +++ b/src/linguist/Qt6LinguistToolsMacros.cmake @@ -195,29 +195,56 @@ set(lupdate_translations \"${ts_files}\") endif() if(NOT arg_NO_GLOBAL_TARGET) - if(NOT TARGET ${QT_GLOBAL_LUPDATE_TARGET}) - add_custom_target(${QT_GLOBAL_LUPDATE_TARGET}) - endif() if(CMAKE_GENERATOR MATCHES "^Visual Studio ") # For the Visual Studio generators we cannot use add_dependencies, because this would # enable ${target}_lupdate in the default build of the solution. See QTBUG-115166 and - # upstream CMake issue #16668 for details. As a work-around, we run the - # ${target}_lupdate through 'cmake --build' as PRE_BUILD step of the global lupdate - # target. - add_custom_command( - TARGET ${QT_GLOBAL_LUPDATE_TARGET} PRE_BUILD - COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" -t ${target}_lupdate - ) + # upstream CMake issue #16668 for details. Instead, we record ${target}_lupdate and + # create an update_translations target at the end of the top-level directory scope. + if(${CMAKE_VERSION} VERSION_LESS "3.19.0") + if(NOT QT_NO_GLOBAL_LUPDATE_TARGET_CREATION_WARNING) + message(WARNING + "Cannot create target ${QT_GLOBAL_LUPDATE_TARGET} with this CMake version. " + "Please upgrade to CMake 3.19.0 or newer. " + "Set QT_NO_GLOBAL_LUPDATE_TARGET_CREATION_WARNING to ON to disable this " + "warning." + ) + endif() + return() + endif() + set(property_name _qt_target_${QT_GLOBAL_LUPDATE_TARGET}_dependencies) + get_property(recorded_targets GLOBAL PROPERTY ${property_name}) + if("${recorded_targets}" STREQUAL "") + cmake_language(EVAL CODE + "cmake_language(DEFER DIRECTORY \"${CMAKE_SOURCE_DIR}\" CALL _qt_internal_add_global_lupdate_target_deferred \"${QT_GLOBAL_LUPDATE_TARGET}\")" + ) + endif() + set_property(GLOBAL APPEND PROPERTY ${property_name} ${target}_lupdate) # Exclude ${target}_lupdate from the solution's default build to avoid it being enabled # should the user add a dependency to it. set_property(TARGET ${target}_lupdate PROPERTY EXCLUDE_FROM_DEFAULT_BUILD ON) else() + if(NOT TARGET ${QT_GLOBAL_LUPDATE_TARGET}) + add_custom_target(${QT_GLOBAL_LUPDATE_TARGET}) + endif() add_dependencies(${QT_GLOBAL_LUPDATE_TARGET} ${target}_lupdate) endif() endif() endfunction() +# Hack for the Visual Studio generator. Create the global lupdate target named ${target} and work +# around the lack of a working add_dependencies by calling 'cmake --build' for every dependency. +function(_qt_internal_add_global_lupdate_target_deferred target) + get_property(target_dependencies GLOBAL PROPERTY _qt_target_${target}_dependencies) + set(target_commands "") + foreach(dependency IN LISTS target_dependencies) + list(APPEND target_commands + COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" -t ${dependency} + ) + endforeach() + add_custom_target(${target} ${target_commands}) +endfunction() + function(qt6_add_lrelease target) set(options NO_TARGET_DEPENDENCY |