blob: cf561212cfc37f7284ede10f18560f36c0cacd17 [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
Keir Mierlea60b2072020-06-11 23:28:18 -070021
22# Note: Colors are unfortunately duplicated in several places; and removing the
23# duplication is not easy. Their locations are:
24#
25# - bootstrap.sh
26# - pw_cli/color.py
27# - pw_env_setup/py/pw_env_setup/colors.py
28#
29# So please keep them matching then modifying them.
30_pw_none() {
31 echo -e "$*"
32}
33
Alexei Frolova137f972020-03-06 11:14:13 -080034_pw_red() {
Alexei Frolov02461482020-03-09 10:19:49 -070035 echo -e "\033[0;31m$*\033[0m"
Alexei Frolova137f972020-03-06 11:14:13 -080036}
37
38_pw_bold_red() {
Alexei Frolov02461482020-03-09 10:19:49 -070039 echo -e "\033[1;31m$*\033[0m"
Alexei Frolova137f972020-03-06 11:14:13 -080040}
41
Keir Mierlea60b2072020-06-11 23:28:18 -070042_pw_yellow() {
43 echo -e "\033[0;33m$*\033[0m"
44}
45
46_pw_bold_yellow() {
47 echo -e "\033[1;33m$*\033[0m"
48}
49
Alexei Frolova137f972020-03-06 11:14:13 -080050_pw_green() {
Alexei Frolov02461482020-03-09 10:19:49 -070051 echo -e "\033[0;32m$*\033[0m"
Alexei Frolova137f972020-03-06 11:14:13 -080052}
53
Keir Mierlea60b2072020-06-11 23:28:18 -070054_pw_bold_green() {
55 echo -e "\033[1;32m$*\033[0m"
56}
57
58_pw_blue() {
59 echo -e "\033[1;34m$*\033[0m"
60}
61
62_pw_cyan() {
63 echo -e "\033[1;36m$*\033[0m"
64}
65
66_pw_magenta() {
Alexei Frolov02461482020-03-09 10:19:49 -070067 echo -e "\033[0;35m$*\033[0m"
Alexei Frolova137f972020-03-06 11:14:13 -080068}
69
Keir Mierlea60b2072020-06-11 23:28:18 -070070_pw_bold_white() {
71 echo -e "\033[1;37m$*\033[0m"
72}
73
74# Note: This banner is duplicated in three places; which is a lesser evil than
75# the contortions that would be needed to share this snippet acros shell,
76# batch, and Python. Locations:
77#
78# - bootstrap.sh
79# - pw_cli/branding.py
80# - pw_env_setup/py/pw_env_setup/windows_env_start.py
81#
82_PW_BANNER=$(cat <<EOF
Alexei Frolova137f972020-03-06 11:14:13 -080083 ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
84 ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ ▒█ ▒█ ▒█ ▒█ ▀█▌
85 ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ ▒█ ▒███ ▒███ ░█ █▌
86 ▒█▀ ░█░ ▓█ █▓ ░█░ ▒█ ▒█ ▒█ ░█ ▄█▌
87 ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
88EOF
89)
90
Keir Mierlea60b2072020-06-11 23:28:18 -070091# Support customizing the branding with a different banner and color.
92if test -f "$PW_BRANDING_BANNER"; then
93 _PW_BANNER=$(cat $PW_BRANDING_BANNER)
94fi
95if test -z "$PW_BRANDING_BANNER_COLOR"; then
96 PW_BRANDING_BANNER_COLOR=magenta
97fi
98
Rob Mohr323f7c42020-02-06 14:38:37 -080099# Users are not expected to set PW_CHECKOUT_ROOT, it's only used because it
100# seems to be impossible to reliably determine the path to a sourced file in
101# dash when sourced from a dash script instead of a dash interactive prompt.
102# To reinforce that users should not be using PW_CHECKOUT_ROOT, it is cleared
103# here after it is used, and other pw tools will complain if they see that
104# variable set.
105# TODO(mohrr) find out a way to do this without PW_CHECKOUT_ROOT.
106if test -n "$PW_CHECKOUT_ROOT"; then
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700107 PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$PW_CHECKOUT_ROOT/bootstrap.sh")"
Rob Mohr323f7c42020-02-06 14:38:37 -0800108 unset PW_CHECKOUT_ROOT
Rob Mohrcacb8772019-11-22 13:14:01 -0800109# Shell: bash.
Rob Mohr323f7c42020-02-06 14:38:37 -0800110elif test -n "$BASH"; then
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700111 PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$BASH_SOURCE")"
Rob Mohrcacb8772019-11-22 13:14:01 -0800112# Shell: zsh.
113elif test -n "$ZSH_NAME"; then
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700114 PW_SETUP_SCRIPT_PATH="$(_pw_abspath "${(%):-%N}")"
Rob Mohrcacb8772019-11-22 13:14:01 -0800115# Shell: dash.
116elif test ${0##*/} = dash; then
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700117 PW_SETUP_SCRIPT_PATH="$(_pw_abspath \
118 "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
Rob Mohrcacb8772019-11-22 13:14:01 -0800119# If everything else fails, try $0. It could work.
120else
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700121 PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$0")"
Rob Mohrcacb8772019-11-22 13:14:01 -0800122fi
123
Rob Mohr6ac3f7d2020-03-27 12:43:28 -0700124# Check if this file is being executed or sourced.
125_pw_sourced=0
126if [ -n "$SWARMING_BOT_ID" ]; then
127 # If set we're running on swarming and don't need this check.
128 _pw_sourced=1
129elif [ -n "$ZSH_EVAL_CONTEXT" ]; then
130 case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac
131elif [ -n "$KSH_VERSION" ]; then
132 [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \
133 "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \
134 && _pw_sourced=1
135elif [ -n "$BASH_VERSION" ]; then
136 (return 0 2>/dev/null) && _pw_sourced=1
137else # All other shells: examine $0 for known shell binary filenames
138 # Detects `sh` and `dash`; add additional shell filenames as needed.
139 case ${0##*/} in sh|dash) _pw_sourced=1;; esac
140fi
141
142if [ "$_pw_sourced" -eq 0 ]; then
143 _PW_NAME=$(basename "$PW_SETUP_SCRIPT_PATH" .sh)
144 _pw_bold_red "Error: Attempting to $_PW_NAME in a subshell"
145 _pw_red " Since $_PW_NAME.sh modifies your shell's environment variables, it"
146 _pw_red " must be sourced rather than executed. In particular, "
147 _pw_red " 'bash $_PW_NAME.sh' will not work since the modified environment "
148 _pw_red " will get destroyed at the end of the script. Instead, source the "
149 _pw_red " script's contents in your shell:"
150 _pw_red ""
151 _pw_red " \$ source $_PW_NAME.sh"
152 exit 1
153fi
154
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700155PW_ROOT="$(dirname "$PW_SETUP_SCRIPT_PATH")"
156
157if [[ "$PW_ROOT" = *" "* ]]; then
158 _pw_bold_red "Error: The Pigweed path contains spaces\n"
159 _pw_red " The path '$PW_ROOT' contains spaces. "
160 _pw_red " Pigweed's Python environment currently requires Pigweed to be "
161 _pw_red " at a path without spaces. Please checkout Pigweed in a directory "
162 _pw_red " without spaces and retry running bootstrap."
163 return
164fi
165
Rob Mohrcacb8772019-11-22 13:14:01 -0800166export PW_ROOT
167
Rob Mohrce87bc02020-07-15 11:16:17 -0700168if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
169 PW_ENVIRONMENT_ROOT="$PW_ROOT/.environment"
170 export PW_ENVIRONMENT_ROOT
171fi
172SETUP_SH="$PW_ENVIRONMENT_ROOT/activate.sh"
Rob Mohr93825562020-01-02 15:20:50 -0800173
Wyatt Hepler5009a6f2020-04-28 18:29:45 -0700174if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then
Rob Mohr34466c42020-03-10 12:04:14 -0700175 _pw_green "\n WELCOME TO...\n"
Keir Mierlea60b2072020-06-11 23:28:18 -0700176 "_pw_$PW_BRANDING_BANNER_COLOR" "$_PW_BANNER\n"
Rob Mohr34466c42020-03-10 12:04:14 -0700177fi
Rob Mohrde7c77a2020-01-16 13:51:44 -0800178
Rob Mohr5898a9d2020-01-22 13:54:43 -0800179# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700180[ "$(basename "$PW_SETUP_SCRIPT_PATH")" = "bootstrap.sh" ] || \
181 [ ! -f "$SETUP_SH" ] || \
182 [ ! -s "$SETUP_SH" ]
183_PW_IS_BOOTSTRAP="$?"
Alexei Frolova137f972020-03-06 11:14:13 -0800184
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700185if [ "$_PW_IS_BOOTSTRAP" -eq 0 ]; then
Rob Mohrd679bde2020-03-18 09:27:17 -0700186 _PW_NAME="bootstrap"
187
Rob Mohr34466c42020-03-10 12:04:14 -0700188 if [ -z "$PW_ENVSETUP_QUIET" ]; then
189 _pw_green " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n"
190 fi
Alexei Frolova137f972020-03-06 11:14:13 -0800191
Alexei Frolov426b9b82020-03-09 14:30:22 -0700192 # Allow forcing a specific version of Python for testing pursposes.
193 if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then
194 PYTHON="$PW_BOOTSTRAP_PYTHON"
Alexei Frolova137f972020-03-06 11:14:13 -0800195 elif which python &> /dev/null; then
196 PYTHON=python
197 else
198 _pw_bold_red "Error: No system Python present\n"
199 _pw_red " Pigweed's bootstrap process requires a local system Python."
200 _pw_red " Please install Python on your system, add it to your PATH"
201 _pw_red " and re-try running bootstrap."
202 return
203 fi
204
Rob Mohrd596a532020-04-03 08:59:12 -0700205 _PW_OLD_CIPD_PACKAGE_FILES="$PW_CIPD_PACKAGE_FILES"
Rob Mohrf7d96572020-07-24 06:29:54 -0700206 PW_CIPD_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cipd_setup/pigweed.json:$PW_ROOT/pw_env_setup/py/pw_env_setup/cipd_setup/luci.json:$PW_CIPD_PACKAGE_FILES"
Rob Mohrd596a532020-04-03 08:59:12 -0700207 export PW_CIPD_PACKAGE_FILES
208
209 _PW_OLD_VIRTUALENV_REQUIREMENTS="$PW_VIRTUALENV_REQUIREMENTS"
210 PW_VIRTUALENV_REQUIREMENTS="$PW_ROOT/pw_env_setup/py/pw_env_setup/virtualenv_setup/requirements.txt:$PW_VIRTUALENV_REQUIREMENTS"
211 export PW_VIRTUALENV_REQUIREMENTS
212
213 _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS="$PW_VIRTUALENV_SETUP_PY_ROOTS"
214 PW_VIRTUALENV_SETUP_PY_ROOTS="$PW_ROOT/*:$PW_VIRTUALENV_SETUP_PY_ROOTS"
215 export PW_VIRTUALENV_SETUP_PY_ROOTS
216
217 _PW_OLD_CARGO_PACKAGE_FILES="$PW_CARGO_PACKAGE_FILES"
218 PW_CARGO_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cargo_setup/packages.txt:$PW_CARGO_PACKAGE_FILES"
219 export PW_CARGO_PACKAGE_FILES
220
Rob Mohr206959a2020-05-12 07:08:20 -0700221 if [ -n "$PW_USE_GCS_ENVSETUP" ]; then
222 _PW_ENV_SETUP="$("$PW_ROOT/pw_env_setup/get_pw_env_setup.sh")"
223 fi
224
225 if [ -n "$_PW_ENV_SETUP" ]; then
Rob Mohrbe792172020-07-21 08:26:47 -0700226 "$_PW_ENV_SETUP" --shell-file "$SETUP_SH" --install-dir "$PW_ENVIRONMENT_ROOT"
Rob Mohr206959a2020-05-12 07:08:20 -0700227 else
Rob Mohrce87bc02020-07-15 11:16:17 -0700228 "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH" --install-dir "$PW_ENVIRONMENT_ROOT"
Rob Mohr206959a2020-05-12 07:08:20 -0700229 fi
Rob Mohrd596a532020-04-03 08:59:12 -0700230
231 PW_CIPD_PACKAGE_FILES="$_PW_OLD_CIPD_PACKAGE_FILES"
232 PW_VIRTUALENV_REQUIREMENTS="$_PW_OLD_VIRTUALENV_REQUIREMENTS"
233 PW_VIRTUALENV_SETUP_PY_ROOTS="$_PW_OLD_VIRTUALENV_SETUP_PY_ROOTS"
234 PW_CARGO_PACKAGE_FILES="$_PW_OLD_CARGO_PACKAGE_FILES"
Alexei Frolova137f972020-03-06 11:14:13 -0800235else
Rob Mohrd679bde2020-03-18 09:27:17 -0700236 _PW_NAME="activate"
237
Rob Mohr34466c42020-03-10 12:04:14 -0700238 if [ -z "$PW_ENVSETUP_QUIET" ]; then
239 _pw_green " ACTIVATOR! This sets your shell environment variables.\n"
240 fi
Rob Mohr93825562020-01-02 15:20:50 -0800241fi
Rob Mohrdc70d1c2019-12-04 10:05:28 -0800242
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700243if [ -f "$SETUP_SH" ]; then
244 . "$SETUP_SH"
Alexei Frolova137f972020-03-06 11:14:13 -0800245
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700246 if [ "$?" -eq 0 ]; then
247 if [ "$_PW_IS_BOOTSTRAP" -eq 0 ] && [ -z "$PW_ENVSETUP_QUIET" ]; then
Rob Mohrd679bde2020-03-18 09:27:17 -0700248 echo "To activate this environment in the future, run this in your "
249 echo "terminal:"
250 echo
Rob Mohr6ac3f7d2020-03-27 12:43:28 -0700251 _pw_green " source ./activate.sh\n"
Rob Mohrd679bde2020-03-18 09:27:17 -0700252 fi
253 else
254 _pw_red "Error during $_PW_NAME--see messages above."
Rob Mohr47bd9232020-03-13 12:07:26 -0700255 fi
256else
Rob Mohrd679bde2020-03-18 09:27:17 -0700257 _pw_red "Error during $_PW_NAME--see messages above."
Alexei Frolova137f972020-03-06 11:14:13 -0800258fi
259
Rob Mohr19087152020-05-08 10:06:39 -0700260unset _PW_ENV_SETUP
Alexei Frolova137f972020-03-06 11:14:13 -0800261unset _PW_IS_BOOTSTRAP
Rob Mohrd679bde2020-03-18 09:27:17 -0700262unset _PW_NAME
Keir Mierlea60b2072020-06-11 23:28:18 -0700263unset _PW_BANNER
Rob Mohrd596a532020-04-03 08:59:12 -0700264unset _PW_OLD_CIPD_PACKAGE_FILES
265unset _PW_OLD_VIRTUALENV_REQUIREMENTS
266unset _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS
267unset _PW_OLD_CARGO_PACKAGE_FILES
Alexei Frolova137f972020-03-06 11:14:13 -0800268unset _pw_abspath
269unset _pw_red
270unset _pw_bold_red
271unset _pw_green
Keir Mierlea60b2072020-06-11 23:28:18 -0700272unset _pw_magenta
Rob Mohr6ac3f7d2020-03-27 12:43:28 -0700273unset _pw_sourced