blob: e13e4ae7867a9602ee57260227148b073a5514aa [file] [log] [blame]
Carlos Chinchilla5b83e822020-08-20 14:12:16 -07001# 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 Mohr4243f012020-10-15 10:53:46 -070015# 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 Chinchilla5b83e822020-08-20 14:12:16 -070024
Rob Mohr4243f012020-10-15 10:53:46 -070025_bootstrap_abspath () {
Rob Mohra4a80b92021-04-19 11:32:09 -070026 $(command -v python3 || command -v python2 || command -v python) -c "import os.path; print(os.path.abspath('$@'))"
Carlos Chinchilla5b83e822020-08-20 14:12:16 -070027}
28
Anthony DiGirolamocf492912020-10-08 18:19:22 -070029# Shell: bash.
Rob Mohr922833a2021-10-15 09:33:46 -070030if test -n "$BASH"; then
Armando Montanezffc59412024-02-05 23:43:37 +000031 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
Anthony DiGirolamocf492912020-10-08 18:19:22 -070032# Shell: zsh.
33elif test -n "$ZSH_NAME"; then
Armando Montanezffc59412024-02-05 23:43:37 +000034 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
Anthony DiGirolamocf492912020-10-08 18:19:22 -070035# Shell: dash.
36elif test ${0##*/} = dash; then
Armando Montanezffc59412024-02-05 23:43:37 +000037 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath \
Rob Mohr4243f012020-10-15 10:53:46 -070038 "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
Anthony DiGirolamocf492912020-10-08 18:19:22 -070039# If everything else fails, try $0. It could work.
40else
Armando Montanezffc59412024-02-05 23:43:37 +000041 _PW_BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
Anthony DiGirolamocf492912020-10-08 18:19:22 -070042fi
43
Rob Mohr4243f012020-10-15 10:53:46 -070044# Check if this file is being executed or sourced.
45_pw_sourced=0
46if [ -n "$SWARMING_BOT_ID" ]; then
47 # If set we're running on swarming and don't need this check.
48 _pw_sourced=1
49elif [ -n "$ZSH_EVAL_CONTEXT" ]; then
50 case $ZSH_EVAL_CONTEXT in *:file) _pw_sourced=1;; esac
51elif [ -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
55elif [ -n "$BASH_VERSION" ]; then
56 (return 0 2>/dev/null) && _pw_sourced=1
57else # 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
60fi
61
Rob Mohrd32b0422021-01-08 12:13:16 -080062# 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 Montanezffc59412024-02-05 23:43:37 +000066SAMPLE_PROJECT_ROOT="$(_bootstrap_abspath "$(dirname "$_PW_BOOTSTRAP_PATH")")"
Rob Mohr4243f012020-10-15 10:53:46 -070067export SAMPLE_PROJECT_ROOT
Rob Mohrd32b0422021-01-08 12:13:16 -080068PW_PROJECT_ROOT="$SAMPLE_PROJECT_ROOT"
Rob Mohr4243f012020-10-15 10:53:46 -070069PW_ROOT="$SAMPLE_PROJECT_ROOT/third_party/pigweed"
Rob Mohrd32b0422021-01-08 12:13:16 -080070
71# Set your project's banner and color.
Rob Mohrcff764f2023-06-20 19:36:38 +000072PW_BRANDING_BANNER="$SAMPLE_PROJECT_ROOT/banner.txt"
73export PW_BRANDING_BANNER
74PW_BRANDING_BANNER_COLOR=magenta
75export PW_BRANDING_BANNER_COLOR
Rob Mohrd32b0422021-01-08 12:13:16 -080076
77sample_project_banner() {
78 cat "$PW_BRANDING_BANNER"
79 echo
80}
81
82PW_BANNER_FUNC="sample_project_banner"
Rob Mohr4243f012020-10-15 10:53:46 -070083
Armando Montanezffc59412024-02-05 23:43:37 +000084# 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.
87if [ ! -e "$PW_ROOT/pw_env_setup/util.sh" ]; then
Anthony DiGirolamo95eff362023-09-14 22:42:25 +000088 echo "Updating git submodules ..."
89 # Init without recursion.
Armando Montanezffc59412024-02-05 23:43:37 +000090 git -C "$SAMPLE_PROJECT_ROOT" submodule update --init
Anthony DiGirolamo95eff362023-09-14 22:42:25 +000091fi
92
Armando Montanezffc59412024-02-05 23:43:37 +000093# Check that the Pico SDK has been checked out.
Anthony DiGirolamo95eff362023-09-14 22:42:25 +000094if [ ! -f "$SAMPLE_PROJECT_ROOT/third_party/pico_sdk/lib/tinyusb/LICENSE" ]; then
95 echo "Updating git submodules for 'third_party/pico_sdk' ..."
Armando Montanezffc59412024-02-05 23:43:37 +000096 git -C "$SAMPLE_PROJECT_ROOT/third_party/pico_sdk/" submodule update --init lib/tinyusb
Carlos Chinchilla5b83e822020-08-20 14:12:16 -070097fi
98
Armando Montanezffc59412024-02-05 23:43:37 +000099########## END PROJECT-SPECIFIC CODE ##########
100export PW_PROJECT_ROOT
101export PW_ROOT
102if [ -n "$PW_BANNER_FUNC" ]; then
103 export PW_BANNER_FUNC
104fi
Carlos Chinchillaad2e5fa2020-10-06 14:11:40 -0700105
Armando Montanezffc59412024-02-05 23:43:37 +0000106. "$PW_ROOT/pw_env_setup/util.sh"
107
108# Check environment properties
Rob Mohrd32b0422021-01-08 12:13:16 -0800109pw_deactivate
Armando Montanezffc59412024-02-05 23:43:37 +0000110pw_eval_sourced "$_pw_sourced" "$_PW_BOOTSTRAP_PATH"
111if ! pw_check_root "$PW_ROOT"; then
112 return
113fi
114
Rob Mohr4243f012020-10-15 10:53:46 -0700115_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
Anthony DiGirolamo4d2d1c72020-10-19 15:31:17 -0700116export _PW_ACTUAL_ENVIRONMENT_ROOT
Rob Mohr4243f012020-10-15 10:53:46 -0700117SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
Carlos Chinchilla5b83e822020-08-20 14:12:16 -0700118
Rob Mohr4243f012020-10-15 10:53:46 -0700119# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
Armando Montanezffc59412024-02-05 23:43:37 +0000120if [ "$(basename "$_PW_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
Rob Mohr4243f012020-10-15 10:53:46 -0700121 [ ! -f "$SETUP_SH" ] || \
122 [ ! -s "$SETUP_SH" ]; then
Rob Mohrd32b0422021-01-08 12:13:16 -0800123######### BEGIN PROJECT-SPECIFIC CODE #########
Rob Mohr11fe6292023-04-20 17:12:07 +0000124 pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_PROJECT_ROOT/pigweed.json"
Rob Mohrd32b0422021-01-08 12:13:16 -0800125########## END PROJECT-SPECIFIC CODE ##########
Rob Mohr4243f012020-10-15 10:53:46 -0700126 pw_finalize bootstrap "$SETUP_SH"
127else
128 pw_activate
129 pw_finalize activate "$SETUP_SH"
130fi
131
Rob Mohr1da70052021-03-04 10:43:28 -0800132if [ "$_PW_ENV_SETUP_STATUS" -eq 0 ]; then
Rob Mohr4243f012020-10-15 10:53:46 -0700133# This is where any additional checks about the environment should go.
Rob Mohrd32b0422021-01-08 12:13:16 -0800134######### BEGIN PROJECT-SPECIFIC CODE #########
Rob Mohr1da70052021-03-04 10:43:28 -0800135 echo -n
Rob Mohrd32b0422021-01-08 12:13:16 -0800136########## END PROJECT-SPECIFIC CODE ##########
Rob Mohr1da70052021-03-04 10:43:28 -0800137fi
Rob Mohr4243f012020-10-15 10:53:46 -0700138
139unset _pw_sourced
Armando Montanezffc59412024-02-05 23:43:37 +0000140unset _PW_BOOTSTRAP_PATH
Rob Mohr4243f012020-10-15 10:53:46 -0700141unset SETUP_SH
142unset _bootstrap_abspath
Armando Montanezffc59412024-02-05 23:43:37 +0000143
144if [[ "$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
154fi
Rob Mohr4243f012020-10-15 10:53:46 -0700155
156pw_cleanup
Armando Montanezffc59412024-02-05 23:43:37 +0000157
158git -C "$PW_ROOT" config blame.ignoreRevsFile .git-blame-ignore-revs