Statistics
| Branch: | Tag: | Revision:

amiro-os / tools / ide / QtCreator / QtCreatorSetup.sh @ 57cbd1cd

History | View | Annotate | Download (34.128 KB)

1 e545e620 Thomas Schöpping
################################################################################
2
# AMiRo-OS is an operating system designed for the Autonomous Mini Robot       #
3
# (AMiRo) platform.                                                            #
4 84f0ce9e Thomas Schöpping
# Copyright (C) 2016..2019  Thomas Schöpping et al.                            #
5 e545e620 Thomas Schöpping
#                                                                              #
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 57cbd1cd Thomas Schöpping
### check whether commands are available #######################################
267
# Checks whether the specified commands are available and can be executed.
268
#
269
# usage:      checkCommand [<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 e545e620 Thomas Schöpping
################################################################################
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 84f0ce9e Thomas Schöpping
  printf "# Copyright (c) 2016..2019  Thomas Schöpping                         #\n"
319 e545e620 Thomas Schöpping
  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 959f302e Thomas Schöpping
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [--module=<module>] [-a|--all] [-c|--clean] [-w|--wipe] [-q|--quit] [--log=<file>]\n"
341 e545e620 Thomas Schöpping
  printf "\n"
342
  printf "options:  -h, --help\n"
343
  printf "              Print this help text.\n"
344 959f302e Thomas Schöpping
  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 e545e620 Thomas Schöpping
  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 amiroosdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../)
369
  local input=""
370
  read -p "Path where to create/delete project files: " -i $amiroosdir -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
#
386
function retrieveGccIncludeDir {
387
  # retrieve binary path or link
388
  local binpath=$(which arm-none-eabi-gcc)
389
  if [ -z "$binpath" ]; then
390
    printError "command 'arm-none-eabi-gcc' not found\n"
391
    return -1
392
  else 
393
394
    # traverse any links
395
    while [ -L "$binpath" ]; do
396 680d05e5 Thomas Schöpping
      binpath=$(realpath $(dirname $binpath)/$(readlink $binpath))
397 e545e620 Thomas Schöpping
    done
398
    printInfo "gcc-arm-none-eabi detected: $binpath\n"
399
400
    # return include path
401
    eval $1=$(realpath $(dirname ${binpath})/../arm-none-eabi/include/)
402
403
    return 0
404
  fi
405
}
406
407 959f302e Thomas Schöpping
### detect available modules ###################################################
408
# Detect all avalable modules supported by AMiRo-OS.
409 e545e620 Thomas Schöpping
#
410 959f302e Thomas Schöpping
# usage:      detectModules <modulearray>
411
# arguments:  <modulearray>
412
#                 Array variable to store all detected modules to.
413
# return:     n/a
414
#
415
function detectModules {
416
  local modulesdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../modules)
417
  local modules_detected=()
418 e545e620 Thomas Schöpping
419 959f302e Thomas Schöpping
  # detect all available modules (via directories)
