xref: /aosp_15_r20/external/open-dice/bootstrap.sh (revision 60b67249c2e226f42f35cc6cfe66c6048e0bae6b)
1# Copyright 2020 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15# This script must be sourced, not executed.
16#
17# Create a new environment:
18#
19#   source bootstrap.sh
20#
21# Activate an existing environment:
22#
23#   source activate.sh
24
25_bootstrap_abspath () {
26  $(command -v python3 || command -v python2 || command -v python) -c "import os.path; print(os.path.abspath('$@'))"
27}
28
29# Shell: bash.
30if test -n "$BASH"; then
31  _BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
32# Shell: zsh.
33elif test -n "$ZSH_NAME"; then
34  _BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
35# Shell: dash.
36elif test ${0##*/} = dash; then
37  _BOOTSTRAP_PATH="$(_bootstrap_abspath \
38    "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
39# If everything else fails, try $0. It could work.
40else
41  _BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
42fi
43
44# Check if this file is being executed or sourced.
45_pw_sourced=0
46if [ -n "$SWARMING_BOT_ID" ]; then
47  # If set we're running on swarming and don't need this check.
48  _pw_sourced=1
49elif [ -n "$ZSH_EVAL_CONTEXT" ]; then
50  case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac
51elif [ -n "$KSH_VERSION" ]; then
52  [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \
53    "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \
54    && _pw_sourced=1
55elif [ -n "$BASH_VERSION" ]; then
56  (return 0 2>/dev/null) && _pw_sourced=1
57else  # All other shells: examine $0 for known shell binary filenames
58  # Detects `sh` and `dash`; add additional shell filenames as needed.
59  case ${0##*/} in sh|dash) _pw_sourced=1;; esac
60fi
61
62# Downstream projects need to set something other than PW_ROOT here, like
63# YOUR_PROJECT_ROOT. Please also set PW_PROJECT_ROOT and PW_ROOT before
64# invoking pw_bootstrap or pw_activate.
65######### BEGIN PROJECT-SPECIFIC CODE #########
66OPEN_DICE_ROOT="$(_bootstrap_abspath "$(dirname "$_BOOTSTRAP_PATH")")"
67export OPEN_DICE_ROOT
68PW_PROJECT_ROOT="$OPEN_DICE_ROOT"
69PW_ROOT="$OPEN_DICE_ROOT/third_party/pigweed/src"
70
71# Set your project's banner and color.
72PW_BRANDING_BANNER="$OPEN_DICE_ROOT/banner.txt"
73export PW_BRANDING_BANNER
74PW_BRANDING_BANNER_COLOR=magenta
75export PW_BRANDING_BANNER_COLOR
76
77open_dice_banner() {
78  cat "$PW_BRANDING_BANNER"
79  echo
80}
81
82PW_BANNER_FUNC="open_dice_banner"
83########## END PROJECT-SPECIFIC CODE ##########
84export PW_BANNER_FUNC
85export PW_PROJECT_ROOT
86export PW_ROOT
87
88_util_sh="$PW_ROOT/pw_env_setup/util.sh"
89
90# Double-check that the Pigweed submodule has been checked out.
91if [ ! -e "$_util_sh" ]; then
92  echo "Error: $_util_sh not found."
93  echo "Did you forget to initialize the git submodules?"
94  echo "To setup the git submodules run:"
95  echo "  git submodule update --init"
96  return
97fi
98
99. "$_util_sh"
100
101pw_deactivate
102pw_eval_sourced "$_pw_sourced"
103pw_check_root "$PW_ROOT"
104_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
105export _PW_ACTUAL_ENVIRONMENT_ROOT
106SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
107
108# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
109if [ "$(basename "$_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
110  [ ! -f "$SETUP_SH" ] || \
111  [ ! -s "$SETUP_SH" ]; then
112# This is where pw_bootstrap is called. Most small projects will include
113# --use-pigweed-defaults.
114######### BEGIN PROJECT-SPECIFIC CODE #########
115  pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_PROJECT_ROOT/pigweed.json"
116########## END PROJECT-SPECIFIC CODE ##########
117  pw_finalize bootstrap "$SETUP_SH"
118else
119  pw_activate
120  pw_finalize activate "$SETUP_SH"
121fi
122
123if [ "$_PW_ENV_SETUP_STATUS" -eq 0 ]; then
124# This is where any additional checks about the environment should go.
125######### BEGIN PROJECT-SPECIFIC CODE #########
126  echo -n
127########## END PROJECT-SPECIFIC CODE ##########
128fi
129
130unset _pw_sourced
131unset _BOOTSTRAP_PATH
132unset SETUP_SH
133unset _bootstrap_abspath
134unset _util_sh
135
136pw_cleanup
137