Statistics
| Branch: | Tag: | Revision:

amiro-os / tools / ide / QtCreator / QtCreatorSetup.sh @ 39e164a2

History | View | Annotate | Download (34.375 KB)

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