Statistics
| Branch: | Tag: | Revision:

amiro-os / kernel / kernelsetup.sh @ db6bd9f8

History | View | Annotate | Download (21.565 KB)

1
################################################################################
2
# AMiRo-OS is an operating system designed for the Autonomous Mini Robot       #
3
# (AMiRo) platform.                                                            #
4
# Copyright (C) 2016..2020  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
# load library
27
source "$(dirname ${BASH_SOURCE[0]})/../tools/bash/setuplib.sh"
28

    
29
### print welcome text #########################################################
30
# Prints a welcome message to standard out.
31
#
32
# usage:      printWelcomeText
33
# arguments:  n/a
34
# return:     n/a
35
#
36
function printWelcomeText {
37
  printf "######################################################################\n"
38
  printf "#                                                                    #\n"
39
  printf "#              Welcome to the ChibiOS submodule setup!               #\n"
40
  printf "#                                                                    #\n"
41
  printf "######################################################################\n"
42
  printf "#                                                                    #\n"
43
  printf "# Copyright (c) 2016..2020  Thomas Schöpping                         #\n"
44
  printf "#                                                                    #\n"
45
  printf "# This is free software; see the source for copying conditions.      #\n"
46
  printf "# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR  #\n"
47
  printf "# A PARTICULAR PURPOSE. The development of this software was         #\n"
48
  printf "# supported by the Excellence Cluster EXC 227 Cognitive Interaction  #\n"
49
  printf "# Technology. The Excellence Cluster EXC 227 is a grant of the       #\n"
50
  printf "# Deutsche Forschungsgemeinschaft (DFG) in the context of the German #\n"
51
  printf "# Excellence Initiative.                                             #\n"
52
  printf "#                                                                    #\n"
53
  printf "######################################################################\n"
54
}
55

    
56
### print help #################################################################
57
# Prints a help text to standard out.
58
#
59
# usage:      printHelp
60
# arguments:  n/a
61
# return:     n/a
62
#
63
function printHelp {
64
  printInfo "printing help text\n"
65
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [-i|--init] [-p|--patch] [-d|--documentation=<option>] [-w|--wipe] [-q|--quit] [--log=<file>]\n"
66
  printf "options:  -h, --help\n"
67
  printf "              Print this help text.\n"
68
  printf "          -i, --init\n"
69
  printf "              Initialize ChibiOS submodule.\n"
70
  printf "          -p, --patch,\n"
71
  printf "              Apply patches to ChibiOS.\n"
72
  printf "          -d, --documentation <option>\n"
73
  printf "              Possible options are: 'g' and 'o' (can be combined).\n"
74
  printf "              - g: Generate HTML documentation of ChibiOS.\n"
75
  printf "              - o: Open HTML documentation of ChibiOS.\n"
76
  printf "          -w, --wipe\n"
77
  printf "              Wipe ChibiOS submodule.\n"
78
  printf "          -q, --quit\n"
79
  printf "              Quit the script.\n"
80
  printf "          --log=<file>\n"
81
  printf "              Specify a log file.\n"
82
}
83

    
84
### initialize ChibiOS submodule ###############################################
85
# Initializes the ChibiOS Git submodule.
86
#
87
# usage:      initChibiOS
88
# arguments:  n/a
89
# return:     0
90
#                 No error or warning occurred.
91
#             1
92
#                 Warning: Aborted by user.
93
#             -1
94
#                 Error: Unexpected user input.
95
#             -1
96
#                 Error: Missing dependency.
97
#
98
function initChibiOS {
99
  printInfo "initializing ChibiOS submodule...\n"
100
  local userdir=$(pwd)
101
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
102
  local chibiosdir=${kerneldir}/ChibiOS
103

    
104
  # if the kernel folder is not empty
105
  if [ ! -z "$(ls -A $chibiosdir)" ]; then
106
    printWarning "$chibiosdir is not empty. Delete and reinitialize? [y/n]\n"
107
    local userinput=""
108
    readUserInput "YyNn" userinput
109
    case "$userinput" in
110
      Y|y)
