Statistics
| Branch: | Tag: | Revision:

amiro-blt / setup.sh @ 73410535

History | View | Annotate | Download (21.676 KB)

1
################################################################################
2
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini    #
3
# Robot (AMiRo) platform.                                                      #
4
# Copyright (C) 2016..2019  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
### check whether commands are available #######################################
267
# Checks whether the specified commands are available and can be executed.
268
#
269
# usage:      checkCommands [<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
################################################################################
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 AMiRo-BLT setup!                   #\n"
315
  printf "#                                                                    #\n"
316
  printf "######################################################################\n"
317
  printf "#                                                                    #\n"
318
  printf "# Copyright (c) 2016..2019  Thomas Schöpping                         #\n"
319
  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
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [-f|--stm32flash] [-s|--SerialBoot] [-e|--IDE] [-c|--compiler] [-q|--quit] [--log=<file>]\n"
341
  printf "\n"
342
  printf "options:  -h, --help\n"
343
  printf "              Print this help text.\n"
344
  printf "          -f, --stm32flash\n"
345
  printf "              Run st,32flash tool setup.\n"
346
  printf "          -s, --SerialBoot\n"
347
  printf "              Run SerialBoot tool setup.\n"
348
  printf "          -e, --IDE\n"
349
  printf "              Enter IDE setup.\n"
350
  printf "          -c, --compiler\n"
351
  printf "              Enter compiler setup.\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
### stm32flash tool setup ######################################################
359
# Fetches the source code for the stm32flash tool and builds the binary.
360
# If the tool was already initialized, it can be wiped and rebuilt.
361
#
362
# usage:      stm32flashSetup
363
# arguments:  n/a
364
# return:     0
365
#                 No error or warning occurred.
366
#             1
367
#                 Warning: Setup aborted by user.
368
#             -1
369
#                 Error: Unexpected user input.
370
#             -2
371
#                 Error: Missing dependecny.
372
#
373
function stm32flashSetup {
374
  local amirobltdir=$(dirname $(realpath ${BASH_SOURCE[0]}))
375
  local stm32flashdir=${amirobltdir}/Host/Source/stm32flash
376
  local userdir=$(pwd)
377

    
378
  # check dependencies
379
  checkCommands git make gcc g++
380
  if [ $? -ne 0 ]; then
381
    printError "Missing dependencies detected.\n"
382
    return -2
383
  fi
384

    
385
  # if the stm32flash folder is not empty
386
  if [ ! -z "$(ls -A $stm32flashdir)" ]; then
387
    printWarning "$stm32flashdir is not empty. Delete and reinitialize? [y/n]\n"
388
    local userinput=""
389
    readUserInput "YyNn" userinput
390
    case "$userinput" in
391
      Y|y)
392
        printInfo "wiping ${stm32flashdir}...\n"
393
        # checkout base commit and delete all local branches
394
        cd $amirobltdir
395
        git submodule update --force --checkout $stm32flashdir | tee -a $LOG_FILE
396
        cd $stm32flashdir
397
        local git_branches=($(git for-each-ref --format="%(refname)"))
398
        for branch in $git_branches ; do
399
          if [[ $branch = *"heads/"* ]]; then
