-
Notifications
You must be signed in to change notification settings - Fork 11
/
setup.sh
executable file
·268 lines (221 loc) · 7.6 KB
/
setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# RoFI environment setup script
SILENT=
silentEcho() {
if [ ! $SILENT ]; then
echo $@
fi
}
# Backup a value of a variable in order to restore it during teardown
backup() {
local varcontent="$(eval echo \"'$'$1\")"
local backupcontent="$(eval echo \"'$ORIGINAL_'$1\")"
# Do not overwrite backups (e.g., when re-invoking setup)
if [ -z "$backupcontent" ]
then
if [ -n "$varcontent" ]
then
eval export ORIGINAL_$1=\""$varcontent"\"
TEARDOWN_CMD="$TEARDOWN_CMD export $1=\"\$ORIGINAL_$1\"; unset ORIGINAL_$1;"
else
# Set backup so it's not overwritten
eval export ORIGINAL_$1=\"" "\"
TEARDOWN_CMD="$TEARDOWN_CMD unset $1; unset ORIGINAL_$1;"
fi
fi
}
teardownImpl() {
eval $TEARDOWN_CMD
unset ROFI_BUILD_CONFIGURATION
unset ROFI_ROOT
unset ROFI_BUILD_DIR
unset TEARDOWN_CMD
unset -f teardown
unset -f teardownImpl
}
teardown() {
silentEcho "Tearing down the RoFI environment"
teardownImpl
}
configurationDesc() {
case "$1" in
"Debug") echo D;;
"Release") echo R;;
"RelWithDebInfo") echo RD;;
"MinSizeRel") echo MSR;;
*) echo U
esac
}
setGazeboVariables() {
backup GAZEBO_NAME
# backup GAZEBO_MASTER_URI
# backup GAZEBO_MODEL_DATABASE_URI
backup GAZEBO_RESOURCE_PATH
backup GAZEBO_PLUGIN_PATH
backup GAZEBO_MODEL_PATH
if [ -z "$GAZEBO_NAME" ]; then
GAZEBO_NAME="gazebo"
fi
local GAZEBO_BIN=$(command -v "$GAZEBO_NAME")
local GAZEBO_SHARE=$(dirname "$(dirname "$GAZEBO_BIN")")/share/gazebo
if [ ! -d "$GAZEBO_SHARE" ]; then
return
fi
# GAZEBO_MASTER_URI=$ORIGINAL_GAZEBO_MASTER_URI
# GAZEBO_MODEL_DATABASE_URI=$ORIGINAL_GAZEBO_MODEL_DATABASE_URI
GAZEBO_RESOURCE_PATH=$ORIGINAL_GAZEBO_RESOURCE_PATH
GAZEBO_PLUGIN_PATH=$ORIGINAL_GAZEBO_PLUGIN_PATH
GAZEBO_MODEL_PATH=$ORIGINAL_GAZEBO_MODEL_PATH
source $GAZEBO_SHARE/setup.sh
}
setupIdf() {
IDF_REQUIRED_VERSION=v5.0-beta1
# We allow the user to set a global path to build dependencies that are
# fetched autonomously, e.g., in the script ~/rofi.pre.env. If there is no
# such path, we put the build tools into the current RoFI worktree.
#
# We also fetch the parameters when the global version doesn't match the
# required version
INSTALLED_VERSION=$(cd ${ROFI_TOOLS_PATH}/esp-idf 2>/dev/null && git describe 2>/dev/null)
if [ ! $ROFI_TOOLS_PATH ]; then
export ROFI_TOOLS_PATH=$ROFI_ROOT/build.deps
elif [ -d "${ROFI_TOOLS_PATH}" ] && [ "$INSTALLED_VERSION" ] && [ "${INSTALLED_VERSION}" != "${IDF_REQUIRED_VERSION}" ]; then
export ROFI_TOOLS_PATH=$ROFI_ROOT/build.deps
fi
mkdir -p $ROFI_TOOLS_PATH
export IDF_PATH=$ROFI_TOOLS_PATH/esp-idf
export IDF_TOOLS_PATH=$ROFI_TOOLS_PATH/esp-tools
export PYTHONPATH="${IDF_PATH}/components/partition_table:$PYTHONPATH"
if [ ! -d $IDF_PATH ]; then
git clone --depth 1 --branch ${IDF_REQUIRED_VERSION} --recursive \
https://github.com/espressif/esp-idf.git $IDF_PATH
$IDF_PATH/install.sh
IDF_POST_INSTALL=1
fi
source $IDF_PATH/export.sh
if [ $IDF_POST_INSTALL ]; then
# When we use IDF, it brings python venv; thus if we want to build doc,
# we have to install spinx and breathe into the venv
pip install sphinx breathe myst_parser sphinx_rtd_theme
fi
IDF_POST_INSTALL=
}
print_help() {
cat << EOF
Usage:
source setup.sh [-s] [-i] <Release|Debug|RelWithDebInfo|MinSizeRel>
Setup environment for given configuration. Release is the default one.
./setup.sh -h
Print help
Options:
-s Make the script silent (e.g., when running from CI)
-i Setup tools for rofi firmware development
EOF
}
############## Main script ##############
## Check if sourced - has to be called outside function
# Took from https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced
([[ -n $ZSH_EVAL_CONTEXT && $ZSH_EVAL_CONTEXT =~ :file$ ]] ||
[[ -n $KSH_VERSION && $(cd "$(dirname -- "$0")" &&
printf '%s' "${PWD%/}/")$(basename -- "$0") != "${.sh.file}" ]] ||
[[ -n $BASH_VERSION ]] && (return 0 2>/dev/null)) && sourced=1
run() {
local OPTIND flag
local ALTER_PROMPT
local SETUP_IDF
while getopts "hsi" flag; do
case "$flag" in
h)
print_help
return 0
;;
s) SILENT=1;;
i) SETUP_IDF=1;;
?)
return 1
;;
esac
done
CWD=$(pwd)
if [ ! -e ${CWD}/setup.sh ] || [ "$(head -n1 ${CWD}/setup.sh)" != "# RoFI environment setup script" ]; then
>&2 echo "The script needs to be invoked from the root of the project."
return 1
fi
## Source user defines
if [ -f ~/.rofi.pre.env ]; then
silentEcho "Applying extra setting from ~/.rofi.pre.env"
source ~/.rofi.pre.env
fi
backup ROFI_SHELL_INDICATOR
backup PATH
backup PYTHONPATH
backup MAKEFLAGS
backup ROFI_CMAKE_GENERATOR
export MAKEFLAGS="${MAKEFLAGS} --no-print-directory"
# Use Ninja if it is available as it is faster
if [ $(command -v ninja) ]; then
export ROFI_CMAKE_GENERATOR="Ninja"
else
export ROFI_CMAKE_GENERATOR="Unix Makefiles"
fi
setGazeboVariables
ROFI_BUILD_CONFIGURATION="Release"
if [ -z "$sourced" ]; then
>&2 echo "Invalid usage; do not invoke directly, use 'source [-f] <Release|Debug|RelWithDebInfo>' instead"
>&2 echo
return 1
fi
ARG1=${@:$OPTIND:1}
if [ -n "${ARG1}" ];
then ROFI_BUILD_CONFIGURATION=${ARG1};
fi
case "$ROFI_BUILD_CONFIGURATION" in
Release|Debug|RelWithDebInfo|MinSizeRel);;
*)
>&2 echo "Unsupported build configuration $ROFI_BUILD_CONFIGURATION"
return 1
;;
esac
silentEcho "Setting up environment for $ROFI_BUILD_CONFIGURATION."
silentEcho " To go back, invoke teardown"
silentEcho " To change the configuration, simply invoke setup again"
export ROFI_SHELL_INDICATOR="🤖 $(configurationDesc $ROFI_BUILD_CONFIGURATION)"
export ROFI_BUILD_CONFIGURATION
export ROFI_ROOT=$(pwd)
export ROFI_BUILD_DIR="$(pwd)/build.${ROFI_BUILD_PREFIX}${ROFI_BUILD_CONFIGURATION}"
export GAZEBO_MODEL_PATH="$ROFI_ROOT/data/gazebo/models:$GAZEBO_MODEL_PATH"
export GAZEBO_PLUGIN_PATH="$ROFI_BUILD_DIR/desktop/lib:$GAZEBO_PLUGIN_PATH"
export GAZEBO_RESOURCE_PATH="$ROFI_ROOT/data/gazebo:$GAZEBO_RESOURCE_PATH"
export GAZEBO_RESOURCE_PATH="$ROFI_BUILD_DIR/desktop/data/gazebo:$GAZEBO_RESOURCE_PATH"
export PATH="$(realpath releng/tools):$ORIGINAL_PATH"
## Add bin directories of the suites to path
for suite in $(find suites -maxdepth 1 -type d -exec basename {} \;); do
export PATH="${ROFI_BUILD_DIR}/$suite/bin:$PATH"
export PYTHONPATH="${ROFI_BUILD_DIR}/$suite/lib:$PYTHONPATH"
done
if [ -n "$SETUP_IDF" ]; then
setupIdf
fi
## Register tab completion
if [ -n "$ZSH_VERSION" ]; then
autoload bashcompinit
bashcompinit
fi
source releng/tools/_registerCompletion.sh
## Source user defines
if [ -f ~/.rofi.post.env ]; then
silentEcho "Applying extra setting from ~/.rofi.post.env"
source ~/.rofi.post.env
fi
}
run $@
if [ $? -ne 0 ]; then
>&2 echo "No changes have been made"
>&2 print_help
teardownImpl
fi
## Cleanup namespace
unset -f print_help
unset -f backup
unset -f configurationDesc
unset -f setGazeboVariables
unset -f run