420
  for dir in $(ls -d ${modulesdir}/*/); do
421
    modules_detected[${#modules_detected[@]}]=$(basename $dir)
422 e545e620 Thomas Schöpping
  done
423
424 959f302e Thomas Schöpping
  # set the output variable
425
  eval "$1=(${modules_detected[*]})"
426 e545e620 Thomas Schöpping
}
427
428 959f302e Thomas Schöpping
### create project files for a single module ###################################
429
# Create project files for a single module.
430 e545e620 Thomas Schöpping
#
431 959f302e Thomas Schöpping
# usage:      createModuleProject <modules> [-m|--module=<module>] [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
432
# arguments:  <modules>
433
#                 Array containing all modules available.
434
#             -m, --module <module>
435
#                 Name (folder name) of the module for which project files shall be generated.
436
#             -p, --path <path>
437 e545e620 Thomas Schöpping
#                 Path where to create the project files.
438
#             --gcc=<path>
439
#                 Path to the GCC include directory.
440
#             -o, --out <var>
441
#                 Variable to store the path to.
442
#             --gccout=<var>
443
#                 Variable to store the path to the GCC include directory to.
444 959f302e Thomas Schöpping
#                 If this optional arguments is absent, ths function will ask for user input.
445 e545e620 Thomas Schöpping
# return:     0
446
#                 No error or warning occurred.
447 959f302e Thomas Schöpping
#             1
448
#                 Aborted by user.
449
#             -1
450
#                 No modules available.
451
#             -2
452
#                 The specified <module> could not be found.
453
#             -3
454
#                 Parsing the project for the specified module failed.
455 57cbd1cd Thomas Schöpping
#             -4
456
#                 Missing dependencies.
457 e545e620 Thomas Schöpping
#
458 959f302e Thomas Schöpping
function createModuleProject {
459 e545e620 Thomas Schöpping
  local userdir=$(pwd)
460 959f302e Thomas Schöpping
  local modulesdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../modules)
461
  local modules=("${!1}")
462
  local module=""
463
  local moduleidx=""
464 e545e620 Thomas Schöpping
  local projectdir=""
465
  local gccincludedir=""
466
  local outvar=""
467
  local gccoutvar=""
468
469 57cbd1cd Thomas Schöpping
  # check dependencies
470
  checkCommands make
471
  if [ $? -ne 0 ]; then
472
    printError "Missing dependencies detected.\n"
473
    return -4
474
  fi
475
476 e545e620 Thomas Schöpping
  # parse arguments
477
  local otherargs=()
478
  while [ $# -gt 0 ]; do
479
    if ( parseIsOption $1 ); then
480
      case "$1" in
481 959f302e Thomas Schöpping
        -m=*|--module=*)
482
          module="${1#*=}"; shift 1;;
483
        -m|--module)
484
          module="$2"; shift 2;;
485 e545e620 Thomas Schöpping
        -p=*|--path=*)
486
          projectdir=$(realpath "${1#*=}"); shift 1;;
487
        -p|--path)
488
          projectdir=$(realpath "$2"); shift 2;;
489
        --gcc=*)
490
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
491
        --gcc)
492
          gccincludedir=$(realpath "$2"); shift 2;;
493
        -o=*|--out=*)
494
          outvar=${1#*=}; shift 1;;
495
        -o|--out)
496
          outvar=$2; shift 2;;
497
        --gccout=*)
498
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
499
        --gccout)
500
          gccoutvar=$(realpath "$2"); shift 2;;
501
        *)
502
          printError "invalid option: $1\n"; shift 1;;
503
      esac
504
    else
505
      otherargs+=("$1")
506
      shift 1
507
    fi
508
  done
509
510 959f302e Thomas Schöpping
  # sanity check for the modules variable
511
  if [ -z "${modules[*]}" ]; then
512
    printError "no modules available\n"
513
    return -1
514
  fi
515
516
  # select module
517
  if [ -z $module ]; then
518
    # list all available modules
519
    printInfo "choose a module or type 'A' to abort:\n"
520
    for (( idx=0; idx<${#modules[@]}; ++idx )); do
521
      printf "%4u: %s\n" $(($idx + 1)) "${modules[$idx]}"
522
    done
523
    # read user input
524
    printLog "read user selection\n"
525
    local userinput=""
526
    while [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#modules[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; do
527
      read -p "your selection: " -e userinput
528
      printLog "user selection: $userinput\n"
529
      if [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#modules[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; then
530
        printWarning "Please enter an integer between 1 and ${#modules[@]} or 'A' to abort.\n"
531
      fi
532
    done
533
    if [[ "$userinput" =~ ^[Aa]$ ]]; then
534
      printWarning "aborted by user\n"
535
      return 1
536
    fi
537
    # store selection
538
    moduleidx=$(($userinput - 1))
539
    module="${modules[$moduleidx]}"
540
    printf "\n"
541
  else
542
    # search all modules for the selected one
543
    for (( idx=0; idx<${#modules[@]}; ++idx )); do
544
      if [ "${modules[$idx]}" = "$module" ]; then
545
        moduleidx=$idx
546
        break
547
      fi
548
    done
549
    # error if the module could not be found
550
    if [ -z $moduleidx ]; then
551
      printError "module ($module) not available\n"
552
      return -2
553
    fi
554
  fi
555 e545e620 Thomas Schöpping
556
  # read absolute project directory if required
557
  if [ -z "$projectdir" ]; then
558
    getProjectDir projectdir
559 959f302e Thomas Schöpping
    printf "\n"
560
  fi
561
562
  # check for existing project files
563 23230307 Thomas Schöpping
  local projectfiles="$(find ${projectdir} -maxdepth 1 -type f | grep -E "${module}\.(includes|files|config|creator)$")"
564
  IFS=$'\n'; projectfiles=($projectfiles); unset IFS
565
  if [ ! -z "${projectfiles[*]}" ]; then
566 959f302e Thomas Schöpping
    printWarning "The following files will be overwritten:\n"
567 23230307 Thomas Schöpping
    for pfile in ${projectfiles[@]}; do
568
      printWarning "\t$(basename $pfile)\n"
569
    done
570 959f302e Thomas Schöpping
    local userinput=""
571
    printInfo "Continue and overwrite? [y/n]\n"
572
    readUserInput "YyNn" userinput
573
    case "$userinput" in
574
      Y|y)
575
        ;;
576
      N|n)
577
        printWarning "Project generation for ${module} module aborted by user\n"
578
        return 1
579
        ;;
580
      *)
581
        printError "unexpected input: ${userinput}\n"; return -999;;
582
    esac
583
    printf "\n"
584 e545e620 Thomas Schöpping
  fi
585
586 959f302e Thomas Schöpping
  # print message
587
  printInfo "generating QtCreator project files for the $module module...\n"
588
589
  # retrieve absolute GCC include path
590 e545e620 Thomas Schöpping
  if [ -z "$gccincludedir" ]; then
591
    retrieveGccIncludeDir gccincludedir
592
  fi
593
594 959f302e Thomas Schöpping
  # change to project directory
595
  cd "$projectdir"
596 e545e620 Thomas Schöpping
597 959f302e Thomas Schöpping
  # run make, but only run the GCC preprocessor and produce no binaries
598 9b0c922c Thomas Schöpping
  local amiroosrootdir=$(realpath $(dirname ${BASH_SOURCE[0]})/../../..)
599 959f302e Thomas Schöpping
  local sourcefiles=()
600
  local sourcefile=""
601 c28a90e7 Thomas Schöpping
  local parse_state="WAIT_FOR_INCLUDE_OR_COMPILE"
602 959f302e Thomas Schöpping
  # capture all output from make and GCC and append the return value of make as last line
603
  printInfo "processing project (this may take a while)...\n"
604 781ae14b Thomas Schöpping
  local rawout=$(make --directory ${amiroosrootdir}/modules/${module} --always-make USE_OPT="-v -E -H" USE_VERBOSE_COMPILE="no" OUTFILES="" 2>&1 && echo $?)
605 959f302e Thomas Schöpping
  # check whether the make call was successfull
606
  if [[ $(echo "${rawout}" | tail -n 1) != "0" ]]; then
607
    printError "executing 'make' in module directory failed\n"
608
    cd "$userdir"
609
    return -3
610 4df853e0 Thomas Schöpping
  else
611
    # cleanup
612
    make --directory ${amiroosrootdir}/modules/${module} clean &>/dev/null
613 959f302e Thomas Schöpping
  fi
614
  # extract file names from raw output
615 c28a90e7 Thomas Schöpping
  IFS=$'\n'; rawout=($rawout); unset IFS
616
  for line in "${rawout[@]}"; do
617
    case $parse_state in
618
      WAIT_FOR_INCLUDE_OR_COMPILE)
619 959f302e Thomas Schöpping
        # lines stating included files look like:
620
        # ... <../relative/path/to/file>
621
        if [[ "$line" =~ ^\.+[[:blank:]].+\..+$ ]]; then
622
          sourcefile=${line##* }
623
          if [[ ! "$sourcefile" =~ ^/ ]]; then
624
            sourcefile=$(realpath ${amiroosrootdir}/modules/${module}/${sourcefile})
625
          fi
626
          sourcefiles[${#sourcefiles[@]}]="$sourcefile"
627
        # whenever the next source file is processed, a message appears like:
628
        # Compining <filnemame>
629
        elif [[ "$line" =~ ^Compiling[[:blank:]].+\..+$ ]]; then
630
          printf "."
631
          sourcefile=${line##* }
632 330feaa3 Thomas Schöpping
          parse_state="WAIT_FOR_COMPILERCALL"
633 959f302e Thomas Schöpping
        fi;;
634
      WAIT_FOR_COMPILERCALL)
635
        # wait for the actual call of the compiler to retrieve the full path to the source file
636
        if [[ "$line" == *${sourcefile}* ]]; then
637
          line="${line%%${sourcefile}*}${sourcefile}"
638
          sourcefile=${line##* }
639
          sourcefile=$(realpath ${amiroosrootdir}/modules/${module}/${line##* })
640
          sourcefiles[${#sourcefiles[@]}]="$sourcefile"
641 330feaa3 Thomas Schöpping
          parse_state="WAIT_FOR_INCLUDE_OR_COMPILE"
642 959f302e Thomas Schöpping
        fi;;
643
    esac
644 c28a90e7 Thomas Schöpping
  done
645 959f302e Thomas Schöpping
  unset rawout
646
  printf "\n"
647
  # sort and remove duplicates
648
  IFS=$'\n'; sourcefiles=($(sort --unique <<< "${sourcefiles[*]}")); unset IFS
649
650
  # extract include paths
651
  local includes=()
652
  for source in ${sourcefiles[*]}; do
653
    includes[${#includes[@]}]="$(dirname ${source})"
654
  done
655
  # sort and remove duplicates
656
  IFS=$'\n'; includes=($(sort --unique <<< "${includes[*]}")); unset IFS
657
658
  # generate the .files file, containing all source files
659
  echo "" > ${projectdir}/${module}.includes
660
  for inc in ${includes[*]}; do
661
    echo "$inc" >> ${projectdir}/${module}.includes
662
  done
663
  # generate the .incldues file, containing all include paths
664
  echo "" > ${projectdir}/${module}.files
665
  for source in ${sourcefiles[*]}; do
666
    # skip GCC files
667
    if [[ ! "$source" =~ .*/gcc.* ]]; then
