diff --git a/cmake/QtBaseGlobalTargets.cmake b/cmake/QtBaseGlobalTargets.cmake index 1e858a0f34a..7a101ed913f 100644 --- a/cmake/QtBaseGlobalTargets.cmake +++ b/cmake/QtBaseGlobalTargets.cmake @@ -325,6 +325,7 @@ set(__public_cmake_helpers cmake/QtPublicFinalizerHelpers.cmake cmake/QtPublicPluginHelpers.cmake cmake/QtPublicTargetHelpers.cmake + cmake/QtPublicToolHelpers.cmake cmake/QtPublicWalkLibsHelpers.cmake cmake/QtPublicFindPackageHelpers.cmake cmake/QtPublicDependencyHelpers.cmake diff --git a/cmake/QtBuild.cmake b/cmake/QtBuild.cmake index dfd96ce5768..74365a0b368 100644 --- a/cmake/QtBuild.cmake +++ b/cmake/QtBuild.cmake @@ -263,23 +263,6 @@ function(qt_setup_tool_path_command) endfunction() qt_setup_tool_path_command() -function(qt_internal_generate_tool_command_wrapper) - get_property(is_called GLOBAL PROPERTY _qt_internal_generate_tool_command_wrapper_called) - if(NOT CMAKE_HOST_WIN32 OR is_called) - return() - endif() - set(bindir "${QT_BUILD_INTERNALS_RELOCATABLE_INSTALL_PREFIX}/${INSTALL_BINDIR}") - file(TO_NATIVE_PATH "${bindir}" bindir) - set(tool_command_wrapper_path "${QT_BUILD_DIR}/${INSTALL_LIBEXECDIR}/qt_setup_tool_path.bat") - file(WRITE "${tool_command_wrapper_path}" "@echo off -set PATH=${bindir};%PATH% -%*") - set(QT_TOOL_COMMAND_WRAPPER_PATH "${tool_command_wrapper_path}" - CACHE INTERNAL "Path to the wrapper of the tool commands") - set_property(GLOBAL PROPERTY _qt_internal_generate_tool_command_wrapper_called TRUE) -endfunction() -qt_internal_generate_tool_command_wrapper() - # Platform define path, etc. if(WIN32) set(QT_DEFAULT_PLATFORM_DEFINITIONS WIN32 _ENABLE_EXTENDED_ALIGNED_STORAGE) @@ -560,6 +543,7 @@ include(QtPublicTargetHelpers) include(QtPublicWalkLibsHelpers) include(QtPublicFindPackageHelpers) include(QtPublicDependencyHelpers) +include(QtPublicToolHelpers) # TODO: This block provides support for old variables. It should be removed once # we remove all references to these variables in other Qt module repos. @@ -598,6 +582,11 @@ if(COMMAND _qt_internal_get_add_plugin_keywords) unset(__qt_internal_add_plugin_multi_args) endif() +# Create tool script wrapper if necessary. +# TODO: Remove once all direct usages of QT_TOOL_COMMAND_WRAPPER_PATH are replaced with function +# calls. +_qt_internal_generate_tool_command_wrapper() + # This sets up the poor man's scope finalizer mechanism. # For newer CMake versions, we use cmake_language(DEFER CALL) instead. if(CMAKE_VERSION VERSION_LESS "3.19.0") diff --git a/cmake/QtPublicToolHelpers.cmake b/cmake/QtPublicToolHelpers.cmake new file mode 100644 index 00000000000..1f3b76b4e81 --- /dev/null +++ b/cmake/QtPublicToolHelpers.cmake @@ -0,0 +1,88 @@ +function(_qt_internal_generate_tool_command_wrapper) + get_property(is_called GLOBAL PROPERTY _qt_internal_generate_tool_command_wrapper_called) + if(NOT CMAKE_HOST_WIN32 OR is_called) + return() + endif() + + set(prefixes "") + + # In a prefix build, the just-built tools should pick up libraries from the current repo build + # dir. + if(QT_BUILD_DIR) + list(APPEND prefixes "${QT_BUILD_DIR}") + endif() + + # Pick up libraries from the main location where Qt was installed during a Qt build. + if(QT_BUILD_INTERNALS_RELOCATABLE_INSTALL_PREFIX) + list(APPEND prefixes "${QT_BUILD_INTERNALS_RELOCATABLE_INSTALL_PREFIX}") + endif() + + # Needed for ExternalProjects examples, where the Qt build dir is passed via this variable + # to the example project. + if(QT_ADDITIONAL_PACKAGES_PREFIX_PATH) + __qt_internal_prefix_paths_to_roots(additional_roots + "${QT_ADDITIONAL_PACKAGES_PREFIX_PATH}") + list(APPEND prefixes ${QT_ADDITIONAL_PACKAGES_PREFIX_PATH}) + endif() + + # Fallback to wherever Qt6 package is. + if(QT6_INSTALL_PREFIX) + list(APPEND prefixes "${QT6_INSTALL_PREFIX}") + endif() + + # When building qtbase, QT6_INSTALL_BINS is not set yet. + if(INSTALL_BINDIR) + set(bin_suffix "${INSTALL_BINDIR}") + else() + set(bin_suffix "${QT6_INSTALL_BINS}") + endif() + + set(path_dirs "") + foreach(prefix IN LISTS prefixes) + set(bin_dir "${prefix}/${bin_suffix}") + if(EXISTS "${bin_dir}") + file(TO_NATIVE_PATH "${bin_dir}" path_dir) + list(APPEND path_dirs "${path_dir}") + endif() + endforeach() + + set(tool_command_wrapper_dir "${CMAKE_BINARY_DIR}/.qt/bin") + file(MAKE_DIRECTORY "${tool_command_wrapper_dir}") + set(tool_command_wrapper_path "${tool_command_wrapper_dir}/qt_setup_tool_path.bat") + + file(WRITE "${tool_command_wrapper_path}" "@echo off +set PATH=${path_dirs};%PATH% +%*") + + set(QT_TOOL_COMMAND_WRAPPER_PATH "${tool_command_wrapper_path}" + CACHE INTERNAL "Path to the wrapper of the tool commands") + + set_property(GLOBAL PROPERTY _qt_internal_generate_tool_command_wrapper_called TRUE) +endfunction() + +# Wraps a tool command with a script that contains the necessary environment for the tool to run +# correctly. +# _qt_internal_wrap_tool_command(var [args...]) +# Arguments: +# APPEND Selects the 'append' mode for the out_variable argument. +# SET Selects the 'set' mode for the out_variable argument. +function(_qt_internal_wrap_tool_command out_variable action) + set(append FALSE) + if(action STREQUAL "APPEND") + set(append TRUE) + elseif(NOT action STREQUAL "SET") + message(FATAL_ERROR "Invalid action specified ${action}. Supported actions: SET, APPEND") + endif() + + # Ensure the script wrapper exists. + _qt_internal_generate_tool_command_wrapper() + + set(cmd COMMAND ${QT_TOOL_COMMAND_WRAPPER_PATH} ${ARGN}) + + if(append) + list(APPEND ${out_variable} ${cmd}) + else() + set(${out_variable} ${cmd}) + endif() + set(${out_variable} "${${out_variable}}" PARENT_SCOPE) +endfunction() diff --git a/src/corelib/Qt6CoreMacros.cmake b/src/corelib/Qt6CoreMacros.cmake index eebccf55b83..5e96369df02 100644 --- a/src/corelib/Qt6CoreMacros.cmake +++ b/src/corelib/Qt6CoreMacros.cmake @@ -2581,30 +2581,6 @@ function(_qt_internal_apply_strict_cpp target) endif() endfunction() -# Wraps a tool command with a script that contains the necessary environment for the tool to run -# correctly. -# _qt_internal_wrap_tool_command(var [args...]) -# Arguments: -# APPEND Selects the 'append' mode for the out_variable argument. -# SET Selects the 'set' mode for the out_variable argument. -function(_qt_internal_wrap_tool_command out_variable action) - set(append FALSE) - if(action STREQUAL "APPEND") - set(append TRUE) - elseif(NOT action STREQUAL "SET") - message(FATAL_ERROR "Invalid action specified ${action}. Supported actions: SET, APPEND") - endif() - - set(cmd COMMAND ${QT_TOOL_COMMAND_WRAPPER_PATH} ${ARGN}) - - if(append) - list(APPEND ${out_variable} ${cmd}) - else() - set(${out_variable} ${cmd}) - endif() - set(${out_variable} "${${out_variable}}" PARENT_SCOPE) -endfunction() - # Copies properties of the dependency to the target. # Arguments: # PROPERTIES list of properties to copy. If not specified the following properties are copied