Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 1 | # Copyright 2019 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 | |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 17 | function _abspath () { |
| 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 | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 21 | # Shell: bash. |
| 22 | if test -n "$BASH"; then |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 23 | PW_SETUP_SCRIPT_PATH=$(_abspath $BASH_SOURCE) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 24 | # Shell: zsh. |
| 25 | elif test -n "$ZSH_NAME"; then |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 26 | PW_SETUP_SCRIPT_PATH=$(_abspath ${(%):-%N}) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 27 | # Shell: dash. |
| 28 | elif test ${0##*/} = dash; then |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 29 | PW_SETUP_SCRIPT_PATH=$(_abspath \ |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 30 | $(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')) |
| 31 | # If everything else fails, try $0. It could work. |
| 32 | else |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 33 | PW_SETUP_SCRIPT_PATH=$(_abspath $0) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 34 | fi |
| 35 | |
Rob Mohr | 32da3ba | 2020-01-09 13:53:50 -0800 | [diff] [blame] | 36 | PW_ROOT=$(dirname $(dirname $PW_SETUP_SCRIPT_PATH)) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 37 | export PW_ROOT |
| 38 | |
Rob Mohr | 32da3ba | 2020-01-09 13:53:50 -0800 | [diff] [blame] | 39 | SETUP_SH="$PW_ROOT/env_setup/.setup.sh" |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 40 | |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame^] | 41 | if [ $(which python3 &> /dev/null) ]; then |
| 42 | PYTHON=python3 |
| 43 | else |
| 44 | PYTHON=python |
| 45 | fi |
| 46 | |
Rob Mohr | 32da3ba | 2020-01-09 13:53:50 -0800 | [diff] [blame] | 47 | # Expanded logic here to make this both easy to read and not bash-specific. |
| 48 | # If user sourced 'bootstrap.sh' or if there is no '.setup.sh' then run |
| 49 | # env_setup.py. |
Keir Mierle | e0dec89 | 2020-01-16 13:41:40 -0800 | [diff] [blame] | 50 | if [ $(basename $PW_SETUP_SCRIPT_PATH) = "bootstrap.sh" ]; then |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame^] | 51 | $PYTHON $PW_ROOT/env_setup/env_setup.py --shell-file $SETUP_SH |
Rob Mohr | 32da3ba | 2020-01-09 13:53:50 -0800 | [diff] [blame] | 52 | elif [ ! -f $SETUP_SH ]; then |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame^] | 53 | $PYTHON $PW_ROOT/env_setup/env_setup.py --shell-file $SETUP_SH |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 54 | fi |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 55 | |
Rob Mohr | 32da3ba | 2020-01-09 13:53:50 -0800 | [diff] [blame] | 56 | source $SETUP_SH |