Rob Mohr | 2cc32ec | 2020-02-05 10:55:12 -0800 | [diff] [blame] | 1 | # Copyright 2020 The Pigweed Authors |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 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 | |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 17 | _bootstrap_abspath () { |
Rob Mohr | 248903c | 2021-04-19 11:30:29 -0700 | [diff] [blame] | 18 | $(command -v python3 || command -v python2 || command -v python) -c "import os.path; print(os.path.abspath('$@'))" |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 19 | } |
| 20 | |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 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. |
| 28 | if test -n "$PW_CHECKOUT_ROOT"; then |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 29 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$PW_CHECKOUT_ROOT/bootstrap.sh")" |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 30 | # Downstream projects need to set PW_CHECKOUT_ROOT to point to Pigweed if |
| 31 | # they're using Pigweed's CI/CQ system. |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 32 | unset PW_CHECKOUT_ROOT |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 33 | # Shell: bash. |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 34 | elif test -n "$BASH"; then |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 35 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 36 | # Shell: zsh. |
| 37 | elif test -n "$ZSH_NAME"; then |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 38 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 39 | # Shell: dash. |
| 40 | elif test ${0##*/} = dash; then |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 41 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath \ |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 42 | "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 43 | # If everything else fails, try $0. It could work. |
| 44 | else |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 45 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 46 | fi |
| 47 | |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 48 | # Check if this file is being executed or sourced. |
| 49 | _pw_sourced=0 |
| 50 | if [ -n "$SWARMING_BOT_ID" ]; then |
| 51 | # If set we're running on swarming and don't need this check. |
| 52 | _pw_sourced=1 |
| 53 | elif [ -n "$ZSH_EVAL_CONTEXT" ]; then |
| 54 | case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac |
| 55 | elif [ -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 |
| 59 | elif [ -n "$BASH_VERSION" ]; then |
| 60 | (return 0 2>/dev/null) && _pw_sourced=1 |
| 61 | else # 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 |
| 64 | fi |
| 65 | |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 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. |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 69 | PW_ROOT="$(dirname "$_PW_BOOTSTRAP_PATH")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 70 | export PW_ROOT |
| 71 | |
Rob Mohr | fcf6037 | 2020-11-04 11:41:36 -0800 | [diff] [blame] | 72 | # Please also set PW_PROJECT_ROOT to YOUR_PROJECT_ROOT. |
| 73 | PW_PROJECT_ROOT="$PW_ROOT" |
| 74 | export PW_PROJECT_ROOT |
| 75 | |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 76 | . "$PW_ROOT/pw_env_setup/util.sh" |
| 77 | |
Rob Mohr | 428e479 | 2020-10-27 11:46:43 -0700 | [diff] [blame] | 78 | pw_deactivate |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 79 | pw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH" |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 80 | pw_check_root "$PW_ROOT" |
| 81 | _PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)" |
Anthony DiGirolamo | 00cb5b2 | 2020-10-19 14:03:11 -0700 | [diff] [blame] | 82 | export _PW_ACTUAL_ENVIRONMENT_ROOT |
Rob Mohr | e34001c | 2020-07-29 09:05:55 -0700 | [diff] [blame] | 83 | SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh" |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 84 | |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 85 | # Downstream projects may wish to set PW_BANNER_FUNC to a function that prints |
| 86 | # an ASCII art banner here. |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame] | 87 | |
Rob Mohr | 5898a9d | 2020-01-22 13:54:43 -0800 | [diff] [blame] | 88 | # Run full bootstrap when invoked as bootstrap, or env file is missing/empty. |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 89 | if [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \ |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 90 | [ ! -f "$SETUP_SH" ] || \ |
Michael Spang | f046fee | 2020-09-09 18:16:13 -0400 | [diff] [blame] | 91 | [ ! -s "$SETUP_SH" ]; then |
Rob Mohr | 1d2ffdf | 2021-05-12 14:44:45 -0700 | [diff] [blame] | 92 | pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_ROOT/pw_env_setup/config.json" |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 93 | pw_finalize bootstrap "$SETUP_SH" |
Michael Spang | f046fee | 2020-09-09 18:16:13 -0400 | [diff] [blame] | 94 | else |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 95 | pw_activate |
| 96 | pw_finalize activate "$SETUP_SH" |
Michael Spang | f046fee | 2020-09-09 18:16:13 -0400 | [diff] [blame] | 97 | fi |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 98 | |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 99 | unset _pw_sourced |
Rob Mohr | dfdce52 | 2021-04-02 12:02:43 -0700 | [diff] [blame] | 100 | unset _PW_BOOTSTRAP_PATH |
Rob Mohr | 895d3df | 2020-10-14 08:42:39 -0700 | [diff] [blame] | 101 | unset SETUP_SH |
| 102 | unset _bootstrap_abspath |
| 103 | |
| 104 | pw_cleanup |