blob: 412d6e7a57116a89dfae94a5424e249ed81b60f4 [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=Build
### manage Goma distributed compilation client
## Usage: fx goma [--browser] [--update]
##
## This makes sure that both initial setup tasks are complete and that
## periodic maintenance actions are done. It's probably only really
## necessary to run it once, but it should be harmless (and reasonably
## fast) to run it again any time, such as after a `jiri update`. It's
## just not a good idea to run it while you have a build running in the
## background, since it may result in restarting the local Goma client
## service and causing all new compilation commands to fail for a moment.
##
## See also `fx goma_auth` and `fx goma_ctl`. This mostly just runs those.
##
## The `fx goma_auth` command is usually only needed once at setup time to
## run `fx goma_auth login`, which is done by `fx goma` if needed. Use `fx
## goma_auth help` for details on `fx goma_auth` subcommands. If something
## seems to be wrong, you can run the command `fx goma_auth logout` and
## then repeat `fx goma`.
##
## The `fx goma_ctl` command controls the Goma client service on your local
## machine. Use `fx goma_ctl help` for details. The common subcommand used
## every day is `fx goma_ctl ensure_start` to make sure your local client
## service is running. (`fx goma` does this for you but only after a few other
## checks that might be somewhat slower than `fx goma_ctl ensure_start` alone.)
##
## The `--browser` switch is passed along to `fx goma_auth login` so that
## it attempts to launch a browser window to perform authentication. This
## may or may not work, depending on your desktop and command-line setup.
## Without that switch, it will print out a URL you need to visit in your
## browser to (authenticate and) copy a token to paste into a prompt.
##
## **NOTE:** _The following features are temporary for the transition._
##
## `fx goma` checks for an old Goma installation and recommends commands to
## update to the current recommended style managed by `fx goma`.
##
## It also checks the current Fuchsia build directory's Goma configuration.
## This is the only aspect of `fx goma` that refers to a build directory
## (as controlled by the `--dir` switch to `fx` or the most recent `fx set`
## or `fx use` command). It will report whether the build is set to use
## the recommended Goma setup managed by `fx goma`. If given the
## `--update` switch, it will modify the existing `args.gn` and then re-run
## `fx gen` to enable Goma with the standard setup. This results in the
## same configuration that a fresh `fx set` will with the `--goma` switch
## (or without the switch, once `fx goma` has been done once).
##
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/../lib/vars.sh || exit $?
set -e
readonly GOMA_AUTH="$PREBUILT_GOMA_DIR/goma_auth.py"
readonly GOMA_CTL="$PREBUILT_GOMA_DIR/goma_ctl.py"
readonly GOMA_HTTP2_PROXY="$PREBUILT_GOMA_DIR/http_proxy"
readonly GOMA_HTTP2_PROXY_PORT="8199"
# The URL of the backend cannot be read from prebuilt. Hard-code it for now.
BACKEND_URL="rbe-prod1.endpoints.fuchsia-infra-goma-prod.cloud.goog"
if [[ ! -x "$GOMA_AUTH" || ! -x "$GOMA_CTL" ]]; then
echo "*** $PREBUILT_GOMA_DIR is not current."
echo "*** Be sure to run \`jiri fetch-packages\` or \`jiri update\`."
exit 1
fi
function main {
local login_args=() update=false
while [ $# -gt 0 ]; do
case "$1" in
--browser)
login_args+=("$1")
shift
;;
--update)
update=true
shift
;;
--disable-http2)
use_http2=false
shift
;;
--kill)
kill-goma
return 0
;;
*)
fx-command-help
return 1
;;
esac
done
# TODO(mcgrathr): Remove these after some period when old installs are gone.
check
check-build $update
"$GOMA_AUTH" info || "$GOMA_AUTH" login "$@"
if "$GOMA_CTL" status; then
"$GOMA_CTL" update_hook
return 0
fi
if $use_http2; then
if [[ -n "$GOMA_SERVER_HOST" ]]; then
BACKEND_URL=$GOMA_SERVER_HOST
fi
setup-http2-proxy
# TODO(haowei): Remove these after HTTP2 proxy is merged into goma_ctl.
echo "*** Using Goma experimental HTTP2 proxy."
echo "*** If you experiencing instability, please run: "
echo "*** \`fx goma --kill && fx goma --disable-http2\`"
echo "*** to restart goma without HTTP2 proxy."
GOMA_SERVER_HOST=127.0.0.1 GOMA_SERVER_PORT="$GOMA_HTTP2_PROXY_PORT" GOMA_USE_SSL=false "$GOMA_CTL" ensure_start
else
"$GOMA_CTL" ensure_start
fi
}
function kill-goma {
set +e
pgrep http_proxy > /dev/null
retcode=$?
set -e
if [ $retcode -eq 0 ]; then
killall http_proxy
fi
"$GOMA_CTL" ensure_stop
}
function setup-http2-proxy {
set +e
pgrep http_proxy > /dev/null
retcode=$?
set -e
if [ $retcode -eq 0 ]; then
return
fi
(
trap '' 1
"$GOMA_HTTP2_PROXY" -server-host "$BACKEND_URL" -port "$GOMA_HTTP2_PROXY_PORT" 2>&1 >/dev/null &
)
}
# TODO(mcgrathr): Remove this after some period when old installs are gone.
function check {
if [[ -n "$GOMA_DIR" ]]; then
echo "*** Detected GOMA_DIR environment variable."
echo "*** Remove GOMA_DIR from the environment and run \`fx goma\` again."
echo "*** \`unset GOMA_DIR\` will remove it from the running shell."
echo "*** But check your dot files to make sure it's not set at login."
fi
local goma_dir="$("$GOMA_CTL" goma_dir 2> /dev/null)"
if [[ -d "$goma_dir" && \
"$(cd "$goma_dir" && /bin/pwd)" = "$(cd "$PREBUILT_GOMA_DIR" && /bin/pwd)" ]]; then
return 0
fi
# Running goma has different directory than current one, test the versions.
local running_goma_version="$("$goma_dir"/compiler_proxy --version)"
local prebuilt_goma_version="$("$PREBUILT_GOMA_DIR"/compiler_proxy --version)"
if [[ "$running_goma_version" = "$prebuilt_goma_version" ]]; then
# Running daemon has same version string as one from prebuilt.
return 0
fi
echo "*** Detected a different version of Goma daemon running at $goma_dir"
echo "*** Recommend \`fx goma_ctl ensure_stop\` and \`fx goma\` to restart Goma daemon with current version."
echo "*** Goma-client should not be used when there is a version mismatch."
return 1
}
# TODO(mcgrathr): Remove this after some period when old installs are gone.
function check-build {
local update=$1
unset USE_GOMA GOMA_DIR # Just in case it's in the environment.
fx-config-read || return
if [[ -z "$USE_GOMA" ]]; then
echo "*** Run \`fx gen\` and then try \`fx goma\` again."
echo "*** Or else just do a fresh \`fx set\` to get the right defaults!"
return 1
fi
local status=0
local gen=false
if [[ "$USE_GOMA" = true ]]; then
echo "$FUCHSIA_BUILD_DIR has Goma enabled."
elif $update; then
echo "*** Enabling Goma in $FUCHSIA_BUILD_DIR..."
sed -e /use_goma/d "$FUCHSIA_BUILD_DIR/args.gn" > "$FUCHSIA_BUILD_DIR/args.gn.new"
mv -f "$FUCHSIA_BUILD_DIR/args.gn.new" "$FUCHSIA_BUILD_DIR/args.gn"
echo "use_goma = true" >> "$FUCHSIA_BUILD_DIR/args.gn"
gen=true
else
echo "$FUCHSIA_BUILD_DIR has Goma disabled."
status=1
fi
if [[ -z "$GOMA_DIR" ]]; then
echo "$FUCHSIA_BUILD_DIR uses prebuilt Goma client."
elif $update; then
echo "*** $FUCHSIA_BUILD_DIR was using non-default goma_dir=\"$GOMA_DIR\"."
sed -i /goma_dir/d "$FUCHSIA_BUILD_DIR/args.gn"
echo "*** Reset to default prebuilt Goma client."
gen=true
else
echo "$FUCHSIA_BUILD_DIR uses non-default goma_dir=\"$GOMA_DIR\"."
status=1
fi
if $gen; then
echo "*** Running \`fx gen\` after $FUCHSIA_BUILD_DIR/args.gn updates..."
fx-command-run gen
fi
if [[ $status -ne 0 ]]; then
echo "*** Run \`fx goma --update\` to reset this build to defaults."
echo "*** Or else just do a fresh \`fx set\` to get the right defaults!"
fi
return $status
}
main "$@"