| #!/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. |
| |
| function find_tree_root { |
| local parent="$1" |
| if [[ ! -d "$parent" ]]; then |
| return 1 |
| fi |
| while [[ ! -d "${parent}/.jiri_root" ]]; do |
| parent="$(dirname "${parent}")" |
| if [[ "$parent" == "/" ]]; then |
| return 1 |
| fi |
| done |
| echo "$parent" |
| } |
| |
| # We walk the parent directories looking for .jiri_root rather than using |
| # BASH_SOURCE so that we find the fuchsia_dir enclosing the current working |
| # directory instead of the one containing this file in case the user has |
| # multiple source trees and is picking up this file from another one. |
| # |
| # NOTE: The FUCHSIA_DIR environment variable is ignored here because it |
| # could point to a different Fuchsia checkout in some developer setups. |
| # |
| # This logic is replicated in //scripts/fx, //scripts/hermetic_env, |
| # //scripts/zsh_completion/_fx, and //src/developer/ffx/scripts. For |
| # consistency, copy any changes here to those files as well. |
| if ! fuchsia_dir="$(find_tree_root "$(pwd)")"; then |
| echo >&2 "ERROR: Cannot find the Platform Source Tree in a parent of the current directory: $(pwd)" |
| exit 1 |
| fi |
| |
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" |
| # In cog workspace, .jiri_root in source tree would symlink to CartFS |
| # workspace, where the out directory exist and tests executed. |
| script_jiri_root="$(dirname $(readlink -f $(find_tree_root "${script_dir}")/.jiri_root))" |
| |
| # In cog workspace, we will check if we are running fx in either source tree or |
| # CartFS workspace. |
| if [[ "${fuchsia_dir}" != "${script_jiri_root}"* && "${script_dir}" != "${fuchsia_dir}"* ]]; then |
| echo >&2 "ERROR: You are executing fx from outside of the current source tree" |
| echo >&2 "ERROR: This is not supported as fx does not have a stable internal API" |
| echo >&2 |
| echo >&2 " 'ffx' was executed from: ${BASH_SOURCE[0]}" |
| echo >&2 " 'fuchsia directory' resolved to: ${fuchsia_dir}" |
| echo >&2 " 'script_dir' resolved to: ${script_dir}" |
| echo >&2 " 'script_jiri_root' resolved to: ${script_jiri_root}" |
| echo >&2 |
| echo >&2 "To run a command in the current Fuchsia directory, run fx from:" |
| echo >&2 " ${fuchsia_dir}/scripts/fx ffx" |
| exit 1 |
| fi |
| |
| # This script is usually used to invoke `ffx` directly while in-tree without |
| # explicitly wrapping it in `fx` (ie. `fx ffx --help`). If we don't set a |
| # wrapper here, the `fx`'s ffx subscript will override it and produce confusing |
| # help output. |
| # So be explicit, and unless something else overrode this when invoking *this* |
| # script, force our 'name' to just be `ffx`. |
| FFX_WRAPPER_INVOKE=${FFX_WRAPPER_INVOKE:-ffx} |
| export FFX_WRAPPER_INVOKE |
| |
| exec "${fuchsia_dir}"/scripts/fx ffx --config fuchsia.analytics.ffx_invoker=fx "$@" |