668
      echo "$source" >> ${projectdir}/${module}.files
669
    fi
670 e545e620 Thomas Schöpping
  done
671 959f302e Thomas Schöpping
  # generate a default project configuration file if it doesn't exist yet
672
  if [ ! -f ${projectdir}/${module}.config ]; then
673
    echo "// Add predefined macros for your project here. For example:" > ${projectdir}/${module}.config
674
    echo "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/${module}.config
675 e545e620 Thomas Schöpping
  fi
676 959f302e Thomas Schöpping
  # generate a default .creator file if it doesn't exist yet
677
  if [ ! -f ${projectdir}/${module}.creator ]; then
678
    echo "[general]" > ${projectdir}/${module}.creator
679 e545e620 Thomas Schöpping
  fi
680
681
  # go back to user directory
682
  cd $userdir
683
684
  # fill the output variables
685
  if [ ! -z "$outvar" ]; then
686
    eval $outvar="$projectdir"
687
  fi
688
  if [ ! -z "$gccoutvar" ]; then
689
    eval $gccoutvar="$gccincludedir"
690
  fi
691
692
  return 0
693
}
694
695 959f302e Thomas Schöpping
### create project files for all modules #######################################
696
# Create project files for all modules.
697 e545e620 Thomas Schöpping
#
698 959f302e Thomas Schöpping
# usage:      createAllProjects <modules> [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
699
# arguments:  <modules>
700
#                 Array containing all modules available.
701
#             -p, --path <path>
702 e545e620 Thomas Schöpping
#                 Path where to create the project files.
703
#             --gcc=<path>
704
#                 Path to the GCC include directory.
705
#             -o, --out <var>
706
#                 Variable to store the path to.
707
#             --gccout=<var>
708
#                 Variable to store the path to the GCC include directory to.
709 959f302e Thomas Schöpping
#                 If this optional arguments is absent, ths function will ask for user input.
710 e545e620 Thomas Schöpping
# return:     0
711
#                 No error or warning occurred.
712 23230307 Thomas Schöpping
#             1
713
#                 Aborted by user.
714 959f302e Thomas Schöpping
#             -1
715
#                 No modules available.
716 e545e620 Thomas Schöpping
#
717 959f302e Thomas Schöpping
function createAllProjects {
718
  local modules=("${!1}")
719 e545e620 Thomas Schöpping
  local projectdir=""
720
  local gccincludedir=""
721
  local outvar=""
722
  local gccoutvar=""
723
724
  # parse arguments
725
  local otherargs=()
726
  while [ $# -gt 0 ]; do
727
    if ( parseIsOption $1 ); then
728
      case "$1" in
729
        -p=*|--path=*)
730
          projectdir=$(realpath "${1#*=}"); shift 1;;
731
        -p|--path)