111
        wipeChibiOS
112
        ;;
113
      N|n)
114
        printWarning "ChibiOS initialization aborted by user\n"
115
        return 1
116
        ;;
117
      *) # sanity check (return error)
118
        printError "unexpected input: $userinput\n"; return -1;;
119
    esac
120
  fi
121

    
122
  # check dependencies
123
  checkCommands git
124
  if [ $? -ne 0 ]; then
125
    printError "Missing dependencies detected.\n"
126
    return -2
127
  fi
128

    
129
  # initialize submodule to default branch
130
  cd $kerneldir
131
  git submodule update --init $chibiosdir 2>&1 | tee -a $LOG_FILE
132
  while [ ${PIPESTATUS[0]} -ne 0 ]; do
133
    printWarning "initialitaion failed. Retry? [y/n]\n"
134
    local userinput=""
135
    readUserInput "YyNn" userinput
136
    case "$userinput" in
137
      Y|y)
138
        git submodule update --init $chibiosdir 2>&1 | tee -a $LOG_FILE;;
139
      N|n)
140
        printWarning "ChibiOS initialization aborted by user\n"
141
        cd $userdir
142
        return 1
143
        ;;
144
      *) # sanity check (return error)
145
        printError "unexpected input: $userinput\n"; return -1;;
146
    esac
147
  done
148
  cd $userdir
149

    
150
  return 0
151
}
152

    
153
### patch ChibiOS ##############################################################
154
# Applies patches to ChibiOS submodule.
155
#
156
# usage:      patchChibiOS
157
# arguments:  n/a
158
# return:     0
159
#                 No error or warning occurred.
160
#             1
161
#                 Warning: ChibiOS not initialized yet.
162
#             2
163
#                 Warning: Setup aborted by user.
164
#             -1
165
#                 Error: Unexpected user input.
166
#
167
function patchChibiOS {
168
  printInfo "applying patches to ChibiOS\n"
169
  local userdir=$(pwd)
170
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
171
  local chibiosdir=${kerneldir}/ChibiOS
172
  local git_branch_patched="AMiRo-OS"
173

    
174
  # check dependencies
175
  checkCommands git
176
  if [ $? -ne 0 ]; then
177
    printError "Missing dependencies detected.\n"
178
    return -2
179
  fi
180

    
181
  # if the ChibiOS folder is empty
182
  if [ -z "$(ls -A $chibiosdir)" ]; then
183
    printWarning "$chibiosdir is empty. Please initialize first.\n"
184
    return 1
185
  else 
186
    # get some information from Git
187
    cd $chibiosdir
188
    local git_branch_current=$(git rev-parse --abbrev-ref HEAD)
189
    local git_branches=$(git for-each-ref --format="%(refname)")
190
    local git_dirtyfiles=($(git ls-files -dmo --exclude-standard --exclude=/doc))
191
    cd $userdir
192

    
193
    local issues=0
194
    # if the current branch is already $git_branch_patched
195
    if [ "$git_branch_current" = "$git_branch_patched" ]; then
196
      issues=$((issues + 1))
197
      printWarning "current branch is already $git_branch_patched\n"
198
    fi
199
    # if the current branch is bot $git_branch_patched, but another branch $git_branch_patched already exists
200
    if [ "$git_branch_current" != "$git_branch_patched" ] && [[ "$git_branches" = *"$git_branch_patched"* ]]; then
201
      issues=$((issues + 1))
202
      printWarning "another branch $git_branch_patched already exists\n"
203
    fi
204
    # if there are untracked, modified, or deleted files
205
    if [ ${#git_dirtyfiles[@]} != 0 ]; then
206
      issues=$((issues + 1))
207
      printWarning "there are ${#git_dirtyfiles[@]} untracked, modified, or deleted files\n"
208
    fi
209
    if [ $issues -gt 0 ]; then
210
      local userinput=""
211
      printWarning "$issues issues detected. Do you want to continue? [y/n]\n"
212
      readUserInput "YyNn" userinput
213
      case "$userinput" in
214
        Y|y)
215
          ;;
216
        N|n)
217
          printfWarning "ChibiOS patching aborted by user\n"
218
          return 2
219
          ;;
220
        *) # sanity check (return error)
