Carlos Chinchilla | 5b83e82 | 2020-08-20 14:12:16 -0700 | [diff] [blame] | 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 | |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 15 | # This script must be sourced, not executed. |
| 16 | # |
| 17 | # Create a new environment: |
| 18 | # |
| 19 | # source bootstrap.sh |
| 20 | # |
| 21 | # Activate an existing environment: |
| 22 | # |
| 23 | # source activate.sh |
Carlos Chinchilla | 5b83e82 | 2020-08-20 14:12:16 -0700 | [diff] [blame] | 24 | |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 25 | _bootstrap_abspath () { |
Rob Mohr | a4a80b9 | 2021-04-19 11:32:09 -0700 | [diff] [blame] | 26 | $(command -v python3 || command -v python2 || command -v python) -c "import os.path; print(os.path.abspath('$@'))" |
Carlos Chinchilla | 5b83e82 | 2020-08-20 14:12:16 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Anthony DiGirolamo | cf49291 | 2020-10-08 18:19:22 -0700 | [diff] [blame] | 29 | # Shell: bash. |
Rob Mohr | 922833a | 2021-10-15 09:33:46 -0700 | [diff] [blame] | 30 | if test -n "$BASH"; then |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 31 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")" |
Anthony DiGirolamo | cf49291 | 2020-10-08 18:19:22 -0700 | [diff] [blame] | 32 | # Shell: zsh. |
| 33 | elif test -n "$ZSH_NAME"; then |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 34 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")" |
Anthony DiGirolamo | cf49291 | 2020-10-08 18:19:22 -0700 | [diff] [blame] | 35 | # Shell: dash. |
| 36 | elif test ${0##*/} = dash; then |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 37 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath \ |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 38 | "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")" |
Anthony DiGirolamo | cf49291 | 2020-10-08 18:19:22 -0700 | [diff] [blame] | 39 | # If everything else fails, try $0. It could work. |
| 40 | else |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 41 | _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")" |
Anthony DiGirolamo | cf49291 | 2020-10-08 18:19:22 -0700 | [diff] [blame] | 42 | fi |
| 43 | |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 44 | # Check if this file is being executed or sourced. |
| 45 | _pw_sourced=0 |
| 46 | if [ -n "$SWARMING_BOT_ID" ]; then |
| 47 | # If set we're running on swarming and don't need this check. |
| 48 | _pw_sourced=1 |
| 49 | elif [ -n "$ZSH_EVAL_CONTEXT" ]; then |
| 50 | case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac |
| 51 | elif [ -n "$KSH_VERSION" ]; then |
| 52 | [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != \ |
| 53 | "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] \ |
| 54 | && _pw_sourced=1 |
| 55 | elif [ -n "$BASH_VERSION" ]; then |
| 56 | (return 0 2>/dev/null) && _pw_sourced=1 |
| 57 | else # All other shells: examine $0 for known shell binary filenames |
| 58 | # Detects `sh` and `dash`; add additional shell filenames as needed. |
| 59 | case ${0##*/} in sh|dash) _pw_sourced=1;; esac |
| 60 | fi |
| 61 | |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 62 | # Downstream projects need to set something other than PW_ROOT here, like |
| 63 | # YOUR_PROJECT_ROOT. Please also set PW_PROJECT_ROOT and PW_ROOT before |
| 64 | # invoking pw_bootstrap or pw_activate. |
| 65 | ######### BEGIN PROJECT-SPECIFIC CODE ######### |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 66 | SAMPLE_PROJECT_ROOT="$(_bootstrap_abspath "$(dirname "$_PW_BOOTSTRAP_PATH")")" |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 67 | export SAMPLE_PROJECT_ROOT |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 68 | PW_PROJECT_ROOT="$SAMPLE_PROJECT_ROOT" |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 69 | PW_ROOT="$SAMPLE_PROJECT_ROOT/third_party/pigweed" |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 70 | |
| 71 | # Set your project's banner and color. |
Rob Mohr | cff764f | 2023-06-20 19:36:38 +0000 | [diff] [blame] | 72 | PW_BRANDING_BANNER="$SAMPLE_PROJECT_ROOT/banner.txt" |
| 73 | export PW_BRANDING_BANNER |
| 74 | PW_BRANDING_BANNER_COLOR=magenta |
| 75 | export PW_BRANDING_BANNER_COLOR |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 76 | |
| 77 | sample_project_banner() { |
| 78 | cat "$PW_BRANDING_BANNER" |
| 79 | echo |
| 80 | } |
| 81 | |
| 82 | PW_BANNER_FUNC="sample_project_banner" |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 83 | |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 84 | # Double-check that the Pigweed submodule has been checked out. The file |
| 85 | # checked here is somewhat arbitrary, but should be a file path that is |
| 86 | # relatively stable. |
| 87 | if [ ! -e "$PW_ROOT/pw_env_setup/util.sh" ]; then |
Anthony DiGirolamo | 95eff36 | 2023-09-14 22:42:25 +0000 | [diff] [blame] | 88 | echo "Updating git submodules ..." |
| 89 | # Init without recursion. |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 90 | git -C "$SAMPLE_PROJECT_ROOT" submodule update --init |
Anthony DiGirolamo | 95eff36 | 2023-09-14 22:42:25 +0000 | [diff] [blame] | 91 | fi |
| 92 | |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 93 | # Check that the Pico SDK has been checked out. |
Anthony DiGirolamo | 95eff36 | 2023-09-14 22:42:25 +0000 | [diff] [blame] | 94 | if [ ! -f "$SAMPLE_PROJECT_ROOT/third_party/pico_sdk/lib/tinyusb/LICENSE" ]; then |
| 95 | echo "Updating git submodules for 'third_party/pico_sdk' ..." |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 96 | git -C "$SAMPLE_PROJECT_ROOT/third_party/pico_sdk/" submodule update --init lib/tinyusb |
Carlos Chinchilla | 5b83e82 | 2020-08-20 14:12:16 -0700 | [diff] [blame] | 97 | fi |
| 98 | |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 99 | ########## END PROJECT-SPECIFIC CODE ########## |
| 100 | export PW_PROJECT_ROOT |
| 101 | export PW_ROOT |
| 102 | if [ -n "$PW_BANNER_FUNC" ]; then |
| 103 | export PW_BANNER_FUNC |
| 104 | fi |
Carlos Chinchilla | ad2e5fa | 2020-10-06 14:11:40 -0700 | [diff] [blame] | 105 | |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 106 | . "$PW_ROOT/pw_env_setup/util.sh" |
| 107 | |
| 108 | # Check environment properties |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 109 | pw_deactivate |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 110 | pw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH" |
| 111 | if ! pw_check_root "$PW_ROOT"; then |
| 112 | return |
| 113 | fi |
| 114 | |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 115 | _PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)" |
Anthony DiGirolamo | 4d2d1c7 | 2020-10-19 15:31:17 -0700 | [diff] [blame] | 116 | export _PW_ACTUAL_ENVIRONMENT_ROOT |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 117 | SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh" |
Carlos Chinchilla | 5b83e82 | 2020-08-20 14:12:16 -0700 | [diff] [blame] | 118 | |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 119 | # Run full bootstrap when invoked as bootstrap, or env file is missing/empty. |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 120 | if [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \ |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 121 | [ ! -f "$SETUP_SH" ] || \ |
| 122 | [ ! -s "$SETUP_SH" ]; then |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 123 | ######### BEGIN PROJECT-SPECIFIC CODE ######### |
Rob Mohr | 11fe629 | 2023-04-20 17:12:07 +0000 | [diff] [blame] | 124 | pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_PROJECT_ROOT/pigweed.json" |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 125 | ########## END PROJECT-SPECIFIC CODE ########## |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 126 | pw_finalize bootstrap "$SETUP_SH" |
| 127 | else |
| 128 | pw_activate |
| 129 | pw_finalize activate "$SETUP_SH" |
| 130 | fi |
| 131 | |
Rob Mohr | 1da7005 | 2021-03-04 10:43:28 -0800 | [diff] [blame] | 132 | if [ "$_PW_ENV_SETUP_STATUS" -eq 0 ]; then |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 133 | # This is where any additional checks about the environment should go. |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 134 | ######### BEGIN PROJECT-SPECIFIC CODE ######### |
Rob Mohr | 1da7005 | 2021-03-04 10:43:28 -0800 | [diff] [blame] | 135 | echo -n |
Rob Mohr | d32b042 | 2021-01-08 12:13:16 -0800 | [diff] [blame] | 136 | ########## END PROJECT-SPECIFIC CODE ########## |
Rob Mohr | 1da7005 | 2021-03-04 10:43:28 -0800 | [diff] [blame] | 137 | fi |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 138 | |
| 139 | unset _pw_sourced |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 140 | unset _PW_BOOTSTRAP_PATH |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 141 | unset SETUP_SH |
| 142 | unset _bootstrap_abspath |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 143 | |
| 144 | if [[ "$TERM" != dumb ]]; then |
| 145 | # Shell completion: bash. |
| 146 | if test -n "$BASH"; then |
| 147 | . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.bash" |
| 148 | . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.bash" |
| 149 | # Shell completion: zsh. |
| 150 | elif test -n "$ZSH_NAME"; then |
| 151 | . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.zsh" |
| 152 | . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.zsh" |
| 153 | fi |
| 154 | fi |
Rob Mohr | 4243f01 | 2020-10-15 10:53:46 -0700 | [diff] [blame] | 155 | |
| 156 | pw_cleanup |
Armando Montanez | ffc5941 | 2024-02-05 23:43:37 +0000 | [diff] [blame] | 157 | |
| 158 | git -C "$PW_ROOT" config blame.ignoreRevsFile .git-blame-ignore-revs |