|  | #!/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] [--disable-http2] [--disable-local-cache] | 
|  | ##                [--local-cache-dir ABS_PATH] | 
|  | ## | 
|  | ## 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. | 
|  | ## | 
|  | ## The `--disable-http2` switch disables the experimental HTTP2 proxy. | 
|  | ## By default, the HTTP2 proxy is enabled to improve Goma network | 
|  | ## performance. | 
|  | ## | 
|  | ## The `--disable-local-cache` switch disables the experimental local | 
|  | ## output cache. By default, this is enabled to reduce Goma network | 
|  | ## traffic. | 
|  | ## | 
|  | ## The `--local-cache-dir ABS_PATH` switch sets the cache directory for Goma. | 
|  | ## By default it uses the `//.jiri_root/goma_cache` directory. If you have | 
|  | ## multiple Fuchsia checkouts, it is recommended to set its value to | 
|  | ## `${HOME}/.goma_cache` to allow cache sharing. The value needs to be an | 
|  | ## absolute path. | 
|  | ## | 
|  | ## The `--kill` switch shuts down all goma related processes. | 
|  | ## | 
|  | ## **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 GOMACC="$PREBUILT_GOMA_DIR/gomacc" | 
|  | readonly PYTHON_CMD="$PREBUILT_PYTHON3_DIR/bin/python3.8" | 
|  | if [[ -z "$GOMA_LOCAL_OUTPUT_CACHE_DIR" ]]; then | 
|  | GOMA_CACHE_DIR="${FUCHSIA_DIR}/.jiri_root/goma_cache" | 
|  | else | 
|  | # inherit the local cache dir from the environment if already set | 
|  | GOMA_CACHE_DIR="$GOMA_LOCAL_OUTPUT_CACHE_DIR" | 
|  | fi | 
|  |  | 
|  | 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 use_http2=true use_local_cache=true use_ats=false | 
|  |  | 
|  | while [ $# -gt 0 ]; do | 
|  | case "$1" in | 
|  | --browser) | 
|  | login_args+=("$1") | 
|  | shift | 
|  | ;; | 
|  | --update) | 
|  | update=true | 
|  | shift | 
|  | ;; | 
|  | --disable-http2) | 
|  | use_http2=false | 
|  | shift | 
|  | ;; | 
|  | --disable-local-cache) | 
|  | use_local_cache=false | 
|  | shift | 
|  | ;; | 
|  | --enable-ats) | 
|  | use_ats=true | 
|  | shift | 
|  | ;; | 
|  | --local-cache-dir) | 
|  | if [[ $# -lt 2 ]]; then | 
|  | fx-command-help | 
|  | return 1 | 
|  | fi | 
|  | shift | 
|  | GOMA_CACHE_DIR="$1" | 
|  | 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 | 
|  |  | 
|  | "$PYTHON_CMD" "$GOMA_AUTH" info || "$PYTHON_CMD" "$GOMA_AUTH" login "$@" | 
|  | if "$PYTHON_CMD" "$GOMA_CTL" status; then | 
|  | "$PYTHON_CMD" "$GOMA_CTL" update_hook | 
|  | return 0 | 
|  | fi | 
|  | goma_tmp_dir="" | 
|  | if $use_local_cache; then | 
|  | goma_tmp_dir="$GOMA_CACHE_DIR" | 
|  | echo "*** Using Goma local output cache at \`$goma_tmp_dir\`" | 
|  | echo "*** If you experience cache errors, please run: " | 
|  | echo "*** \`fx goma --kill && fx goma --disable-local-cache\`" | 
|  | echo "*** to restart goma without local caching." | 
|  | echo "" | 
|  | echo "*** If you want to change the local output cache directory, please run" | 
|  | echo "*** \`fx goma --kill && fx goma --local-cache-dir ABS_PATH\`" | 
|  | echo "*** to restart goma with your desired cache location." | 
|  | echo "" | 
|  | fi | 
|  | if $use_http2; then | 
|  | 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." | 
|  | echo "" | 
|  | fi | 
|  | if ! $use_ats; then | 
|  | echo "*** Goma Arbitrary Toolchain Support(ATS) is disabled." | 
|  | echo "*** If you would like to use this feature, please run: " | 
|  | echo "*** \`fx goma --kill && fx goma --enable-ats\`" | 
|  | echo "*** to restart goma with ATS." | 
|  | echo "" | 
|  | fi | 
|  |  | 
|  | GOMA_ARBITRARY_TOOLCHAIN_SUPPORT="$use_ats" GOMACTL_USE_PROXY="$use_http2" GOMA_LOCAL_OUTPUT_CACHE_DIR="$goma_tmp_dir" "$PYTHON_CMD" "$GOMA_CTL" ensure_start | 
|  | } | 
|  |  | 
|  | function kill-goma { | 
|  | "$PYTHON_CMD" "$GOMA_CTL" ensure_stop | 
|  | } | 
|  |  | 
|  | # 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="$("$PYTHON_CMD" "$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 "$@" |