732
          projectdir=$(realpath "$2"); shift 2;;
733
        --gcc=*)
734
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
735
        --gcc)
736
          gccincludedir=$(realpath "$2"); shift 2;;
737
        -o=*|--out=*)
738
          outvar=${1#*=}; shift 1;;
739
        -o|--out)
740
          outvar=$2; shift 2;;
741
        --gccout=*)
742
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
743
        --gccout)
744
          gccoutvar=$(realpath "$2"); shift 2;;
745
        *)
746
          printError "invalid option: $1\n"; shift 1;;
747
      esac
748
    else
749
      otherargs+=("$1")
750
      shift 1
751
    fi
752
  done
753
754 959f302e Thomas Schöpping
  # sanity check for the modules variable
755
  if [ -z "${modules[*]}" ]; then
756
    printError "no modules available\n"
757
    return -1
758
  fi
759 e545e620 Thomas Schöpping
760
  # read absolute project directory if required
761
  if [ -z "$projectdir" ]; then
762
    getProjectDir projectdir
763
  fi
764
765 23230307 Thomas Schöpping
  # check for existing project files
766
  local projectfiles=()
767
  for module in ${modules[@]}; do
768
    local pfiles="$(find ${projectdir} -maxdepth 1 -type f | grep -E "${module}\.(includes|files|config|creator)$")"
