Statistics
| Branch: | Tag: | Revision:

amiro-os / tools / ide / QtCreator / QtCreatorSetup.sh @ 1c1b3372

History | View | Annotate | Download (33.094 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
################################################################################
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 84f0ce9e Thomas Schöpping
  printf "# Copyright (c) 2016..2019  Thomas Schöpping                         #\n"
285 e545e620 Thomas Schöpping
  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 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"
307 e545e620 Thomas Schöpping
  printf "\n"
308
  printf "options:  -h, --help\n"
309
  printf "              Print this help text.\n"
310 959f302e Thomas Schöpping
  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 e545e620 Thomas Schöpping
  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 680d05e5 Thomas Schöpping
      binpath=$(realpath $(dirname $binpath)/$(readlink $binpath))
363 e545e620 Thomas Schöpping
    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 959f302e Thomas Schöpping
### detect available modules ###################################################
374
# Detect all avalable modules supported by AMiRo-OS.
375 e545e620 Thomas Schöpping
#
376 959f302e Thomas Schöpping
# 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 e545e620 Thomas Schöpping
385 959f302e Thomas Schöpping
  # detect all available modules (via directories)
386
  for dir in $(ls -d ${modulesdir}/*/); do
387
    modules_detected[${#modules_detected[@]}]=$(basename $dir)
388 e545e620 Thomas Schöpping
  done
389
390 959f302e Thomas Schöpping
  # set the output variable
391
  eval "$1=(${modules_detected[*]})"
392 e545e620 Thomas Schöpping
}
393
394 959f302e Thomas Schöpping
### create project files for a single module ###################################
395
# Create project files for a single module.
396 e545e620 Thomas Schöpping
#
397 959f302e Thomas Schöpping
# 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 e545e620 Thomas Schöpping
#                 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 959f302e Thomas Schöpping
#                 If this optional arguments is absent, ths function will ask for user input.
411 e545e620 Thomas Schöpping
# return:     0
412
#                 No error or warning occurred.
413 959f302e Thomas Schöpping
#             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 e545e620 Thomas Schöpping
#
422 959f302e Thomas Schöpping
function createModuleProject {
423 e545e620 Thomas Schöpping
  local userdir=$(pwd)
424 959f302e Thomas Schöpping
  local modulesdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../modules)
425
  local modules=("${!1}")
426
  local module=""
427
  local moduleidx=""
428 e545e620 Thomas Schöpping
  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 959f302e Thomas Schöpping
        -m=*|--module=*)
439
          module="${1#*=}"; shift 1;;
440
        -m|--module)
441
          module="$2"; shift 2;;
442 e545e620 Thomas Schöpping
        -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 959f302e Thomas Schöpping
  # 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 e545e620 Thomas Schöpping
513
  # read absolute project directory if required
514
  if [ -z "$projectdir" ]; then
515
    getProjectDir projectdir
516 959f302e Thomas Schöpping
    printf "\n"
517
  fi
518
519
  # check for existing project files
520 23230307 Thomas Schöpping
  local projectfiles="$(find ${projectdir} -maxdepth 1 -type f | grep -E "${module}\.(includes|files|config|creator)$")"
521
  IFS=$'\n'; projectfiles=($projectfiles); unset IFS
522
  if [ ! -z "${projectfiles[*]}" ]; then
523 959f302e Thomas Schöpping
    printWarning "The following files will be overwritten:\n"
524 23230307 Thomas Schöpping
    for pfile in ${projectfiles[@]}; do
525
      printWarning "\t$(basename $pfile)\n"
526
    done
527 959f302e Thomas Schöpping
    local userinput=""
528
    printInfo "Continue and overwrite? [y/n]\n"
529
    readUserInput "YyNn" userinput
530
    case "$userinput" in
531
      Y|y)
532
        ;;
533
      N|n)
534
        printWarning "Project generation for ${module} module aborted by user\n"
535
        return 1
536
        ;;
537
      *)
538
        printError "unexpected input: ${userinput}\n"; return -999;;
539
    esac
540
    printf "\n"
541 e545e620 Thomas Schöpping
  fi
542
543 959f302e Thomas Schöpping
  # print message
544
  printInfo "generating QtCreator project files for the $module module...\n"
545
546
  # retrieve absolute GCC include path
547 e545e620 Thomas Schöpping
  if [ -z "$gccincludedir" ]; then
548
    retrieveGccIncludeDir gccincludedir
549
  fi
550
551 959f302e Thomas Schöpping
  # change to project directory
552
  cd "$projectdir"
553 e545e620 Thomas Schöpping
554 959f302e Thomas Schöpping
  # run make, but only run the GCC preprocessor and produce no binaries
555 9b0c922c Thomas Schöpping
  local amiroosrootdir=$(realpath $(dirname ${BASH_SOURCE[0]})/../../..)
556 959f302e Thomas Schöpping
  local sourcefiles=()
557
  local sourcefile=""
558 c28a90e7 Thomas Schöpping
  local parse_state="WAIT_FOR_INCLUDE_OR_COMPILE"
559 959f302e Thomas Schöpping
  # capture all output from make and GCC and append the return value of make as last line
560
  printInfo "processing project (this may take a while)...\n"
561 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 $?)
562 959f302e Thomas Schöpping
  # check whether the make call was successfull
563
  if [[ $(echo "${rawout}" | tail -n 1) != "0" ]]; then
564
    printError "executing 'make' in module directory failed\n"
565
    cd "$userdir"
566
    return -3
567 4df853e0 Thomas Schöpping
  else
568
    # cleanup
569
    make --directory ${amiroosrootdir}/modules/${module} clean &>/dev/null
570 959f302e Thomas Schöpping
  fi
571
  # extract file names from raw output
572 c28a90e7 Thomas Schöpping
  IFS=$'\n'; rawout=($rawout); unset IFS
573
  for line in "${rawout[@]}"; do
574
    case $parse_state in
575
      WAIT_FOR_INCLUDE_OR_COMPILE)
576 959f302e Thomas Schöpping
        # lines stating included files look like:
577
        # ... <../relative/path/to/file>
578
        if [[ "$line" =~ ^\.+[[:blank:]].+\..+$ ]]; then
579
          sourcefile=${line##* }
580
          if [[ ! "$sourcefile" =~ ^/ ]]; then
581
            sourcefile=$(realpath ${amiroosrootdir}/modules/${module}/${sourcefile})
582
          fi
583
          sourcefiles[${#sourcefiles[@]}]="$sourcefile"
584
        # whenever the next source file is processed, a message appears like:
585
        # Compining <filnemame>
586
        elif [[ "$line" =~ ^Compiling[[:blank:]].+\..+$ ]]; then
587
          printf "."
588
          sourcefile=${line##* }
589 330feaa3 Thomas Schöpping
          parse_state="WAIT_FOR_COMPILERCALL"
590 959f302e Thomas Schöpping
        fi;;
591
      WAIT_FOR_COMPILERCALL)
592
        # wait for the actual call of the compiler to retrieve the full path to the source file
593
        if [[ "$line" == *${sourcefile}* ]]; then
594
          line="${line%%${sourcefile}*}${sourcefile}"
595
          sourcefile=${line##* }
596
          sourcefile=$(realpath ${amiroosrootdir}/modules/${module}/${line##* })
597
          sourcefiles[${#sourcefiles[@]}]="$sourcefile"
598 330feaa3 Thomas Schöpping
          parse_state="WAIT_FOR_INCLUDE_OR_COMPILE"
599 959f302e Thomas Schöpping
        fi;;
600
    esac
601 c28a90e7 Thomas Schöpping
  done
602 959f302e Thomas Schöpping
  unset rawout
603
  printf "\n"
604
  # sort and remove duplicates
605
  IFS=$'\n'; sourcefiles=($(sort --unique <<< "${sourcefiles[*]}")); unset IFS
606
607
  # extract include paths
608
  local includes=()
609
  for source in ${sourcefiles[*]}; do
610
    includes[${#includes[@]}]="$(dirname ${source})"
611
  done
612
  # sort and remove duplicates
613
  IFS=$'\n'; includes=($(sort --unique <<< "${includes[*]}")); unset IFS
614
615
  # generate the .files file, containing all source files
616
  echo "" > ${projectdir}/${module}.includes
617
  for inc in ${includes[*]}; do
618
    echo "$inc" >> ${projectdir}/${module}.includes
619
  done
620
  # generate the .incldues file, containing all include paths
621
  echo "" > ${projectdir}/${module}.files
622
  for source in ${sourcefiles[*]}; do
623
    # skip GCC files
624
    if [[ ! "$source" =~ .*/gcc.* ]]; then
