qt5/cmake/QtIRProcessHelpers.cmake

166 lines
4.8 KiB
CMake
Raw Normal View History

CMake: Rewrite init-repository using CMake and .sh / .bat scripts init-repository is now implemented using CMake + .sh / .bat scripts. The intent behind the change is not to require Perl to checkout and build Qt, because it can be troublesome to acquire on Windows and it can also lead to issues during builds due to CMake picking up a Perl distribution-shipped compiler. All previous options were ported over like - module-subset - alternates - etc. A few new options were added: - --resolve-deps / --no-resolve-deps - --optional-deps / --no-optional-deps - --verbose and some other internal ones for testing reasons. The new script does automatic resolving of dependencies based on the depends / recommends keys in .gitmodules unless --no-resolve-deps is passed. So if you configure with --module-subset=qtsvg, the script will also initialize qtbase. If --no-optional-deps is passed, only required dependencies ('depends' ky) will be included and optional dependencies ('recommends' key) will be excluded. The new script now has a new default behavior when calling init-repository a second time with --force, without specifying a --module-subset option. Instead of initializing all submodules, it will just update the existing / previously initialized submodules. It also understands a new module-subset keyword "existing", which expands to the previously initialized submodules, so someone can initialize an additional submodule by calling init-repository -f --module-subset=existing,qtsvg Implementation notes: The overall code flow is init-repository -> cmake/QtIRScript.cmake -> qt_ir_run_main_script -> qt_ir_run_after_args_parsed -> qt_ir_handle_init_submodules (recursive) -> qt_ir_clone_one_submodule with some bells and whistles on the side. The command line parsing is an adapted copy of the functions in qtbase/cmake/QtProcessConfigureArgs.cmake. We can't use those exact functions because qtbase is not available when init-repository is initially called, and force cloning qtbase was deemed undesirable. We also have a new mechanism to detect whether init-repository was previously called. The perl script used the existence of the qtbase submodule as the check. In the cmake script, we instead set a custom marker into the local repo config file. Otherwise the code logic should be a faithful reimplementation of init-repository.pl aside from some small things like logging and progress reporting. The pre-existing git cloning logic in QtTopLevelHelpers was not used because it would not be compatible with the alternates option and I didn't want to accidentally break the pre-existing code. Plus init-repository is a bit opinionated about how it clones and checks out repos. The dependency collection and sorting logic uses the pre-existing code though. See follow up commit about implicitly calling init-repository when qt5/configure is called and the repo was not initialized before. [ChangeLog][General] init-repository was rewritten using CMake. Perl is no longer required to initialize the qt5.git super repo. Task-number: QTBUG-120030 Task-number: QTBUG-122622 Change-Id: Ibc38ab79d3fdedd62111ebbec496eabd64c20d2b Reviewed-by: Alexey Edelev <alexey.edelev@qt.io> Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io> Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
2023-12-15 10:22:55 +00:00
# Copyright (C) 2024 The Qt Company Ltd.
# SPDX-License-Identifier: BSD-3-Clause
# A low-level execute_process wrapper that can be used to execute a single command
# while controlling the verbosity and error handling.
function(qt_ir_execute_process)
set(options
QUIET
)
set(oneValueArgs
WORKING_DIRECTORY
OUT_RESULT_VAR
OUT_OUTPUT_VAR
OUT_ERROR_VAR
)
set(multiValueArgs
COMMAND_ARGS
EP_EXTRA_ARGS
)
cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT arg_COMMAND_ARGS)
message(FATAL_ERROR "Missing arguments to qt_ir_execute_process")
endif()
if(arg_WORKING_DIRECTORY)
set(working_dir_value "${arg_WORKING_DIRECTORY}")
else()
set(working_dir_value ".")
endif()
set(working_dir WORKING_DIRECTORY "${working_dir_value}")
set(result_variable "")
if(arg_OUT_RESULT_VAR)
set(result_variable RESULT_VARIABLE proc_result)
endif()
set(swallow_output "")
if(arg_OUT_OUTPUT_VAR OR arg_QUIET)
list(APPEND swallow_output OUTPUT_VARIABLE proc_output)
endif()
if(arg_OUT_ERROR_VAR OR arg_QUIET)
list(APPEND swallow_output ERROR_VARIABLE proc_error)
endif()
if(NOT arg_QUIET)
set(working_dir_message "")
qt_ir_is_verbose(verbose)
if(verbose)
set(working_dir_message " current working dir: ")
if(NOT working_dir_value STREQUAL ".")
string(APPEND working_dir_message "${working_dir_value}")
endif()
endif()
string(REPLACE ";" " " command_args_string "${arg_COMMAND_ARGS}")
message("+ ${command_args_string}${working_dir_message}")
endif()
execute_process(
COMMAND ${arg_COMMAND_ARGS}
${working_dir}
${result_variable}
${swallow_output}
${arg_EP_EXTRA_ARGS}
)
if(arg_OUT_RESULT_VAR)
set(${arg_OUT_RESULT_VAR} "${proc_result}" PARENT_SCOPE)
endif()
if(arg_OUT_OUTPUT_VAR)
set(${arg_OUT_OUTPUT_VAR} "${proc_output}" PARENT_SCOPE)
endif()
if(arg_OUT_ERROR_VAR)
set(${arg_OUT_ERROR_VAR} "${proc_error}" PARENT_SCOPE)
endif()
endfunction()
# A higher level execute_process wrapper that can be used to execute a single command
# that is a bit more opinionated and expects options related to init-repository
# functionality.
# It handles queietness, error handling and logging.
# It also allows for slightly more compact syntax for calling processes.
function(qt_ir_execute_process_and_log_and_handle_error)
set(options
NO_HANDLE_ERROR
FORCE_VERBOSE
FORCE_QUIET
)
set(oneValueArgs
WORKING_DIRECTORY
OUT_RESULT_VAR
OUT_OUTPUT_VAR
OUT_ERROR_VAR
ERROR_MESSAGE
)
set(multiValueArgs
COMMAND_ARGS
EP_EXTRA_ARGS
)
cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
qt_ir_get_option_value(quiet quiet)
set(quiet_option "")
if((quiet OR arg_FORCE_QUIET) AND NOT arg_FORCE_VERBOSE)
set(quiet_option "QUIET")
endif()
set(working_dir "")
if(arg_WORKING_DIRECTORY)
set(working_dir WORKING_DIRECTORY "${arg_WORKING_DIRECTORY}")
endif()
set(extra_args "")
if(arg_EP_EXTRA_ARGS)
set(extra_args EP_EXTRA_ARGS "${arg_EP_EXTRA_ARGS}")
endif()
set(out_output_var "")
if(arg_OUT_OUTPUT_VAR OR quiet)
set(out_output_var OUT_OUTPUT_VAR proc_output)
endif()
set(out_error_var "")
if(arg_OUT_ERROR_VAR OR quiet)
set(out_error_var OUT_ERROR_VAR proc_error)
endif()
qt_ir_execute_process(
${quiet_option}
COMMAND_ARGS ${arg_COMMAND_ARGS}
OUT_RESULT_VAR proc_result
${extra_args}
${working_dir}
${out_output_var}
${out_error_var}
)
if(NOT proc_result EQUAL 0 AND NOT arg_NO_HANDLE_ERROR)
set(error_message "")
if(arg_ERROR_MESSAGE)
set(error_message "${arg_ERROR_MESSAGE}\n")
endif()
string(REPLACE ";" " " cmd "${arg_COMMAND_ARGS}")
string(APPEND error_message "${cmd} exited with status: ${proc_result}\n")
if(proc_output)
string(APPEND error_message "stdout: ${proc_output}\n")
endif()
if(proc_error)
string(APPEND error_message "stderr: ${proc_error}\n")
endif()
message(FATAL_ERROR "${error_message}")
endif()
if(arg_OUT_RESULT_VAR)
set(${arg_OUT_RESULT_VAR} "${proc_result}" PARENT_SCOPE)
endif()
if(arg_OUT_OUTPUT_VAR)
set(${arg_OUT_OUTPUT_VAR} "${proc_output}" PARENT_SCOPE)
endif()
if(arg_OUT_ERROR_VAR)
set(${arg_OUT_ERROR_VAR} "${proc_error}" PARENT_SCOPE)
endif()
endfunction()