769
    IFS=$'\n'; pfiles=($pfiles); unset IFS
770
    projectfiles=( ${projectfiles[*]} ${pfiles[*]} )
771
  done
772
  if [ ! -z "${projectfiles[*]}" ]; then
773
    printWarning "The following files will be removed:\n"
774
    for pfile in ${projectfiles[@]}; do
775
      printWarning "\t$(basename $pfile)\n"
776
    done
777
    local userinput=""
778
    printInfo "Continue and overwrite? [y/n]\n"
779
    readUserInput "YyNn" userinput
780
    case "${userinput}" in
781
      Y|y)
782
        for pfile in ${projectfiles[*]}; do
783
          rm "$pfile"
784
        done
785
        ;;
786
      N|n)
787
        printWarning "Project generation aborted by user\n"
788
        return 1
789
        ;;
790
      *)
791
        printError "unexpected input: ${userinput}\n"
792
        return 999
793
        ;;
794
    esac
795
  fi
796
797 959f302e Thomas Schöpping
  # print message
798
  printf "\n"
799
  printInfo "generating QtCreator project files for all modules...\n"
800
801
  # retrieve absolute GCC include path
802 e545e620 Thomas Schöpping
  if [ -z "$gccincludedir" ]; then
803
    retrieveGccIncludeDir gccincludedir
804
  fi
805
806 959f302e Thomas Schöpping
  # iterate over all modules
807
  local retval=1
808
  for module in ${modules[@]}; do
809
    if [ $retval != 0 ]; then
810
      printf "\n"
811
    fi
812
    createModuleProject modules[@] --module="$module" --path="$projectdir" --gcc="$gccincludedir"
813
    retval=$?
814 e545e620 Thomas Schöpping
  done
815
816
  return 0
