Statistics
| Branch: | Tag: | Revision:

amiro-blt / ide / QtCreator / QtCreatorSetup.sh @ 0a42f078

History | View | Annotate | Download (31.6 KB)

1
################################################################################
2
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini    #
3
# Robot (AMiRo) platform.                                                      #
4
# Copyright (C) 2016..2017  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
  printLog "exiting $(realpath ${BASH_SOURCE[0]})\n"
110
  printf "######################################################################\n"
111
  exit 0
112
}
113

    
114
### read a user input ##########################################################
115
# Reads a single character user input from a set up <options> and stores it in
116
# a given <return> variable.
117
#
118
# usage:      readUserInput <options> <return>
119
# arguments:  <options>
120
#                 String definiing the set of valid characters.
121
#                 If the string is empty, the user can input any character.
122
#             <return>
123
#                 Variable to store the selected character to.
124
# return:     n/a
125
#
126
function readUserInput {
127
  local input=""
128
  # read user input
129
  while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do
130
    read -p "your selection: " -n 1 -e input
131
    if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then
132
      printWarning "[$input] is no valid action\n"
133
    fi
134
  done
135
  printLog "[$input] has been selected\n"
136
  eval $2="$input"
137
}
138

    
139
### check whether argument is an option ########################################
140
# Checks a <string> whether it is an option.
141
# Options are defined to either start with '--' followed by any string, or
142
# to start with a single '-' followed by a single character, or
143
# to start with a single '-' followed by a single character, a '=' and any string.
144
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--'
145
#
146
# usage:      parseIsOption <string>
147
# arguments:  <string>
148
#                 A string to check whether it is an option.
149
# return:     0
150
#                 <string> is an option.
151
#             -1
152
#                 <string> is not an option.
153
#
154
function parseIsOption {
155
  if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then
156
    return 0
157
  else
158
    return -1
159
  fi
160
}
161

    
162
### set the log file ###########################################################
163
# Sets a specified <infile> as log file and checks whether it already exists.
164
# If so, the log may either be appended to the file, its content can be cleared,
165
# or no log is generated at all.
166
# The resulting path is stored in <outvar>.
167
#
168
# usage:      setLogFile [--option=<option>] [--quiet] <infile> <outvar>
169
# arguments:  --option=<option>
170
#                 Select what to do if <file> already exists.
171
#                 Possible values are 'a', 'r' and 'n'.
172
#                 - a: append
173
#                 - r: delete and restart
174
#                 - n: no log
175
#                 If no option is secified but <file> exists, an interactive selection is provided.
176
#             --quiet
177
#                 Suppress all messages.
178
#             <infile>
179
#                 Path of the wanted log file.
180
#             <outvar>
181
#                 Variable to store the path of the log file to.
182
# return:     0
183
#                 No error or warning occurred.
184
#             -1
185
#                 Error: invalid input
186
#
187
function setLogFile {
188
  local filepath=""
189
  local option=""
190
  local quiet=false
191

    
192
  # parse arguments
193
  local otherargs=()
194
  while [ $# -gt 0 ]; do
195
    if ( parseIsOption $1 ); then
196
      case "$1" in
197
        -o=*|--option=*)
198
          option=${1#*=}; shift 1;;
199
        -o*|--option*)
200
          option="$2"; shift 2;;
201
        -q|--quiet)
202
          quiet=true; shift 1;;
203
        *)
204
          printError "invalid option: $1\n"; shift 1;;
205
      esac
206
    else
207
      otherargs+=("$1")
208
      shift 1
209
    fi
210
  done
211
  filepath=$(realpath ${otherargs[0]})
212

    
213
  # if file already exists
214
  if [ -e $filepath ]; then
215
    # if no option was specified, ask what to do
216
    if [ -z "$option" ]; then
217
      printWarning "log file $filepath already esists\n"
218
      local userinput=""
219
      printf "Select what to do:\n"
220
      printf "  [A] - append log\n"
221
      printf "  [R] - restart log (delete existing file)\n"
222
      printf "  [N] - no log\n"
223
      readUserInput "AaRrNn" userinput
224
      option=${userinput,,}
225
    fi
226
    # evaluate option
227
    case "$option" in
228
      a)
229
        if [ $quiet = false ]; then
230
          printInfo "appending log to $filepath\n"
231
        fi
232
        printf "\n" >> $filepath
233
        printf "######################################################################\n" >> $filepath
234
        printf "\n" >> $filepath
235
        ;;
