Statistics
| Branch: | Tag: | Revision:

amiro-os / tools / ide / QtCreator / QtCreatorSetup.sh @ 9df585c4

History | View | Annotate | Download (32.293 KB)

1
################################################################################
2
# AMiRo-OS is an operating system designed for the Autonomous Mini Robot       #
3
# (AMiRo) platform.                                                            #
4
# Copyright (C) 2016..2018  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
################################################################################
267
# SPECIFIC FUNCTIONS                                                           #
268
################################################################################
269

    
270
### print welcome text #########################################################
271
# Prints a welcome message to standard out.
272
#
273
# usage:      printWelcomeText
274
# arguments:  n/a
275
# return:     n/a
276
#
277
function printWelcomeText {
278
  printf "######################################################################\n"
279
  printf "#                                                                    #\n"
280
  printf "#                  Welcome to the QtCreator setup!                   #\n"
281
  printf "#                                                                    #\n"
282
  printf "######################################################################\n"
283
  printf "#                                                                    #\n"
284
  printf "# Copyright (c) 2016..2018  Thomas Schöpping                         #\n"
285
  printf "#                                                                    #\n"
286
  printf "# This is free software; see the source for copying conditions.      #\n"
287
  printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n"
288
  printf "# A PARTICULAR PURPOSE. The development of this software was         #\n"
289
  printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction  #\n"
290
  printf "# Technology. The Excellence Cluster EXC 227 is a grant of the       #\n"
291
  printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n"
292
  printf "# Excellence Initiative.                                             #\n"
293
  printf "#                                                                    #\n"
294
  printf "######################################################################\n"
295
}
296

    
297
### print help #################################################################
298
# Prints a help text to standard out.
299
#
300
# usage:      printHelp
301
# arguments:  n/a
302
# return:     n/a
303
#
304
function printHelp {
305
  printInfo "printing help text\n"
306
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [--module=<module>] [-a|--all] [-c|--clean] [-w|--wipe] [-q|--quit] [--log=<file>]\n"
307
  printf "\n"
308
  printf "options:  -h, --help\n"
309
  printf "              Print this help text.\n"
310
  printf "          --module=<module>\n"
311
  printf "              Create project for a single module.\n"
312
  printf "          -a, --all\n"
313
  printf "              Create projects for all modules.\n"
314
  printf "          -c, --clean\n"
315
  printf "              Delete project files.\n"
316
  printf "          -w, --wipe\n"
317
  printf "              Delete project and .user files.\n"
318
  printf "          -q, --quit\n"
319
  printf "              Quit the script.\n"
320
  printf "          --log=<file>\n"
321
  printf "              Specify a log file.\n"
322
}
323

    
324
### read directory where to create/delete projects #############################
325
# Read the directory where to create/delete project files from user.
326
#
327
# usage:      getProjectDir <pathvar>
328
# arguments:  <pathvar>
329
#                 Variable to store the selected path to.
330
# return:     n/a
331
#
332
function getProjectDir {
333
  printLog "reading path for project files from user...\n"
334
  local amiroosdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../)
335
  local input=""
336
  read -p "Path where to create/delete project files: " -i $amiroosdir -e input
337
  printLog "user selected path $(realpath $input)\n"
338
  eval $1="$(realpath $input)"
339
}
340

    
341
### retrieves the ARM-NONE-EABI-GCC include directory ##########################
342
# Retrieves the include directory of the currently set arm-none-eabi-gcc.
343
#
344
# usage:      retrieveGccIncludeDir <path>
345
# arguments:  <path>
346
#                 Variable to store the path to.
347
# return:    0
348
#                 No error or warning occurred.
349
#            -1
350
#                 Error: Command 'arm-none-eabi-gcc' not found.
351
#
352
function retrieveGccIncludeDir {
353
  # retrieve binary path or link
354
  local binpath=$(which arm-none-eabi-gcc)
355
  if [ -z "$binpath" ]; then
356
    printError "command 'arm-none-eabi-gcc' not found\n"
357
    return -1
358
  else 
359

    
360
    # traverse any links
361
    while [ -L "$binpath" ]; do
362
      binpath=$(realpath $(dirname $binpath)/$(readlink $binpath))
363
    done
364
    printInfo "gcc-arm-none-eabi detected: $binpath\n"
365

    
366
    # return include path
367
    eval $1=$(realpath $(dirname ${binpath})/../arm-none-eabi/include/)
368

    
369
    return 0
370
  fi
371
}
372

    
373
### detect available modules ###################################################
374
# Detect all avalable modules supported by AMiRo-OS.
375
#
376
# usage:      detectModules <modulearray>
377
# arguments:  <modulearray>
378
#                 Array variable to store all detected modules to.
379
# return:     n/a
380
#
381
function detectModules {
382
  local modulesdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../modules)
