xref: /aosp_15_r20/external/pigweed/bootstrap.sh (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2020 The Pigweed Authors
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 tested on bash, zsh, and dash.
16
17_bootstrap_abspath () {
18  $(command -v python3 || command -v python) -c "import os.path; print(os.path.abspath('$@'))"
19}
20
21# Shell: bash.
22if test -n "$BASH"; then
23  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
24# Shell: zsh.
25elif test -n "$ZSH_NAME"; then
26  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
27# Shell: dash.
28elif test ${0##*/} = dash; then
29  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath \
30    "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
31# If everything else fails, try $0. It could work.
32else
33  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
34fi
35
36# Check if this file is being executed or sourced.
37_pw_sourced=0
38if [ -n "$SWARMING_BOT_ID" ]; then
39  # If set we're running on swarming and don't need this check.
40  _pw_sourced=1
41elif [ -n "$ZSH_EVAL_CONTEXT" ]; then
42  case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac
43elif [ -n "$KSH_VERSION" ]; then
44  [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \
45    "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \
46    && _pw_sourced=1
47elif [ -n "$BASH_VERSION" ]; then
48  (return 0 2>/dev/null) && _pw_sourced=1
49else  # All other shells: examine $0 for known shell binary filenames
50  # Detects `sh` and `dash`; add additional shell filenames as needed.
51  case ${0##*/} in sh|dash) _pw_sourced=1;; esac
52fi
53
54# Downstream projects need to set something other than PW_ROOT here, like
55# YOUR_PROJECT_ROOT. Please also set PW_PROJECT_ROOT and PW_ROOT before
56# invoking pw_bootstrap or pw_activate.
57######### BEGIN PROJECT-SPECIFIC CODE #########
58PW_ROOT="$(dirname "$_PW_BOOTSTRAP_PATH")"
59
60# Please also set PW_PROJECT_ROOT to YOUR_PROJECT_ROOT.
61PW_PROJECT_ROOT="$PW_ROOT"
62
63# Downstream projects may wish to set PW_BANNER_FUNC to a function that prints
64# an ASCII art banner here.
65########## END PROJECT-SPECIFIC CODE ##########
66export PW_ROOT
67export PW_PROJECT_ROOT
68if [ -n "$PW_BANNER_FUNC" ]; then
69  export PW_BANNER_FUNC
70fi
71
72. "$PW_ROOT/pw_env_setup/util.sh"
73
74# Check environment properties
75pw_deactivate
76pw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH"
77if ! pw_check_root "$PW_ROOT"; then
78  return
79fi
80
81_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
82export _PW_ACTUAL_ENVIRONMENT_ROOT
83SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
84
85# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
86if [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
87  [ ! -f "$SETUP_SH" ] || \
88  [ ! -s "$SETUP_SH" ]; then
89######### BEGIN PROJECT-SPECIFIC CODE #########
90  pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT"
91########## END PROJECT-SPECIFIC CODE ##########
92  pw_finalize bootstrap "$SETUP_SH"
93else
94  pw_activate
95  pw_finalize activate "$SETUP_SH"
96fi
97
98if [ "$_PW_ENV_SETUP_STATUS" -eq 0 ]; then
99# This is where any additional checks about the environment should go.
100######### BEGIN PROJECT-SPECIFIC CODE #########
101  echo -n
102########## END PROJECT-SPECIFIC CODE ##########
103fi
104
105unset _pw_sourced
106unset _PW_BOOTSTRAP_PATH
107unset SETUP_SH
108unset _bootstrap_abspath
109
110if [[ "$TERM" != dumb ]]; then
111  # Shell completion: bash.
112  if test -n "$BASH"; then
113    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.bash"
114    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.bash"
115  # Shell completion: zsh.
116  elif test -n "$ZSH_NAME"; then
117    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.zsh"
118    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.zsh"
119  fi
120fi
121
122pw_cleanup
123
124git -C "$PW_ROOT" config blame.ignoreRevsFile .git-blame-ignore-revs
125