236
      r)
237
        echo -n "" > $filepath
238
        if [ $quiet = false ]; then
239
          printInfo "content of $filepath wiped\n"
240
        fi
241
        ;;
242
      n)
243
        if [ $quiet = false ]; then
244
          printInfo "no log file will be generated\n"
245
        fi
246
        filepath=""
247
        ;;
248
      *) # sanity check (return error)
249
        printError "unexpected argument: $option\n"; return -1;;
250
    esac
251
  else
252
    if [ $quiet = false ]; then
253
      printInfo "log file set to $filepath\n"
254
    fi
255
  fi
256

    
257
  eval ${otherargs[1]}="$filepath"
258

    
259
  return 0
260
}
261

    
262
################################################################################
263
# SPECIFIC FUNCTIONS                                                           #
264
################################################################################
265

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

    
293
### print help #################################################################
294
# Prints a help text to standard out.
295
#
296
# usage:      printHelp
297
# arguments:  n/a
298
# return:     n/a
299
#
300
function printHelp {
301
  printInfo "printing help text\n"
302
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [-c|--clean] [-w|--wipe] [--LightRing] [--PowerManagement] [--DiWheelDrive] [-a|--all] [-q|--quit] [--log=<file>]\n"
303
  printf "\n"
304
  printf "options:  -h, --help\n"
305
  printf "              Print this help text.\n"
306
  printf "          -c, --clean\n"
307
  printf "              Delete project files.\n"
308
  printf "          -w, --wipe\n"
309
  printf "              Delete project and .user files.\n"
310
  printf "          --LightRing\n"
311
  printf "              Create project for the LightRing module.\n"
312
  printf "          --PowerManagement\n"
313
  printf "              Create project for the PowerManagement module.\n"
314
  printf "          --DiWheelDrive\n"
315
  printf "              Create project for the DiWheelDrive module.\n"
316
  printf "          -a, --all\n"
317
  printf "              Create projects for all modules.\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 amirobltdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../Target/)
335
  local input=""
336
  read -p "Path where to create/delete project files: " -i $amirobltdir -e input
337
  printLog "user selected path $(realpath $input)\n"
338
  eval $1="$(realpath $input)"
339
}
340

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

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

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

    
369
    return 0
370
  fi
371
}
372

    
373
### delete project files #######################################################
374
# Deletes all project files and optionally .user files, too.
375
#
376
# usage:      deleteProjects [-p|--path=<path>] [-o|--out=<var>] [-w|-wipe]
377
# arguments:  -p, --path <path>
378
#                 Path where to delete the project files.
379
#             -o, --out <var>
380
#                 Variable to store the path to.
381
#             -w, --wipe
382
#                 Delete .user files as well.
383
# return:
384
#  -  0: no error
385
#  -  1: warning: function aborted by user
386
#  - -1: error: unexpected user input
387
function deleteProjects {
388
  local projectdir=""
389
  local outvar=""
390
  local wipe=false
391

    
392
  # parse arguments
393
  local otherargs=()
394
  while [ $# -gt 0 ]; do
395
    if ( parseIsOption $1 ); then
396
      case "$1" in
397
        -p=*|--path=*)
398
          projectdir=$(realpath "${1#*=}"); shift 1;;
399
        -p|--path)
400
          projectdir=$(realpath "$2"); shift 2;;
401
        -o=*|--out=*)
402
          outvar=${1#*=}; shift 1;;
403
        -o|--out)
404
          outvar=$2; shift 2;;
405
        -w|--wipe)
406
          wipe=true; shift 1;;
407
        *)
408
          printError "invalid option: $1\n"; shift 1;;
409
      esac
410
    else
411
      otherargs+=("$1")
412
      shift 1
413
    fi
414
  done
415

    
416
  # print message
417
  if [ $wipe != true ]; then
418
    printInfo "deleting all QtCreator project files (*.includes, *.files, *.config, and *.creator)\n"
419
  else
420
    printInfo "deleting all QtCreator project files (*.includes, *.files, *.config, *.creator, and *.user)\n"
421
  fi
422

    
423
  # read project directory if required
424
  if [ -z "$projectdir" ]; then
425
    getProjectDir projectdir
426
  fi
427

    
428
  # remove all project files
429
  rm ${projectdir}/LightRing.includes 2>&1 | tee -a $LOG_FILE
430
  rm ${projectdir}/PowerManagement.includes 2>&1 | tee -a $LOG_FILE