221
          printError "unexpected input: $userinput\n"; return -1;;
222
      esac
223
    fi
224

    
225
    # create a new branch and apply the patches
226
    local patches=${kerneldir}/patches/*.patch
227
    cd $chibiosdir
228
    git checkout -b "$git_branch_patched" 2>&1 | tee -a $LOG_FILE
229
    for patch in $patches; do
230
      printInfo "applying $(basename ${patch})...\n"
231
      git apply --whitespace=nowarn --ignore-space-change --ignore-whitespace < $patch 2>&1 | tee -a $LOG_FILE
232
#      # These lines are disabled for safety reasons:
233
#      #   Filed commits are detected as valid changes by the super-project.
234
#      #   This would lead to errorneous updates of the super-project, so to point to one of those (local) commits.
235
#      #   Since these commits are not pushed upstream, initialization of the super-project will therefore fail, because
236
#      #   the referenced hashes (after patching) do not exist in a clean copy of this sub-project.
237
#      git add $(git ls-files -dmo --exclude-standard --exclude=/doc) $(git diff --name-only) 2>&1 | tee -a $LOG_FILE
238
#      git commit --message="$patch applied" 2>&1 | tee -a $LOG_FILE
239
    done
240
    cd $userdir
241

    
242
    return 0
243
  fi
244
}
245

    
246
### ChibiOS dcoumentation setup ################################################
247
#
248
# usage:      documentation [<option>]
249
# arguments:  <option>
250
#                 Can be either 'g' or 'o' to generate or open HTML documentation respectively.
251
# return:     0
252
#                 No error or warning occurred.
253
#             1
254
#                 Warning: Kernel not nitialized yet.
255
#             2
256
#                 Warning: Setup aborted by user.
257
#             3
258
#                 Warning: Issues occurred.
259
#             -1
260
#                 Error: Unexpected user input.
261
#             -2
262
#                 Error: Missing dependencies.
263
#
264
function documentation {
265
  local userdir=$(pwd)
266
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
267
  local chibiosdir=${kerneldir}/ChibiOS
268

    
269
  # if the ChibiOS folder is empty
270
  if [ -z "$(ls -A $chibiosdir)" ]; then
271
    printWarning "$chibiosdir is empty. Please initialize first.\n"
272
    return 1
273
  else
274
    local option="";
275
    # if no argument was specified, ask what to do
276
    if [ $# -eq 0 ]; then
277
      printInfo "ChibiOS documentation setup\n"
278
      printf "Please select one of the following actions:\n"
279
      printf "  [G] - generate HTML documentation\n"
280
      printf "  [O] - open HTML documentation\n"
281
      printf "  [A] - abort this setup\n"
282
      local userinput
283
      readUserInput "GgOoAa" userinput
284
      option=${userinput,,}
285
      if [ $option = "a" ]; then
286
        printInfo "ChibiOS documentation setup aborted by user\n"
287
        return 2
288
      fi
289
    else
290
      option="$1"
291
    fi
292

    
293
    local issues=0
294
    case "$option" in
295
      # generate HTML documentation
296
      g)
297
        # check dependencies
298
        checkCommands doxygen
299
        if [ $? -ne 0 ]; then
300
          printError "Missing dependencies detected.\n"
301
          return -2
302
        fi
303

    
304
        # ChibiOS/HAL: check if required files exis
305
        if [ -f ${chibiosdir}/doc/hal/makehtml.sh ]; then
306
          printInfo "generating ChibiOS/HAL documentation...\n"
307
          cd ${chibiosdir}/doc/hal
308
          ${chibiosdir}/doc/hal/makehtml.sh 2>&1 | tee -a $LOG_FILE
309
          cd $userdir
310
          printInfo "access ChibiOS/HAL documentation via ${chibiosdir}doc/hal/html/index.html\n"
311
        else
312
          issues=$((issues + 1))
313
          printError "could not generate ChibiOS/HAL documentation\n"
314
        fi
315
        # ChibiOS/RT: check if required files exis
316
        if [ -f ${chibiosdir}/doc/rt/makehtml.sh ]; then
317
          printInfo "generating ChibiOS/RT documentation...\n"
318
          cd ${chibiosdir}/doc/rt
319
          ${chibiosdir}/doc/rt/makehtml.sh 2>&1 | tee -a $LOG_FILE
320
          cd $userdir
321
          printInfo "access ChibiOS/RT documentation via ${chibiosdir}doc/rt/html/index.html\n"
322
        else
323
          issues=$((issues + 1))
324
          printError "could not generate ChibiOS/RT documentation\n"
325
        fi
326
        # ChibiOS/NIL: check if required files exis
327
        if [ -f ${chibiosdir}/doc/nil/makehtml.sh ]; then
328
          printInfo "generating ChibiOS/NIL documentation...\n"
329
          cd ${chibiosdir}/doc/nil
330
          ${chibiosdir}/doc/nil/makehtml.sh 2>&1 | tee -a $LOG_FILE
331
          cd $userdir
332
          printInfo "access ChibiOS/NIL documentation via ${chibiosdir}edoc/nil/html/index.html\n"
333
        else
334
          issues=$((issues + 1))
335
          printError "could not generate ChibiOS/NIL documentation\n"
336
        fi
337
        ;;
338

    
339
      # open HTML documentation
340
      o)
341
        # ChibiOS/HAL: check if required files exis
342
        if [ -f ${chibiosdir}/doc/hal/html/index.html ]; then
343
          printInfo "open ChibiOS/HAL documentation\n"
344
          xdg-open ${chibiosdir}/doc/hal/html/index.html &> /dev/null &
345
        else
346
          issues=$((issues + 1))
347
          printError "could not open ChibiOS/HAL documentation\n"
348
        fi
349
        # ChibiOS/RT: check if required files exis
350
        if [ -f ${chibiosdir}/doc/rt/html/index.html ]; then
351
          printInfo "open ChibiOS/RT documentation\n"
352
          xdg-open ${chibiosdir}/doc/rt/html/index.html &> /dev/null &
353
        else
354
          issues=$((issues + 1))
355
          printError "could not open ChibiOS/RT documentation\n"
356
        fi
357
        # ChibiOS/NIL: check if required files exis
358
        if [ -f ${chibiosdir}/doc/nil/html/index.html ]; then
359
          printInfo "open ChibiOS/NIL documentation\n"
360
          xdg-open ${chibiosdir}/doc/nil/html/index.html &> /dev/null &
361
        else
362
          issues=$((issues + 1))
363
          printError "could not open ChibiOS/NIL documentation\n"
364
        fi
365
        ;;
366

    
367
      *) # sanity check (return error)
368
        printError "unexpected input: $userinput\n"; return -1;;
369
    esac
370

    
371
    if [ $issues -gt 0 ]; then
372
      return 3
373
    else
374
      return 0
375
    fi
376
  fi
377
}
378

    
379
### reset ChibiOS submodule and wipe directory #################################
380
# Resets the ChibiOS Git submodule and wipes the directory.
381
#
382
# usage:      wipeChibiOS
383
# arguments:  n/a
384
# return:     0
385
#                 No error or warning occurred.
386
#             1
387
#                 Warning: Submodule directory is already empty.
388
#             2
389
#                 Warning: Wiping aborted by user.
390
#             -1
391
#                 Error: Unexpected user input.
392
#
393
function wipeChibiOS {
394
  printInfo "reset and wipe Git submodule $kerneldir\n"
395
  local userdir=$(pwd)
396
  local kerneldir=$(dirname $(realpath ${BASH_SOURCE[0]}))
397
  local chibiosdir=${kerneldir}/ChibiOS
398
  local git_branch_patched="AMiRo-OS"
399

    
400
  # check dependencies
401
  checkCommands git
402
  if [ $? -ne 0 ]; then
403
    printError "Missing dependencies detected.\n"
404
    return -2
405
  fi
406

    
407
  # if the ChibiOS folder is empty
408
  if [ -z "$(ls -A $chibiosdir)" ]; then
409
    printInfo "$chibiosdir is alread empty\n"
410
    return 1
411
  else 
412
    # get some information from Git
413
    cd $kerneldir
414
    local git_basehash=($(git ls-tree -d HEAD $kerneldir)); git_basehash=${git_basehash[2]}
415
    cd $chibiosdir
416
    local git_branch_current=$(git rev-parse --abbrev-ref HEAD)
417
    local git_difftobase="$(git diff ${git_basehash}..HEAD)"
418
    local git_commits=$(git log --format=oneline ${git_basehash}..HEAD)
419
    local git_dirtyfiles=($(git ls-files -dmo --exclude-standard --exclude=/doc))
420
    cd $userdir
421
    local issues=0
422
    # if the HEAD is neither detached, nor is the current branch $git_branch_patched
423
    if [ "$git_branch_current" != "HEAD" ] && [ "$git_branch_current" != "$git_branch_patched" ]; then
424
      issues=$((issues + 1))
425
      printWarning "modifications to ChibiOS Git submodule detected\n"
426
    fi
427
    # if HEAD is ahead of submodule base commit but with more than just applied patches
428
    if [ -n "$git_difftobase" ] && [ -n "$(echo $git_commits | grep -Ev '\.patch applied$')" ]; then
429
      issues=$((issues + 1))
430
      printWarning "HEAD is ahead of submodule base by unexpected commits\n"
431
    fi
432
    # if there are untracked, modified, or deleted files
433
    if [ ${#git_dirtyfiles[@]} != 0 ]; then
434
      issues=$((issues + 1))
435
      printWarning "there are ${#git_dirtyfiles[@]} untracked, modified, or deleted files\n"
436
    fi
437
    if [ $issues -gt 0 ]; then
438
      local userinput=""
439
      printWarning "$issues issues detected. Do you want to continue? [y/n]\n"
440
      readUserInput "YyNn" userinput
441
      case "$userinput" in
442
        Y|y)
443
          ;;
444
        N|n)
445
          printfWarning "wiping ChibiOS Git submodule aborted by user\n"
446
          return 2
447
          ;;
448
        *) # sanity check (return error)
449
          printError "unexpected input: $userinput\n"; return -1;;
450
      esac
451
    fi
452

    
453
    # checkout base commit and delete all local branches
454
    cd $kerneldir
455
    git submodule update --force --checkout $kerneldir | tee -a $LOG_FILE
456
    cd $chibiosdir
457
    local git_branches=($(git for-each-ref --format="%(refname)"))
458
    for branch in $git_branches; do
459
      if [[ $branch = *"heads/"* ]]; then
460
        git branch -D ${branch##*/} | tee -a $LOG_FILE
