Statistics
| Branch: | Tag: | Revision:

amiro-os / tools / ide / QtCreator / QtCreatorSetup.sh @ 959f302e

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