625
      echo "$source" >> ${projectdir}/${module}.files
626
    fi
627 e545e620 Thomas Schöpping
  done
628 959f302e Thomas Schöpping
  # generate a default project configuration file if it doesn't exist yet
629
  if [ ! -f ${projectdir}/${module}.config ]; then
630
    echo "// Add predefined macros for your project here. For example:" > ${projectdir}/${module}.config
631
    echo "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/${module}.config
632 e545e620 Thomas Schöpping
  fi
633 959f302e Thomas Schöpping
  # generate a default .creator file if it doesn't exist yet
634
  if [ ! -f ${projectdir}/${module}.creator ]; then
635
    echo "[general]" > ${projectdir}/${module}.creator
636 e545e620 Thomas Schöpping
  fi
637
638
  # go back to user directory
639
  cd $userdir
640
641
  # fill the output variables
642
  if [ ! -z "$outvar" ]; then
643
    eval $outvar="$projectdir"
644
  fi
645
  if [ ! -z "$gccoutvar" ]; then
646
    eval $gccoutvar="$gccincludedir"
647
  fi
648
649
  return 0
650
}
651
652 959f302e Thomas Schöpping
### create project files for all modules #######################################
653
# Create project files for all modules.
654 e545e620 Thomas Schöpping
#
655 959f302e Thomas Schöpping
# usage:      createAllProjects <modules> [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
656
# arguments:  <modules>
657
#                 Array containing all modules available.
658
#             -p, --path <path>
659 e545e620 Thomas Schöpping
#                 Path where to create the project files.
660
#             --gcc=<path>
661
#                 Path to the GCC include directory.
662
#             -o, --out <var>
663
#                 Variable to store the path to.
664
#             --gccout=<var>
665
#                 Variable to store the path to the GCC include directory to.
666 959f302e Thomas Schöpping
#                 If this optional arguments is absent, ths function will ask for user input.
667 e545e620 Thomas Schöpping
# return:     0
668
#                 No error or warning occurred.
669 23230307 Thomas Schöpping
#             1
670
#                 Aborted by user.
671 959f302e Thomas Schöpping
#             -1
672
#                 No modules available.
673 e545e620 Thomas Schöpping
#
674 959f302e Thomas Schöpping
function createAllProjects {
675
  local modules=("${!1}")
676 e545e620 Thomas Schöpping
  local projectdir=""
677
  local gccincludedir=""
678
  local outvar=""
679
  local gccoutvar=""
680
681
  # parse arguments
682
  local otherargs=()
683
  while [ $# -gt 0 ]; do
684
    if ( parseIsOption $1 ); then
685
      case "$1" in
686
        -p=*|--path=*)
687
          projectdir=$(realpath "${1#*=}"); shift 1;;
688
        -p|--path)