383
  local modules_detected=()
384

    
385
  # detect all available modules (via directories)
386
  for dir in $(ls -d ${modulesdir}/*/); do
387
    modules_detected[${#modules_detected[@]}]=$(basename $dir)
388
  done
389

    
390
  # set the output variable
391
  eval "$1=(${modules_detected[*]})"
392
}
393

    
394
### create project files for a single module ###################################
395
# Create project files for a single module.
396
#
397
# usage:      createModuleProject <modules> [-m|--module=<module>] [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
398
# arguments:  <modules>
399
#                 Array containing all modules available.
400
#             -m, --module <module>
401
#                 Name (folder name) of the module for which project files shall be generated.
402
#             -p, --path <path>
403
#                 Path where to create the project files.
404
#             --gcc=<path>
405
#                 Path to the GCC include directory.
406
#             -o, --out <var>
407
#                 Variable to store the path to.
408
#             --gccout=<var>
409
#                 Variable to store the path to the GCC include directory to.
410
#                 If this optional arguments is absent, ths function will ask for user input.
411
# return:     0
412
#                 No error or warning occurred.
413
#             1
414
#                 Aborted by user.
415
#             -1
416
#                 No modules available.
417
#             -2
418
#                 The specified <module> could not be found.
419
#             -3
420
#                 Parsing the project for the specified module failed.
421
#
422
function createModuleProject {
423
  local userdir=$(pwd)
424
  local modulesdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../modules)
425
  local modules=("${!1}")
426
  local module=""
427
  local moduleidx=""
428
  local projectdir=""
429
  local gccincludedir=""
430
  local outvar=""
431
  local gccoutvar=""
432

    
433
  # parse arguments
434
  local otherargs=()
435
  while [ $# -gt 0 ]; do
436
    if ( parseIsOption $1 ); then
437
      case "$1" in
438
        -m=*|--module=*)
439
          module="${1#*=}"; shift 1;;
440
        -m|--module)
441
          module="$2"; shift 2;;
442
        -p=*|--path=*)
443
          projectdir=$(realpath "${1#*=}"); shift 1;;
444
        -p|--path)
445
          projectdir=$(realpath "$2"); shift 2;;
446
        --gcc=*)
447
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
448
        --gcc)
449
          gccincludedir=$(realpath "$2"); shift 2;;
450
        -o=*|--out=*)
451
          outvar=${1#*=}; shift 1;;
452
        -o|--out)
453
          outvar=$2; shift 2;;
454
        --gccout=*)
455
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
456
        --gccout)
457
          gccoutvar=$(realpath "$2"); shift 2;;
458
        *)
459
          printError "invalid option: $1\n"; shift 1;;
460
      esac
461
    else
462
      otherargs+=("$1")
463
      shift 1
464
    fi
465
  done
466

    
467
  # sanity check for the modules variable
468
  if [ -z "${modules[*]}" ]; then
469
    printError "no modules available\n"
470
    return -1
471
  fi
472

    
473
  # select module
474
  if [ -z $module ]; then
475
    # list all available modules
476
    printInfo "choose a module or type 'A' to abort:\n"
477
    for (( idx=0; idx<${#modules[@]}; ++idx )); do
478
      printf "%4u: %s\n" $(($idx + 1)) "${modules[$idx]}"
479
    done
480
    # read user input
481
    printLog "read user selection\n"
482
    local userinput=""
483
    while [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#modules[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; do
484
      read -p "your selection: " -e userinput
485
      printLog "user selection: $userinput\n"
486
      if [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#modules[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; then
487
        printWarning "Please enter an integer between 1 and ${#modules[@]} or 'A' to abort.\n"
488
      fi
489
    done
490
    if [[ "$userinput" =~ ^[Aa]$ ]]; then
491
      printWarning "aborted by user\n"
492
      return 1
493
    fi
494
    # store selection
495
    moduleidx=$(($userinput - 1))
496
    module="${modules[$moduleidx]}"
497
    printf "\n"
498
  else
499
    # search all modules for the selected one
500
    for (( idx=0; idx<${#modules[@]}; ++idx )); do
501
      if [ "${modules[$idx]}" = "$module" ]; then
502
        moduleidx=$idx
503
        break
504
      fi
505
    done
506
    # error if the module could not be found
507
    if [ -z $moduleidx ]; then
508
      printError "module ($module) not available\n"
509
      return -2
510
    fi
511
  fi
512

    
513
  # read absolute project directory if required
514
  if [ -z "$projectdir" ]; then
515
    getProjectDir projectdir
516
    printf "\n"
517
  fi
518

    
519
  # check for existing project files
520
  if [ -f ${projectdir}/${module}.includes ] || [ -f ${projectdir}/${module}.files ] || [ -f ${projectdir}/${module}.config ] || [ -f ${projectdir}/${module}.creator ]; then
521
    printWarning "The following files will be overwritten:\n"
522
    if [ -f ${projectdir}/${module}.includes ]; then
523
      printWarning "\t${module}.includes\n"
524
    fi
525
    if [ -f ${projectdir}/${module}.files ]; then
526
      printWarning "\t${module}.files\n"
527
    fi
528
    if [ -f ${projectdir}/${module}.config ]; then
529
      printWarning "\t${module}.config\n"
530
    fi
531
    if [ -f ${projectdir}/${module}.creator ]; then
532
      printWarning "\t${module}.creator\n"
533
    fi
534
    local userinput=""
535
    printInfo "Continue and overwrite? [y/n]\n"
536
    readUserInput "YyNn" userinput
537
    case "$userinput" in
538
      Y|y)
539
        ;;
540
      N|n)
541
        printWarning "Project generation for ${module} module aborted by user\n"
542
        return 1
543
        ;;
544
      *)
545
        printError "unexpected input: ${userinput}\n"; return -999;;
546
    esac
547
    printf "\n"
548
  fi
549

    
550
  # print message
551
  printInfo "generating QtCreator project files for the $module module...\n"
552

    
553
  # retrieve absolute GCC include path
554
  if [ -z "$gccincludedir" ]; then
555
    retrieveGccIncludeDir gccincludedir
556
  fi
557

    
558
  # change to project directory
559
  cd "$projectdir"
560

    
561
  # run make, but only run the GCC preprocessor and produce no binaries
562
  local amiroosrootdir=$(realpath $(dirname ${BASH_SOURCE[0]})/../../..)
563
  local sourcefiles=()
564
  local sourcefile=""
565
  local parse_state="WAIT_FOR_INCLUDE_OR_COMPILE"
566
  # capture all output from make and GCC and append the return value of make as last line
567
  printInfo "processing project (this may take a while)...\n"
568
  local rawout=$(make --directory ${amiroosrootdir}/modules/${module} --always-make USE_OPT="-v -E -H" OUTFILES="" --jobs="$(nproc --all)" 2>&1 && echo $?)
569
  # check whether the make call was successfull
570
  if [[ $(echo "${rawout}" | tail -n 1) != "0" ]]; then
571
    printError "executing 'make' in module directory failed\n"
572
    cd "$userdir"
573
    return -3
574
  fi
575
  # extract file names from raw output
576
  IFS=$'\n'; rawout=($rawout); unset IFS
577
  for line in "${rawout[@]}"; do
578
    case $parse_state in
579
      WAIT_FOR_INCLUDE_OR_COMPILE)
580
        # lines stating included files look like:
581
        # ... <../relative/path/to/file>
582
        if [[ "$line" =~ ^\.+[[:blank:]].+\..+$ ]]; then
583
          sourcefile=${line##* }
584
          if [[ ! "$sourcefile" =~ ^/ ]]; then
585
            sourcefile=$(realpath ${amiroosrootdir}/modules/${module}/${sourcefile})
586
          fi
587
          sourcefiles[${#sourcefiles[@]}]="$sourcefile"
588
        # whenever the next source file is processed, a message appears like:
589
        # Compining <filnemame>
590
        elif [[ "$line" =~ ^Compiling[[:blank:]].+\..+$ ]]; then
591
          printf "."
592
          sourcefile=${line##* }
593
          capture_state="WAIT_FOR_COMPILERCALL"
594
        fi;;
595
      WAIT_FOR_COMPILERCALL)
596
        # wait for the actual call of the compiler to retrieve the full path to the source file
597
        if [[ "$line" == *${sourcefile}* ]]; then
598
          line="${line%%${sourcefile}*}${sourcefile}"
599
          sourcefile=${line##* }
600
          sourcefile=$(realpath ${amiroosrootdir}/modules/${module}/${line##* })
601
          sourcefiles[${#sourcefiles[@]}]="$sourcefile"
602
          capture_state="WAIT_FOR_INCLUDE_OR_COMPILING"
603
        fi;;
604
    esac
605
  done
606
  unset rawout
607
  printf "\n"
608
  # sort and remove duplicates
609
  IFS=$'\n'; sourcefiles=($(sort --unique <<< "${sourcefiles[*]}")); unset IFS
610

    
611
  # extract include paths
612
  local includes=()
613
  for source in ${sourcefiles[*]}; do
614
    includes[${#includes[@]}]="$(dirname ${source})"
615
  done
616
  # sort and remove duplicates
617
  IFS=$'\n'; includes=($(sort --unique <<< "${includes[*]}")); unset IFS
618

    
619
  # generate the .files file, containing all source files
620
  echo "" > ${projectdir}/${module}.includes
621
  for inc in ${includes[*]}; do
622
    echo "$inc" >> ${projectdir}/${module}.includes
623
  done
624
  # generate the .incldues file, containing all include paths
625
  echo "" > ${projectdir}/${module}.files
626
  for source in ${sourcefiles[*]}; do
627
    # skip GCC files
628
    if [[ ! "$source" =~ .*/gcc.* ]]; then