817
}
818
819 959f302e Thomas Schöpping
### delete project files #######################################################
820
# Deletes all project files and optionally .user files, too.
821 e545e620 Thomas Schöpping
#
822 959f302e Thomas Schöpping
# usage:      deleteProjects [-p|--path=<path>] [-m|--module=<module>] [-o|--out=<var>] [-w|-wipe]
823 e545e620 Thomas Schöpping
# arguments:  -p, --path <path>
824 959f302e Thomas Schöpping
#                 Path where to delete the project files.
825
#             -m, --module <module>
826
#                 Module name for which the project files shall be deleted.
827 e545e620 Thomas Schöpping
#             -o, --out <var>
828
#                 Variable to store the path to.
829 959f302e Thomas Schöpping
#             -w, --wipe
830
#                 Delete .user files as well.
831
# return:
832
#  -  0: no error
833 e545e620 Thomas Schöpping
#
834 959f302e Thomas Schöpping
function deleteProjects {
835
  local modulename=""
836 e545e620 Thomas Schöpping
  local projectdir=""
837
  local outvar=""
838 959f302e Thomas Schöpping
  local wipe=false
839
  local files=""
840 e545e620 Thomas Schöpping
841
  # parse arguments
842
  local otherargs=()
843
  while [ $# -gt 0 ]; do
844
    if ( parseIsOption $1 ); then
845
      case "$1" in
846
        -p=*|--path=*)
847
          projectdir=$(realpath "${1#*=}"); shift 1;;
848
        -p|--path)
849
          projectdir=$(realpath "$2"); shift 2;;
850 959f302e Thomas Schöpping
        -m=*|--module=*)
851
          modulename="${1#*=}"; shift 1;;
852
        -m|--module)
853
          modulename="${2}"; shift 2;;
854 e545e620 Thomas Schöpping
        -o=*|--out=*)
855
          outvar=${1#*=}; shift 1;;
856
        -o|--out)
857
          outvar=$2; shift 2;;
858 959f302e Thomas Schöpping
        -w|--wipe)
859
          wipe=true; shift 1;;
860 e545e620 Thomas Schöpping
        *)
861
          printError "invalid option: $1\n"; shift 1;;
862
      esac
863
    else
864
      otherargs+=("$1")
865
      shift 1
866
    fi
867
  done
868
869
  # read absolute project directory if required
870
  if [ -z "$projectdir" ]; then
871
    getProjectDir projectdir
872
  fi
873
874 959f302e Thomas Schöpping
  # list all files to be deleted
875
  if [ -z "$modulename" ]; then
876
    if [ $wipe != true ]; then
877
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator)$")
878
    else
879
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator|creator\.user)$")
880
    fi
881
  else
882
    if [ $wipe != true ]; then
883
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.${modulename}\.(includes|files|config|creator)$")
884
    else
885
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.${modulename}\.(includes|files|config|creator|creator\.user)$")
886
    fi
887 e545e620 Thomas Schöpping
  fi
888 959f302e Thomas Schöpping
  if [ ! -z "$files" ]; then
889
    printInfo "Deleting the following files:\n"
890
    while read line; do
891
      printInfo "\t$(basename ${line})\n"
892
      rm ${line} 2>&1 | tee -a $LOG_FILE
893
    done <<< "${files}"
894
  else
895
    printInfo "No project files found\n"
896 e545e620 Thomas Schöpping
  fi
897
898 959f302e Thomas Schöpping
  # store the path to the output variable, if required
899 e545e620 Thomas Schöpping
  if [ ! -z "$outvar" ]; then
900
    eval $outvar="$projectdir"
901
  fi
902
903
  return 0
904
}
905
906
### main function of this script ###############################################
907
# Creates, deletes and wipes QtCreator project files for the three AMiRo base modules.
908
#
909
# usage:      see function printHelp
910
# arguments:  see function printHelp
911
# return:     0
912
#                 No error or warning ocurred.
913
#
914
function main {
915
# print welcome/info text if not suppressed
916
  if [[ $@ != *"--noinfo"* ]]; then
917
    printWelcomeText
918
  else
919
    printf "######################################################################\n"
920
  fi
921
  printf "\n"
922
923
  # if --help or -h was specified, print the help text and exit
924
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
925
    printHelp
926
    printf "\n"
927
    quitScript
928
  fi
929
930
  # set log file if specified
931
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
932
    # get the parameter (file name)
933
    local cmdidx=1
934
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
935
      cmdidx=$[cmdidx + 1]
936
    done
937
    local cmd="${!cmdidx}"
938
    local logfile=""
939
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
940
      logfile=${cmd#*=}
941
    else
942
      local filenameidx=$((cmdidx + 1))
943
      logfile="${!filenameidx}"
944
    fi
945
    # optionally force silent appending
946
    if [[ "$cmd" = "--LOG"* ]]; then
947
      setLogFile --option=c --quiet "$logfile" LOG_FILE
948
    else
949
      setLogFile "$logfile" LOG_FILE
950
      printf "\n"
951
    fi
952
  fi
953
  # log script name
954
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
955
956 959f302e Thomas Schöpping
  # detect available modules and inform user
957
  local modules=()
958
  detectModules modules
959
  case "${#modules[@]}" in
960
    0)
961
      printInfo "no module has been detected\n";;
962
    1)
