blob: 0f1a5d15d0aee5514edb907c9d3f86beb08fdad4 [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 Mohrfdc459a2020-02-03 07:48:11 -080017_abspath () {
Rob Mohr93825562020-01-02 15:20:50 -080018 python -c "import os.path; print(os.path.abspath('$@'))"
Rob Mohrdc70d1c2019-12-04 10:05:28 -080019}
20
Rob Mohr323f7c42020-02-06 14:38:37 -080021# 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.
28if 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 Mohrcacb8772019-11-22 13:14:01 -080031# Shell: bash.
Rob Mohr323f7c42020-02-06 14:38:37 -080032elif test -n "$BASH"; then
Rob Mohr93825562020-01-02 15:20:50 -080033 PW_SETUP_SCRIPT_PATH=$(_abspath $BASH_SOURCE)
Rob Mohrcacb8772019-11-22 13:14:01 -080034# Shell: zsh.
35elif test -n "$ZSH_NAME"; then
Rob Mohr93825562020-01-02 15:20:50 -080036 PW_SETUP_SCRIPT_PATH=$(_abspath ${(%):-%N})
Rob Mohrcacb8772019-11-22 13:14:01 -080037# Shell: dash.
38elif test ${0##*/} = dash; then
Rob Mohr93825562020-01-02 15:20:50 -080039 PW_SETUP_SCRIPT_PATH=$(_abspath \
Rob Mohrcacb8772019-11-22 13:14:01 -080040 $(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;'))
41# If everything else fails, try $0. It could work.
42else
Rob Mohr93825562020-01-02 15:20:50 -080043 PW_SETUP_SCRIPT_PATH=$(_abspath $0)
Rob Mohrcacb8772019-11-22 13:14:01 -080044fi
45
Rob Mohr32da3ba2020-01-09 13:53:50 -080046PW_ROOT=$(dirname $(dirname $PW_SETUP_SCRIPT_PATH))
Rob Mohrcacb8772019-11-22 13:14:01 -080047export PW_ROOT
48
Rob Mohr2cc32ec2020-02-05 10:55:12 -080049SETUP_SH="$PW_ROOT/pw_env_setup/.setup.sh"
Rob Mohr93825562020-01-02 15:20:50 -080050
Keir Mierle63092342020-01-16 15:14:56 -080051# Try to use Python 3 if possible by default, before Python 2.
52if which python3 &> /dev/null; then
Rob Mohrde7c77a2020-01-16 13:51:44 -080053 PYTHON=python3
54else
55 PYTHON=python
56fi
57
Rob Mohr5898a9d2020-01-22 13:54:43 -080058# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
59if \
60 [ $(basename $PW_SETUP_SCRIPT_PATH) = "bootstrap.sh" ] || \
61 [ ! -f $SETUP_SH ] || \
62 [ ! -s $SETUP_SH ]; then
Rob Mohr2cc32ec2020-02-05 10:55:12 -080063 $PYTHON $PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py --shell-file $SETUP_SH
Rob Mohr93825562020-01-02 15:20:50 -080064fi
Rob Mohrdc70d1c2019-12-04 10:05:28 -080065
Rob Mohrfdc459a2020-02-03 07:48:11 -080066. $SETUP_SH