400
            git branch -D ${branch##*/} | tee -a $LOG_FILE
401
          fi
402
        done
403
        cd $amirobltdir
404
        # deinit stm32flash submodule and delete any remaining files
405
        git submodule deinit -f $stm32flashdir 2>&1 | tee -a $LOG_FILE
406
        rm -rf $stm32flashdir/*
407
        cd $userdir
408
        ;;
409
      N|n)
410
        printWarning "stm32flash setup aborted by user\n"
411
        return 1
412
        ;;
413
      *) # sanity check (return error)
414
        printError "unexpected input: $userinput\n";
415
        return -1
416
        ;;
417
    esac
418
  fi
419

    
420
  # initialize submodule
421
  printInfo "initializing stm32flash submodule...\n"
422
  cd $amirobltdir
423
  git submodule update --init $stm32flashdir 2>&1 | tee -a $LOG_FILE
424
  while [ ${PIPESTATUS[0]} -ne 0 ]; do
425
    printWarning "initialitaion failed. Retry? [y/n]\n"
426
    local userinput=""
427
    readUserInput "YyNn" userinput
428
    case "$userinput" in
429
      Y|y)
430
        git submodule update --init $stm32flashdir 2>&1 | tee -a $LOG_FILE;;
431
      N|n)
432
        printWarning "stm32flash initialization aborted by user\n"
433
        cd $userdir
434
        return 1
435
        ;;
436
      *) # sanity check (return error)
437
        printError "unexpected input: $userinput\n"; return -1;;
438
    esac
439
  done
440
  cd $userdir
441

    
442
  # build the stm32flash tool
443
  printInfo "compiling stm32flash\n"
444
  cd $stm32flashdir
445
  make 2>&1 | tee -a $LOG_FILE
446
  cd $userdir
447

    
448
  return 0
449
}
450

    
451
### SerialBoot tool setup ######################################################
452
# Builds the SerialBoot tool.
453
# If the tool was built before, it can be deleted and rebuilt.
454
#
455
# usage:      serialBootSetup
456
# arguments:  n/a
457
# return:     0
458
#                 No errort or warning occurred.
459
#             1
460
#                 Warning: Setup aborted by user.
461
#             -1
462
#                 Error: Unexpected user input.
463
#             -2
464
#                 Error: Missing dependecny.
465
#
466
function serialBootSetup {
467
  local amirobltdir=$(dirname $(realpath ${BASH_SOURCE[0]}))
468
  local serialbootdir=${amirobltdir}/Host/Source/SerialBoot
469
  local userdir=$(pwd)
470

    
471
  # check dependencies
472
  checkCommands make cmake gcc g++
473
  if [ $? -ne 0 ]; then
474
    printError "Missing dependencies detected.\n"
475
    return -2
476
  fi
477

    
478
  # if a build folder already exists
479
  if [ -d "${serialbootdir}/build/" ]; then
480
    printWarning "SerialBoot has been built before. Delete and rebuild? [y/n]\n"
481
    local userinput=""
482
    readUserInput "YyNn" userinput
483
    case "$userinput" in
484
      Y|y)
485
        printInfo "deleting ${serialbootdir}/build/...\n"
486
        rm -rf "${serialbootdir}/build/"
487
        ;;
488
      N|n)
489
        printWarning "SerialBoot setup aborted by user\n"
490
        return 1
491
        ;;
492
      *) # sanity check (return error)
493
        printError "unexpected input: $userinput\n";
494
        return -1
495
        ;;
496
    esac
497
  fi
498

    
499
  # build SerialBoot
500
  printInfo "compiling SerialBoot...\n"
501
  mkdir ${serialbootdir}/build
502
  cd ${serialbootdir}/build
503
  cmake .. 2>&1 | tee -a $LOG_FILE
504
  make 2>&1 | tee -a $LOG_FILE
505
  cd $userdir
506

    
507
  return 0
508
}
509

    
510
### IDE setup ##################################################################
511
# Enter the IDE setup.
512
#
513
# usage:      ideSetup
514
# arguments:  n/a
515
# return:     n/a
516
#
517
function ideSetup {
518
  printInfo "entering IDE setup\n"
519
  printf "\n"
520
  if [ -z "$LOG_FILE" ]; then
521
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/ide/idesetup.sh --noinfo
522
  else
523
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/ide/idesetup.sh --LOG="$LOG_FILE" --noinfo
524
  fi
525
}
526

    
527
### compiler setup #############################################################
528
# Enter the compiler setup.
529
#
530
# usage:      compilerSetup
531
# arguments:  n/a
532
# return:     n/a
533
#
534
function compilerSetup {
535
  printInfo "entering compiler setup\n"
536
  printf "\n"
537
  if [ -z "$LOG_FILE" ]; then
538
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/compiler/compilersetup.sh --noinfo
539
  else
540
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/compiler/compilersetup.sh --LOG="$LOG_FILE" --noinfo
541
  fi
542
}
543

    
544
### main function of this script ###############################################
545
# A setup to initialize dependencies, setup IDE projects and configure the
546
# compiler setup.
547
#
548
# usage:      see function printHelp
549
# arguments:  see function printHelp
550
# return:     0
551
#                 No error or warning occurred.
552
#
553
function main {
554
  # print welcome/info text if not suppressed
555
  if [[ $@ != *"--noinfo"* ]]; then
556
    printWelcomeText
557
  else
558
    printf "######################################################################\n"
559
  fi
560
  printf "\n"
561

    
562
  # if --help or -h was specified, print the help text and exit
563
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
564
    printHelp
565
    printf "\n"
566
    quitScript
567
  fi
568

    
569
  # set log file if specified
570
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
571
    # get the parameter (file name)
572
    local cmdidx=1
573
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
574
      cmdidx=$[cmdidx + 1]
575
    done
576
    local cmd="${!cmdidx}"
577
    local logfile=""
578
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
579
      logfile=${cmd#*=}
580
    else
581
      local filenameidx=$((cmdidx + 1))
582
      logfile="${!filenameidx}"
583
    fi
584
    # optionally force silent appending
585
    if [[ "$cmd" = "--LOG"* ]]; then
586
      setLogFile --option=c --quiet "$logfile" LOG_FILE
587
    else
588
      setLogFile "$logfile" LOG_FILE
589
      printf "\n"
590
    fi
591
  fi
592
  # log script name
593
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
594

    
595
  # parse arguments
596
  local otherargs=()
597
  while [ $# -gt 0 ]; do
598
    if ( parseIsOption $1 ); then
599
      case "$1" in
600
        -h|--help) # already handled; ignore
601
          shift 1;;
602
        -f|--stm32flash)
603
          stm32flashSetup; printf "\n"; shift 1;;
604
        -s|--SerialBoot)
605
          serialBootSetup; printf "\n"; shift 1;;
606
        -e|--IDE)
607
          ideSetup; printf "\n"; shift 1;;
608
        -c|--compiler)
609
          compilerSetup; printf "\n"; shift 1;;
610
        -q|--quit)
611
          quitScript; shift 1;;
612
        --log=*|--LOG=*) # already handled; ignore
613
          shift 1;;
614
        --log|--LOG) # already handled; ignore
615
          shift 2;;
616
        --noinfo) # already handled; ignore
617
          shift 1;;
618
        *)
619
          printError "invalid option: $1\n"; shift 1;;
620
      esac
621
    else
622
      otherargs+=("$1")
623
      shift 1
624
    fi
625
  done
626

    
627
  # interactive menu
628
  while ( true ); do
629
    # main menu info prompt and selection
630
    printInfo "AMiRo-BLT setup main menu\n"
631
    printf "Please select one of the following actions:\n"
632
    printf "  [F] - get and build stm32flash tool\n"
633
    printf "  [S] - build SerialBoot tool\n"
634
    printf "  [E] - IDE project setup\n"
635
    printf "  [C] - enter compiler setup\n"
636
    printf "  [Q] - quit this setup\n"
637
    local userinput=""
638
    readUserInput "FfSsEeCcQq" userinput
639
    printf "\n"
640

    
641
    # evaluate user selection
642
    case "$userinput" in
643
      F|f)
644
        stm32flashSetup; printf "\n";;
645
      S|s)
646
        serialBootSetup; printf "\n";;
647
      E|e)
648
        ideSetup; printf "\n";;
649
      C|c)
650
        compilerSetup; printf "\n";;
651
      Q|q)
652
        quitScript;;
653
      *) # sanity check (exit with error)
654
        printError "unexpected argument: $userinput\n";;
655
    esac
656
  done
657

    
658
  exit 0
659
}
660

    
661
################################################################################
662
# SCRIPT ENTRY POINT                                                           #
663
################################################################################
664

    
665
main "$@"
666