461
      fi
462
    done
463
    cd $userdir
464

    
465
    # deinitialize ChibiOS submodule and delete any remaining files
466
    cd $kerneldir
467
    git submodule deinit -f $chibiosdir 2>&1 | tee -a $LOG_FILE
468
    rm -rf $chibiosdir/*
469
    cd $userdir
470

    
471
    return 0
472
  fi
473
}
474

    
475
### main function of this script ###############################################
476
# The kernel setup provides comfortable initialization, patching, documentation
477
# generation and cleanup for ChibiOS.
478
#
479
# usage:      see function printHelp
480
# arguments:  see function printHelp
481
# return:     0
482
#                 No error or warning occurred.
483
#
484
function main {
485
  # print welcome/info text if not suppressed
486
  if [[ $@ != *"--noinfo"* ]]; then
487
    printWelcomeText
488
  else
489
    printf "######################################################################\n"
490
  fi
491
  printf "\n"
492

    
493
  # if --help or -h was specified, print the help text and exit
494
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
495
    printHelp
496
    printf "\n"
497
    quitScript
498
  fi
499

    
500
  # set log file if specified
501
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
502
    # get the parameter (file name)
503
    local cmdidx=1
504
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
505
      cmdidx=$[cmdidx + 1]
506
    done
507
    local cmd="${!cmdidx}"
508
    local logfile=""
509
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
510
      logfile=${cmd#*=}
511
    else
512
      local filenameidx=$((cmdidx + 1))
513
      logfile="${!filenameidx}"
514
    fi
515
    # optionally force silent appending
516
    if [[ "$cmd" = "--LOG"* ]]; then
517
      setLogFile --option=c --quiet "$logfile" LOG_FILE
518
    else
519
      setLogFile "$logfile" LOG_FILE
520
      printf "\n"
521
    fi
522
  fi
523
  # log script name
524
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
525

    
526
  # parse arguments
527
  local otherargs=()
528
  while [ $# -gt 0 ]; do
529
    if ( parseIsOption $1 ); then
530
      case "$1" in
531
        -h|--help) # already handled; ignore
532
          shift 1;;
533
        -i|--init)
534
           initChibiOS; printf "\n"; shift 1;;
535
        -p|--patch)
536
           patchChibiOS; printf "\n"; shift 1;;
537
        -d=*|--documentation=*)
538
           documentation "${1#*=}"; printf "\n"; shift 1;;
539
        -d|--documentation)
540
           if ( ! parseIsOption $2 ); then
541
             documentation "$2"; printf "\n"; shift 2
542
           else
543
             documentation; printf "\n"; shift 1
544
           fi;;
545
        -w|--wipe)
546
          wipeChibiOS; printf "\n"; shift 1;;
547
        -q|--quit)
548
          quitScript; shift 1;;
549
        --log=*|--LOG=*) # already handled; ignore
550
          shift 1;;
551
        --log|--LOG) # already handled; ignore
552
          shift 2;;
553
        --noinfo) # already handled; ignore
554
          shift 1;;
555
        *)
556
          printError "invalid option: $1\n"; shift 1;;
557
      esac
558
    else
559
      otherargs+=("$1")
560
      shift 1
561
    fi
562
  done
563

    
564
  # interactive menu
565
  while ( true ); do
566
    # main menu info prompt and selection
567
    printInfo "ChibiOS kernel setup main menu\n"
568
    printf "Please select one of the following actions:\n"
569
    printf "  [I] - initialize ChibiOS submodule\n"
570
    printf "  [P] - apply patches to ChibiOS\n"
571
    printf "  [D] - generate or open HTML documentation\n"
572
    printf "  [W] - wipe ChibiOS submodule\n"
573
    printf "  [Q] - quit this setup\n"
574
    local userinput=""
575
    readUserInput "IiPpDdWwQq" userinput
576
    printf "\n"
577

    
578
    # evaluate user selection
579
    case "$userinput" in
580
      I|i)
581
        initChibiOS; printf "\n";;
582
      P|p)
583
        patchChibiOS; printf "\n";;
584
      D|d)
585
        documentation; printf "\n";;
586
      W|w)
587
        wipeChibiOS; printf "\n";;
588
      Q|q)
589
        quitScript;;
590
      *) # sanity check (exit with error)
591
        printError "unexpected argument: $userinput\n";;
592
    esac
593
  done
594

    
595
  exit 0
596
}
597

    
598
################################################################################
599
# SCRIPT ENTRY POINT                                                           #
600
################################################################################
601

    
602
main "$@"