| #!/bin/bash | |
| # Copyright 2020 The Fuchsia Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style license that can be | |
| # found in the LICENSE file. | |
| #### CATEGORY=Run, Inspect and Debug | |
| #### PREBUILTS=${PREBUILT_CLANG_DIR} | |
| ### run a host tool produced by the build | |
| ## usage: fx host-tool [--no-build] [--print] TOOLNAME [TOOL_ARGS...] | |
| ## | |
| ## This looks for a tool named TOOLNAME in fx_host_tools.json first, then falls | |
| ## back to tool_paths.json, and executes it with the provided TOOL_ARGS. | |
| ## --no-build does not attempt to build the tool if it does not exist | |
| ## --print print the tool path instead of executing it. TOOL_ARGS | |
| ## is ignored, but build behaves the same. | |
| ## | |
| ## If the tool is not registered in fx_host_tools.json or known to the GN build | |
| ## graph, a proper message will be printed and the script fails. | |
| ## This script is specially useful for other scripts, via fx-command-run, | |
| ## although it can also be used directly by final users. | |
| ## | |
| ## The tool will be built if host-tool is being run interactively or invoked by | |
| ## an agent, otherwise an error will be produced if the tool is missing. | |
| set -e | |
| # shellcheck source=tools/devshell/lib/vars.sh | |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? | |
| # shellcheck source=tools/devshell/lib/host_symbolizer.sh | |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/host_symbolizer.sh || exit $? | |
| no_build_flag=false | |
| print_only=false | |
| while [[ $# -gt 0 && $1 == --* ]]; do | |
| if [[ $1 == '--no-build' ]]; then | |
| no_build_flag=true | |
| shift | |
| elif [[ $1 == '--print' ]]; then | |
| print_only=true | |
| shift | |
| else | |
| fx-error "Unrecognized option: $1" | |
| fx-command-help | |
| exit 1 | |
| fi | |
| done | |
| if [[ $# -eq 0 || $1 == -* ]]; then | |
| fx-error "Invalid syntax" | |
| fx-command-help | |
| exit 1 | |
| fi | |
| toolname="$1" | |
| shift | |
| # Check the fx_host_tools.json host-tool registry first (vendor + core). | |
| reg_info="" | |
| reg_rc=0 | |
| reg_info="$(resolve_registered_host_tool "${toolname}")" || reg_rc=$? | |
| if [[ ${reg_rc} -eq 2 ]]; then | |
| exit 2 | |
| elif [[ ${reg_rc} -eq 0 ]]; then | |
| mapfile -t reg_fields <<< "${reg_info}" | |
| reg_vendor="${reg_fields[0]:-}" | |
| reg_target="${reg_fields[1]:-}" | |
| reg_script="${reg_fields[2]:-}" | |
| reg_inject="${reg_fields[3]:-0}" | |
| if [[ -n "${reg_target}" ]]; then | |
| # Export build dir and default target for compiled tools when configured, | |
| # without failing if no build dir exists (e.g. --no-build on a staged tool). | |
| if fx-build-dir-if-present >/dev/null 2>&1; then | |
| if [[ -f "${FUCHSIA_BUILD_DIR}/fx.config" ]]; then | |
| fx-build-config-load || exit $? | |
| elif [[ -d "${FUCHSIA_BUILD_DIR}" ]]; then | |
| fx-export-default-target | |
| fi | |
| fi | |
| fi | |
| pass_no_build=false | |
| non_tty_skip=false | |
| if [[ -n "${reg_target}" ]]; then | |
| if $no_build_flag; then | |
| pass_no_build=true | |
| elif [[ "${FX_FORCE_BUILD_HOST_TOOL:-0}" == "1" ]]; then | |
| # Let builder.py honor FX_FORCE_BUILD_HOST_TOOL=1 (or reject conflicts | |
| # with FX_NO_BUILD_HOST_TOOL=1). | |
| pass_no_build=false | |
| elif [[ "${FX_NO_BUILD_HOST_TOOL:-0}" == "1" ]]; then | |
| pass_no_build=true | |
| elif [[ "${FX_FORCE_BUILD:-0}" == "1" ]]; then | |
| pass_no_build=false | |
| elif [[ "${FX_NO_BUILD:-0}" == "1" ]]; then | |
| pass_no_build=true | |
| elif ! fx-is-stderr-tty && ! is_invoked_by_agent; then | |
| pass_no_build=true | |
| non_tty_skip=true | |
| fi | |
| fi | |
| ensure_rc=0 | |
| if $non_tty_skip; then | |
| ensure_err="" | |
| ensure_err="$(fx-ensure-registered-host-tool --no-build "${toolname}" "${reg_vendor}" "${reg_target}" "${reg_script}" 2>&1)" || ensure_rc=$? | |
| if [[ ${ensure_rc} -eq 0 ]]; then | |
| if [[ -n "${ensure_err}" ]]; then | |
| printf '%s\n' "${ensure_err}" >&2 | |
| fi | |
| elif [[ ${ensure_rc} -eq 1 ]]; then | |
| # Match host-tool's missing-tool error when building is skipped due to | |
| # non-TTY stderr rather than an explicit --no-build flag. | |
| _fx_registry_log "build '${toolname}' (${reg_target}): binary missing and stderr is not a TTY" | |
| fx-error "Tool \"${toolname}\" is required by this command and has not been built." | |
| fx-error "Please build it first: fx build --host @${reg_target#@}" | |
| exit 2 | |
| else | |
| if [[ -n "${ensure_err}" ]]; then | |
| printf '%s\n' "${ensure_err}" >&2 | |
| fi | |
| exit "${ensure_rc}" | |
| fi | |
| elif $pass_no_build; then | |
| fx-ensure-registered-host-tool --no-build "${toolname}" "${reg_vendor}" "${reg_target}" "${reg_script}" || ensure_rc=$? | |
| if [[ ${ensure_rc} -ne 0 ]]; then | |
| # Match host-tool's exit code 2 when a known tool is unbuilt and building is disabled. | |
| if [[ ${ensure_rc} -eq 1 ]]; then | |
| exit 2 | |
| fi | |
| exit "${ensure_rc}" | |
| fi | |
| else | |
| fx-ensure-registered-host-tool "${toolname}" "${reg_vendor}" "${reg_target}" "${reg_script}" || exit $? | |
| fi | |
| raw_reg_cmd="" | |
| raw_reg_cmd="$(_find_registered_executable_from_entry "${toolname}" "${reg_target}" "${reg_script}" "${reg_vendor}")" || exit $? | |
| mapfile -t reg_cmd <<< "${raw_reg_cmd}" | |
| if $print_only; then | |
| # For Python scripts, _find_registered_executable_from_entry outputs | |
| # (python3, script_path); print only the resolved tool/script path. | |
| echo "${reg_cmd[${#reg_cmd[@]}-1]}" | |
| exit 0 | |
| fi | |
| _fx_apply_prebuilt_paths "${reg_inject}" | |
| exec "${reg_cmd[@]}" "$@" | |
| fi | |
| _fx_registry_log "lookup '${toolname}': not in registry; falling back to tool_paths.json" | |
| # Fall back to GN tool_paths.json for unregistered host tools. | |
| fx-config-read | |
| build=true | |
| if $no_build_flag || [[ "${FX_NO_BUILD_HOST_TOOL:-0}" == "1" && "${FX_FORCE_BUILD_HOST_TOOL:-0}" != "1" ]]; then | |
| build=false | |
| elif [[ "${FX_FORCE_BUILD_HOST_TOOL:-0}" != "1" && "${FX_FORCE_BUILD:-0}" != "1" && "${FX_NO_BUILD:-0}" == "1" ]]; then | |
| build=false | |
| fi | |
| # list-build-artifacts fails with a reasonable error message if tool is unknown | |
| toolpath="$(fx-command-run list-build-artifacts --name "${toolname}" --expect-one tools)" | |
| resolved_tool="$(realpath -m "${FUCHSIA_BUILD_DIR}/${toolpath}")" | |
| if [[ "${resolved_tool}" == "${FUCHSIA_DIR}/prebuilt/"* ]]; then | |
| fx-ensure-prebuilt "${resolved_tool}" | |
| fi | |
| if [[ ! -f "${FUCHSIA_BUILD_DIR}/${toolpath}" ]]; then | |
| tool_label="$(fx-command-run list-build-artifacts --show-label --name "${toolname}" --expect-one tools)" | |
| # If the desire is to build, then it's important for the build to | |
| # have stdout and stderr to print to. There are two possible | |
| # conditions, one is that host-tool is being run with stdout going to | |
| # a terminal, the other is that stdout is being captured, for example | |
| # if the user is running `fx set-device`. In the latter case it is | |
| # desirable to attach the build to the controlling terminal, if there | |
| # is one. In the case where there is not, fall back to printing an | |
| # error, as if --no-build was passed. If stderr is also redirected, | |
| # then the build is unconditionally skipped unless invoked by an agent. | |
| if ! fx-is-stderr-tty && ! is_invoked_by_agent; then | |
| build=false | |
| fi | |
| if ! $build; then | |
| fx-error "Tool \"${toolname}\" is required by this command and has not been built." | |
| fx-error "Please build it first: fx build ${tool_label}" | |
| exit 2 | |
| fi | |
| { | |
| echo "Building required tool '${toolname}' from \"${tool_label}\"..." | |
| fx-command-run build "${tool_label}" || exit $? | |
| } 1>&2 | |
| fi | |
| if $print_only; then | |
| echo "${FUCHSIA_BUILD_DIR}/${toolpath}" | |
| else | |
| exec "${FUCHSIA_BUILD_DIR}/${toolpath}" "$@" | |
| fi |