629
      echo "$source" >> ${projectdir}/${module}.files
630
    fi
631
  done
632
  # generate a default project configuration file if it doesn't exist yet
633
  if [ ! -f ${projectdir}/${module}.config ]; then
634
    echo "// Add predefined macros for your project here. For example:" > ${projectdir}/${module}.config
635
    echo "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/${module}.config
636
  fi
637
  # generate a default .creator file if it doesn't exist yet
638
  if [ ! -f ${projectdir}/${module}.creator ]; then
639
    echo "[general]" > ${projectdir}/${module}.creator
640
  fi
641

    
642
  # go back to user directory
643
  cd $userdir
644

    
645
  # fill the output variables
646
  if [ ! -z "$outvar" ]; then
647
    eval $outvar="$projectdir"
648
  fi
649
  if [ ! -z "$gccoutvar" ]; then
650
    eval $gccoutvar="$gccincludedir"
651
  fi
652

    
653
  return 0
654
}
655

    
656
### create project files for all modules #######################################
657
# Create project files for all modules.
658
#
659
# usage:      createAllProjects <modules> [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
660
# arguments:  <modules>
661
#                 Array containing all modules available.
662
#             -p, --path <path>
663
#                 Path where to create the project files.
664
#             --gcc=<path>
665
#                 Path to the GCC include directory.
666
#             -o, --out <var>
667
#                 Variable to store the path to.
668
#             --gccout=<var>
669
#                 Variable to store the path to the GCC include directory to.
670
#                 If this optional arguments is absent, ths function will ask for user input.
671
# return:     0
672
#                 No error or warning occurred.
673
#             -1
674
#                 No modules available.
675
#
676
function createAllProjects {
677
  local modules=("${!1}")
678
  local projectdir=""
679
  local gccincludedir=""
680
  local outvar=""
681
  local gccoutvar=""
682

    
683
  # parse arguments
684
  local otherargs=()
685
  while [ $# -gt 0 ]; do
686
    if ( parseIsOption $1 ); then
687
      case "$1" in
688
        -p=*|--path=*)
689
          projectdir=$(realpath "${1#*=}"); shift 1;;
690
        -p|--path)
