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 | fdc459a | 2020-02-03 07:48:11 -0800 | [diff] [blame] | 17 | _abspath () { |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 18 | 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 |
| 29 | PW_SETUP_SCRIPT_PATH=$(_abspath "$PW_CHECKOUT_ROOT/pw_env_setup/bootstrap.sh") |
| 30 | unset PW_CHECKOUT_ROOT |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 31 | # Shell: bash. |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 32 | elif test -n "$BASH"; then |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 33 | PW_SETUP_SCRIPT_PATH=$(_abspath $BASH_SOURCE) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 34 | # Shell: zsh. |
| 35 | elif test -n "$ZSH_NAME"; then |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 36 | PW_SETUP_SCRIPT_PATH=$(_abspath ${(%):-%N}) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 37 | # Shell: dash. |
| 38 | elif test ${0##*/} = dash; then |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 39 | PW_SETUP_SCRIPT_PATH=$(_abspath \ |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 40 | $(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')) |
| 41 | # If everything else fails, try $0. It could work. |
| 42 | else |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 43 | PW_SETUP_SCRIPT_PATH=$(_abspath $0) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 44 | fi |
| 45 | |
Rob Mohr | 32da3ba | 2020-01-09 13:53:50 -0800 | [diff] [blame] | 46 | PW_ROOT=$(dirname $(dirname $PW_SETUP_SCRIPT_PATH)) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 47 | export PW_ROOT |
| 48 | |
Rob Mohr | 2cc32ec | 2020-02-05 10:55:12 -0800 | [diff] [blame] | 49 | SETUP_SH="$PW_ROOT/pw_env_setup/.setup.sh" |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 50 | |
Keir Mierle | 6309234 | 2020-01-16 15:14:56 -0800 | [diff] [blame] | 51 | # Try to use Python 3 if possible by default, before Python 2. |
| 52 | if which python3 &> /dev/null; then |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame] | 53 | PYTHON=python3 |
| 54 | else |
| 55 | PYTHON=python |
| 56 | fi |
| 57 | |
Rob Mohr | 5898a9d | 2020-01-22 13:54:43 -0800 | [diff] [blame] | 58 | # Run full bootstrap when invoked as bootstrap, or env file is missing/empty. |
| 59 | if \ |
| 60 | [ $(basename $PW_SETUP_SCRIPT_PATH) = "bootstrap.sh" ] || \ |
| 61 | [ ! -f $SETUP_SH ] || \ |
| 62 | [ ! -s $SETUP_SH ]; then |
Rob Mohr | 2cc32ec | 2020-02-05 10:55:12 -0800 | [diff] [blame] | 63 | $PYTHON $PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py --shell-file $SETUP_SH |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 64 | fi |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 65 | |
Rob Mohr | fdc459a | 2020-02-03 07:48:11 -0800 | [diff] [blame] | 66 | . $SETUP_SH |