689
          projectdir=$(realpath "$2"); shift 2;;
690
        --gcc=*)
691
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
692
        --gcc)
693
          gccincludedir=$(realpath "$2"); shift 2;;
694
        -o=*|--out=*)
695
          outvar=${1#*=}; shift 1;;
696
        -o|--out)
697
          outvar=$2; shift 2;;
698
        --gccout=*)
699
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
700
        --gccout)
701
          gccoutvar=$(realpath "$2"); shift 2;;
702
        *)
703
          printError "invalid option: $1\n"; shift 1;;
704
      esac
705
    else
706
      otherargs+=("$1")
707
      shift 1
708
    fi
709
  done
710
711 959f302e Thomas Schöpping
  # sanity check for the modules variable
712
  if [ -z "${modules[*]}" ]; then
713
    printError "no modules available\n"
714
    return -1
715
  fi
716 e545e620 Thomas Schöpping
717
  # read absolute project directory if required
718
  if [ -z "$projectdir" ]; then
719
    getProjectDir projectdir
720
  fi
721
722 23230307 Thomas Schöpping
  # check for existing project files
723
  local projectfiles=()
724
  for module in ${modules[@]}; do
725
    local pfiles="$(find ${projectdir} -maxdepth 1 -type f | grep -E "${module}\.(includes|files|config|creator)$")"
