diff options
author | Amir Masoud Abdol <amir.abdol@qt.io> | 2023-05-03 16:01:27 +0200 |
---|---|---|
committer | Amir Masoud Abdol <amir.abdol@qt.io> | 2023-06-01 12:02:28 +0200 |
commit | a33dd60e21aaa81b5b8f96835eccc2dcb33940eb (patch) | |
tree | cca7f886ae650e9e0bfb1a7434a7dd4d02405f00 /cmake | |
parent | 344bf51cfd8536eb8f1f4a11d982cca0b849119e (diff) |
Add TRY_RUN option to qt_internal_add_tool
To make sure that Qt essential tools can be executed after a successful
build, we can now pass the TRY_RUN argument to `qt_internal_add_tool`.
On Windows, this option creates a custom command and a custom target
(${target}_try_run) for the tool, and tries to run the tool from a batch
script. If the program fails to run because of missing libraries, an
error will be shown, and build halts; otherwise,
`${target_name}_try_run_passed` file will be generated and the build
continues.
Pick-to: 6.5
Task-number: QTBUG-113273
Task-number: QTBUG-112747
Change-Id: I760588714bcf9db69505abe3df717733352a8284
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/QtToolHelpers.cmake | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/cmake/QtToolHelpers.cmake b/cmake/QtToolHelpers.cmake index 6902a1e219..21c608e304 100644 --- a/cmake/QtToolHelpers.cmake +++ b/cmake/QtToolHelpers.cmake @@ -15,6 +15,10 @@ # INSTALL_VERSIONED_LINK # Prefix build only. On installation, create a versioned hard-link of the installed file. # E.g. create a link of "bin/qmake6" to "bin/qmake". +# TRY_RUN +# On Windows, it creates a helper batch script that tests whether the tool can be executed +# successfully or not. If not, build halts and an error will be show, with tips on what +# might be cause, and how to fix it. # # One-value Arguments: # EXTRA_CMAKE_FILES @@ -42,7 +46,8 @@ function(qt_internal_add_tool target_name) USER_FACING INSTALL_VERSIONED_LINK EXCEPTIONS - NO_UNITY_BUILD) + NO_UNITY_BUILD + TRY_RUN) set(one_value_keywords TOOLS_TARGET INSTALL_DIR @@ -224,10 +229,58 @@ function(qt_internal_add_tool target_name) qt_internal_apply_staging_prefix_build_rpath_workaround() endif() + if(arg_TRY_RUN AND WIN32) + _qt_internal_add_try_run_post_build(${target_name}) + endif() + qt_enable_separate_debug_info(${target_name} "${install_dir}" QT_EXECUTABLE) qt_internal_install_pdb_files(${target_name} "${install_dir}") endfunction() +function(_qt_internal_add_try_run_post_build target) + qt_internal_get_upper_case_main_cmake_configuration(main_cmake_configuration) + get_target_property(target_out_dir ${target} + RUNTIME_OUTPUT_DIRECTORY_${main_cmake_configuration}) + get_target_property(target_bin_dir ${target} + BINARY_DIR) + + set(try_run_scripts_path "${target_bin_dir}/${target}_try_run.bat") + # The only reason -h is passed is because some of the tools, e.g., moc + # wait for an input without any arguments. + + qt_configure_file(OUTPUT "${try_run_scripts_path}" + CONTENT "@echo off + +${target_out_dir}/${target}.exe -h > nul 2>&1 + +if \"%errorlevel%\" == \"-1073741515\" ( +echo +echo '${target}' is built successfully, but some of the libraries +echo necessary for running it are missing. If you are building Qt with +echo 3rdparty libraries, make sure that you add their directory to the +echo PATH environment variable. +echo +exit /b %errorlevel% +) +echo. > ${target_bin_dir}/${target}_try_run_passed" + ) + + add_custom_command( + OUTPUT + ${target_bin_dir}/${target}_try_run_passed + DEPENDS + ${target} + COMMAND + cmd /c ${try_run_scripts_path} + COMMENT + "Testing ${target} by trying to run it." + VERBATIM + ) + + add_custom_target(${target}_try_run ALL + DEPENDS ${target_bin_dir}/${target}_try_run_passed) +endfunction() + function(qt_export_tools module_name) # Bail out when not building tools. if(NOT QT_WILL_BUILD_TOOLS) |