blob: b05c1ec8ce794d409308ea1c7212e56ae8507ff6 [file] [log] [blame] [edit]
#!/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.
# This script is a replacement for /usr/bin/env that adds Fuchsia-specific
# information and provides access to prebuilt tools in the source tree.
#
# This is not a subcommand of `fx` because multiple pieces of project
# infrastructure run outside of the build and need access to in-tree prebuilts.
set -e
# Filter out invalid FUCHSIA_DIR settings.
if [[ -n "${FUCHSIA_DIR}" ]]; then
if [[ ! -d "${FUCHSIA_DIR}" ]]; then
unset FUCHSIA_DIR
elif [[ "${FUCHSIA_DIR#/}" == "${FUCHSIA_DIR}" ]]; then
unset FUCHSIA_DIR
else
PWD="$(pwd)"
if [[ "${PWD#${FUCHSIA_DIR}}" = "${PWD}" ]]; then
# FUCHSIA_DIR does not contain the current directory
unset FUCHSIA_DIR
fi
fi
fi
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"
}
# Mimic `fx` behavior to set up a consistent root and get prebuilt paths.
#
# 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
_this_script="$0"
if [[ "$_this_script" != "${fuchsia_dir}/.jiri_root/bin/hermetic-env" &&
"$_this_script" != "${fuchsia_dir}/scripts/hermetic-env" ]]; then
_this_fuchsia_dir="$(find_tree_root "$(dirname "$_this_script")")"
if [[ $? -ne 0 || "$_this_fuchsia_dir" != "$fuchsia_dir" ]]; then
if ! cmp -s "$_this_script" "${fuchsia_dir}/.jiri_root/bin/hermetic-env"; then
echo >&2 "WARNING: You are executing hermetic-env from outside of the"
echo >&2 "current source tree, and it is a different version."
echo >&2 "This can cause inconsistencies. Please update your PATH."
echo >&2 " 'hermetic-env' was executed from: ${_this_script}"
exit 1
fi
fi
fi
export FUCHSIA_DIR="${fuchsia_dir}"
declare -r prebuilt_sh="${fuchsia_dir}/tools/devshell/lib/prebuilt.sh"
source "${prebuilt_sh}" || exit $?
if [[ "${PATH#${PREBUILT_ALL_PATHS}}" == "${PATH}" ]]; then
# Prebuilts have not been added to the path
if [[ x"${FUCHSIA_HERMETIC_TOOLS}" == xy ]]; then
readonly newpath="${PREBUILT_ALL_PATHS}:${FUCHSIA_DIR}/tools/system"
else
readonly newpath="${PREBUILT_ALL_PATHS}:${PATH}"
fi
else
readonly newpath="${PATH}"
fi
export PATH="${newpath}"
# Forward the args to the system `env`.
exec /usr/bin/env -S -i \
"FUCHSIA_DIR=${fuchsia_dir}" \
"TERM=${TERM}" \
"PATH=${newpath}" \
"FUCHSIA_HERMETIC_TOOLS=${FUCHSIA_HERMETIC_TOOLS}" \
${NINJA_STATUS+"NINJA_STATUS=${NINJA_STATUS}"} \
${GOMA_DISABLED+"GOMA_DISABLED=${GOMA_DISABLED}"} \
${TMPDIR+"TMPDIR=${TMPDIR}"} \
"$@"