blob: 2c5befa0aa46465f0dd832629bd1b9a1ce64dc64 [file] [log] [blame]
Rob Mohr895d3df2020-10-14 08:42:39 -07001# Copyright 2020 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_pw_abspath () {
16 python -c "import os.path; print(os.path.abspath('$@'))"
17}
18
Rob Mohrf5712ee2020-11-09 09:39:52 -080019# Just in case PATH isn't already exported.
20export PATH
Rob Mohr895d3df2020-10-14 08:42:39 -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.
30pw_none() {
31 echo -e "$*"
32}
33
34pw_red() {
35 echo -e "\033[0;31m$*\033[0m"
36}
37
38pw_bold_red() {
39 echo -e "\033[1;31m$*\033[0m"
40}
41
42pw_yellow() {
43 echo -e "\033[0;33m$*\033[0m"
44}
45
46pw_bold_yellow() {
47 echo -e "\033[1;33m$*\033[0m"
48}
49
50pw_green() {
51 echo -e "\033[0;32m$*\033[0m"
52}
53
54pw_bold_green() {
55 echo -e "\033[1;32m$*\033[0m"
56}
57
58pw_blue() {
59 echo -e "\033[1;34m$*\033[0m"
60}
61
62pw_cyan() {
63 echo -e "\033[1;36m$*\033[0m"
64}
65
66pw_magenta() {
67 echo -e "\033[0;35m$*\033[0m"
68}
69
70pw_bold_white() {
71 echo -e "\033[1;37m$*\033[0m"
72}
73
74pw_eval_sourced() {
75 if [ "$1" -eq 0 ]; then
76 _PW_NAME=$(basename "$PW_SETUP_SCRIPT_PATH" .sh)
77 pw_bold_red "Error: Attempting to $_PW_NAME in a subshell"
78 pw_red " Since $_PW_NAME.sh modifies your shell's environment variables,"
79 pw_red " it must be sourced rather than executed. In particular, "
80 pw_red " 'bash $_PW_NAME.sh' will not work since the modified "
81 pw_red " environment will get destroyed at the end of the script. "
82 pw_red " Instead, source the script's contents in your shell:"
83 pw_red ""
84 pw_red " \$ source $_PW_NAME.sh"
85 exit 1
86 fi
87}
88
89pw_check_root() {
90 _PW_ROOT="$1"
91 if [[ "$_PW_ROOT" = *" "* ]]; then
92 pw_bold_red "Error: The Pigweed path contains spaces\n"
93 pw_red " The path '$_PW_ROOT' contains spaces. "
94 pw_red " Pigweed's Python environment currently requires Pigweed to be "
95 pw_red " at a path without spaces. Please checkout Pigweed in a "
96 pw_red " directory without spaces and retry running bootstrap."
97 return
98 fi
99}
100
101pw_get_env_root() {
102 # PW_ENVIRONMENT_ROOT allows developers to specify where the environment
103 # should be installed. bootstrap.sh scripts should not use that variable to
104 # store the result of this function. This separation allows scripts to assume
105 # PW_ENVIRONMENT_ROOT came from the developer and not from a previous
106 # bootstrap possibly from another workspace.
107 if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
Rob Mohr582586b2020-11-13 12:33:06 -0800108 if [ -n "$PW_PROJECT_ROOT" ]; then
109 echo "$PW_PROJECT_ROOT/.environment"
110 else
111 echo "$PW_ROOT/.environment"
112 fi
Rob Mohr895d3df2020-10-14 08:42:39 -0700113 else
114 echo "$PW_ENVIRONMENT_ROOT"
115 fi
116}
117
118# Note: This banner is duplicated in three places; which is a lesser evil than
119# the contortions that would be needed to share this snippet across shell,
120# batch, and Python. Locations:
121#
122# - pw_env_setup/util.sh
123# - pw_cli/branding.py
124# - pw_env_setup/py/pw_env_setup/windows_env_start.py
125#
126_PW_BANNER=$(cat <<EOF
127 ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
128 ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ ▒█ ▒█ ▒█ ▒█ ▀█▌
129 ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ ▒█ ▒███ ▒███ ░█ █▌
130 ▒█▀ ░█░ ▓█ █▓ ░█░ ▒█ ▒█ ▒█ ░█ ▄█▌
131 ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
132EOF
133)
134
135_pw_banner() {
136 if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then
137 pw_magenta "$_PW_BANNER\n"
138 fi
139}
140
141_PW_BANNER_FUNC="_pw_banner"
142
143_pw_hello() {
144 _PW_TEXT="$1"
145 if [ -n "$PW_BANNER_FUNC" ]; then
146 _PW_BANNER_FUNC="$PW_BANNER_FUNC"
147 fi
148 if [ -z "$PW_ENVSETUP_QUIET" ]; then
149 pw_green "\n WELCOME TO...\n"
150 "$_PW_BANNER_FUNC"
151 pw_green "$_PW_TEXT"
152 fi
153}
154
Rob Mohr428e4792020-10-27 11:46:43 -0700155pw_deactivate() {
Rob Mohr926bb602020-11-05 14:08:04 -0800156 # Assume PW_ROOT and PW_PROJECT_ROOT has already been set and we need to
157 # preserve their values.
Rob Mohr428e4792020-10-27 11:46:43 -0700158 _NEW_PW_ROOT="$PW_ROOT"
Rob Mohr926bb602020-11-05 14:08:04 -0800159 _NEW_PW_PROJECT_ROOT="$PW_PROJECT_ROOT"
Rob Mohr428e4792020-10-27 11:46:43 -0700160
161 # Find deactivate script and run it.
162 _PW_DEACTIVATE_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/deactivate.sh"
163 if [ -f "$_PW_DEACTIVATE_SH" ]; then
164 . "$_PW_DEACTIVATE_SH"
165 fi
166
Rob Mohr061f9722020-10-30 16:06:52 -0700167 # If there's a _pw_deactivate function run it. Redirect output to /dev/null
168 # in case _pw_deactivate doesn't exist.
Rob Mohrc0a8e7b2020-11-02 15:22:38 -0800169 if [ -n "$(command -v _pw_deactivate)" ]; then
170 _pw_deactivate &> /dev/null
171 fi
Rob Mohr061f9722020-10-30 16:06:52 -0700172
Rob Mohr926bb602020-11-05 14:08:04 -0800173 # Restore.
Rob Mohr428e4792020-10-27 11:46:43 -0700174 PW_ROOT="$_NEW_PW_ROOT"
175 export PW_ROOT
Rob Mohr926bb602020-11-05 14:08:04 -0800176 PW_PROJECT_ROOT="$_NEW_PW_PROJECT_ROOT"
177 export PW_PROJECT_ROOT
Rob Mohr428e4792020-10-27 11:46:43 -0700178}
179
Rob Mohr895d3df2020-10-14 08:42:39 -0700180# The next three functions use the following variables.
181# * PW_BANNER_FUNC: function to print banner
182# * PW_BOOTSTRAP_PYTHON: specific Python interpreter to use for bootstrap
183# * PW_USE_GCS_ENVSETUP: attempt to grab env setup executable from GCS if "true"
184# * PW_ROOT: path to Pigweed root
185# * PW_ENVSETUP_QUIET: limit output if "true"
186#
187# All arguments passed in are passed on to env_setup.py in pw_bootstrap,
188# pw_activate takes no arguments, and pw_finalize takes the name of the script
189# "bootstrap" or "activate" and the path to the setup script written by
190# bootstrap.sh.
191pw_bootstrap() {
192 _pw_hello " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n"
193
194 # Allow forcing a specific version of Python for testing pursposes.
195 if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then
196 _PW_PYTHON="$PW_BOOTSTRAP_PYTHON"
197 elif which python &> /dev/null; then
198 _PW_PYTHON=python
199 else
200 pw_bold_red "Error: No system Python present\n"
201 pw_red " Pigweed's bootstrap process requires a local system Python."
202 pw_red " Please install Python on your system, add it to your PATH"
203 pw_red " and re-try running bootstrap."
204 return
205 fi
206
207 if [ -n "$PW_USE_GCS_ENVSETUP" ]; then
208 _PW_ENV_SETUP="$("$PW_ROOT/pw_env_setup/get_pw_env_setup.sh")"
209 fi
210
211 if [ -n "$_PW_ENV_SETUP" ]; then
212 "$_PW_ENV_SETUP" "$@"
Rob Mohr4f59add2021-03-04 10:22:07 -0800213 _PW_ENV_SETUP_STATUS="$?"
Rob Mohr895d3df2020-10-14 08:42:39 -0700214 else
215 "$_PW_PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" "$@"
Rob Mohr4f59add2021-03-04 10:22:07 -0800216 _PW_ENV_SETUP_STATUS="$?"
Rob Mohr895d3df2020-10-14 08:42:39 -0700217 fi
218}
219
220pw_activate() {
221 _pw_hello " ACTIVATOR! This sets your shell environment variables.\n"
Rob Mohr4f59add2021-03-04 10:22:07 -0800222 _PW_ENV_SETUP_STATUS=0
Rob Mohr895d3df2020-10-14 08:42:39 -0700223}
224
225pw_finalize() {
226 _PW_NAME="$1"
227 _PW_SETUP_SH="$2"
Rob Mohr4f59add2021-03-04 10:22:07 -0800228
229 if [ "$_PW_ENV_SETUP_STATUS" -ne 0 ]; then
230 return
231 fi
232
Rob Mohr895d3df2020-10-14 08:42:39 -0700233 if [ -f "$_PW_SETUP_SH" ]; then
234 . "$_PW_SETUP_SH"
235
236 if [ "$?" -eq 0 ]; then
237 if [ "$_PW_NAME" = "bootstrap" ] && [ -z "$PW_ENVSETUP_QUIET" ]; then
238 echo "To activate this environment in the future, run this in your "
239 echo "terminal:"
240 echo
241 pw_green " source ./activate.sh\n"
242 fi
243 else
244 pw_red "Error during $_PW_NAME--see messages above."
245 fi
246 else
247 pw_red "Error during $_PW_NAME--see messages above."
248 fi
249}
250
251pw_cleanup() {
252 unset _PW_BANNER
253 unset _PW_BANNER_FUNC
254 unset _PW_ENV_SETUP
255 unset _PW_NAME
256 unset _PW_PYTHON
257 unset _PW_SETUP_SH
Rob Mohr428e4792020-10-27 11:46:43 -0700258 unset _PW_DEACTIVATE_SH
259 unset _NEW_PW_ROOT
Rob Mohr4f59add2021-03-04 10:22:07 -0800260 unset _PW_ENV_SETUP_STATUS
Rob Mohr895d3df2020-10-14 08:42:39 -0700261
262 unset _pw_abspath
263 unset pw_none
264 unset pw_red
265 unset pw_bold_red
266 unset pw_yellow
267 unset pw_bold_yellow
268 unset pw_green
269 unset pw_bold_green
270 unset pw_blue
271 unset pw_cyan
272 unset pw_magenta
273 unset pw_bold_white
274 unset pw_eval_sourced
275 unset pw_check_root
276 unset pw_get_env_root
277 unset _pw_banner
278 unset pw_bootstrap
279 unset pw_activate
280 unset pw_finalize
281 unset _pw_cleanup
Rob Mohr895d3df2020-10-14 08:42:39 -0700282}