691
          projectdir=$(realpath "$2"); shift 2;;
692
        --gcc=*)
693
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
694
        --gcc)
695
          gccincludedir=$(realpath "$2"); shift 2;;
696
        -o=*|--out=*)
697
          outvar=${1#*=}; shift 1;;
698
        -o|--out)
699
          outvar=$2; shift 2;;
700
        --gccout=*)
701
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
702
        --gccout)
703
          gccoutvar=$(realpath "$2"); shift 2;;
704
        *)
705
          printError "invalid option: $1\n"; shift 1;;
706
      esac
707
    else
708
      otherargs+=("$1")
709
      shift 1
710
    fi
711
  done
712

    
713
  # sanity check for the modules variable
714
  if [ -z "${modules[*]}" ]; then
715
    printError "no modules available\n"
716
    return -1
717
  fi
718

    
719
  # read absolute project directory if required
720
  if [ -z "$projectdir" ]; then
721
    getProjectDir projectdir
722
  fi
723

    
724
  # print message
725
  printf "\n"
726
  printInfo "generating QtCreator project files for all modules...\n"
727

    
728
  # retrieve absolute GCC include path
729
  if [ -z "$gccincludedir" ]; then
730
    retrieveGccIncludeDir gccincludedir
731
  fi
732

    
733
  # iterate over all modules
