xref: /aosp_15_r20/external/pigweed/bootstrap.sh (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker# Copyright 2020 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker#
3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker# the License at
6*61c4878aSAndroid Build Coastguard Worker#
7*61c4878aSAndroid Build Coastguard Worker#     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker#
9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker# the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker# This script must be tested on bash, zsh, and dash.
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker_bootstrap_abspath () {
18*61c4878aSAndroid Build Coastguard Worker  $(command -v python3 || command -v python) -c "import os.path; print(os.path.abspath('$@'))"
19*61c4878aSAndroid Build Coastguard Worker}
20*61c4878aSAndroid Build Coastguard Worker
21*61c4878aSAndroid Build Coastguard Worker# Shell: bash.
22*61c4878aSAndroid Build Coastguard Workerif test -n "$BASH"; then
23*61c4878aSAndroid Build Coastguard Worker  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
24*61c4878aSAndroid Build Coastguard Worker# Shell: zsh.
25*61c4878aSAndroid Build Coastguard Workerelif test -n "$ZSH_NAME"; then
26*61c4878aSAndroid Build Coastguard Worker  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
27*61c4878aSAndroid Build Coastguard Worker# Shell: dash.
28*61c4878aSAndroid Build Coastguard Workerelif test ${0##*/} = dash; then
29*61c4878aSAndroid Build Coastguard Worker  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath \
30*61c4878aSAndroid Build Coastguard Worker    "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
31*61c4878aSAndroid Build Coastguard Worker# If everything else fails, try $0. It could work.
32*61c4878aSAndroid Build Coastguard Workerelse
33*61c4878aSAndroid Build Coastguard Worker  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
34*61c4878aSAndroid Build Coastguard Workerfi
35*61c4878aSAndroid Build Coastguard Worker
36*61c4878aSAndroid Build Coastguard Worker# Check if this file is being executed or sourced.
37*61c4878aSAndroid Build Coastguard Worker_pw_sourced=0
38*61c4878aSAndroid Build Coastguard Workerif [ -n "$SWARMING_BOT_ID" ]; then
39*61c4878aSAndroid Build Coastguard Worker  # If set we're running on swarming and don't need this check.
40*61c4878aSAndroid Build Coastguard Worker  _pw_sourced=1
41*61c4878aSAndroid Build Coastguard Workerelif [ -n "$ZSH_EVAL_CONTEXT" ]; then
42*61c4878aSAndroid Build Coastguard Worker  case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac
43*61c4878aSAndroid Build Coastguard Workerelif [ -n "$KSH_VERSION" ]; then
44*61c4878aSAndroid Build Coastguard Worker  [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \
45*61c4878aSAndroid Build Coastguard Worker    "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \
46*61c4878aSAndroid Build Coastguard Worker    && _pw_sourced=1
47*61c4878aSAndroid Build Coastguard Workerelif [ -n "$BASH_VERSION" ]; then
48*61c4878aSAndroid Build Coastguard Worker  (return 0 2>/dev/null) && _pw_sourced=1
49*61c4878aSAndroid Build Coastguard Workerelse  # All other shells: examine $0 for known shell binary filenames
50*61c4878aSAndroid Build Coastguard Worker  # Detects `sh` and `dash`; add additional shell filenames as needed.
51*61c4878aSAndroid Build Coastguard Worker  case ${0##*/} in sh|dash) _pw_sourced=1;; esac
52*61c4878aSAndroid Build Coastguard Workerfi
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker# Downstream projects need to set something other than PW_ROOT here, like
55*61c4878aSAndroid Build Coastguard Worker# YOUR_PROJECT_ROOT. Please also set PW_PROJECT_ROOT and PW_ROOT before
56*61c4878aSAndroid Build Coastguard Worker# invoking pw_bootstrap or pw_activate.
57*61c4878aSAndroid Build Coastguard Worker######### BEGIN PROJECT-SPECIFIC CODE #########
58*61c4878aSAndroid Build Coastguard WorkerPW_ROOT="$(dirname "$_PW_BOOTSTRAP_PATH")"
59*61c4878aSAndroid Build Coastguard Worker
60*61c4878aSAndroid Build Coastguard Worker# Please also set PW_PROJECT_ROOT to YOUR_PROJECT_ROOT.
61*61c4878aSAndroid Build Coastguard WorkerPW_PROJECT_ROOT="$PW_ROOT"
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard Worker# Downstream projects may wish to set PW_BANNER_FUNC to a function that prints
64*61c4878aSAndroid Build Coastguard Worker# an ASCII art banner here.
65*61c4878aSAndroid Build Coastguard Worker########## END PROJECT-SPECIFIC CODE ##########
66*61c4878aSAndroid Build Coastguard Workerexport PW_ROOT
67*61c4878aSAndroid Build Coastguard Workerexport PW_PROJECT_ROOT
68*61c4878aSAndroid Build Coastguard Workerif [ -n "$PW_BANNER_FUNC" ]; then
69*61c4878aSAndroid Build Coastguard Worker  export PW_BANNER_FUNC
70*61c4878aSAndroid Build Coastguard Workerfi
71*61c4878aSAndroid Build Coastguard Worker
72*61c4878aSAndroid Build Coastguard Worker. "$PW_ROOT/pw_env_setup/util.sh"
73*61c4878aSAndroid Build Coastguard Worker
74*61c4878aSAndroid Build Coastguard Worker# Check environment properties
75*61c4878aSAndroid Build Coastguard Workerpw_deactivate
76*61c4878aSAndroid Build Coastguard Workerpw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH"
77*61c4878aSAndroid Build Coastguard Workerif ! pw_check_root "$PW_ROOT"; then
78*61c4878aSAndroid Build Coastguard Worker  return
79*61c4878aSAndroid Build Coastguard Workerfi
80*61c4878aSAndroid Build Coastguard Worker
81*61c4878aSAndroid Build Coastguard Worker_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
82*61c4878aSAndroid Build Coastguard Workerexport _PW_ACTUAL_ENVIRONMENT_ROOT
83*61c4878aSAndroid Build Coastguard WorkerSETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard Worker# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
86*61c4878aSAndroid Build Coastguard Workerif [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
87*61c4878aSAndroid Build Coastguard Worker  [ ! -f "$SETUP_SH" ] || \
88*61c4878aSAndroid Build Coastguard Worker  [ ! -s "$SETUP_SH" ]; then
89*61c4878aSAndroid Build Coastguard Worker######### BEGIN PROJECT-SPECIFIC CODE #########
90*61c4878aSAndroid Build Coastguard Worker  pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT"
91*61c4878aSAndroid Build Coastguard Worker########## END PROJECT-SPECIFIC CODE ##########
92*61c4878aSAndroid Build Coastguard Worker  pw_finalize bootstrap "$SETUP_SH"
93*61c4878aSAndroid Build Coastguard Workerelse
94*61c4878aSAndroid Build Coastguard Worker  pw_activate
95*61c4878aSAndroid Build Coastguard Worker  pw_finalize activate "$SETUP_SH"
96*61c4878aSAndroid Build Coastguard Workerfi
97*61c4878aSAndroid Build Coastguard Worker
98*61c4878aSAndroid Build Coastguard Workerif [ "$_PW_ENV_SETUP_STATUS" -eq 0 ]; then
99*61c4878aSAndroid Build Coastguard Worker# This is where any additional checks about the environment should go.
100*61c4878aSAndroid Build Coastguard Worker######### BEGIN PROJECT-SPECIFIC CODE #########
101*61c4878aSAndroid Build Coastguard Worker  echo -n
102*61c4878aSAndroid Build Coastguard Worker########## END PROJECT-SPECIFIC CODE ##########
103*61c4878aSAndroid Build Coastguard Workerfi
104*61c4878aSAndroid Build Coastguard Worker
105*61c4878aSAndroid Build Coastguard Workerunset _pw_sourced
106*61c4878aSAndroid Build Coastguard Workerunset _PW_BOOTSTRAP_PATH
107*61c4878aSAndroid Build Coastguard Workerunset SETUP_SH
108*61c4878aSAndroid Build Coastguard Workerunset _bootstrap_abspath
109*61c4878aSAndroid Build Coastguard Worker
110*61c4878aSAndroid Build Coastguard Workerif [[ "$TERM" != dumb ]]; then
111*61c4878aSAndroid Build Coastguard Worker  # Shell completion: bash.
112*61c4878aSAndroid Build Coastguard Worker  if test -n "$BASH"; then
113*61c4878aSAndroid Build Coastguard Worker    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.bash"
114*61c4878aSAndroid Build Coastguard Worker    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.bash"
115*61c4878aSAndroid Build Coastguard Worker  # Shell completion: zsh.
116*61c4878aSAndroid Build Coastguard Worker  elif test -n "$ZSH_NAME"; then
117*61c4878aSAndroid Build Coastguard Worker    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.zsh"
118*61c4878aSAndroid Build Coastguard Worker    . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.zsh"
119*61c4878aSAndroid Build Coastguard Worker  fi
120*61c4878aSAndroid Build Coastguard Workerfi
121*61c4878aSAndroid Build Coastguard Worker
122*61c4878aSAndroid Build Coastguard Workerpw_cleanup
123*61c4878aSAndroid Build Coastguard Worker
124*61c4878aSAndroid Build Coastguard Workergit -C "$PW_ROOT" config blame.ignoreRevsFile .git-blame-ignore-revs
125