Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_env_setup / util.sh
1 # 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
19 # Just in case PATH isn't already exported.
20 export PATH
21
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
34 pw_red() {
35   echo -e "\033[0;31m$*\033[0m"
36 }
37
38 pw_bold_red() {
39   echo -e "\033[1;31m$*\033[0m"
40 }
41
42 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
50 pw_green() {
51   echo -e "\033[0;32m$*\033[0m"
52 }
53
54 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() {
67   echo -e "\033[0;35m$*\033[0m"
68 }
69
70 pw_bold_white() {
71   echo -e "\033[1;37m$*\033[0m"
72 }
73
74 pw_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
89 pw_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
101 pw_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
108     if [ -n "$PW_PROJECT_ROOT" ]; then
109       echo "$PW_PROJECT_ROOT/.environment"
110     else
111       echo "$PW_ROOT/.environment"
112     fi
113   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   ▒█      ░█░ ░▓███▀   ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀
132 EOF
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
155 pw_deactivate() {
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"
160
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
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
171   fi
172
173   # Restore.
174   PW_ROOT="$_NEW_PW_ROOT"
175   export PW_ROOT
176   PW_PROJECT_ROOT="$_NEW_PW_PROJECT_ROOT"
177   export PW_PROJECT_ROOT
178 }
179
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"
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.
191 pw_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" "$@"
213   else
214     "$_PW_PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" "$@"
215   fi
216 }
217
218 pw_activate() {
219   _pw_hello "  ACTIVATOR! This sets your shell environment variables.\n"
220 }
221
222 pw_finalize() {
223   _PW_NAME="$1"
224   _PW_SETUP_SH="$2"
225   if [ -f "$_PW_SETUP_SH" ]; then
226     . "$_PW_SETUP_SH"
227
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 "
231         echo "terminal:"
232         echo
233         pw_green "  source ./activate.sh\n"
234       fi
235     else
236       pw_red "Error during $_PW_NAME--see messages above."
237     fi
238   else
239     pw_red "Error during $_PW_NAME--see messages above."
240   fi
241 }
242
243 pw_cleanup() {
244   unset _PW_BANNER
245   unset _PW_BANNER_FUNC
246   unset _PW_ENV_SETUP
247   unset _PW_NAME
248   unset _PW_PYTHON
249   unset _PW_SETUP_SH
250   unset _PW_DEACTIVATE_SH
251   unset _NEW_PW_ROOT
252
253   unset _pw_abspath
254   unset pw_none
255   unset pw_red
256   unset pw_bold_red
257   unset pw_yellow
258   unset pw_bold_yellow
259   unset pw_green
260   unset pw_bold_green
261   unset pw_blue
262   unset pw_cyan
263   unset pw_magenta
264   unset pw_bold_white
265   unset pw_eval_sourced
266   unset pw_check_root
267   unset pw_get_env_root
268   unset _pw_banner
269   unset pw_bootstrap
270   unset pw_activate
271   unset pw_finalize
272   unset _pw_cleanup
273 }