| #!/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 |
| ### run a host tool produced by the build |
| |
| ## usage: fx host-tool [--no-build|--force-build] TOOLNAME [TOOL_ARGS...] |
| ## |
| ## This looks for a tool named TOOLNAME in 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 |
| ## --force-build always attempt to build the tool, even if it exists |
| |
| ## If the tool is not known to the build system, for example if it is not in |
| ## 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. |
| |
| set -e |
| |
| source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh || exit $? |
| fx-config-read |
| |
| build=true |
| force_build=false |
| |
| if [[ $# -gt 0 ]]; then |
| if [[ $1 == '--no-build' ]]; then |
| build=false |
| shift |
| elif [[ $1 == '--force-build' ]]; then |
| force_build=true |
| shift |
| fi |
| fi |
| |
| if [[ $# -eq 0 || $1 == -* ]]; then |
| fx-error "Invalid syntax" |
| fx-command-help |
| exit 1 |
| fi |
| |
| toolname="$1" |
| shift |
| |
| # 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)" |
| |
| needs_build=false |
| |
| if $force_build; then |
| needs_build=true |
| elif [[ ! -f "${FUCHSIA_BUILD_DIR}/${toolpath}" ]]; then |
| if ! $build; then |
| fx-error "Tool does not exist and flag --no-build was used. Please build it first." |
| fx-error " ${FUCHSIA_BUILD_DIR}/${toolpath}" |
| exit 2 |
| fi |
| needs_build=true |
| fi |
| |
| if $needs_build; then |
| fx-command-run build --no-zircon "${toolpath}" |
| fi |
| |
| "${FUCHSIA_BUILD_DIR}/${toolpath}" "$@" |