431
  rm ${projectdir}/DiWheelDrive.includes 2>&1 | tee -a $LOG_FILE
432

    
433
  rm ${projectdir}/LightRing.files 2>&1 | tee -a $LOG_FILE
434
  rm ${projectdir}/PowerManagement.files 2>&1 | tee -a $LOG_FILE
435
  rm ${projectdir}/DiWheelDrive.files 2>&1 | tee -a $LOG_FILE
436

    
437
  rm ${projectdir}/LightRing.config 2>&1 | tee -a $LOG_FILE
438
  rm ${projectdir}/PowerManagement.config 2>&1 | tee -a $LOG_FILE
439
  rm ${projectdir}/DiWheelDrive.config 2>&1 | tee -a $LOG_FILE
440

    
441
  rm ${projectdir}/LightRing.creator 2>&1 | tee -a $LOG_FILE
442
  rm ${projectdir}/PowerManagement.creator 2>&1 | tee -a $LOG_FILE
443
  rm ${projectdir}/DiWheelDrive.creator 2>&1 | tee -a $LOG_FILE
444

    
445
  if [ $wipe == true ]; then
446
    rm ${projectdir}/LightRing.user 2>&1 | tee -a $LOG_FILE
447
    rm ${projectdir}/PowerManagement.user 2>&1 | tee -a $LOG_FILE
448
    rm ${projectdir}/DiWheelDrive.user 2>&1 | tee -a $LOG_FILE
449
  fi
450

    
451
  # store the path to the output variable, if required
452
  if [ ! -z "$outvar" ]; then
453
    eval $outvar="$projectdir"
454
  fi
455

    
456
  return 0
457
}
458

    
459
### create LightRing project files #############################################
460
# Create project files for the LightRing module.
461
#
462
# usage:      createLightRingProject [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
463
# arguments:  -p, --path <path>
464
#                 Path where to create the project files.
465
#             --gcc=<path>
466
#                 Path to the GCC include directory.
467
#             -o, --out <var>
468
#                 Variable to store the path to.
469
#             --gccout=<var>
470
#                 Variable to store the path to the GCC include directory to.
471
# return:     0
472
#                 No error or warning occurred.
473
#
474
function createLightRingProject {
475
  local projectdir=""
476
  local gccincludedir=""
477
  local outvar=""
478
  local gccoutvar=""
479

    
480
  # parse arguments
481
  local otherargs=()
482
  while [ $# -gt 0 ]; do
483
    if ( parseIsOption $1 ); then
484
      case "$1" in
485
        -p=*|--path=*)
486
          projectdir=$(realpath "${1#*=}"); shift 1;;
487
        -p|--path)
488
          projectdir=$(realpath "$2"); shift 2;;
489
        --gcc=*)
490
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
491
        --gcc)
492
          gccincludedir=$(realpath "$2"); shift 2;;
493
        -o=*|--out=*)
494
          outvar=${1#*=}; shift 1;;
495
        -o|--out)
496
          outvar=$2; shift 2;;
497
        --gccout=*)
498
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
499
        --gccout)
500
          gccoutvar=$(realpath "$2"); shift 2;;
501
        *)
502
          printError "invalid option: $1\n"; shift 1;;
503
      esac
504
    else
505
      otherargs+=("$1")
506
      shift 1
507
    fi
508
  done
509

    
510
  # print message
511
  printInfo "creating QtCreator project files for the LightRing module...\n"
512

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

    
518
  # retrieve gcc-arm-none-eabi include dir
519
  if [ -z "$gccincludedir" ]; then
520
    retrieveGccIncludeDir gccincludedir
521
  fi
522

    
523
  # create project files
524
  # generate a file that contains all subdirectories as includes (but ignore hidden and documentation directories)
525
  find $gccincludedir -type d > ${projectdir}/LightRing.includes
526
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Source/) -type d | grep -v "ARMCM4_STM32" >> ${projectdir}/LightRing.includes
527
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Demo/ARMCM3_STM32F103_LightRing_GCC/Boot/) -type d | grep -v "uip\|fatfs\|ethernetlib\|cmd\|ide" >> ${projectdir}/LightRing.includes
528
  # generate a file that specifies all files
529
  echo -n "" > ${projectdir}/LightRing.files
530
  for path in `cat ${projectdir}/LightRing.includes`; do
531
    find $path -maxdepth 1 -type f \( ! -iname ".*" \) | grep -v "/arm-none-eabi/" | grep -E ".*(\.h|\.c|\.x)$" >> ${projectdir}/LightRing.files
