blob: a064895c88afd043a531dde5fde49fe28c841a20 [file] [log] [blame]
#!/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
## --check-firewall print a warning if the tool isn't included in firewall
## rules. This is a no-op outside of macOS.
## 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
check_firewall=false
function is_macos {
[[ "$(uname -s)" == "Darwin" ]]
}
function firewall_cmd_macos {
local fw
if type -P "socketfilterfw"; then
fw="socketfilterfw"
else
fw="/usr/libexec/ApplicationFirewall/socketfilterfw"
fi
"$fw" "$@"
}
function check_firewall_func {
if is_macos; then
if firewall_cmd_macos --getglobalstate | grep "disabled" > /dev/null; then
return 0
fi
if ! firewall_cmd_macos --getappblocked "$1" | grep "permitted" > /dev/null; then
fx-warn "Firewall rules are not configured, you may need to run \"fx setup-macos\""
return 0
fi
fi
}
while [[ $1 == --* ]]; do
if [[ $1 == '--no-build' ]]; then
build=false
shift
elif [[ $1 == '--force-build' ]]; then
force_build=true
shift
elif [[ $1 == '--check-firewall' ]]; then
check_firewall=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
# 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)"
# Print a warning if the firewall doesn't allow this tool from running.
if $check_firewall; then
check_firewall_func "${FUCHSIA_BUILD_DIR}/${toolpath}"
fi
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}" "$@"