Statistics
| Branch: | Tag: | Revision:

amiro-os / tools / ide / QtCreator / QtCreatorSetup.sh @ 330feaa3

History | View | Annotate | Download (32.999 KB)

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