532
  done
533
  # generate a default project configuration file if none exists so far
534
  if [ ! -f ${projectdir}/LightRing.config ]; then
535
    echo -e "// Add predefined macros for your project here. For example:" > ${projectdir}/LightRing.config
536
    echo -e "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/LightRing.config
537
    echo -e "" >> ${projectdir}/LightRing.config
538
  fi
539
  # generate a default .creator file if none exists so far
540
  if [ ! -f ${projectdir}/LightRing.creator ]; then
541
    echo -e "[general]" > ${projectdir}/LightRing.creator
542
    echo -e "" >> ${projectdir}/LightRing.creator
543
  fi
544

    
545
  # fill the output variables
546
  if [ ! -z "$outvar" ]; then
547
    eval $outvar="$projectdir"
548
  fi
549
  if [ ! -z "$gccoutvar" ]; then
550
    eval $gccoutvar="$gccincludedir"
551
  fi
552

    
553
  return 0
554
}
555

    
556
### create PowerManagement project files #######################################
557
# Create project files for the PowerManagement module.
558
#
559
# usage:      createPowerManagementProject [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
560
# arguments:  -p, --path <path>
561
#                 Path where to create the project files.
562
#             --gcc=<path>
563
#                 Path to the GCC include directory.
564
#             -o, --out <var>
565
#                 Variable to store the path to.
566
#             --gccout=<var>
567
#                 Variable to store the path to the GCC include directory to.
568
# return:     0
569
#                 No error or warning occurred.
570
#
571
function createPowerManagementProject {
572
  local projectdir=""
573
  local gccincludedir=""
574
  local outvar=""
575
  local gccoutvar=""
576

    
577
  # parse arguments
578
  local otherargs=()
579
  while [ $# -gt 0 ]; do
580
    if ( parseIsOption $1 ); then
581
      case "$1" in
582
        -p=*|--path=*)
583
          projectdir=$(realpath "${1#*=}"); shift 1;;
584
        -p|--path)
585
          projectdir=$(realpath "$2"); shift 2;;
586
        --gcc=*)
587
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
588
        --gcc)
589
          gccincludedir=$(realpath "$2"); shift 2;;
590
        -o=*|--out=*)
591
          outvar=${1#*=}; shift 1;;
592
        -o|--out)
593
          outvar=$2; shift 2;;
594
        --gccout=*)
595
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
596
        --gccout)
597
          gccoutvar=$(realpath "$2"); shift 2;;
598
        *)
599
          printError "invalid option: $1\n"; shift 1;;
600
      esac
601
    else
602
      otherargs+=("$1")
603
      shift 1
604
    fi
605
  done
606

    
607
  # print message
608
  printInfo "creating QtCreator project files for the PowerManagement module...\n"
609

    
610
  # read project directory if required
611
  if [ -z "$projectdir" ]; then
612
    getProjectDir projectdir
613
  fi
614

    
615
  # retrieve gcc-arm-none-eabi include dir
616
  if [ -z "$gccincludedir" ]; then
617
    retrieveGccIncludeDir gccincludedir
618
  fi
619

    
620
  # create project files
621
  # generate a file that contains all subdirectories as includes (but ignore hidden and documentation directories)
622
  find $gccincludedir -type d > ${projectdir}/PowerManagement.includes
623
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Source/) -type d | grep -v "ARMCM4_STM32" >> ${projectdir}/PowerManagement.includes
624
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Demo/ARMCM4_STM32F405_Power_Management_GCC/Boot/) -type d | grep -v "uip\|fatfs\|ethernetlib\|cmd\|ide" >> ${projectdir}/PowerManagement.includes
625
  # generate a file that specifies all files
626
  echo -n "" > ${projectdir}/PowerManagement.files
627
  for path in `cat ${projectdir}/PowerManagement.includes`; do
628
    find $path -maxdepth 1 -type f \( ! -iname ".*" \) | grep -v "/arm-none-eabi/" | grep -E ".*(\.h|\.c|\.x)$" >> ${projectdir}/PowerManagement.files
629
  done
630
  # generate a default project configuration file if none exists so far
631
  if [ ! -f ${projectdir}/PowerManagement.config ]; then
632
    echo -e "// Add predefined macros for your project here. For example:" > ${projectdir}/PowerManagement.config
633
    echo -e "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/PowerManagement.config
634
    echo -e "" >> ${projectdir}/PowerManagement.config
635
  fi
