amiro-blt / setup.sh @ 0a42f078
History | View | Annotate | Download (19.6 KB)
1 |
################################################################################ |
---|---|
2 |
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini # |
3 |
# Robot (AMiRo) platform. # |
4 |
# Copyright (C) 2016..2017 Thomas Schöpping et al. # |
5 |
# # |
6 |
# This program is free software: you can redistribute it and/or modify # |
7 |
# it under the terms of the GNU General Public License as published by # |
8 |
# the Free Software Foundation, either version 3 of the License, or # |
9 |
# (at your option) any later version. # |
10 |
# # |
11 |
# This program is distributed in the hope that it will be useful, # |
12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
14 |
# GNU General Public License for more details. # |
15 |
# # |
16 |
# You should have received a copy of the GNU General Public License # |
17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. # |
18 |
# # |
19 |
# This research/work was supported by the Cluster of Excellence Cognitive # |
20 |
# Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is # |
21 |
# funded by the German Research Foundation (DFG). # |
22 |
################################################################################ |
23 |
|
24 |
#!/bin/bash |
25 |
|
26 |
################################################################################ |
27 |
# GENERIC FUNCTIONS # |
28 |
################################################################################ |
29 |
|
30 |
### print an error message ##################################################### |
31 |
# Prints a error <message> to standard output. |
32 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
33 |
# |
34 |
# usage: printError <message> |
35 |
# arguments: <message> |
36 |
# Message string to print. |
37 |
# return: n/a |
38 |
# |
39 |
function printError { |
40 |
local string="ERROR: $1" |
41 |
# if a log file is specified |
42 |
if [ -n "$LOG_FILE" ]; then |
43 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
44 |
fi |
45 |
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
46 |
} |
47 |
|
48 |
### print a warning message #################################################### |
49 |
# Prints a warning <message> to standard output. |
50 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
51 |
# |
52 |
# usage: printMessage <message> |
53 |
# arguments: <message> |
54 |
# Message string to print. |
55 |
# return: n/a |
56 |
# |
57 |
function printWarning { |
58 |
local string="WARNING: $1" |
59 |
# if a log file is specified |
60 |
if [ -n "$LOG_FILE" ]; then |
61 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
62 |
fi |
63 |
printf "$(tput setaf 3)>>> $string$(tput sgr 0)" |
64 |
} |
65 |
|
66 |
### print an information message ############################################### |
67 |
# Prints an information <message> to standard output. |
68 |
#If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
69 |
# |
70 |
# usage: printInfo <message> |
71 |
# arguments: <message> |
72 |
# Message string to print. |
73 |
# return: n/a |
74 |
# |
75 |
function printInfo { |
76 |
local string="INFO: $1" |
77 |
# if a log file is specified |
78 |
if [ -n "$LOG_FILE" ]; then |
79 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
80 |
fi |
81 |
printf "$(tput setaf 2)>>> $string$(tput sgr 0)" |
82 |
} |
83 |
|
84 |
### print a message to file #################################################### |
85 |
# Appends a <message> to a log file, specified by the variable 'LOG_FILE'. |
86 |
# |
87 |
# usage printLog <message> |
88 |
# arguments: <message> |
89 |
# Message string to print. |
90 |
# return: n/a |
91 |
# |
92 |
function printLog { |
93 |
local string="LOG: $1" |
94 |
# if a log file is specified |
95 |
if [ -n "$LOG_FILE" ]; then |
96 |
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
97 |
fi |
98 |
} |
99 |
|
100 |
### exit the script normally ################################################### |
101 |
# Prints a delimiter and exits the script normally (returns 0). |
102 |
# |
103 |
# usage: quitScript |
104 |
# arguments: n/a |
105 |
# return: 0 |
106 |
# No error or warning occurred. |
107 |
# |
108 |
function quitScript { |
109 |
printLog "exiting $(realpath ${BASH_SOURCE[0]})\n" |
110 |
printf "######################################################################\n" |
111 |
exit 0 |
112 |
} |
113 |
|
114 |
### read a user input ########################################################## |
115 |
# Reads a single character user input from a set up <options> and stores it in |
116 |
# a given <return> variable. |
117 |
# |
118 |
# usage: readUserInput <options> <return> |
119 |
# arguments: <options> |
120 |
# String definiing the set of valid characters. |
121 |
# If the string is empty, the user can input any character. |
122 |
# <return> |
123 |
# Variable to store the selected character to. |
124 |
# return: n/a |
125 |
# |
126 |
function readUserInput { |
127 |
local input="" |
128 |
# read user input |
129 |
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
130 |
read -p "your selection: " -n 1 -e input |
131 |
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
132 |
printWarning "[$input] is no valid action\n" |
133 |
fi |
134 |
done |
135 |
printLog "[$input] has been selected\n" |
136 |
eval $2="$input" |
137 |
} |
138 |
|
139 |
### check whether argument is an option ######################################## |
140 |
# Checks a <string> whether it is an option. |
141 |
# Options are defined to either start with '--' followed by any string, or |
142 |
# to start with a single '-' followed by a single character, or |
143 |
# to start with a single '-' followed by a single character, a '=' and any string. |
144 |
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
145 |
# |
146 |
# usage: parseIsOption <string> |
147 |
# arguments: <string> |
148 |
# A string to check whether it is an option. |
149 |
# return: 0 |
150 |
# <string> is an option. |
151 |
# -1 |
152 |
# <string> is not an option. |
153 |
# |
154 |
function parseIsOption { |
155 |
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
156 |
return 0 |
157 |
else |
158 |
return -1 |
159 |
fi |
160 |
} |
161 |
|
162 |
### set the log file ########################################################### |
163 |
# Sets a specified <infile> as log file and checks whether it already exists. |
164 |
# If so, the log may either be appended to the file, its content can be cleared, |
165 |
# or no log is generated at all. |
166 |
# The resulting path is stored in <outvar>. |
167 |
# |
168 |
# usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
169 |
# arguments: --option=<option> |
170 |
# Select what to do if <file> already exists. |
171 |
# Possible values are 'a', 'r' and 'n'. |
172 |
# - a: append |
173 |
# - r: delete and restart |
174 |
# - n: no log |
175 |
# If no option is secified but <file> exists, an interactive selection is provided. |
176 |
# --quiet |
177 |
# Suppress all messages. |
178 |
# <infile> |
179 |
# Path of the wanted log file. |
180 |
# <outvar> |
181 |
# Variable to store the path of the log file to. |
182 |
# return: 0 |
183 |
# No error or warning occurred. |
184 |
# -1 |
185 |
# Error: invalid input |
186 |
# |
187 |
function setLogFile { |
188 |
local filepath="" |
189 |
local option="" |
190 |
local quiet=false |
191 |
|
192 |
# parse arguments |
193 |
local otherargs=() |
194 |
while [ $# -gt 0 ]; do |
195 |
if ( parseIsOption $1 ); then |
196 |
case "$1" in |
197 |
-o=*|--option=*) |
198 |
option=${1#*=}; shift 1;; |
199 |
-o*|--option*) |
200 |
option="$2"; shift 2;; |
201 |
-q|--quiet) |
202 |
quiet=true; shift 1;; |
203 |
*) |
204 |
printError "invalid option: $1\n"; shift 1;; |
205 |
esac |
206 |
else |
207 |
otherargs+=("$1") |
208 |
shift 1 |
209 |
fi |
210 |
done |
211 |
filepath=$(realpath ${otherargs[0]}) |
212 |
|
213 |
# if file already exists |
214 |
if [ -e $filepath ]; then |
215 |
# if no option was specified, ask what to do |
216 |
if [ -z "$option" ]; then |
217 |
printWarning "log file $filepath already esists\n" |
218 |
local userinput="" |
219 |
printf "Select what to do:\n" |
220 |
printf " [A] - append log\n" |
221 |
printf " [R] - restart log (delete existing file)\n" |
222 |
printf " [N] - no log\n" |
223 |
readUserInput "AaRrNn" userinput |
224 |
option=${userinput,,} |
225 |
fi |
226 |
# evaluate option |
227 |
case "$option" in |
228 |
a) |
229 |
if [ $quiet = false ]; then |
230 |
printInfo "appending log to $filepath\n" |
231 |
fi |
232 |
printf "\n" >> $filepath |
233 |
printf "######################################################################\n" >> $filepath |
234 |
printf "\n" >> $filepath |
235 |
;; |
236 |
r) |
237 |
echo -n "" > $filepath |
238 |
if [ $quiet = false ]; then |
239 |
printInfo "content of $filepath wiped\n" |
240 |
fi |
241 |
;; |
242 |
n) |
243 |
if [ $quiet = false ]; then |
244 |
printInfo "no log file will be generated\n" |
245 |
fi |
246 |
filepath="" |
247 |
;; |
248 |
*) # sanity check (return error) |
249 |
printError "unexpected argument: $option\n"; return -1;; |
250 |
esac |
251 |
else |
252 |
if [ $quiet = false ]; then |
253 |
printInfo "log file set to $filepath\n" |
254 |
fi |
255 |
fi |
256 |
|
257 |
eval ${otherargs[1]}="$filepath" |
258 |
|
259 |
return 0 |
260 |
} |
261 |
|
262 |
################################################################################ |
263 |
# SPECIFIC FUNCTIONS # |
264 |
################################################################################ |
265 |
|
266 |
### print welcome text ######################################################### |
267 |
# Prints a welcome message to standard out. |
268 |
# |
269 |
# usage: printWelcomeText |
270 |
# arguments: n/a |
271 |
# return: n/a |
272 |
# |
273 |
function printWelcomeText { |
274 |
printf "######################################################################\n" |
275 |
printf "# #\n" |
276 |
printf "# Welcome to the AMiRo-BLT setup! #\n" |
277 |
printf "# #\n" |
278 |
printf "######################################################################\n" |
279 |
printf "# #\n" |
280 |
printf "# Copyright (c) 2016..2017 Thomas Schöpping #\n" |
281 |
printf "# #\n" |
282 |
printf "# This is free software; see the source for copying conditions. #\n" |
283 |
printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR #\n" |
284 |
printf "# A PARTICULAR PURPOSE. The development of this software was #\n" |
285 |
printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction #\n" |
286 |
printf "# Technology. The Excellence Cluster EXC 227 is a grant of the #\n" |
287 |
printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n" |
288 |
printf "# Excellence Initiative. #\n" |
289 |
printf "# #\n" |
290 |
printf "######################################################################\n" |
291 |
} |
292 |
|
293 |
### print help ################################################################# |
294 |
# Prints a help text to standard out. |
295 |
# |
296 |
# usage: printHelp |
297 |
# arguments: n/a |
298 |
# return: n/a |
299 |
# |
300 |
function printHelp { |
301 |
printInfo "printing help text\n" |
302 |
printf "usage: $(basename ${BASH_SOURCE[0]}) [-h|--help] [-f|--stm32flash] [-s|--SerialBoot] [-e|--IDE] [-c|--compiler] [-q|--quit] [--log=<file>]\n" |
303 |
printf "\n" |
304 |
printf "options: -h, --help\n" |
305 |
printf " Print this help text.\n" |
306 |
printf " -f, --stm32flash\n" |
307 |
printf " Run st,32flash tool setup.\n" |
308 |
printf " -s, --SerialBoot\n" |
309 |
printf " Run SerialBoot tool setup.\n" |
310 |
printf " -e, --IDE\n" |
311 |
printf " Enter IDE setup.\n" |
312 |
printf " -c, --compiler\n" |
313 |
printf " Enter compiler setup.\n" |
314 |
printf " -q, --quit\n" |
315 |
printf " Quit the script.\n" |
316 |
printf " --log=<file>\n" |
317 |
printf " Specify a log file.\n" |
318 |
} |
319 |
|
320 |
### stm32flash tool setup ###################################################### |
321 |
# Fetches the source code for the stm32flash tool and builds the binary. |
322 |
# If the tool was already initialized, it can be wiped and rebuilt. |
323 |
# |
324 |
# usage: stm32flashSetup |
325 |
# arguments: n/a |
326 |
# return: 0 |
327 |
# No error or warning occurred. |
328 |
# 1 |
329 |
# Warning: Setup aborted by user. |
330 |
# -1 |
331 |
# Error: Unexpected user input. |
332 |
# |
333 |
function stm32flashSetup { |
334 |
local stm32flashdir=$(dirname $(realpath ${BASH_SOURCE[0]}))/Host/Source/stm32flash/ |
335 |
local userdir=$(pwd) |
336 |
|
337 |
# if the stm32flash folder is not empty |
338 |
if [ ! -z "$(ls -A $stm32flashdir)" ]; then |
339 |
printWarning "$stm32flashdir is not empty. Delete and reinitialize? [y/n]\n" |
340 |
local userinput="" |
341 |
readUserInput "YyNn" userinput |
342 |
case "$userinput" in |
343 |
Y|y) |
344 |
printInfo "wiping ${stm32flashdir}...\n" |
345 |
# checkout base commit and delete all local branches |
346 |
git submodule update --force --checkout $stm32flashdir | tee -a $LOG_FILE |
347 |
cd $stm32flashdir |
348 |
local git_branches=($(git for-each-ref --format="%(refname)")) |
349 |
for branch in $git_branches ; do |
350 |
if [[ $branch = *"heads/"* ]]; then |
351 |
git branch -D ${branch##*/} | tee -a $LOG_FILE |
352 |
fi |
353 |
done |
354 |
cd $userdir |
355 |
# deinit stm32flash submodule and delete any remaining files |
356 |
git submodule deinit -f $stm32flashdir 2>&1 | tee -a $LOG_FILE |
357 |
rm -rf $stm32flashdir/* |
358 |
;; |
359 |
N|n) |
360 |
printWarning "stm32flash setup aborted by user\n" |
361 |
return 1 |
362 |
;; |
363 |
*) # sanity check (return error) |
364 |
printError "unexpected input: $userinput\n"; |
365 |
return -1 |
366 |
;; |
367 |
esac |
368 |
fi |
369 |
|
370 |
# initialize submodule |
371 |
printInfo "initializing stm32flash submodule...\n" |
372 |
git submodule update --init $stm32flashdir 2>&1 | tee -a $LOG_FILE |
373 |
|
374 |
# build the stm32flash tool |
375 |
printInfo "compiling stm32flash\n" |
376 |
userdir=${PWD} |
377 |
cd "$stm32flashdir" |
378 |
make 2>&1 | tee -a $LOG_FILE |
379 |
cd "$userdir" |
380 |
|
381 |
return 0 |
382 |
} |
383 |
|
384 |
### SerialBoot tool setup ###################################################### |
385 |
# Builds the SerialBoot tool. |
386 |
# If the tool was built before, it can be deleted and rebuilt. |
387 |
# |
388 |
# usage: serialBootSetup |
389 |
# arguments: n/a |
390 |
# return: 0 |
391 |
# No errort or warning occurred. |
392 |
# 1 |
393 |
# Warning: Setup aborted by user. |
394 |
# -1 |
395 |
# Error: Unexpected user input. |
396 |
# |
397 |
function serialBootSetup { |
398 |
local serialbootdir=$(dirname $(realpath ${BASH_SOURCE[0]}))/Host/Source/SerialBoot/ |
399 |
local userdir=$(pwd) |
400 |
|
401 |
# if a build folder already exists |
402 |
if [ -d "${serialbootdir}/build/" ]; then |
403 |
printWarning "SerialBoot has been built before. Delete and rebuild? [y/n]\n" |
404 |
local userinput="" |
405 |
readUserInput "YyNn" userinput |
406 |
case "$userinput" in |
407 |
Y|y) |
408 |
printInfo "deleting ${serialbootdir}build/...\n" |
409 |
rm -rf "${serialbootdir}build/" |
410 |
;; |
411 |
N|n) |
412 |
printWarning "SerialBoot setup aborted by user\n" |
413 |
return 1 |
414 |
;; |
415 |
*) # sanity check (return error) |
416 |
printError "unexpected input: $userinput\n"; |
417 |
return -1 |
418 |
;; |
419 |
esac |
420 |
fi |
421 |
|
422 |
# build SerialBoot |
423 |
printInfo "compiling SerialBoot...\n" |
424 |
mkdir ${serialbootdir}build/ |
425 |
cd ${serialbootdir}build/ |
426 |
cmake .. 2>&1 | tee -a $LOG_FILE |
427 |
make 2>&1 | tee -a $LOG_FILE |
428 |
cd $userdir |
429 |
|
430 |
return 0 |
431 |
} |
432 |
|
433 |
### IDE setup ################################################################## |
434 |
# Enter the IDE setup. |
435 |
# |
436 |
# usage: ideSetup |
437 |
# arguments: n/a |
438 |
# return: n/a |
439 |
# |
440 |
function ideSetup { |
441 |
printInfo "entering IDE setup\n" |
442 |
printf "\n" |
443 |
if [ -z "$LOG_FILE" ]; then |
444 |
$(dirname $(realpath ${BASH_SOURCE[0]}))/ide/idesetup.sh --noinfo |
445 |
else |
446 |
$(dirname $(realpath ${BASH_SOURCE[0]}))/ide/idesetup.sh --LOG="$LOG_FILE" --noinfo |
447 |
fi |
448 |
} |
449 |
|
450 |
### compiler setup ############################################################# |
451 |
# Enter the compiler setup. |
452 |
# |
453 |
# usage: compilerSetup |
454 |
# arguments: n/a |
455 |
# return: n/a |
456 |
# |
457 |
function compilerSetup { |
458 |
printInfo "entering IDE setup\n" |
459 |
printf "\n" |
460 |
if [ -z "$LOG_FILE" ]; then |
461 |
$(dirname $(realpath ${BASH_SOURCE[0]}))/compiler/compilersetup.sh --noinfo |
462 |
else |
463 |
$(dirname $(realpath ${BASH_SOURCE[0]}))/compiler/compilersetup.sh --LOG="$LOG_FILE" --noinfo |
464 |
fi |
465 |
} |
466 |
|
467 |
### main function of this script ############################################### |
468 |
# A setup to initialize dependencies, setup IDE projects and configure the |
469 |
# compiler setup. |
470 |
# |
471 |
# usage: see function printHelp |
472 |
# arguments: see function printHelp |
473 |
# return: 0 |
474 |
# No error or warning occurred. |
475 |
# |
476 |
function main { |
477 |
# print welcome/info text if not suppressed |
478 |
if [[ $@ != *"--noinfo"* ]]; then |
479 |
printWelcomeText |
480 |
else |
481 |
printf "######################################################################\n" |
482 |
fi |
483 |
printf "\n" |
484 |
|
485 |
# set log file if specified |
486 |
if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then |
487 |
# get the parameter (file name) |
488 |
local cmdidx=1 |
489 |
while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do |
490 |
cmdidx=$[cmdidx + 1] |
491 |
done |
492 |
local cmd="${!cmdidx}" |
493 |
local logfile="" |
494 |
if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then |
495 |
logfile=${cmd#*=} |
496 |
else |
497 |
local filenameidx=$((cmdidx + 1)) |
498 |
logfile="${!filenameidx}" |
499 |
fi |
500 |
# optionally force silent appending |
501 |
if [[ "$cmd" = "--LOG"* ]]; then |
502 |
setLogFile --option=a --quiet "$logfile" LOG_FILE |
503 |
else |
504 |
setLogFile "$logfile" LOG_FILE |
505 |
printf "\n" |
506 |
fi |
507 |
fi |
508 |
|
509 |
# log script name |
510 |
printLog "this is $(realpath ${BASH_SOURCE[0]})\n" |
511 |
|
512 |
# if --help or -h was specified, print the help text and exit |
513 |
if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then |
514 |
printHelp |
515 |
printf "\n" |
516 |
quitScript |
517 |
fi |
518 |
|
519 |
# parse arguments |
520 |
local otherargs=() |
521 |
while [ $# -gt 0 ]; do |
522 |
if ( parseIsOption $1 ); then |
523 |
case "$1" in |
524 |
-h|--help) # already handled; ignore |
525 |
shift 1;; |
526 |
-f|--stm32flash) |
527 |
stm32flashSetup; printf "\n"; shift 1;; |
528 |
-s|--SerialBoot) |
529 |
serialBootSetup; printf "\n"; shift 1;; |
530 |
-e|--IDE) |
531 |
ideSetup; printf "\n"; shift 1;; |
532 |
-c|--compiler) |
533 |
compilerSetup; printf "\n"; shift 1;; |
534 |
-q|--quit) |
535 |
quitScript; shift 1;; |
536 |
--log=*|--LOG=*) # already handled; ignore |
537 |
shift 1;; |
538 |
--log|--LOG) # already handled; ignore |
539 |
shift 2;; |
540 |
--noinfo) # already handled; ignore |
541 |
shift 1;; |
542 |
*) |
543 |
printError "invalid option: $1\n"; shift 1;; |
544 |
esac |
545 |
else |
546 |
otherargs+=("$1") |
547 |
shift 1 |
548 |
fi |
549 |
done |
550 |
|
551 |
# interactive menu |
552 |
while ( true ); do |
553 |
# main menu info prompt and selection |
554 |
printInfo "AMiRo-BLT setup main menu\n" |
555 |
printf "Please select one of the following actions:\n" |
556 |
printf " [F] - get and build stm32flash tool\n" |
557 |
printf " [S] - build SerialBoot tool\n" |
558 |
printf " [E] - IDE project setup\n" |
559 |
printf " [C] - enter compiler setup\n" |
560 |
printf " [Q] - quit this setup\n" |
561 |
local userinput="" |
562 |
readUserInput "FfSsEeCcQq" userinput |
563 |
printf "\n" |
564 |
|
565 |
# evaluate user selection |
566 |
case "$userinput" in |
567 |
F|f) |
568 |
stm32flashSetup; printf "\n";; |
569 |
S|s) |
570 |
serialBootSetup; printf "\n";; |
571 |
E|e) |
572 |
ideSetup; printf "\n";; |
573 |
C|c) |
574 |
compilerSetup; printf "\n";; |
575 |
Q|q) |
576 |
quitScript;; |
577 |
*) # sanity check (exit with error) |
578 |
printError "unexpected argument: $userinput\n";; |
579 |
esac |
580 |
done |
581 |
|
582 |
exit 0 |
583 |
} |
584 |
|
585 |
################################################################################ |
586 |
# SCRIPT ENTRY POINT # |
587 |
################################################################################ |
588 |
|
589 |
main "$@" |
590 |
|