Rob Mohr | 2cc32ec | 2020-02-05 10:55:12 -0800 | [diff] [blame] | 1 | # Copyright 2020 The Pigweed Authors |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 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 | # This script must be tested on bash, zsh, and dash. |
| 16 | |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 17 | _pw_abspath () { |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 18 | python -c "import os.path; print(os.path.abspath('$@'))" |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 19 | } |
| 20 | |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 21 | _pw_red() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 22 | echo -e "\033[0;31m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | _pw_bold_red() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 26 | echo -e "\033[1;31m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | _pw_green() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 30 | echo -e "\033[0;32m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | _pw_bright_magenta() { |
Alexei Frolov | 0246148 | 2020-03-09 10:19:49 -0700 | [diff] [blame] | 34 | echo -e "\033[0;35m$*\033[0m" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | _PIGWEED_BANNER=$(cat <<EOF |
| 38 | ▒█████▄ █▓ ▄███▒ ▒█ ▒█ ░▓████▒ ░▓████▒ ▒▓████▄ |
| 39 | ▒█░ █░ ░█▒ ██▒ ▀█▒ ▒█░ █ ▒█ ▒█ ▀ ▒█ ▀ ▒█ ▀█▌ |
| 40 | ▒█▄▄▄█░ ░█▒ █▓░ ▄▄░ ▒█░ █ ▒█ ▒███ ▒███ ░█ █▌ |
| 41 | ▒█▀ ░█░ ▓█ █▓ ░█░ █ ▒█ ▒█ ▄ ▒█ ▄ ░█ ▄█▌ |
| 42 | ▒█ ░█░ ░▓███▀ ▒█▓▀▓█░ ░▓████▒ ░▓████▒ ▒▓████▀ |
| 43 | EOF |
| 44 | ) |
| 45 | |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 46 | # Users are not expected to set PW_CHECKOUT_ROOT, it's only used because it |
| 47 | # seems to be impossible to reliably determine the path to a sourced file in |
| 48 | # dash when sourced from a dash script instead of a dash interactive prompt. |
| 49 | # To reinforce that users should not be using PW_CHECKOUT_ROOT, it is cleared |
| 50 | # here after it is used, and other pw tools will complain if they see that |
| 51 | # variable set. |
| 52 | # TODO(mohrr) find out a way to do this without PW_CHECKOUT_ROOT. |
| 53 | if test -n "$PW_CHECKOUT_ROOT"; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 54 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$PW_CHECKOUT_ROOT/bootstrap.sh")" |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 55 | unset PW_CHECKOUT_ROOT |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 56 | # Shell: bash. |
Rob Mohr | 323f7c4 | 2020-02-06 14:38:37 -0800 | [diff] [blame] | 57 | elif test -n "$BASH"; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 58 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$BASH_SOURCE")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 59 | # Shell: zsh. |
| 60 | elif test -n "$ZSH_NAME"; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 61 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "${(%):-%N}")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 62 | # Shell: dash. |
| 63 | elif test ${0##*/} = dash; then |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 64 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath \ |
| 65 | "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 66 | # If everything else fails, try $0. It could work. |
| 67 | else |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 68 | PW_SETUP_SCRIPT_PATH="$(_pw_abspath "$0")" |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 69 | fi |
| 70 | |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 71 | # Check if this file is being executed or sourced. |
| 72 | _pw_sourced=0 |
| 73 | if [ -n "$SWARMING_BOT_ID" ]; then |
| 74 | # If set we're running on swarming and don't need this check. |
| 75 | _pw_sourced=1 |
| 76 | elif [ -n "$ZSH_EVAL_CONTEXT" ]; then |
| 77 | case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac |
| 78 | elif [ -n "$KSH_VERSION" ]; then |
| 79 | [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \ |
| 80 | "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \ |
| 81 | && _pw_sourced=1 |
| 82 | elif [ -n "$BASH_VERSION" ]; then |
| 83 | (return 0 2>/dev/null) && _pw_sourced=1 |
| 84 | else # All other shells: examine $0 for known shell binary filenames |
| 85 | # Detects `sh` and `dash`; add additional shell filenames as needed. |
| 86 | case ${0##*/} in sh|dash) _pw_sourced=1;; esac |
| 87 | fi |
| 88 | |
| 89 | if [ "$_pw_sourced" -eq 0 ]; then |
| 90 | _PW_NAME=$(basename "$PW_SETUP_SCRIPT_PATH" .sh) |
| 91 | _pw_bold_red "Error: Attempting to $_PW_NAME in a subshell" |
| 92 | _pw_red " Since $_PW_NAME.sh modifies your shell's environment variables, it" |
| 93 | _pw_red " must be sourced rather than executed. In particular, " |
| 94 | _pw_red " 'bash $_PW_NAME.sh' will not work since the modified environment " |
| 95 | _pw_red " will get destroyed at the end of the script. Instead, source the " |
| 96 | _pw_red " script's contents in your shell:" |
| 97 | _pw_red "" |
| 98 | _pw_red " \$ source $_PW_NAME.sh" |
| 99 | exit 1 |
| 100 | fi |
| 101 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 102 | PW_ROOT="$(dirname "$PW_SETUP_SCRIPT_PATH")" |
| 103 | |
| 104 | if [[ "$PW_ROOT" = *" "* ]]; then |
| 105 | _pw_bold_red "Error: The Pigweed path contains spaces\n" |
| 106 | _pw_red " The path '$PW_ROOT' contains spaces. " |
| 107 | _pw_red " Pigweed's Python environment currently requires Pigweed to be " |
| 108 | _pw_red " at a path without spaces. Please checkout Pigweed in a directory " |
| 109 | _pw_red " without spaces and retry running bootstrap." |
| 110 | return |
| 111 | fi |
| 112 | |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 113 | export PW_ROOT |
| 114 | |
Rob Mohr | 3352ccf | 2020-03-11 14:01:18 -0700 | [diff] [blame] | 115 | SETUP_SH="$PW_ROOT/pw_env_setup/.env_setup.sh" |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 116 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 117 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 118 | _pw_green "\n WELCOME TO...\n" |
| 119 | _pw_bright_magenta "$_PIGWEED_BANNER\n" |
| 120 | fi |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame] | 121 | |
Rob Mohr | 5898a9d | 2020-01-22 13:54:43 -0800 | [diff] [blame] | 122 | # Run full bootstrap when invoked as bootstrap, or env file is missing/empty. |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 123 | [ "$(basename "$PW_SETUP_SCRIPT_PATH")" = "bootstrap.sh" ] || \ |
| 124 | [ ! -f "$SETUP_SH" ] || \ |
| 125 | [ ! -s "$SETUP_SH" ] |
| 126 | _PW_IS_BOOTSTRAP="$?" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 127 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 128 | if [ "$_PW_IS_BOOTSTRAP" -eq 0 ]; then |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 129 | _PW_NAME="bootstrap" |
| 130 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 131 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 132 | _pw_green " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n" |
| 133 | fi |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 134 | |
Alexei Frolov | 426b9b8 | 2020-03-09 14:30:22 -0700 | [diff] [blame] | 135 | # Allow forcing a specific version of Python for testing pursposes. |
| 136 | if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then |
| 137 | PYTHON="$PW_BOOTSTRAP_PYTHON" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 138 | elif which python &> /dev/null; then |
| 139 | PYTHON=python |
| 140 | else |
| 141 | _pw_bold_red "Error: No system Python present\n" |
| 142 | _pw_red " Pigweed's bootstrap process requires a local system Python." |
| 143 | _pw_red " Please install Python on your system, add it to your PATH" |
| 144 | _pw_red " and re-try running bootstrap." |
| 145 | return |
| 146 | fi |
| 147 | |
Rob Mohr | d596a53 | 2020-04-03 08:59:12 -0700 | [diff] [blame] | 148 | _PW_OLD_CIPD_PACKAGE_FILES="$PW_CIPD_PACKAGE_FILES" |
| 149 | PW_CIPD_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cipd_setup/*.json:$PW_CIPD_PACKAGE_FILES" |
| 150 | export PW_CIPD_PACKAGE_FILES |
| 151 | |
| 152 | _PW_OLD_VIRTUALENV_REQUIREMENTS="$PW_VIRTUALENV_REQUIREMENTS" |
| 153 | PW_VIRTUALENV_REQUIREMENTS="$PW_ROOT/pw_env_setup/py/pw_env_setup/virtualenv_setup/requirements.txt:$PW_VIRTUALENV_REQUIREMENTS" |
| 154 | export PW_VIRTUALENV_REQUIREMENTS |
| 155 | |
| 156 | _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS="$PW_VIRTUALENV_SETUP_PY_ROOTS" |
| 157 | PW_VIRTUALENV_SETUP_PY_ROOTS="$PW_ROOT/*:$PW_VIRTUALENV_SETUP_PY_ROOTS" |
| 158 | export PW_VIRTUALENV_SETUP_PY_ROOTS |
| 159 | |
| 160 | _PW_OLD_CARGO_PACKAGE_FILES="$PW_CARGO_PACKAGE_FILES" |
| 161 | PW_CARGO_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cargo_setup/packages.txt:$PW_CARGO_PACKAGE_FILES" |
| 162 | export PW_CARGO_PACKAGE_FILES |
| 163 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 164 | "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH" |
Rob Mohr | d596a53 | 2020-04-03 08:59:12 -0700 | [diff] [blame] | 165 | |
| 166 | |
| 167 | PW_CIPD_PACKAGE_FILES="$_PW_OLD_CIPD_PACKAGE_FILES" |
| 168 | PW_VIRTUALENV_REQUIREMENTS="$_PW_OLD_VIRTUALENV_REQUIREMENTS" |
| 169 | PW_VIRTUALENV_SETUP_PY_ROOTS="$_PW_OLD_VIRTUALENV_SETUP_PY_ROOTS" |
| 170 | PW_CARGO_PACKAGE_FILES="$_PW_OLD_CARGO_PACKAGE_FILES" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 171 | else |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 172 | _PW_NAME="activate" |
| 173 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 174 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 175 | _pw_green " ACTIVATOR! This sets your shell environment variables.\n" |
| 176 | fi |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 177 | fi |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 178 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 179 | if [ -f "$SETUP_SH" ]; then |
| 180 | . "$SETUP_SH" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 181 | |
Wyatt Hepler | 77bd6d2 | 2020-03-19 10:13:30 -0700 | [diff] [blame] | 182 | if [ "$?" -eq 0 ]; then |
| 183 | if [ "$_PW_IS_BOOTSTRAP" -eq 0 ] && [ -z "$PW_ENVSETUP_QUIET" ]; then |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 184 | echo "To activate this environment in the future, run this in your " |
| 185 | echo "terminal:" |
| 186 | echo |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 187 | _pw_green " source ./activate.sh\n" |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 188 | fi |
| 189 | else |
| 190 | _pw_red "Error during $_PW_NAME--see messages above." |
Rob Mohr | 47bd923 | 2020-03-13 12:07:26 -0700 | [diff] [blame] | 191 | fi |
| 192 | else |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 193 | _pw_red "Error during $_PW_NAME--see messages above." |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 194 | fi |
| 195 | |
| 196 | unset _PW_IS_BOOTSTRAP |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 197 | unset _PW_NAME |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 198 | unset _PIGWEED_BANNER |
Rob Mohr | d596a53 | 2020-04-03 08:59:12 -0700 | [diff] [blame] | 199 | unset _PW_OLD_CIPD_PACKAGE_FILES |
| 200 | unset _PW_OLD_VIRTUALENV_REQUIREMENTS |
| 201 | unset _PW_OLD_VIRTUALENV_SETUP_PY_ROOTS |
| 202 | unset _PW_OLD_CARGO_PACKAGE_FILES |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 203 | unset _pw_abspath |
| 204 | unset _pw_red |
| 205 | unset _pw_bold_red |
| 206 | unset _pw_green |
| 207 | unset _pw_bright_magenta |
Rob Mohr | 6ac3f7d | 2020-03-27 12:43:28 -0700 | [diff] [blame] | 208 | unset _pw_sourced |