636
  # generate a default .creator file if none exists so far
637
  if [ ! -f ${projectdir}/PowerManagement.creator ]; then
638
    echo -e "[general]" > ${projectdir}/PowerManagement.creator
639
    echo -e "" >> ${projectdir}/PowerManagement.creator
640
  fi
641

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

    
650
  return 0
651
}
652

    
653
### create DiWheelDrive project files ##########################################
654
# Create project files for the DiWheelDrive module.
655
#
656
# usage:      createDiWheelDriveProject [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
657
# arguments:  -p, --path <path>
658
#                 Path where to create the project files.
659
#             --gcc=<path>
660
#                 Path to the GCC include directory.
661
#             -o, --out <var>
662
#                 Variable to store the path to.
663
#             --gccout=<var>
664
#                 Variable to store the path to the GCC include directory to.
665
# return:     0
666
#                 No error or warning occurred.
667
#
668
function createDiWheelDriveProject {
669
  local projectdir=""
670
  local gccincludedir=""
671
  local outvar=""
672
  local gccoutvar=""
673

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

    
704
  # print message
705
  printInfo "creating QtCreator project files for the DiWheelDrive module...\n"
706

    
707
  # read project directory if required
708
  if [ -z "$projectdir" ]; then
709
    getProjectDir projectdir
710
  fi
711

    
712
  # retrieve gcc-arm-none-eabi include dir
713
  if [ -z "$gccincludedir" ]; then
714
    retrieveGccIncludeDir gccincludedir
715
  fi
716

    
717
  # create project files
718
  # generate a file that contains all subdirectories as includes (but ignore hidden and documentation directories)
719
  find $gccincludedir -type d > ${projectdir}/DiWheelDrive.includes
720
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Source/) -type d | grep -v "ARMCM4_STM32" >> ${projectdir}/DiWheelDrive.includes
721
  find $(realpath $(dirname ${BASH_SOURCE[0]})/../../Target/Demo/ARMCM3_STM32F103_DiWheelDrive_GCC/Boot/) -type d | grep -v "uip\|fatfs\|ethernetlib\|cmd\|ide" >> ${projectdir}/DiWheelDrive.includes
722
  # generate a file that specifies all files
723
  echo -n "" > ${projectdir}/DiWheelDrive.files
724
  for path in `cat ${projectdir}/DiWheelDrive.includes`; do
725
    find $path -maxdepth 1 -type f \( ! -iname ".*" \) | grep -v "/arm-none-eabi/" | grep -E ".*(\.h|\.c|\.x)$" >> ${projectdir}/DiWheelDrive.files
726
  done
727
  # generate a default project configuration file if none exists so far
728
  if [ ! -f ${projectdir}/DiWheelDrive.config ]; then
729
    echo -e "// Add predefined macros for your project here. For example:" > ${projectdir}/DiWheelDrive.config
730
    echo -e "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/DiWheelDrive.config
731
    echo -e "" >> ${projectdir}/DiWheelDrive.config
732
  fi
733
  # generate a default .creator file if none exists so far
734
  if [ ! -f ${projectdir}/DiWheelDrive.creator ]; then
735
    echo -e "[general]" > ${projectdir}/DiWheelDrive.creator
736
    echo -e "" >> ${projectdir}/DiWheelDrive.creator
737
  fi
738

    
739
  # fill the output variables
740
  if [ ! -z "$outvar" ]; then
741
    eval $outvar="$projectdir"
742
  fi
743
  if [ ! -z "$gccoutvar" ]; then
744
    eval $gccoutvar="$gccincludedir"
745
  fi
746

    
747
  return 0
