amiro-blt / tools / ide / QtCreator / QtCreatorSetup.sh @ 4745cc4c
History | View | Annotate | Download (34.381 KB)
1 |
################################################################################ |
---|---|
2 |
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini # |
3 |
# Robot (AMiRo) platform. # |
4 |
# Copyright (C) 2016..2019 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 |
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
110 |
printf "\n" |
111 |
printf "######################################################################\n" |
112 |
exit 0 |
113 |
} |
114 |
|
115 |
### read a user input ########################################################## |
116 |
# Reads a single character user input from a set up <options> and stores it in |
117 |
# a given <return> variable. |
118 |
# |
119 |
# usage: readUserInput <options> <return> |
120 |
# arguments: <options> |
121 |
# String definiing the set of valid characters. |
122 |
# If the string is empty, the user can input any character. |
123 |
# <return> |
124 |
# Variable to store the selected character to. |
125 |
# return: n/a |
126 |
# |
127 |
function readUserInput { |
128 |
local input="" |
129 |
# read user input |
130 |
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
131 |
read -p "your selection: " -n 1 -e input |
132 |
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
133 |
printWarning "[$input] is no valid action\n" |
134 |
fi |
135 |
done |
136 |
printLog "[$input] has been selected\n" |
137 |
eval $2="$input" |
138 |
} |
139 |
|
140 |
### check whether argument is an option ######################################## |
141 |
# Checks a <string> whether it is an option. |
142 |
# Options are defined to either start with '--' followed by any string, or |
143 |
# to start with a single '-' followed by a single character, or |
144 |
# to start with a single '-' followed by a single character, a '=' and any string. |
145 |
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
146 |
# |
147 |
# usage: parseIsOption <string> |
148 |
# arguments: <string> |
149 |
# A string to check whether it is an option. |
150 |
# return: 0 |
151 |
# <string> is an option. |
152 |
# -1 |
153 |
# <string> is not an option. |
154 |
# |
155 |
function parseIsOption { |
156 |
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
157 |
return 0 |
158 |
else |
159 |
return -1 |
160 |
fi |
161 |
} |
162 |
|
163 |
### set the log file ########################################################### |
164 |
# Sets a specified <infile> as log file and checks whether it already exists. |
165 |
# If so, the log may either be appended to the file, its content can be cleared, |
166 |
# or no log is generated at all. |
167 |
# The resulting path is stored in <outvar>. |
168 |
# |
169 |
# usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
170 |
# arguments: --option=<option> |
171 |
# Select what to do if <file> already exists. |
172 |
# Possible values are 'a', 'c', 'r' and 'n'. |
173 |
# - a: append (starts with a separator) |
174 |
# - c: continue (does not insert a seperator) |
175 |
# - r: delete and restart |
176 |
# - n: no log |
177 |
# If no option is secified but <file> exists, an interactive selection is provided. |
178 |
# --quiet |
179 |
# Suppress all messages. |
180 |
# <infile> |
181 |
# Path of the wanted log file. |
182 |
# <outvar> |
183 |
# Variable to store the path of the log file to. |
184 |
# return: 0 |
185 |
# No error or warning occurred. |
186 |
# -1 |
187 |
# Error: invalid input |
188 |
# |
189 |
function setLogFile { |
190 |
local filepath="" |
191 |
local option="" |
192 |
local quiet=false |
193 |
|
194 |
# parse arguments |
195 |
local otherargs=() |
196 |
while [ $# -gt 0 ]; do |
197 |
if ( parseIsOption $1 ); then |
198 |
case "$1" in |
199 |
-o=*|--option=*) |
200 |
option=${1#*=}; shift 1;; |
201 |
-o*|--option*) |
202 |
option="$2"; shift 2;; |
203 |
-q|--quiet) |
204 |
quiet=true; shift 1;; |
205 |
*) |
206 |
printError "invalid option: $1\n"; shift 1;; |
207 |
esac |
208 |
else |
209 |
otherargs+=("$1") |
210 |
shift 1 |
211 |
fi |
212 |
done |
213 |
filepath=$(realpath ${otherargs[0]}) |
214 |
|
215 |
# if file already exists |
216 |
if [ -e $filepath ]; then |
217 |
# if no option was specified, ask what to do |
218 |
if [ -z "$option" ]; then |
219 |
printWarning "log file $filepath already esists\n" |
220 |
local userinput="" |
221 |
printf "Select what to do:\n" |
222 |
printf " [A] - append log\n" |
223 |
printf " [R] - restart log (delete existing file)\n" |
224 |
printf " [N] - no log\n" |
225 |
readUserInput "AaRrNn" userinput |
226 |
option=${userinput,,} |
227 |
fi |
228 |
# evaluate option |
229 |
case "$option" in |
230 |
a|c) |
231 |
if [ $quiet = false ]; then |
232 |
printInfo "appending log to $filepath\n" |
233 |
fi |
234 |
if [ $option != c ]; then |
235 |
printf "\n" >> $filepath |
236 |
printf "######################################################################\n" >> $filepath |
237 |
printf "\n" >> $filepath |
238 |
fi |
239 |
;; |
240 |
r) |
241 |
echo -n "" > $filepath |
242 |
if [ $quiet = false ]; then |
243 |
printInfo "content of $filepath wiped\n" |
244 |
fi |
245 |
;; |
246 |
n) |
247 |
if [ $quiet = false ]; then |
248 |
printInfo "no log file will be generated\n" |
249 |
fi |
250 |
filepath="" |
251 |
;; |
252 |
*) # sanity check (return error) |
253 |
printError "unexpected argument: $option\n"; return -1;; |
254 |
esac |
255 |
else |
256 |
if [ $quiet = false ]; then |
257 |
printInfo "log file set to $filepath\n" |
258 |
fi |
259 |
fi |
260 |
|
261 |
eval ${otherargs[1]}="$filepath" |
262 |
|
263 |
return 0 |
264 |
} |
265 |
|
266 |
### check whether commands are available ####################################### |
267 |
# Checks whether the specified commands are available and can be executed. |
268 |
# |
269 |
# usage: checkCommands [<command> <command> ...] |
270 |
# arguments: <command> |
271 |
# Name of the command to check. |
272 |
# return: 0 |
273 |
# All requested commands are available. |
274 |
# >0 |
275 |
# Number of requested commands that were not found. |
276 |
# -1 |
277 |
# No argument given. |
278 |
# |
279 |
function checkCommands { |
280 |
local status=0 |
281 |
|
282 |
# return if no argument was specified |
283 |
if [ $# -eq 0 ]; then |
284 |
return -1 |
285 |
fi |
286 |
|
287 |
# check all specified commands |
288 |
while [ $# -gt 0 ]; do |
289 |
command -v $1 &>/dev/null |
290 |
if [ $? -ne 0 ]; then |
291 |
printWarning "Command '$1' not available.\n" |
292 |
status=$((status + 1)) |
293 |
fi |
294 |
shift 1 |
295 |
done |
296 |
|
297 |
return $status |
298 |
} |
299 |
|
300 |
################################################################################ |
301 |
# SPECIFIC FUNCTIONS # |
302 |
################################################################################ |
303 |
|
304 |
### print welcome text ######################################################### |
305 |
# Prints a welcome message to standard out. |
306 |
# |
307 |
# usage: printWelcomeText |
308 |
# arguments: n/a |
309 |
# return: n/a |
310 |
# |
311 |
function printWelcomeText { |
312 |
printf "######################################################################\n" |
313 |
printf "# #\n" |
314 |
printf "# Welcome to the QtCreator setup! #\n" |
315 |
printf "# #\n" |
316 |
printf "######################################################################\n" |
317 |
printf "# #\n" |
318 |
printf "# Copyright (c) 2016..2019 Thomas Schöpping #\n" |
319 |
printf "# #\n" |
320 |
printf "# This is free software; see the source for copying conditions. #\n" |
321 |
printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR #\n" |
322 |
printf "# A PARTICULAR PURPOSE. The development of this software was #\n" |
323 |
printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction #\n" |
324 |
printf "# Technology. The Excellence Cluster EXC 227 is a grant of the #\n" |
325 |
printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n" |
326 |
printf "# Excellence Initiative. #\n" |
327 |
printf "# #\n" |
328 |
printf "######################################################################\n" |
329 |
} |
330 |
|
331 |
### print help ################################################################# |
332 |
# Prints a help text to standard out. |
333 |
# |
334 |
# usage: printHelp |
335 |
# arguments: n/a |
336 |
# return: n/a |
337 |
# |
338 |
function printHelp { |
339 |
printInfo "printing help text\n" |
340 |
printf "usage: $(basename ${BASH_SOURCE[0]}) [-h|--help] [--module=<module>] [-a|--all] [-c|--clean] [-w|--wipe] [-q|--quit] [--log=<file>]\n" |
341 |
printf "\n" |
342 |
printf "options: -h, --help\n" |
343 |
printf " Print this help text.\n" |
344 |
printf " --module=<module>\n" |
345 |
printf " Create project for a single module.\n" |
346 |
printf " -a, --all\n" |
347 |
printf " Create projects for all modules.\n" |
348 |
printf " -c, --clean\n" |
349 |
printf " Delete project files.\n" |
350 |
printf " -w, --wipe\n" |
351 |
printf " Delete project and .user files.\n" |
352 |
printf " -q, --quit\n" |
353 |
printf " Quit the script.\n" |
354 |
printf " --log=<file>\n" |
355 |
printf " Specify a log file.\n" |
356 |
} |
357 |
|
358 |
### read directory where to create/delete projects ############################# |
359 |
# Read the directory where to create/delete project files from user. |
360 |
# |
361 |
# usage: getProjectDir <pathvar> |
362 |
# arguments: <pathvar> |
363 |
# Variable to store the selected path to. |
364 |
# return: n/a |
365 |
# |
366 |
function getProjectDir { |
367 |
printLog "reading path for project files from user...\n" |
368 |
local amirobltdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../Target/) |
369 |
local input="" |
370 |
read -p "Path where to create/delete project files: " -i $amirobltdir -e input |
371 |
printLog "user selected path $(realpath $input)\n" |
372 |
eval $1="$(realpath $input)" |
373 |
} |
374 |
|
375 |
### retrieves the ARM-NONE-EABI-GCC include directory ########################## |
376 |
# Retrieves the include directory of the currently set arm-none-eabi-gcc. |
377 |
# |
378 |
# usage: retrieveGccIncludeDir <path> |
379 |
# arguments: <path> |
380 |
# Variable to store the path to. |
381 |
# return: 0 |
382 |
# No error or warning occurred. |
383 |
# -1 |
384 |
# Error: Command 'arm-none-eabi-gcc' not found. |
385 |
# -2 |
386 |
# Error: include directory could not be resolved |
387 |
# |
388 |
function retrieveGccIncludeDir { |
389 |
# retrieve binary path or link |
390 |
local binpath=$(which arm-none-eabi-gcc) |
391 |
local gccincpath="" |
392 |
if [ -z "$binpath" ]; then |
393 |
printError "command 'arm-none-eabi-gcc' not found\n" |
394 |
return -1 |
395 |
else |
396 |
|
397 |
# traverse any links |
398 |
while [ -L "$binpath" ]; do |
399 |
binpath=$(realpath $(dirname $binpath)/$(readlink $binpath)) |
400 |
done |
401 |
printInfo "gcc-arm-none-eabi detected: $binpath\n" |
402 |
|
403 |
# return include path |
404 |
gccincpath=$(realpath $(dirname ${binpath})/../arm-none-eabi/include/) |
405 |
if [ ! -d "$gccincpath" ]; then |
406 |
printWarning "$gccincpath does not exist\n" |
407 |
return -2 |
408 |
else |
409 |
eval $1="$gccincpath" |
410 |
return 0 |
411 |
fi |
412 |
fi |
413 |
} |
414 |
|
415 |
### detect available modules ################################################### |
416 |
# Detect all avalable modules supported by AMiRo-OS. |
417 |
# |
418 |
# usage: detectModules <modulearray> |
419 |
# arguments: <modulearray> |
420 |
# Array variable to store all detected modules to. |
421 |
# return: n/a |
422 |
# |
423 |
function detectModules { |
424 |
local modulesdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../Target/Modules) |
425 |
local modules_detected=() |
426 |
|
427 |
# detect all available modules (via directories) |
428 |
for dir in $(ls -d ${modulesdir}/*/); do |
429 |
modules_detected[${#modules_detected[@]}]=$(basename $dir) |
430 |
done |
431 |
|
432 |
# set the output variable |
433 |
eval "$1=(${modules_detected[*]})" |
434 |
} |
435 |
|
436 |
### create project files for a single module ################################### |
437 |
# Create project files for a single module. |
438 |
# |
439 |
# usage: createModuleProject <modules> [-m|--module=<module>] [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>] |
440 |
# arguments: <modules> |
441 |
# Array containing all modules available. |
442 |
# -m, --module <module> |
443 |
# Name (folder name) of the module for which project files shall be generated. |
444 |
# -p, --path <path> |
445 |
# Path where to create the project files. |
446 |
# --gcc=<path> |
447 |
# Path to the GCC include directory. |
448 |
# -o, --out <var> |
449 |
# Variable to store the path to. |
450 |
# --gccout=<var> |
451 |
# Variable to store the path to the GCC include directory to. |
452 |
# If this optional arguments is absent, ths function will ask for user input. |
453 |
# return: 0 |
454 |
# No error or warning occurred. |
455 |
# 1 |
456 |
# Aborted by user. |
457 |
# -1 |
458 |
# No modules available. |
459 |
# -2 |
460 |
# The specified <module> could not be found. |
461 |
# -3 |
462 |
# Parsing the project for the specified module failed. |
463 |
# -4 |
464 |
# Missing dependencies. |
465 |
# |
466 |
function createModuleProject { |
467 |
local userdir=$(pwd) |
468 |
local modulesdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../Target/Modules) |
469 |
local modules=("${!1}") |
470 |
local module="" |
471 |
local moduleidx="" |
472 |
local projectdir="" |
473 |
local gccincludedir="" |
474 |
local outvar="" |
475 |
local gccoutvar="" |
476 |
|
477 |
# check dependencies |
478 |
checkCommands make |
479 |
if [ $? -ne 0 ]; then |
480 |
printError "Missing dependencies detected.\n" |
481 |
return -4 |
482 |
fi |
483 |
|
484 |
# parse arguments |
485 |
local otherargs=() |
486 |
while [ $# -gt 0 ]; do |
487 |
if ( parseIsOption $1 ); then |
488 |
case "$1" in |
489 |
-m=*|--module=*) |
490 |
module="${1#*=}"; shift 1;; |
491 |
-m|--module) |
492 |
module="$2"; shift 2;; |
493 |
-p=*|--path=*) |
494 |
projectdir=$(realpath "${1#*=}"); shift 1;; |
495 |
-p|--path) |
496 |
projectdir=$(realpath "$2"); shift 2;; |
497 |
--gcc=*) |
498 |
gccincludedir=$(realpath "${1#*=}"); shift 1;; |
499 |
--gcc) |
500 |
gccincludedir=$(realpath "$2"); shift 2;; |
501 |
-o=*|--out=*) |
502 |
outvar=${1#*=}; shift 1;; |
503 |
-o|--out) |
504 |
outvar=$2; shift 2;; |
505 |
--gccout=*) |
506 |
gccoutvar=$(realpath "${1#*=}"); shift 1;; |
507 |
--gccout) |
508 |
gccoutvar=$(realpath "$2"); shift 2;; |
509 |
*) |
510 |
printError "invalid option: $1\n"; shift 1;; |
511 |
esac |
512 |
else |
513 |
otherargs+=("$1") |
514 |
shift 1 |
515 |
fi |
516 |
done |
517 |
|
518 |
# sanity check for the modules variable |
519 |
if [ -z "${modules[*]}" ]; then |
520 |
printError "no modules available\n" |
521 |
return -1 |
522 |
fi |
523 |
|
524 |
# select module |
525 |
if [ -z $module ]; then |
526 |
# list all available modules |
527 |
printInfo "choose a module or type 'A' to abort:\n" |
528 |
for (( idx=0; idx<${#modules[@]}; ++idx )); do |
529 |
printf "%4u: %s\n" $(($idx + 1)) "${modules[$idx]}" |
530 |
done |
531 |
# read user input |
532 |
printLog "read user selection\n" |
533 |
local userinput="" |
534 |
while [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#modules[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; do |
535 |
read -p "your selection: " -e userinput |
536 |
printLog "user selection: $userinput\n" |
537 |
if [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#modules[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; then |
538 |
printWarning "Please enter an integer between 1 and ${#modules[@]} or 'A' to abort.\n" |
539 |
fi |
540 |
done |
541 |
if [[ "$userinput" =~ ^[Aa]$ ]]; then |
542 |
printWarning "aborted by user\n" |
543 |
return 1 |
544 |
fi |
545 |
# store selection |
546 |
moduleidx=$(($userinput - 1)) |
547 |
module="${modules[$moduleidx]}" |
548 |
printf "\n" |
549 |
else |
550 |
# search all modules for the selected one |
551 |
for (( idx=0; idx<${#modules[@]}; ++idx )); do |
552 |
if [ "${modules[$idx]}" = "$module" ]; then |
553 |
moduleidx=$idx |
554 |
break |
555 |
fi |
556 |
done |
557 |
# error if the module could not be found |
558 |
if [ -z $moduleidx ]; then |
559 |
printError "module ($module) not available\n" |
560 |
return -2 |
561 |
fi |
562 |
fi |
563 |
|
564 |
# read absolute project directory if required |
565 |
if [ -z "$projectdir" ]; then |
566 |
getProjectDir projectdir |
567 |
printf "\n" |
568 |
fi |
569 |
|
570 |
# check for existing project files |
571 |
local projectfiles="$(find ${projectdir} -maxdepth 1 -type f | grep -E "${module}\.(includes|files|config|creator)$")" |
572 |
IFS=$'\n'; projectfiles=($projectfiles); unset IFS |
573 |
if [ ! -z "${projectfiles[*]}" ]; then |
574 |
printWarning "The following files will be overwritten:\n" |
575 |
for pfile in ${projectfiles[@]}; do |
576 |
printWarning "\t$(basename $pfile)\n" |
577 |
done |
578 |
local userinput="" |
579 |
printInfo "Continue and overwrite? [y/n]\n" |
580 |
readUserInput "YyNn" userinput |
581 |
case "$userinput" in |
582 |
Y|y) |
583 |
;; |
584 |
N|n) |
585 |
printWarning "Project generation for ${module} module aborted by user\n" |
586 |
return 1 |
587 |
;; |
588 |
*) |
589 |
printError "unexpected input: ${userinput}\n"; return -999;; |
590 |
esac |
591 |
printf "\n" |
592 |
fi |
593 |
|
594 |
# print message |
595 |
printInfo "generating QtCreator project files for the $module module...\n" |
596 |
|
597 |
# retrieve absolute GCC include path |
598 |
if [ -z "$gccincludedir" ]; then |
599 |
retrieveGccIncludeDir gccincludedir |
600 |
fi |
601 |
|
602 |
# change to project directory |
603 |
local amirobltrootdir=$(realpath $(dirname ${BASH_SOURCE[0]})/../../..) |
604 |
cd "$projectdir" |
605 |
|
606 |
# run make, but only run the GCC preprocessor and produce no binaries |
607 |
local sourcefiles=() |
608 |
local sourcefile="" |
609 |
local parse_state="WAIT_FOR_INCLUDE_OR_COMPILE" |
610 |
# capture all output from make and GCC and append the return value of make as last line |
611 |
printInfo "processing project (this may take a while)...\n" |
612 |
local rawout=$(make --directory ${amirobltrootdir}/Target/Modules/${module}/Boot --always-make USER_CFLAGS="-v -H" 2>&1 && echo $?) |
613 |
# check whether the make call was successfull |
614 |
if [[ $(echo "${rawout}" | tail -n 1) != "0" ]]; then |
615 |
printError "executing 'make' in module directory failed\n" |
616 |
cd "$userdir" |
617 |
return -3 |
618 |
fi |
619 |
# extract file names from raw output |
620 |
IFS=$'\n'; rawout=($rawout); unset IFS |
621 |
for line in "${rawout[@]}"; do |
622 |
case $parse_state in |
623 |
WAIT_FOR_INCLUDE_OR_COMPILE) |
624 |
# lines stating included files look like: |
625 |
# ... <../relative/path/to/file> |
626 |
if [[ "$line" =~ ^\.+[[:blank:]].+\..+$ ]]; then |
627 |
sourcefile=${line##* } |
628 |
if [[ ! "$sourcefile" =~ ^/ ]]; then |
629 |
sourcefile=$(realpath ${amirobltrootdir}/Target/Modules/${module}/Boot/${sourcefile}) |
630 |
fi |
631 |
sourcefiles[${#sourcefiles[@]}]="$sourcefile" |
632 |
# whenever the next source file is processed, a message appears like: |
633 |
# Compining <filnemame> |
634 |
elif [[ "$line" =~ ^\+\+\+[[:blank:]]Compiling[[:blank:]]\[(.+\.c)\]$ ]]; then |
635 |
printf "." |
636 |
sourcefile=${BASH_REMATCH[1]} |
637 |
parse_state="WAIT_FOR_COMPILERCALL" |
638 |
fi;; |
639 |
WAIT_FOR_COMPILERCALL) |
640 |
# wait for the actual call of the compiler to retrieve the full path to the source file |
641 |
if [[ "$line" == *${sourcefile}* ]]; then |
642 |
line="${line%%${sourcefile}*}${sourcefile}" |
643 |
sourcefile=$(realpath ${amirobltrootdir}/Target/Modules/${module}/Boot/${line##* }) |
644 |
sourcefiles[${#sourcefiles[@]}]="$sourcefile" |
645 |
parse_state="WAIT_FOR_INCLUDE_OR_COMPILE" |
646 |
fi;; |
647 |
esac |
648 |
done |
649 |
unset rawout |
650 |
printf "\n" |
651 |
# sort and remove duplicates |
652 |
IFS=$'\n'; sourcefiles=($(sort --unique <<< "${sourcefiles[*]}")); unset IFS |
653 |
|
654 |
# extract include paths |
655 |
local includes=() |
656 |
for source in ${sourcefiles[*]}; do |
657 |
includes[${#includes[@]}]="$(dirname ${source})" |
658 |
done |
659 |
# sort and remove duplicates |
660 |
IFS=$'\n'; includes=($(sort --unique <<< "${includes[*]}")); unset IFS |
661 |
|
662 |
# generate the .files file, containing all source files |
663 |
echo "" > ${projectdir}/${module}.includes |
664 |
for inc in ${includes[*]}; do |
665 |
echo "$inc" >> ${projectdir}/${module}.includes |
666 |
done |
667 |
# generate the .incldues file, containing all include paths |
668 |
echo "" > ${projectdir}/${module}.files |
669 |
for source in ${sourcefiles[*]}; do |
670 |
# skip GCC files |
671 |
if [[ ! "$source" =~ .*/gcc.* ]]; then |
672 |
echo "$source" >> ${projectdir}/${module}.files |
673 |
fi |
674 |
done |
675 |
# generate a default project configuration file if it doesn't exist yet |
676 |
if [ ! -f ${projectdir}/${module}.config ]; then |
677 |
echo "// Add predefined macros for your project here. For example:" > ${projectdir}/${module}.config |
678 |
echo "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/${module}.config |
679 |
fi |
680 |
# generate a default .creator file if it doesn't exist yet |
681 |
if [ ! -f ${projectdir}/${module}.creator ]; then |
682 |
echo "[general]" > ${projectdir}/${module}.creator |
683 |
fi |
684 |
|
685 |
# go back to user directory |
686 |
cd $userdir |
687 |
|
688 |
# fill the output variables |
689 |
if [ ! -z "$outvar" ]; then |
690 |
eval $outvar="$projectdir" |
691 |
fi |
692 |
if [ ! -z "$gccoutvar" ]; then |
693 |
eval $gccoutvar="$gccincludedir" |
694 |
fi |
695 |
|
696 |
return 0 |
697 |
} |
698 |
|
699 |
### create project files for all modules ####################################### |
700 |
# Create project files for all modules. |
701 |
# |
702 |
# usage: createAllProjects <modules> [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>] |
703 |
# arguments: <modules> |
704 |
# Array containing all modules available. |
705 |
# -p, --path <path> |
706 |
# Path where to create the project files. |
707 |
# --gcc=<path> |
708 |
# Path to the GCC include directory. |
709 |
# -o, --out <var> |
710 |
# Variable to store the path to. |
711 |
# --gccout=<var> |
712 |
# Variable to store the path to the GCC include directory to. |
713 |
# If this optional arguments is absent, ths function will ask for user input. |
714 |
# return: 0 |
715 |
# No error or warning occurred. |
716 |
# 1 |
717 |
# Aborted by user. |
718 |
# -1 |
719 |
# No modules available. |
720 |
# |
721 |
function createAllProjects { |
722 |
local modules=("${!1}") |
723 |
local projectdir="" |
724 |
local gccincludedir="" |
725 |
local outvar="" |
726 |
local gccoutvar="" |
727 |
|
728 |
# parse arguments |
729 |
local otherargs=() |
730 |
while [ $# -gt 0 ]; do |
731 |
if ( parseIsOption $1 ); then |
732 |
case "$1" in |
733 |
-p=*|--path=*) |
734 |
projectdir=$(realpath "${1#*=}"); shift 1;; |
735 |
-p|--path) |
736 |
projectdir=$(realpath "$2"); shift 2;; |
737 |
--gcc=*) |
738 |
gccincludedir=$(realpath "${1#*=}"); shift 1;; |
739 |
--gcc) |
740 |
gccincludedir=$(realpath "$2"); shift 2;; |
741 |
-o=*|--out=*) |
742 |
outvar=${1#*=}; shift 1;; |
743 |
-o|--out) |
744 |
outvar=$2; shift 2;; |
745 |
--gccout=*) |
746 |
gccoutvar=$(realpath "${1#*=}"); shift 1;; |
747 |
--gccout) |
748 |
gccoutvar=$(realpath "$2"); shift 2;; |
749 |
*) |
750 |
printError "invalid option: $1\n"; shift 1;; |
751 |
esac |
752 |
else |
753 |
otherargs+=("$1") |
754 |
shift 1 |
755 |
fi |
756 |
done |
757 |
|
758 |
# sanity check for the modules variable |
759 |
if [ -z "${modules[*]}" ]; then |
760 |
printError "no modules available\n" |
761 |
return -1 |
762 |
fi |
763 |
|
764 |
# read absolute project directory if required |
765 |
if [ -z "$projectdir" ]; then |
766 |
getProjectDir projectdir |
767 |
fi |
768 |
|
769 |
# check for existing project files |
770 |
local projectfiles=() |
771 |
for module in ${modules[@]}; do |
772 |
local pfiles="$(find ${projectdir} -maxdepth 1 -type f | grep -E "${module}\.(includes|files|config|creator)$")" |
773 |
IFS=$'\n'; pfiles=($pfiles); unset IFS |
774 |
projectfiles=( ${projectfiles[*]} ${pfiles[*]} ) |
775 |
done |
776 |
if [ ! -z "${projectfiles[*]}" ]; then |
777 |
printWarning "The following files will be removed:\n" |
778 |
for pfile in ${projectfiles[@]}; do |
779 |
printWarning "\t$(basename $pfile)\n" |
780 |
done |
781 |
local userinput="" |
782 |
printInfo "Continue and overwrite? [y/n]\n" |
783 |
readUserInput "YyNn" userinput |
784 |
case "${userinput}" in |
785 |
Y|y) |
786 |
for pfile in ${projectfiles[*]}; do |
787 |
rm "$pfile" |
788 |
done |
789 |
;; |
790 |
N|n) |
791 |
printWarning "Project generation aborted by user\n" |
792 |
return 1 |
793 |
;; |
794 |
*) |
795 |
printError "unexpected input: ${userinput}\n" |
796 |
return 999 |
797 |
;; |
798 |
esac |
799 |
fi |
800 |
|
801 |
# print message |
802 |
printf "\n" |
803 |
printInfo "generating QtCreator project files for all modules...\n" |
804 |
|
805 |
# retrieve absolute GCC include path |
806 |
if [ -z "$gccincludedir" ]; then |
807 |
retrieveGccIncludeDir gccincludedir |
808 |
fi |
809 |
|
810 |
# iterate over all modules |
811 |
local retval=1 |
812 |
for module in ${modules[@]}; do |
813 |
if [ $retval != 0 ]; then |
814 |
printf "\n" |
815 |
fi |
816 |
createModuleProject modules[@] --module="$module" --path="$projectdir" --gcc="$gccincludedir" |
817 |
retval=$? |
818 |
done |
819 |
|
820 |
return 0 |
821 |
} |
822 |
|
823 |
### delete project files ####################################################### |
824 |
# Deletes all project files and optionally .user files, too. |
825 |
# |
826 |
# usage: deleteProjects [-p|--path=<path>] [-m|--module=<module>] [-o|--out=<var>] [-w|-wipe] |
827 |
# arguments: -p, --path <path> |
828 |
# Path where to delete the project files. |
829 |
# -m, --module <module> |
830 |
# Module name for which the project files shall be deleted. |
831 |
# -o, --out <var> |
832 |
# Variable to store the path to. |
833 |
# -w, --wipe |
834 |
# Delete .user files as well. |
835 |
# return: |
836 |
# - 0: no error |
837 |
# |
838 |
function deleteProjects { |
839 |
local modulename="" |
840 |
local projectdir="" |
841 |
local outvar="" |
842 |
local wipe=false |
843 |
local files="" |
844 |
|
845 |
# parse arguments |
846 |
local otherargs=() |
847 |
while [ $# -gt 0 ]; do |
848 |
if ( parseIsOption $1 ); then |
849 |
case "$1" in |
850 |
-p=*|--path=*) |
851 |
projectdir=$(realpath "${1#*=}"); shift 1;; |
852 |
-p|--path) |
853 |
projectdir=$(realpath "$2"); shift 2;; |
854 |
-m=*|--module=*) |
855 |
modulename="${1#*=}"; shift 1;; |
856 |
-m|--module) |
857 |
modulename="${2}"; shift 2;; |
858 |
-o=*|--out=*) |
859 |
outvar=${1#*=}; shift 1;; |
860 |
-o|--out) |
861 |
outvar=$2; shift 2;; |
862 |
-w|--wipe) |
863 |
wipe=true; shift 1;; |
864 |
*) |
865 |
printError "invalid option: $1\n"; shift 1;; |
866 |
esac |
867 |
else |
868 |
otherargs+=("$1") |
869 |
shift 1 |
870 |
fi |
871 |
done |
872 |
|
873 |
# read absolute project directory if required |
874 |
if [ -z "$projectdir" ]; then |
875 |
getProjectDir projectdir |
876 |
fi |
877 |
|
878 |
# list all files to be deleted |
879 |
if [ -z "$modulename" ]; then |
880 |
if [ $wipe != true ]; then |
881 |
files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator|cflags|cxxflags)$") |
882 |
else |
883 |
files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator|cflags|cxxflags|creator(\.user(\..+)?)?)$") |
884 |
fi |
885 |
else |
886 |
if [ $wipe != true ]; then |
887 |
files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^${modulename}\.(includes|files|config|creator|cflags|cxxflags)$") |
888 |
else |
889 |
files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^${modulename}\.(includes|files|config|creator|cflags|cxxflags|creator(\.user(\..+)?)?)$") |
890 |
fi |
891 |
fi |
892 |
if [ ! -z "$files" ]; then |
893 |
printInfo "Deleting the following files:\n" |
894 |
while read line; do |
895 |
printInfo "\t$(basename ${line})\n" |
896 |
rm ${line} 2>&1 | tee -a $LOG_FILE |
897 |
done <<< "${files}" |
898 |
else |
899 |
printInfo "No project files found\n" |
900 |
fi |
901 |
|
902 |
# store the path to the output variable, if required |
903 |
if [ ! -z "$outvar" ]; then |
904 |
eval $outvar="$projectdir" |
905 |
fi |
906 |
|
907 |
return 0 |
908 |
} |
909 |
|
910 |
### main function of this script ############################################### |
911 |
# Creates, deletes and wipes QtCreator project files for the three AMiRo base modules. |
912 |
# |
913 |
# usage: see function printHelp |
914 |
# arguments: see function printHelp |
915 |
# return: 0 |
916 |
# No error or warning ocurred. |
917 |
# |
918 |
function main { |
919 |
# print welcome/info text if not suppressed |
920 |
if [[ $@ != *"--noinfo"* ]]; then |
921 |
printWelcomeText |
922 |
else |
923 |
printf "######################################################################\n" |
924 |
fi |
925 |
printf "\n" |
926 |
|
927 |
# if --help or -h was specified, print the help text and exit |
928 |
if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then |
929 |
printHelp |
930 |
printf "\n" |
931 |
quitScript |
932 |
fi |
933 |
|
934 |
# set log file if specified |
935 |
if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then |
936 |
# get the parameter (file name) |
937 |
local cmdidx=1 |
938 |
while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do |
939 |
cmdidx=$[cmdidx + 1] |
940 |
done |
941 |
local cmd="${!cmdidx}" |
942 |
local logfile="" |
943 |
if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then |
944 |
logfile=${cmd#*=} |
945 |
else |
946 |
local filenameidx=$((cmdidx + 1)) |
947 |
logfile="${!filenameidx}" |
948 |
fi |
949 |
# optionally force silent appending |
950 |
if [[ "$cmd" = "--LOG"* ]]; then |
951 |
setLogFile --option=c --quiet "$logfile" LOG_FILE |
952 |
else |
953 |
setLogFile "$logfile" LOG_FILE |
954 |
printf "\n" |
955 |
fi |
956 |
fi |
957 |
# log script name |
958 |
printLog "this is $(realpath ${BASH_SOURCE[0]})\n" |
959 |
|
960 |
# detect available modules and inform user |
961 |
local modules=() |
962 |
detectModules modules |
963 |
case "${#modules[@]}" in |
964 |
0) |
965 |
printInfo "no module has been detected\n";; |
966 |
1) |
967 |
printInfo "1 module has been detected:\n";; |
968 |
*) |
969 |
printInfo "${#modules[@]} modules have been detected:\n" |
970 |
esac |
971 |
for (( idx=0; idx<${#modules[@]}; ++idx )); do |
972 |
printInfo " - ${modules[$idx]}\n" |
973 |
done |
974 |
printf "\n" |
975 |
|
976 |
# parse arguments |
977 |
local otherargs=() |
978 |
while [ $# -gt 0 ]; do |
979 |
if ( parseIsOption $1 ); then |
980 |
case "$1" in |
981 |
-h|--help) # already handled; ignore |
982 |
shift 1;; |
983 |
-m=*|--module=*) |
984 |
createModuleProject modules[@] --module="${1#*=}"; printf "\n"; shift 1;; |
985 |
-m|--module) |
986 |
createModuleProject modules[@] --module="${2}"; printf "\n"; shift 2;; |
987 |
-a|--all) |
988 |
createAllProjects modules[@]; shift 1;; |
989 |
-c|--clean) |
990 |
deleteProjects; printf "\n"; shift 1;; |
991 |
-w|--wipe) |
992 |
deleteProjects --wipe; printf "\n"; shift 1;; |
993 |
-q|--quit) |
994 |
quitScript; shift 1;; |
995 |
--log=*|--LOG=*) # already handled; ignore |
996 |
shift 1;; |
997 |
--log|--LOG) # already handled; ignore |
998 |
shift 2;; |
999 |
--noinfo) # already handled; ignore |
1000 |
shift 1;; |
1001 |
*) |
1002 |
printError "invalid option: $1\n"; shift 1;; |
1003 |
esac |
1004 |
else |
1005 |
otherargs+=("$1") |
1006 |
shift 1 |
1007 |
fi |
1008 |
done |
1009 |
|
1010 |
# interactive menu |
1011 |
while ( true ); do |
1012 |
# main menu info prompt and selection |
1013 |
printInfo "QtCreator setup main menu\n" |
1014 |
printf "Please select one of the following actions:\n" |
1015 |
printf " [M] - create a project for a single module\n" |
1016 |
printf " [A] - create a project for all modules\n" |
1017 |
printf " [C] - clean all project files\n" |
1018 |
printf " [W] - wipe all project and .user files\n" |
1019 |
printf " [Q] - quit this setup\n" |
1020 |
local userinput="" |
1021 |
readUserInput "MmAaCcWwQq" userinput |
1022 |
printf "\n" |
1023 |
|
1024 |
# evaluate user selection |
1025 |
case "$userinput" in |
1026 |
M|m) |
1027 |
createModuleProject modules[@]; printf "\n";; |
1028 |
A|a) |
1029 |
createAllProjects modules[@]; printf "\n";; |
1030 |
C|c) |
1031 |
deleteProjects; printf "\n";; |
1032 |
W|w) |
1033 |
deleteProjects --wipe; printf "\n";; |
1034 |
Q|q) |
1035 |
quitScript;; |
1036 |
*) # sanity check (exit with error) |
1037 |
printError "unexpected argument: $userinput\n";; |
1038 |
esac |
1039 |
done |
1040 |
|
1041 |
exit 0 |
1042 |
} |
1043 |
|
1044 |
################################################################################ |
1045 |
# SCRIPT ENTRY POINT # |
1046 |
################################################################################ |
1047 |
|
1048 |
main "$@" |
1049 |
|