726
    IFS=$'\n'; pfiles=($pfiles); unset IFS
727
    projectfiles=( ${projectfiles[*]} ${pfiles[*]} )
728
  done
729
  if [ ! -z "${projectfiles[*]}" ]; then
730
    printWarning "The following files will be removed:\n"
731
    for pfile in ${projectfiles[@]}; do
732
      printWarning "\t$(basename $pfile)\n"
733
    done
734
    local userinput=""
735
    printInfo "Continue and overwrite? [y/n]\n"
736
    readUserInput "YyNn" userinput
737
    case "${userinput}" in
738
      Y|y)
739
        for pfile in ${projectfiles[*]}; do
740
          rm "$pfile"
741
        done
742
        ;;
743
      N|n)
744
        printWarning "Project generation aborted by user\n"
745
        return 1
746
        ;;
747
      *)
748
        printError "unexpected input: ${userinput}\n"
749
        return 999
750
        ;;
751
    esac
752
  fi
753
754 959f302e Thomas Schöpping
  # print message
755
  printf "\n"
756
  printInfo "generating QtCreator project files for all modules...\n"
757
758
  # retrieve absolute GCC include path
759 e545e620 Thomas Schöpping
  if [ -z "$gccincludedir" ]; then
760
    retrieveGccIncludeDir gccincludedir
761
  fi
762
763 959f302e Thomas Schöpping
  # iterate over all modules
764
  local retval=1
765
  for module in ${modules[@]}; do
766
    if [ $retval != 0 ]; then
767
      printf "\n"
768
    fi
769
    createModuleProject modules[@] --module="$module" --path="$projectdir" --gcc="$gccincludedir"
770
    retval=$?
771 e545e620 Thomas Schöpping
  done
772
773
  return 0
774
}
775
776 959f302e Thomas Schöpping
### delete project files #######################################################
777
# Deletes all project files and optionally .user files, too.
778 e545e620 Thomas Schöpping
#
779 959f302e Thomas Schöpping
# usage:      deleteProjects [-p|--path=<path>] [-m|--module=<module>] [-o|--out=<var>] [-w|-wipe]
780 e545e620 Thomas Schöpping
# arguments:  -p, --path <path>
781 959f302e Thomas Schöpping
#                 Path where to delete the project files.
782
#             -m, --module <module>
783
#                 Module name for which the project files shall be deleted.
784 e545e620 Thomas Schöpping
#             -o, --out <var>
785
#                 Variable to store the path to.
786 959f302e Thomas Schöpping
#             -w, --wipe
787
#                 Delete .user files as well.
788
# return:
789
#  -  0: no error
790 e545e620 Thomas Schöpping
#
791 959f302e Thomas Schöpping
function deleteProjects {
792
  local modulename=""
793 e545e620 Thomas Schöpping
  local projectdir=""
794
  local outvar=""
795 959f302e Thomas Schöpping
  local wipe=false
796
  local files=""
797 e545e620 Thomas Schöpping
798
  # parse arguments
799
  local otherargs=()
800
  while [ $# -gt 0 ]; do
801
    if ( parseIsOption $1 ); then
802
      case "$1" in
803
        -p=*|--path=*)
804
          projectdir=$(realpath "${1#*=}"); shift 1;;
805
        -p|--path)
