1 # Copyright 2020 The Pigweed Authors
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
7 # https://www.apache.org/licenses/LICENSE-2.0
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
16 python -c "import os.path; print(os.path.abspath('$@'))"
19 # Just in case PATH isn't already exported.
22 # Note: Colors are unfortunately duplicated in several places; and removing the
23 # duplication is not easy. Their locations are:
27 # - pw_env_setup/py/pw_env_setup/colors.py
29 # So please keep them matching then modifying them.
35 echo -e "\033[0;31m$*\033[0m"
39 echo -e "\033[1;31m$*\033[0m"
43 echo -e "\033[0;33m$*\033[0m"
47 echo -e "\033[1;33m$*\033[0m"
51 echo -e "\033[0;32m$*\033[0m"
55 echo -e "\033[1;32m$*\033[0m"
59 echo -e "\033[1;34m$*\033[0m"
63 echo -e "\033[1;36m$*\033[0m"
67 echo -e "\033[0;35m$*\033[0m"
71 echo -e "\033[1;37m$*\033[0m"
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:"
84 pw_red " \$ source $_PW_NAME.sh"
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."
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
108 if [ -n "$PW_PROJECT_ROOT" ]; then
109 echo "$PW_PROJECT_ROOT/.environment"
111 echo "$PW_ROOT/.environment"
114 echo "$PW_ENVIRONMENT_ROOT"
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:
122 # - pw_env_setup/util.sh
123 # - pw_cli/branding.py
124 # - pw_env_setup/py/pw_env_setup/windows_env_start.py
126 _PW_BANNER=$(cat <<EOF
127 ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄
128 ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█ ▒█ ▀ ▒█ ▀ ▒█ ▀█▌
129 ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█ ▒███ ▒███ ░█ █▌
130 ▒█▀ ░█░ ▓█ █▓ ░█░ █ ▒█ ▒█ ▄ ▒█ ▄ ░█ ▄█▌
131 ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
136 if [ -z "$PW_ENVSETUP_QUIET" ] && [ -z "$PW_ENVSETUP_NO_BANNER" ]; then
137 pw_magenta "$_PW_BANNER\n"
141 _PW_BANNER_FUNC="_pw_banner"
145 if [ -n "$PW_BANNER_FUNC" ]; then
146 _PW_BANNER_FUNC="$PW_BANNER_FUNC"
148 if [ -z "$PW_ENVSETUP_QUIET" ]; then
149 pw_green "\n WELCOME TO...\n"
156 # Assume PW_ROOT and PW_PROJECT_ROOT has already been set and we need to
157 # preserve their values.
158 _NEW_PW_ROOT="$PW_ROOT"
159 _NEW_PW_PROJECT_ROOT="$PW_PROJECT_ROOT"
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"
167 # If there's a _pw_deactivate function run it. Redirect output to /dev/null
168 # in case _pw_deactivate doesn't exist.
169 if [ -n "$(command -v _pw_deactivate)" ]; then
170 _pw_deactivate &> /dev/null
174 PW_ROOT="$_NEW_PW_ROOT"
176 PW_PROJECT_ROOT="$_NEW_PW_PROJECT_ROOT"
177 export PW_PROJECT_ROOT
180 # 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"
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
192 _pw_hello " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n"
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
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."
207 if [ -n "$PW_USE_GCS_ENVSETUP" ]; then
208 _PW_ENV_SETUP="$("$PW_ROOT/pw_env_setup/get_pw_env_setup.sh")"
211 if [ -n "$_PW_ENV_SETUP" ]; then
212 "$_PW_ENV_SETUP" "$@"
214 "$_PW_PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" "$@"
219 _pw_hello " ACTIVATOR! This sets your shell environment variables.\n"
225 if [ -f "$_PW_SETUP_SH" ]; then
228 if [ "$?" -eq 0 ]; then
229 if [ "$_PW_NAME" = "bootstrap" ] && [ -z "$PW_ENVSETUP_QUIET" ]; then
230 echo "To activate this environment in the future, run this in your "
233 pw_green " source ./activate.sh\n"
236 pw_red "Error during $_PW_NAME--see messages above."
239 pw_red "Error during $_PW_NAME--see messages above."
245 unset _PW_BANNER_FUNC
250 unset _PW_DEACTIVATE_SH
265 unset pw_eval_sourced
267 unset pw_get_env_root