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  $(which python || which python3 || which python2) -c "import os.path; print(os.path.abspath('$@'))"
19}
20
21# Users are not expected to set PW_CHECKOUT_ROOT, it's only used because it
22# seems to be impossible to reliably determine the path to a sourced file in
23# dash when sourced from a dash script instead of a dash interactive prompt.
24# To reinforce that users should not be using PW_CHECKOUT_ROOT, it is cleared
25# here after it is used, and other pw tools will complain if they see that
26# variable set.
27# TODO(mohrr) find out a way to do this without PW_CHECKOUT_ROOT.
28if test -n "$PW_CHECKOUT_ROOT"; then
29  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$PW_CHECKOUT_ROOT/bootstrap.sh")"
30  # Downstream projects need to set PW_CHECKOUT_ROOT to point to Pigweed if
31  # they're using Pigweed's CI/CQ system.
32  unset PW_CHECKOUT_ROOT
33# Shell: bash.
34elif test -n "$BASH"; then
35  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
36# Shell: zsh.
37elif test -n "$ZSH_NAME"; then
38  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
39# Shell: dash.
40elif test ${0##*/} = dash; then
41  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath \
42    "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
43# If everything else fails, try $0. It could work.
44else
45  _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
46fi
47
48# Check if this file is being executed or sourced.
49_pw_sourced=0
50if [ -n "$SWARMING_BOT_ID" ]; then
51  # If set we're running on swarming and don't need this check.
52  _pw_sourced=1
53elif [ -n "$ZSH_EVAL_CONTEXT" ]; then
54  case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac
55elif [ -n "$KSH_VERSION" ]; then
56  [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \
57    "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \
58    && _pw_sourced=1
59elif [ -n "$BASH_VERSION" ]; then
60  (return 0 2>/dev/null) && _pw_sourced=1
61else  # All other shells: examine $0 for known shell binary filenames
62  # Detects `sh` and `dash`; add additional shell filenames as needed.
63  case ${0##*/} in sh|dash) _pw_sourced=1;; esac
64fi
65
66# Downstream projects need to set something other than PW_ROOT here, like
67# YOUR_PROJECT_ROOT. Please also set PW_ROOT before invoking pw_bootstrap or
68# pw_activate.
69PW_ROOT="$(dirname "$_PW_BOOTSTRAP_PATH")"
70export PW_ROOT
71
72# Please also set PW_PROJECT_ROOT to YOUR_PROJECT_ROOT.
73PW_PROJECT_ROOT="$PW_ROOT"
74export PW_PROJECT_ROOT
75
76. "$PW_ROOT/pw_env_setup/util.sh"
77
78pw_deactivate
79pw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH"
80pw_check_root "$PW_ROOT"
81_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
82export _PW_ACTUAL_ENVIRONMENT_ROOT
83SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
84
85# Downstream projects may wish to set PW_BANNER_FUNC to a function that prints
86# an ASCII art banner here.
87
88# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
89if [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
90  [ ! -f "$SETUP_SH" ] || \
91  [ ! -s "$SETUP_SH" ]; then
92  pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --json-file "$_PW_ACTUAL_ENVIRONMENT_ROOT/actions.json" --config-file "$PW_ROOT/pw_env_setup/config.json"
93  pw_finalize bootstrap "$SETUP_SH"
94else
95  pw_activate
96  pw_finalize activate "$SETUP_SH"
97fi
98
99unset _pw_sourced
100unset _PW_BOOTSTRAP_PATH
101unset SETUP_SH
102unset _bootstrap_abspath
103
104pw_cleanup
105