blob: fc9dfde19782807960be03601bb0601685efd4a4 [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 Mohre34001c2020-07-29 09:05:55 -0700168# PW_ENVIRONMENT_ROOT allows developers to specify where the environment should
169# be installed. _PW_ACTUAL_ENVIRONMENT_ROOT is where Pigweed keeps that
170# information. This separation allows Pigweed to assume PW_ENVIRONMENT_ROOT
171# came from the developer and not from a previous bootstrap possibly from
172# another workspace.
Rob Mohrce87bc02020-07-15 11:16:17 -0700173if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
Rob Mohre34001c2020-07-29 09:05:55 -0700174 _PW_ACTUAL_ENVIRONMENT_ROOT="$PW_ROOT/.environment"
175 export _PW_ACTUAL_ENVIRONMENT_ROOT
176else
177 _PW_ACTUAL_ENVIRONMENT_ROOT="$PW_ENVIRONMENT_ROOT"
178 export _PW_ACTUAL_ENVIRONMENT_ROOT
Rob Mohrce87bc02020-07-15 11:16:17 -0700179fi
Rob Mohre34001c2020-07-29 09:05:55 -0700180SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
Rob Mohr93825562020-01-02 15:20:50 -0800181
Wyatt Hepler5009a6f2020-04-28 18:29:45 -0700182if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then
Rob Mohr34466c42020-03-10 12:04:14 -0700183 _pw_green "\n WELCOME TO...\n"
Keir Mierlea60b2072020-06-11 23:28:18 -0700184 "_pw_$PW_BRANDING_BANNER_COLOR" "$_PW_BANNER\n"
Rob Mohr34466c42020-03-10 12:04:14 -0700185fi
Rob Mohrde7c77a2020-01-16 13:51:44 -0800186
Rob Mohr5898a9d2020-01-22 13:54:43 -0800187# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
Michael Spangf046fee2020-09-09 18:16:13 -0400188if [ "$(basename "$PW_SETUP_SCRIPT_PATH")" = "bootstrap.sh" ] || \
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700189 [ ! -f "$SETUP_SH" ] || \
Michael Spangf046fee2020-09-09 18:16:13 -0400190 [ ! -s "$SETUP_SH" ]; then
191 _PW_IS_BOOTSTRAP=0
192else
193 _PW_IS_BOOTSTRAP=1
194fi
Alexei Frolova137f972020-03-06 11:14:13 -0800195
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700196if [ "$_PW_IS_BOOTSTRAP" -eq 0 ]; then
Rob Mohrd679bde2020-03-18 09:27:17 -0700197 _PW_NAME="bootstrap"
198
Rob Mohr34466c42020-03-10 12:04:14 -0700199 if [ -z "$PW_ENVSETUP_QUIET" ]; then
200 _pw_green " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n"
201 fi
Alexei Frolova137f972020-03-06 11:14:13 -0800202
Alexei Frolov426b9b82020-03-09 14:30:22 -0700203 # Allow forcing a specific version of Python for testing pursposes.
204 if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then
205 PYTHON="$PW_BOOTSTRAP_PYTHON"
Alexei Frolova137f972020-03-06 11:14:13 -0800206 elif which python &> /dev/null; then
207 PYTHON=python
208 else
209 _pw_bold_red "Error: No system Python present\n"
210 _pw_red " Pigweed's bootstrap process requires a local system Python."
211 _pw_red " Please install Python on your system, add it to your PATH"
212 _pw_red " and re-try running bootstrap."
213 return
214 fi
215
Rob Mohrd596a532020-04-03 08:59:12 -0700216 _PW_OLD_CIPD_PACKAGE_FILES="$PW_CIPD_PACKAGE_FILES"
Rob Mohrf7d96572020-07-24 06:29:54 -0700217 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 -0700218 export PW_CIPD_PACKAGE_FILES
219
220 _PW_OLD_VIRTUALENV_REQUIREMENTS="$PW_VIRTUALENV_REQUIREMENTS"
221 PW_VIRTUALENV_REQUIREMENTS="$PW_ROOT/pw_env_setup/py/pw_env_setup/virtualenv_setup/requirements.txt:$PW_VIRTUALENV_REQUIREMENTS"
222 export PW_VIRTUALENV_REQUIREMENTS
223
224 _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS="$PW_VIRTUALENV_SETUP_PY_ROOTS"
225 PW_VIRTUALENV_SETUP_PY_ROOTS="$PW_ROOT/*:$PW_VIRTUALENV_SETUP_PY_ROOTS"
226 export PW_VIRTUALENV_SETUP_PY_ROOTS
227
228 _PW_OLD_CARGO_PACKAGE_FILES="$PW_CARGO_PACKAGE_FILES"
229 PW_CARGO_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cargo_setup/packages.txt:$PW_CARGO_PACKAGE_FILES"
230 export PW_CARGO_PACKAGE_FILES
231
Rob Mohr206959a2020-05-12 07:08:20 -0700232 if [ -n "$PW_USE_GCS_ENVSETUP" ]; then
233 _PW_ENV_SETUP="$("$PW_ROOT/pw_env_setup/get_pw_env_setup.sh")"
234 fi
235
236 if [ -n "$_PW_ENV_SETUP" ]; then
Rob Mohre34001c2020-07-29 09:05:55 -0700237 "$_PW_ENV_SETUP" --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT"
Rob Mohr206959a2020-05-12 07:08:20 -0700238 else
Rob Mohre34001c2020-07-29 09:05:55 -0700239 "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT"
Rob Mohr206959a2020-05-12 07:08:20 -0700240 fi
Rob Mohrd596a532020-04-03 08:59:12 -0700241
242 PW_CIPD_PACKAGE_FILES="$_PW_OLD_CIPD_PACKAGE_FILES"
243 PW_VIRTUALENV_REQUIREMENTS="$_PW_OLD_VIRTUALENV_REQUIREMENTS"
244 PW_VIRTUALENV_SETUP_PY_ROOTS="$_PW_OLD_VIRTUALENV_SETUP_PY_ROOTS"
245 PW_CARGO_PACKAGE_FILES="$_PW_OLD_CARGO_PACKAGE_FILES"
Alexei Frolova137f972020-03-06 11:14:13 -0800246else
Rob Mohrd679bde2020-03-18 09:27:17 -0700247 _PW_NAME="activate"
248
Rob Mohr34466c42020-03-10 12:04:14 -0700249 if [ -z "$PW_ENVSETUP_QUIET" ]; then
250 _pw_green " ACTIVATOR! This sets your shell environment variables.\n"
251 fi
Rob Mohr93825562020-01-02 15:20:50 -0800252fi
Rob Mohrdc70d1c2019-12-04 10:05:28 -0800253
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700254if [ -f "$SETUP_SH" ]; then
255 . "$SETUP_SH"
Alexei Frolova137f972020-03-06 11:14:13 -0800256
Wyatt Hepler77bd6d22020-03-19 10:13:30 -0700257 if [ "$?" -eq 0 ]; then
258 if [ "$_PW_IS_BOOTSTRAP" -eq 0 ] && [ -z "$PW_ENVSETUP_QUIET" ]; then
Rob Mohrd679bde2020-03-18 09:27:17 -0700259 echo "To activate this environment in the future, run this in your "
260 echo "terminal:"
261 echo
Rob Mohr6ac3f7d2020-03-27 12:43:28 -0700262 _pw_green " source ./activate.sh\n"
Rob Mohrd679bde2020-03-18 09:27:17 -0700263 fi
264 else
265 _pw_red "Error during $_PW_NAME--see messages above."
Rob Mohr47bd9232020-03-13 12:07:26 -0700266 fi
267else
Rob Mohrd679bde2020-03-18 09:27:17 -0700268 _pw_red "Error during $_PW_NAME--see messages above."
Alexei Frolova137f972020-03-06 11:14:13 -0800269fi
270
Rob Mohr19087152020-05-08 10:06:39 -0700271unset _PW_ENV_SETUP
Alexei Frolova137f972020-03-06 11:14:13 -0800272unset _PW_IS_BOOTSTRAP
Rob Mohrd679bde2020-03-18 09:27:17 -0700273unset _PW_NAME
Keir Mierlea60b2072020-06-11 23:28:18 -0700274unset _PW_BANNER
Rob Mohrd596a532020-04-03 08:59:12 -0700275unset _PW_OLD_CIPD_PACKAGE_FILES
276unset _PW_OLD_VIRTUALENV_REQUIREMENTS
277unset _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS
278unset _PW_OLD_CARGO_PACKAGE_FILES
Alexei Frolova137f972020-03-06 11:14:13 -0800279unset _pw_abspath
280unset _pw_red
281unset _pw_bold_red
282unset _pw_green
Keir Mierlea60b2072020-06-11 23:28:18 -0700283unset _pw_magenta
Rob Mohr6ac3f7d2020-03-27 12:43:28 -0700284unset _pw_sourced