blob: cc9fb61e1f33e2a33f5967eeb70b0bdd38df88e9 [file] [log] [blame]
Anthony DiGirolamob302d742023-09-12 16:48:53 +00001# Copyright 2023 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# 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
24
25_bootstrap_abspath () {
26 $(command -v python3 || command -v python2 || command -v python) -c "import os.path; print(os.path.abspath('$@'))"
27}
28
29# Shell: bash.
30if test -n "$BASH"; then
31 _BOOTSTRAP_PATH="$(_bootstrap_abspath "$BASH_SOURCE")"
32# Shell: zsh.
33elif test -n "$ZSH_NAME"; then
34 _BOOTSTRAP_PATH="$(_bootstrap_abspath "${(%):-%N}")"
35# Shell: dash.
36elif test ${0##*/} = dash; then
37 _BOOTSTRAP_PATH="$(_bootstrap_abspath \
38 "$(lsof -p $$ -Fn0 | tail -1 | sed 's#^[^/]*##;')")"
39# If everything else fails, try $0. It could work.
40else
41 _BOOTSTRAP_PATH="$(_bootstrap_abspath "$0")"
42fi
43
44# 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
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 #########
66export KUDZU_ROOT="$(_bootstrap_abspath "$(dirname "$_BOOTSTRAP_PATH")")"
67# Make pw experimental presubmit happy
68export PIGWEED_EXPERIMENTAL_ROOT="$KUDZU_ROOT"
Anthony DiGirolamo41de34b2024-06-06 20:08:19 +000069export PW_PROJECT_ROOT="$KUDZU_ROOT"
70export PW_ROOT="$KUDZU_ROOT/third_party/pigweed"
Anthony DiGirolamob302d742023-09-12 16:48:53 +000071
72# Set your project's banner and color.
73export PW_BRANDING_BANNER="$KUDZU_ROOT/banner.txt"
74export PW_BRANDING_BANNER_COLOR=magenta
75
76KUDZU_banner() {
77 cat "$PW_BRANDING_BANNER"
78 echo
79}
80
Anthony DiGirolamo41de34b2024-06-06 20:08:19 +000081export PW_BANNER_FUNC="KUDZU_banner"
Anthony DiGirolamob302d742023-09-12 16:48:53 +000082########## END PROJECT-SPECIFIC CODE ##########
Anthony DiGirolamob302d742023-09-12 16:48:53 +000083
84_util_sh="$PW_ROOT/pw_env_setup/util.sh"
85
86# Double-check that the Pigweed submodule has been checked out.
87if [ ! -e "$_util_sh" ]; then
Armando Montanez76d7b192023-10-06 16:35:47 +000088 echo "Updating git submodules ..."
89 # Init without recursion.
90 git submodule update --init
91fi
Armando Montanez52ad5a72023-10-06 16:48:42 +000092if [ ! -f "$PW_PROJECT_ROOT/third_party/pico-sdk/lib/tinyusb/LICENSE" ]; then
Armando Montanez76d7b192023-10-06 16:35:47 +000093 echo "Updating git submodules for 'third_party/pico-sdk' ..."
94 # Init tinyusb only with no recursion.
Armando Montanez52ad5a72023-10-06 16:48:42 +000095 pushd third_party/pico-sdk/
Armando Montanez76d7b192023-10-06 16:35:47 +000096 git submodule update --init lib/tinyusb
97 popd
Anthony DiGirolamob302d742023-09-12 16:48:53 +000098fi
99
100. "$_util_sh"
101
102pw_deactivate
103pw_eval_sourced "$_pw_sourced"
104pw_check_root "$PW_ROOT"
105_PW_ACTUAL_ENVIRONMENT_ROOT="$(pw_get_env_root)"
106export _PW_ACTUAL_ENVIRONMENT_ROOT
107SETUP_SH="$_PW_ACTUAL_ENVIRONMENT_ROOT/activate.sh"
108
109# Run full bootstrap when invoked as bootstrap, or env file is missing/empty.
110if [ "$(basename "$_BOOTSTRAP_PATH")" = "bootstrap.sh" ] || \
111 [ ! -f "$SETUP_SH" ] || \
112 [ ! -s "$SETUP_SH" ]; then
113# This is where pw_bootstrap is called. Most small projects will include
114# --use-pigweed-defaults.
115######### BEGIN PROJECT-SPECIFIC CODE #########
116 pw_bootstrap --shell-file "$SETUP_SH" --install-dir "$_PW_ACTUAL_ENVIRONMENT_ROOT" --config-file "$PW_PROJECT_ROOT/pigweed.json"
117########## END PROJECT-SPECIFIC CODE ##########
118 pw_finalize bootstrap "$SETUP_SH"
119else
120 pw_activate
121 pw_finalize activate "$SETUP_SH"
122fi
123
124# This is where any additional checks about the environment should go.
125######### BEGIN PROJECT-SPECIFIC CODE #########
126########## END PROJECT-SPECIFIC CODE ##########
127
128unset _pw_sourced
129unset _BOOTSTRAP_PATH
130unset SETUP_SH
131unset _bootstrap_abspath
132unset _util_sh
133
134pw_install_post_checkout_hook
135
Anthony DiGirolamo41de34b2024-06-06 20:08:19 +0000136if [[ "$TERM" != dumb ]]; then
137 # Shell completion: bash.
138 if test -n "$BASH"; then
139 . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.bash"
140 . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.bash"
141 # Shell completion: zsh.
142 elif test -n "$ZSH_NAME"; then
143 . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw.zsh"
144 . "$PW_ROOT/pw_cli/py/pw_cli/shell_completion/pw_build.zsh"
145 fi
146fi
147
Anthony DiGirolamob302d742023-09-12 16:48:53 +0000148pw_cleanup