blob: 5d1ffe469a79b751a23638c4e2916bfd219a2066 [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
Alexei Frolova137f972020-03-06 11:14:13 -080017_pw_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
Alexei Frolova137f972020-03-06 11:14:13 -080021_pw_red() {
22 echo -e "\e[0;31m$*\e[0m"
23}
24
25_pw_bold_red() {
26 echo -e "\e[1;31m$*\e[0m"
27}
28
29_pw_green() {
30 echo -e "\e[0;32m$*\e[0m"
31}
32
33_pw_bright_magenta() {
34 echo -e "\e[0;35m$*\e[0m"
35}
36
37_PIGWEED_BANNER=$(cat <<EOF
38 ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
39 ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ ▒█ ▒█ ▒█ ▒█ ▀█▌
40 ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ ▒█ ▒███ ▒███ ░█ █▌
41 ▒█▀ ░█░ ▓█ █▓ ░█░ ▒█ ▒█ ▒█ ░█ ▄█▌
42 ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
43EOF
44)
45
Rob Mohr323f7c42020-02-06 14:38:37 -080046# Users are not expected to set PW_CHECKOUT_ROOT, it's only used because it
47# seems to be impossible to reliably determine the path to a sourced file in
48# dash when sourced from a dash script instead of a dash interactive prompt.
49# To reinforce that users should not be using PW_CHECKOUT_ROOT, it is cleared
50# here after it is used, and other pw tools will complain if they see that
51# variable set.
52# TODO(mohrr) find out a way to do this without PW_CHECKOUT_ROOT.
53if test -n "$PW_CHECKOUT_ROOT"; then
Alexei Frolova137f972020-03-06 11:14:13 -080054 PW_SETUP_SCRIPT_PATH=$(_pw_abspath "$PW_CHECKOUT_ROOT/pw_env_setup/bootstrap.sh")
Rob Mohr323f7c42020-02-06 14:38:37 -080055 unset PW_CHECKOUT_ROOT
Rob Mohrcacb8772019-11-22 13:14:01 -080056# Shell: bash.
Rob Mohr323f7c42020-02-06 14:38:37 -080057elif test -n "$BASH"; then
Alexei Frolova137f972020-03-06 11:14:13 -080058 PW_SETUP_SCRIPT_PATH=$(_pw_abspath $BASH_SOURCE)
Rob Mohrcacb8772019-11-22 13:14:01 -080059# Shell: zsh.
60elif test -n "$ZSH_NAME"; then
Alexei Frolova137f972020-03-06 11:14:13 -080061 PW_SETUP_SCRIPT_PATH=$(_pw_abspath ${(%):-%N})
Rob Mohrcacb8772019-11-22 13:14:01 -080062# Shell: dash.
63elif test ${0##*/} = dash; then
Alexei Frolova137f972020-03-06 11:14:13 -080064 PW_SETUP_SCRIPT_PATH=$(_pw_abspath \
Rob Mohrcacb8772019-11-22 13:14:01 -080065 $(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;'))
66# If everything else fails, try $0. It could work.
67else
Alexei Frolova137f972020-03-06 11:14:13 -080068 PW_SETUP_SCRIPT_PATH=$(_pw_abspath $0)
Rob Mohrcacb8772019-11-22 13:14:01 -080069fi
70
Rob Mohr32da3ba2020-01-09 13:53:50 -080071PW_ROOT=$(dirname $(dirname $PW_SETUP_SCRIPT_PATH))
Rob Mohrcacb8772019-11-22 13:14:01 -080072export PW_ROOT
73
Rob Mohr2cc32ec2020-02-05 10:55:12 -080074SETUP_SH="$PW_ROOT/pw_env_setup/.setup.sh"
Rob Mohr93825562020-01-02 15:20:50 -080075
Alexei Frolova137f972020-03-06 11:14:13 -080076_pw_green "\n WELCOME TO...\n"
77_pw_bright_magenta "$_PIGWEED_BANNER\n"
Rob Mohrde7c77a2020-01-16 13:51:44 -080078
Rob Mohr5898a9d2020-01-22 13:54:43 -080079# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
Alexei Frolova137f972020-03-06 11:14:13 -080080[ $(basename $PW_SETUP_SCRIPT_PATH) = "bootstrap.sh" ] || \
Rob Mohr5898a9d2020-01-22 13:54:43 -080081 [ ! -f $SETUP_SH ] || \
Alexei Frolova137f972020-03-06 11:14:13 -080082 [ ! -s $SETUP_SH ]
83_PW_IS_BOOTSTRAP=$?
84
85if [ $_PW_IS_BOOTSTRAP -eq 0 ]; then
86 _pw_green " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n"
87
88 # Try to use Python 3 if possible by default, before Python 2.
89 if [ -z "$PW_BOOTSTRAP_PY27" ] && which python3 &> /dev/null; then
90 PYTHON=python3
91 elif which python &> /dev/null; then
92 PYTHON=python
93 else
94 _pw_bold_red "Error: No system Python present\n"
95 _pw_red " Pigweed's bootstrap process requires a local system Python."
96 _pw_red " Please install Python on your system, add it to your PATH"
97 _pw_red " and re-try running bootstrap."
98 return
99 fi
100
Rob Mohr2cc32ec2020-02-05 10:55:12 -0800101 $PYTHON $PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py --shell-file $SETUP_SH
Alexei Frolova137f972020-03-06 11:14:13 -0800102else
103 _pw_green " ACTIVATOR! This sets your shell environment variables.\n"
Rob Mohr93825562020-01-02 15:20:50 -0800104fi
Rob Mohrdc70d1c2019-12-04 10:05:28 -0800105
Rob Mohrfdc459a2020-02-03 07:48:11 -0800106. $SETUP_SH
Alexei Frolova137f972020-03-06 11:14:13 -0800107
108if [ $_PW_IS_BOOTSTRAP -eq 0 ]; then
109 echo
110 echo "To activate this environment in the future, run this in your terminal:"
111 echo
112 _pw_green " . pw_env_setup/env_setup.sh\n"
113fi
114
115unset _PW_IS_BOOTSTRAP
116unset _PIGWEED_BANNER
117unset _pw_abspath
118unset _pw_red
119unset _pw_bold_red
120unset _pw_green
121unset _pw_bright_magenta