748
}
749

    
750
### create project files for al modules ########################################
751
# Create project files for all modules.
752
#
753
# usage:      createAllProjects
754
# arguments:  n/a
755
# return:     0
756
#                 No error or warning occurred.
757
#
758
function createAllProjects {
759
  # print message
760
  printInfo "creating QtCreator project files for the DiWheelDrive module...\n"
761

    
762
  # read project directory
763
  local projectdir=""
764
  getProjectDir projectdir
765
  printInfo "files will be created in $projectdir\n"
766

    
767
  # retrieve gcc-arm-none-eabi include dir
768
  retrieveGccIncludeDir gccincludedir
769

    
770
  # create projects
771
  createLightRingProject --path="$projectdir" --gcc="$gccincludedir"
772
  createPowerManagementProject --path="$projectdir" --gcc="$gccincludedir"
773
  createDiWheelDriveProject --path="$projectdir" --gcc="$gccincludedir"
774

    
775
  return 0
776
}
777

    
778
### main function of this script ###############################################
779
# Creates, deletes and wipes QtCreator project files for the three AMiRo base modules.
780
#
781
# usage:      see function printHelp
782
# arguments:  see function printHelp
783
# return:     0
784
#                 No error or warning ocurred.
785
#
786
function main {
787
# print welcome/info text if not suppressed
788
  if [[ $@ != *"--noinfo"* ]]; then
789
    printWelcomeText
790
  else
791
    printf "######################################################################\n"
792
  fi
793
  printf "\n"
794

    
795
  # set log file if specified
796
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
797
    # get the parameter (file name)
798
    local cmdidx=1
799
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
800
      cmdidx=$[cmdidx + 1]
801
    done
802
    local cmd="${!cmdidx}"
803
    local logfile=""
804
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
805
      logfile=${cmd#*=}
806
    else
807
      local filenameidx=$((cmdidx + 1))
808
      logfile="${!filenameidx}"
809
    fi
810
    # optionally force silent appending
811
    if [[ "$cmd" = "--LOG"* ]]; then
812
      setLogFile --option=a --quiet "$logfile" LOG_FILE
813
    else
814
      setLogFile "$logfile" LOG_FILE
815
      printf "\n"
816
    fi
817
  fi
818

    
819
  # log script name
820
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
821

    
822
  # if --help or -h was specified, print the help text and exit
823
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
824
    printHelp
825
    printf "\n"
826
    quitScript
827
  fi
828

    
829
  # parse arguments
830
  local otherargs=()
831
  while [ $# -gt 0 ]; do
832
    if ( parseIsOption $1 ); then
833
      case "$1" in
834
        -h|--help) # already handled; ignore
835
          shift 1;;
836
        -c|--clean)
837
          deleteProjects; printf "\n"; shift 1;;
838
        -w|--wipe)
839
          deleteProjects --wipe; printf "\n"; shift 1;;
840
        --LightRing)
841
          createLightRingProject; printf "\n"; shift 1;;
842
        --PowerManagement)
843
          createPowerManagementProject; printf "\n"; shift 1;;
844
        --DiWheelDrive)
845
          createDiWheelDriveProject; printf "\n"; shift 1;;
846
        -a|--all)
847
          createAllProjects; printf "\n"; shift 1;;
848
        -q|--quit)
849
          quitScript; shift 1;;
850
        --log=*|--LOG=*) # already handled; ignore
851
          shift 1;;
852
        --log|--LOG) # already handled; ignore
853
          shift 2;;
854
        --noinfo) # already handled; ignore
855
          shift 1;;
856
        *)
857
          printError "invalid option: $1\n"; shift 1;;
858
      esac
859
    else
860
      otherargs+=("$1")
861
      shift 1
862
    fi
863
  done
864

    
865
# interactive menu
866
  while ( true ); do
867
    # main menu info prompt and selection
868
    printInfo "QtCreator setup main menu\n"
869
    printf "Please select one of the following actions:\n"
870
    printf "  [C] - clean project files\n"
871
    printf "  [W] - wipe project and .user files\n"
872
    printf "  [L] - create a project for the LightRing module\n"
873
    printf "  [P] - create a project for the PowerManagement module\n"
874
    printf "  [D] - create a project for the DiWheelDrive module\n"
875
    printf "  [A] - create a project for all modules\n"
876
    printf "  [Q] - quit this setup\n"
877
    local userinput=""
878
    readUserInput "CcWwLlPpDdAaQq" userinput
879
    printf "\n"
880

    
881
    # evaluate user selection
882
    case "$userinput" in
883
      C|c)
884
        deleteProjects; printf "\n";;
885
      W|w)
886
        deleteProjects --wipe; printf "\n";;
887
      L|l)
888
        createLightRingProject; printf "\n";;
889
      P|p)
890
        createPowerManagementProject; printf "\n";;
891
      D|d)
892
        createDiWheelDriveProject; printf "\n";;
893
      A|a)
894
        createAllProjects; printf "\n";;
895
      Q|q)
896
        quitScript;;
897
      *) # sanity check (exit with error)
898
        printError "unexpected argument: $userinput\n";;
899
    esac
900
  done
901

    
902
  exit 0
903
}
904

    
905
################################################################################
906
# SCRIPT ENTRY POINT                                                           #
907
################################################################################
908

    
909
main "$@"
910