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 |
Rob Mohr | 33181e6 | 2020-03-11 10:00:32 -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 |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [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 |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [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 |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 64 | PW_SETUP_SCRIPT_PATH=$(_pw_abspath \ |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 65 | $(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')) |
| 66 | # If everything else fails, try $0. It could work. |
| 67 | else |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [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 | 0812466 | 2020-03-10 11:30:33 -0700 | [diff] [blame] | 71 | PW_ROOT=$(dirname $PW_SETUP_SCRIPT_PATH) |
Rob Mohr | cacb877 | 2019-11-22 13:14:01 -0800 | [diff] [blame] | 72 | export PW_ROOT |
| 73 | |
Rob Mohr | 3352ccf | 2020-03-11 14:01:18 -0700 | [diff] [blame] | 74 | SETUP_SH="$PW_ROOT/pw_env_setup/.env_setup.sh" |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 75 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 76 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 77 | _pw_green "\n WELCOME TO...\n" |
| 78 | _pw_bright_magenta "$_PIGWEED_BANNER\n" |
| 79 | fi |
Rob Mohr | de7c77a | 2020-01-16 13:51:44 -0800 | [diff] [blame] | 80 | |
Rob Mohr | 5898a9d | 2020-01-22 13:54:43 -0800 | [diff] [blame] | 81 | # Run full bootstrap when invoked as bootstrap, or env file is missing/empty. |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 82 | [ $(basename $PW_SETUP_SCRIPT_PATH) = "bootstrap.sh" ] || \ |
Rob Mohr | 5898a9d | 2020-01-22 13:54:43 -0800 | [diff] [blame] | 83 | [ ! -f $SETUP_SH ] || \ |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 84 | [ ! -s $SETUP_SH ] |
| 85 | _PW_IS_BOOTSTRAP=$? |
| 86 | |
| 87 | if [ $_PW_IS_BOOTSTRAP -eq 0 ]; then |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 88 | _PW_NAME="bootstrap" |
| 89 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 90 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 91 | _pw_green " BOOTSTRAP! Bootstrap may take a few minutes; please be patient.\n" |
| 92 | fi |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 93 | |
Alexei Frolov | 426b9b8 | 2020-03-09 14:30:22 -0700 | [diff] [blame] | 94 | # Allow forcing a specific version of Python for testing pursposes. |
| 95 | if [ -n "$PW_BOOTSTRAP_PYTHON" ]; then |
| 96 | PYTHON="$PW_BOOTSTRAP_PYTHON" |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 97 | elif which python &> /dev/null; then |
| 98 | PYTHON=python |
| 99 | else |
| 100 | _pw_bold_red "Error: No system Python present\n" |
| 101 | _pw_red " Pigweed's bootstrap process requires a local system Python." |
| 102 | _pw_red " Please install Python on your system, add it to your PATH" |
| 103 | _pw_red " and re-try running bootstrap." |
| 104 | return |
| 105 | fi |
| 106 | |
Rob Mohr | 2cc32ec | 2020-02-05 10:55:12 -0800 | [diff] [blame] | 107 | $PYTHON $PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py --shell-file $SETUP_SH |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 108 | else |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 109 | _PW_NAME="activate" |
| 110 | |
Rob Mohr | 34466c4 | 2020-03-10 12:04:14 -0700 | [diff] [blame] | 111 | if [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 112 | _pw_green " ACTIVATOR! This sets your shell environment variables.\n" |
| 113 | fi |
Rob Mohr | 9382556 | 2020-01-02 15:20:50 -0800 | [diff] [blame] | 114 | fi |
Rob Mohr | dc70d1c | 2019-12-04 10:05:28 -0800 | [diff] [blame] | 115 | |
Rob Mohr | 47bd923 | 2020-03-13 12:07:26 -0700 | [diff] [blame] | 116 | if [ -f $SETUP_SH ]; then |
| 117 | . $SETUP_SH |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 118 | |
Keir Mierle | a1f08ac | 2020-03-18 16:07:09 -0700 | [diff] [blame] | 119 | if [ $? -eq 0 ]; then |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 120 | if [ $_PW_IS_BOOTSTRAP -eq 0 ] && [ -z "$PW_ENVSETUP_QUIET" ]; then |
| 121 | echo "To activate this environment in the future, run this in your " |
| 122 | echo "terminal:" |
| 123 | echo |
| 124 | _pw_green " . ./activate.sh\n" |
| 125 | fi |
| 126 | else |
| 127 | _pw_red "Error during $_PW_NAME--see messages above." |
Rob Mohr | 47bd923 | 2020-03-13 12:07:26 -0700 | [diff] [blame] | 128 | fi |
| 129 | else |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 130 | _pw_red "Error during $_PW_NAME--see messages above." |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 131 | fi |
| 132 | |
| 133 | unset _PW_IS_BOOTSTRAP |
Rob Mohr | d679bde | 2020-03-18 09:27:17 -0700 | [diff] [blame] | 134 | unset _PW_NAME |
Alexei Frolov | a137f97 | 2020-03-06 11:14:13 -0800 | [diff] [blame] | 135 | unset _PIGWEED_BANNER |
| 136 | unset _pw_abspath |
| 137 | unset _pw_red |
| 138 | unset _pw_bold_red |
| 139 | unset _pw_green |
| 140 | unset _pw_bright_magenta |