963
      printInfo "1 module has been detected:\n";;
964
    *)
965
      printInfo "${#modules[@]} modules have been detected:\n"
966
  esac
967
  for (( idx=0; idx<${#modules[@]}; ++idx )); do
968
    printInfo "  - ${modules[$idx]}\n"
969
  done
970
  printf "\n"
971
972 e545e620 Thomas Schöpping
  # parse arguments
973
  local otherargs=()
974
  while [ $# -gt 0 ]; do
975
    if ( parseIsOption $1 ); then
976
      case "$1" in
977
        -h|--help) # already handled; ignore
978
          shift 1;;
979 959f302e Thomas Schöpping
        -m=*|--module=*)
980
          createModuleProject modules[@] --module="${1#*=}"; printf "\n"; shift 1;;
981
        -m*|--module*)
982
           createModuleProject modules[@] --module="${2}"; printf "\n"; shift 2;;
983
        -a|--all)
984
           createAllProjects modules[@]; shift 1;;
985 e545e620 Thomas Schöpping
        -c|--clean)
986
          deleteProjects; printf "\n"; shift 1;;
987
        -w|--wipe)
988
          deleteProjects --wipe; printf "\n"; shift 1;;
989
        -q|--quit)
990
          quitScript; shift 1;;
991
        --log=*|--LOG=*) # already handled; ignore
992
          shift 1;;
993
        --log|--LOG) # already handled; ignore
994
          shift 2;;
995
        --noinfo) # already handled; ignore
996
          shift 1;;
997
        *)
998
          printError "invalid option: $1\n"; shift 1;;
999
      esac
1000
    else
1001
      otherargs+=("$1")
1002
      shift 1
1003
    fi
1004
  done
1005
1006
  # interactive menu
1007
  while ( true ); do
1008
    # main menu info prompt and selection
1009
    printInfo "QtCreator setup main menu\n"
1010
    printf "Please select one of the following actions:\n"
1011 959f302e Thomas Schöpping
    printf "  [M] - create a project for a single module\n"
1012 e545e620 Thomas Schöpping
    printf "  [A] - create a project for all modules\n"
1013 959f302e Thomas Schöpping
    printf "  [C] - clean all project files\n"
1014
    printf "  [W] - wipe all project and .user files\n"
1015 e545e620 Thomas Schöpping
    printf "  [Q] - quit this setup\n"
1016
    local userinput=""
1017 959f302e Thomas Schöpping
    readUserInput "MmAaCcWwQq" userinput
1018 e545e620 Thomas Schöpping
    printf "\n"
1019
1020
    # evaluate user selection
1021
    case "$userinput" in
1022 959f302e Thomas Schöpping
      M|m)
1023
        createModuleProject modules[@]; printf "\n";;
1024
      A|a)
1025
        createAllProjects modules[@]; printf "\n";;
1026 e545e620 Thomas Schöpping
      C|c)
1027
        deleteProjects; printf "\n";;
1028
      W|w)
1029
        deleteProjects --wipe; printf "\n";;
1030
      Q|q)
1031
        quitScript;;
1032
      *) # sanity check (exit with error)
1033
        printError "unexpected argument: $userinput\n";;
1034
    esac
1035
  done
1036
1037
  exit 0
1038
}
1039
1040
################################################################################
1041
# SCRIPT ENTRY POINT                                                           #
1042
################################################################################
1043
1044
main "$@"