806
          projectdir=$(realpath "$2"); shift 2;;
807 959f302e Thomas Schöpping
        -m=*|--module=*)
808
          modulename="${1#*=}"; shift 1;;
809
        -m|--module)
810
          modulename="${2}"; shift 2;;
811 e545e620 Thomas Schöpping
        -o=*|--out=*)
812
          outvar=${1#*=}; shift 1;;
813
        -o|--out)
814
          outvar=$2; shift 2;;
815 959f302e Thomas Schöpping
        -w|--wipe)
816
          wipe=true; shift 1;;
817 e545e620 Thomas Schöpping
        *)
818
          printError "invalid option: $1\n"; shift 1;;
819
      esac
820
    else
821
      otherargs+=("$1")
822
      shift 1
823
    fi
824
  done
825
826
  # read absolute project directory if required
827
  if [ -z "$projectdir" ]; then
828
    getProjectDir projectdir
829
  fi
830
831 959f302e Thomas Schöpping
  # list all files to be deleted
832
  if [ -z "$modulename" ]; then
833
    if [ $wipe != true ]; then
834
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator)$")
835
    else
836
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.+\.(includes|files|config|creator|creator\.user)$")
837
    fi
838
  else
839
    if [ $wipe != true ]; then
840
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.${modulename}\.(includes|files|config|creator)$")
841
    else
842
      files=$(find "${projectdir}" -maxdepth 1 -type f | grep -E "^.${modulename}\.(includes|files|config|creator|creator\.user)$")
843
    fi
844 e545e620 Thomas Schöpping
  fi
845 959f302e Thomas Schöpping
  if [ ! -z "$files" ]; then
846
    printInfo "Deleting the following files:\n"
847
    while read line; do
848
      printInfo "\t$(basename ${line})\n"
849
      rm ${line} 2>&1 | tee -a $LOG_FILE
850
    done <<< "${files}"
851
  else
852
    printInfo "No project files found\n"
853 e545e620 Thomas Schöpping
  fi
854
855 959f302e Thomas Schöpping
  # store the path to the output variable, if required
856 e545e620 Thomas Schöpping
  if [ ! -z "$outvar" ]; then
857
    eval $outvar="$projectdir"
858
  fi
859
860
  return 0
861
}
862
863
### main function of this script ###############################################
864
# Creates, deletes and wipes QtCreator project files for the three AMiRo base modules.
865
#
866
# usage:      see function printHelp
867
# arguments:  see function printHelp
868
# return:     0
869
#                 No error or warning ocurred.
870
#
871
function main {
872
# print welcome/info text if not suppressed
873
  if [[ $@ != *"--noinfo"* ]]; then
874
    printWelcomeText
875
  else
876
    printf "######################################################################\n"
877
  fi
878
  printf "\n"
879
880
  # if --help or -h was specified, print the help text and exit
881
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
882
    printHelp
883
    printf "\n"
884
    quitScript
885
  fi
886
887
  # set log file if specified
888
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
889
    # get the parameter (file name)
890
    local cmdidx=1
891
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
892
      cmdidx=$[cmdidx + 1]
893
    done
894
    local cmd="${!cmdidx}"
895
    local logfile=""
896
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
897
      logfile=${cmd#*=}
898
    else
899
      local filenameidx=$((cmdidx + 1))
900
      logfile="${!filenameidx}"
901
    fi
902
    # optionally force silent appending
903
    if [[ "$cmd" = "--LOG"* ]]; then
904
      setLogFile --option=c --quiet "$logfile" LOG_FILE
905
    else
906
      setLogFile "$logfile" LOG_FILE
907
      printf "\n"
908
    fi
909
  fi
910
  # log script name
911
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
912
913 959f302e Thomas Schöpping
  # detect available modules and inform user
914
  local modules=()
915
  detectModules modules
916
  case "${#modules[@]}" in
917
    0)
918
      printInfo "no module has been detected\n";;
919
    1)
