blob: 75da8d9723b631e61dad83ffc4e585646c90a55a [file] [log] [blame]
Rob Mohr2cc32ec2020-02-05 10:55:12 -08001# Copyright 2020 The Pigweed Authors
Rob Mohrcacb8772019-11-22 13:14:01 -08002#
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
Rob Mohr895d3df2020-10-14 08:42:39 -070017_bootstrap_abspath () {
Rob Mohr248903c2021-04-19 11:30:29 -070018 $(command -v python3 || command -v python2 || command -v python) -c "import os.path; print(os.path.abspath('$@'))"
Rob Mohrdc70d1c2019-12-04 10:05:28 -080019}
20
Rob Mohrcacb8772019-11-22 13:14:01 -080021# Shell: bash.
Rob Mohrab90ea82021-10-15 08:03:59 -070022if test -n "$BASH"; then
Rob Mohrdfdce522021-04-02 12:02:43 -070023 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
Rob Mohrcacb8772019-11-22 13:14:01 -080024# Shell: zsh.
25elif test -n "$ZSH_NAME"; then
Rob Mohrdfdce522021-04-02 12:02:43 -070026 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
Rob Mohrcacb8772019-11-22 13:14:01 -080027# Shell: dash.
28elif test ${0##*/} = dash; then
Rob Mohrdfdce522021-04-02 12:02:43 -070029 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath \
Wyatt Hepler77bd6d22020-03-19 10:13:30 -070030 "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
Rob Mohrcacb8772019-11-22 13:14:01 -080031# If everything else fails, try $0. It could work.
32else
Rob Mohrdfdce522021-04-02 12:02:43 -070033 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
Rob Mohrcacb8772019-11-22 13:14:01 -080034fi
35
Rob Mohr6ac3f7d2020-03-27 12:43:28 -070036# 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
Rob Mohr895d3df2020-10-14 08:42:39 -070054# 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.
Rob Mohrdfdce522021-04-02 12:02:43 -070057PW_ROOT="$(dirname "$_PW_BOOTSTRAP_PATH")"
Rob Mohrcacb8772019-11-22 13:14:01 -080058export PW_ROOT
59
Rob Mohrfcf60372020-11-04 11:41:36 -080060# Please also set PW_PROJECT_ROOT to YOUR_PROJECT_ROOT.
61PW_PROJECT_ROOT="$PW_ROOT"
62export PW_PROJECT_ROOT
63
Rob Mohr895d3df2020-10-14 08:42:39 -070064. "$PW_ROOT/pw_env_setup/util.sh"
65
Rob Mohr428e4792020-10-27 11:46:43 -070066pw_deactivate
Rob Mohrdfdce522021-04-02 12:02:43 -070067pw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH"
Rob Mohr895d3df2020-10-14 08:42:39 -070068pw_check_root "$PW_ROOT"
69_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
Anthony DiGirolamo00cb5b22020-10-19 14:03:11 -070070export _PW_ACTUAL_ENVIRONMENT_ROOT
Rob Mohre34001c2020-07-29 09:05:55 -070071SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
Rob Mohr93825562020-01-02 15:20:50 -080072
Rob Mohr895d3df2020-10-14 08:42:39 -070073# Downstream projects may wish to set PW_BANNER_FUNC to a function that prints
74# an ASCII art banner here.
Rob Mohrde7c77a2020-01-16 13:51:44 -080075
Rob Mohr5898a9d2020-01-22 13:54:43 -080076# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
Rob Mohrdfdce522021-04-02 12:02:43 -070077if [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
Wyatt Hepler77bd6d22020-03-19 10:13:30 -070078 [ ! -f "$SETUP_SH" ] || \
Michael Spangf046fee2020-09-09 18:16:13 -040079 [ ! -s "$SETUP_SH" ]; then
Rob Mohr1d2ffdf2021-05-12 14:44:45 -070080 pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_ROOT/pw_env_setup/config.json"
Rob Mohr895d3df2020-10-14 08:42:39 -070081 pw_finalize bootstrap "$SETUP_SH"
Michael Spangf046fee2020-09-09 18:16:13 -040082else
Rob Mohr895d3df2020-10-14 08:42:39 -070083 pw_activate
84 pw_finalize activate "$SETUP_SH"
Michael Spangf046fee2020-09-09 18:16:13 -040085fi
Alexei Frolova137f972020-03-06 11:14:13 -080086
Rob Mohr6ac3f7d2020-03-27 12:43:28 -070087unset _pw_sourced
Rob Mohrdfdce522021-04-02 12:02:43 -070088unset _PW_BOOTSTRAP_PATH
Rob Mohr895d3df2020-10-14 08:42:39 -070089unset SETUP_SH
90unset _bootstrap_abspath
91
92pw_cleanup