• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 python2 || 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_ROOT before invoking pw_bootstrap or
56# pw_activate.
57PW_ROOT="$(dirname "$_PW_BOOTSTRAP_PATH")"
58export PW_ROOT
59
60# Please also set PW_PROJECT_ROOT to YOUR_PROJECT_ROOT.
61PW_PROJECT_ROOT="$PW_ROOT"
62export PW_PROJECT_ROOT
63
64. "$PW_ROOT/pw_env_setup/util.sh"
65
66pw_deactivate
67pw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH"
68pw_check_root "$PW_ROOT"
69_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
70export _PW_ACTUAL_ENVIRONMENT_ROOT
71SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
72
73# Downstream projects may wish to set PW_BANNER_FUNC to a function that prints
74# an ASCII art banner here.
75
76# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
77if [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
78  [ ! -f "$SETUP_SH" ] || \
79  [ ! -s "$SETUP_SH" ]; then
80  pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_ROOT/pw_env_setup/config.json"
81  pw_finalize bootstrap "$SETUP_SH"
82else
83  pw_activate
84  pw_finalize activate "$SETUP_SH"
85fi
86
87unset _pw_sourced
88unset _PW_BOOTSTRAP_PATH
89unset SETUP_SH
90unset _bootstrap_abspath
91
92pw_cleanup
93