920
      printInfo "1 module has been detected:\n";;
921
    *)
922
      printInfo "${#modules[@]} modules have been detected:\n"
923
  esac
924
  for (( idx=0; idx<${#modules[@]}; ++idx )); do
925
    printInfo "  - ${modules[$idx]}\n"
926
  done
927
  printf "\n"
928
929 e545e620 Thomas Schöpping
  # parse arguments
930
  local otherargs=()
931
  while [ $# -gt 0 ]; do
932
    if ( parseIsOption $1 ); then
933
      case "$1" in
934
        -h|--help) # already handled; ignore
935
          shift 1;;
936 959f302e Thomas Schöpping
        -m=*|--module=*)
937
          createModuleProject modules[@] --module="${1#*=}"; printf "\n"; shift 1;;
938
        -m*|--module*)
939
           createModuleProject modules[@] --module="${2}"; printf "\n"; shift 2;;
940
        -a|--all)
941
           createAllProjects modules[@]; shift 1;;
942 e545e620 Thomas Schöpping
        -c|--clean)
943
          deleteProjects; printf "\n"; shift 1;;
944
        -w|--wipe)
945
          deleteProjects --wipe; printf "\n"; shift 1;;
946
        -q|--quit)
947
          quitScript; shift 1;;
948
        --log=*|--LOG=*) # already handled; ignore
949
          shift 1;;
950
        --log|--LOG) # already handled; ignore
951
          shift 2;;
952
        --noinfo) # already handled; ignore
953
          shift 1;;
954
        *)
955
          printError "invalid option: $1\n"; shift 1;;
956
      esac
957
    else
958
      otherargs+=("$1")
959
      shift 1
960
    fi
961
  done
962
963
  # interactive menu
964
  while ( true ); do
965
    # main menu info prompt and selection
966
    printInfo "QtCreator setup main menu\n"
967
    printf "Please select one of the following actions:\n"
968 959f302e Thomas Schöpping
    printf "  [M] - create a project for a single module\n"
969 e545e620 Thomas Schöpping
    printf "  [A] - create a project for all modules\n"
970 959f302e Thomas Schöpping
    printf "  [C] - clean all project files\n"
971
    printf "  [W] - wipe all project and .user files\n"
972 e545e620 Thomas Schöpping
    printf "  [Q] - quit this setup\n"
973
    local userinput=""
974 959f302e Thomas Schöpping
    readUserInput "MmAaCcWwQq" userinput
975 e545e620 Thomas Schöpping
    printf "\n"
976
977
    # evaluate user selection
978
    case "$userinput" in
979 959f302e Thomas Schöpping
      M|m)
980
        createModuleProject modules[@]; printf "\n";;
981
      A|a)
982
        createAllProjects modules[@]; printf "\n";;
983 e545e620 Thomas Schöpping
      C|c)
984
        deleteProjects; printf "\n";;
985
      W|w)
986
        deleteProjects --wipe; printf "\n";;
987
      Q|q)
988
        quitScript;;
989
      *) # sanity check (exit with error)
990
        printError "unexpected argument: $userinput\n";;
991
    esac
992
  done
993
994
  exit 0
995
}
996
997
################################################################################
998
# SCRIPT ENTRY POINT                                                           #
999
################################################################################
1000
1001
main "$@"