734
  local retval=1
735
  for module in ${modules[@]}; do
736
    if [ $retval != 0 ]; then
737
      printf "\n"
738
    fi
739
    createModuleProject modules[@] --module="$module" --path="$projectdir" --gcc="$gccincludedir"
740
    retval=$?
741
  done
742

    
743
  return 0
744
}
745

    
746
### delete project files #######################################################
747
# Deletes all project files and optionally .user files, too.
748
#
749
# usage:      deleteProjects [-p|--path=<path>] [-m|--module=<module>] [-o|--out=<var>] [-w|-wipe]
750
# arguments:  -p, --path <path>
751
#                 Path where to delete the project files.
752
#             -m, --module <module>
753
#                 Module name for which the project files shall be deleted.
754
#             -o, --out <var>
755
#                 Variable to store the path to.
756
#             -w, --wipe
757
#                 Delete .user files as well.
758
# return:
759
#  -  0: no error
760
#
761
function deleteProjects {
762
  local modulename=""
763
  local projectdir=""
764
  local outvar=""
765
  local wipe=false
766
  local files=""
767

    
768
  # parse arguments
769
  local otherargs=()
770
  while [ $# -gt 0 ]; do
771
    if ( parseIsOption $1 ); then
772
      case "$1" in
773
        -p=*|--path=*)
774
          projectdir=$(realpath "${1#*=}"); shift 1;;
775
        -p|--path)
776
          projectdir=$(realpath "$2"); shift 2;;
777
        -m=*|--module=*)
778
          modulename="${1#*=}"; shift 1;;
779
        -m|--module)
780
          modulename="${2}"; shift 2;;
781
        -o=*|--out=*)
782
          outvar=${1#*=}; shift 1;;
783
        -o|--out)
784
          outvar=$2; shift 2;;
785
        -w|--wipe)
786
          wipe=true; shift 1;;
787
        *)
788
          printError "invalid option: $1\n"; shift 1;;
789
      esac
790
    else
791
      otherargs+=("$1")
792
      shift 1
793
    fi
794
  done
795

    
796
  # read absolute project directory if required
797
  if [ -z "$projectdir" ]; then
798
    getProjectDir projectdir
799
  fi
800

    
801
  # list all files to be deleted
802
  if [ -z "$modulename" ]; then
803
    if [ $wipe != true ]; then
804
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator)$")
805
    else
806
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator|creator\.user)$")
807
    fi
808
  else
809
    if [ $wipe != true ]; then
810
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.${modulename}\.(includes|files|config|creator)$")
811
    else
812
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.${modulename}\.(includes|files|config|creator|creator\.user)$")
813
    fi
814
  fi
815
  if [ ! -z "$files" ]; then
816
    printInfo "Deleting the following files:\n"
817
    while read line; do
818
      printInfo "\t$(basename ${line})\n"
819
      rm ${line} 2>&1 | tee -a $LOG_FILE
820
    done <<< "${files}"
821
  else
822
    printInfo "No project files found\n"
823
  fi
824

    
825
  # store the path to the output variable, if required
826
  if [ ! -z "$outvar" ]; then
827
    eval $outvar="$projectdir"
828
  fi
829

    
830
  return 0
831
}
832

    
833
### main function of this script ###############################################
834
# Creates, deletes and wipes QtCreator project files for the three AMiRo base modules.
835
#
836
# usage:      see function printHelp
837
# arguments:  see function printHelp
838
# return:     0
839
#                 No error or warning ocurred.
840
#
841
function main {
842
# print welcome/info text if not suppressed
843
  if [[ $@ != *"--noinfo"* ]]; then
844
    printWelcomeText
845
  else
846
    printf "######################################################################\n"
847
  fi
848
  printf "\n"
849

    
850
  # if --help or -h was specified, print the help text and exit
851
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
852
    printHelp
853
    printf "\n"
854
    quitScript
855
  fi
856

    
857
  # set log file if specified
858
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
859
    # get the parameter (file name)
860
    local cmdidx=1
861
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
862
      cmdidx=$[cmdidx + 1]
863
    done
864
    local cmd="${!cmdidx}"
865
    local logfile=""
866
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
867
      logfile=${cmd#*=}
868
    else
869
      local filenameidx=$((cmdidx + 1))
870
      logfile="${!filenameidx}"
871
    fi
872
    # optionally force silent appending
873
    if [[ "$cmd" = "--LOG"* ]]; then
874
      setLogFile --option=c --quiet "$logfile" LOG_FILE
875
    else
876
      setLogFile "$logfile" LOG_FILE
877
      printf "\n"
878
    fi
879
  fi
880
  # log script name
881
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
882

    
883
  # detect available modules and inform user
884
  local modules=()
885
  detectModules modules
886
  case "${#modules[@]}" in
887
    0)
888
      printInfo "no module has been detected\n";;
889
    1)
890
      printInfo "1 module has been detected:\n";;
891
    *)
892
      printInfo "${#modules[@]} modules have been detected:\n"
893
  esac
894
  for (( idx=0; idx<${#modules[@]}; ++idx )); do
895
    printInfo "  - ${modules[$idx]}\n"
896
  done
897
  printf "\n"
898

    
899
  # parse arguments
900
  local otherargs=()
901
  while [ $# -gt 0 ]; do
902
    if ( parseIsOption $1 ); then
903
      case "$1" in
904
        -h|--help) # already handled; ignore
905
          shift 1;;
906
        -m=*|--module=*)
907
          createModuleProject modules[@] --module="${1#*=}"; printf "\n"; shift 1;;
908
        -m*|--module*)
909
           createModuleProject modules[@] --module="${2}"; printf "\n"; shift 2;;
910
        -a|--all)
911
           createAllProjects modules[@]; shift 1;;
912
        -c|--clean)
913
          deleteProjects; printf "\n"; shift 1;;
914
        -w|--wipe)
915
          deleteProjects --wipe; printf "\n"; shift 1;;
916
        -q|--quit)
917
          quitScript; shift 1;;
918
        --log=*|--LOG=*) # already handled; ignore
919
          shift 1;;
920
        --log|--LOG) # already handled; ignore
921
          shift 2;;
922
        --noinfo) # already handled; ignore
923
          shift 1;;
924
        *)
925
          printError "invalid option: $1\n"; shift 1;;
926
      esac
927
    else
928
      otherargs+=("$1")
929
      shift 1
930
    fi
931
  done
932

    
933
  # interactive menu
934
  while ( true ); do
935
    # main menu info prompt and selection
936
    printInfo "QtCreator setup main menu\n"
937
    printf "Please select one of the following actions:\n"
938
    printf "  [M] - create a project for a single module\n"
939
    printf "  [A] - create a project for all modules\n"
940
    printf "  [C] - clean all project files\n"
941
    printf "  [W] - wipe all project and .user files\n"
942
    printf "  [Q] - quit this setup\n"
943
    local userinput=""
944
    readUserInput "MmAaCcWwQq" userinput
945
    printf "\n"
946

    
947
    # evaluate user selection
948
    case "$userinput" in
949
      M|m)
950
        createModuleProject modules[@]; printf "\n";;
951
      A|a)
952
        createAllProjects modules[@]; printf "\n";;
953
      C|c)
954
        deleteProjects; printf "\n";;
955
      W|w)
956
        deleteProjects --wipe; printf "\n";;
957
      Q|q)
958
        quitScript;;
959
      *) # sanity check (exit with error)
960
        printError "unexpected argument: $userinput\n";;
961
    esac
962
  done
963

    
964
  exit 0
965
}
966

    
967
################################################################################
968
# SCRIPT ENTRY POINT                                                           #
969
################################################################################
970

    
971
main "$@"
972