Statistics
| Branch: | Tag: | Revision:

amiro-blt / setup.sh @ ec50c96e

History | View | Annotate | Download (13.7 KB)

1 4cce70a8 Thomas Schöpping
################################################################################
2
# AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini    #
3
# Robot (AMiRo) platform.                                                      #
4 94886d84 Thomas Schöpping
# Copyright (C) 2016..2020  Thomas Schöpping et al.                            #
5 4cce70a8 Thomas Schöpping
#                                                                              #
6
# This program is free software: you can redistribute it and/or modify         #
7
# it under the terms of the GNU General Public License as published by         #
8
# the Free Software Foundation, either version 3 of the License, or            #
9
# (at your option) any later version.                                          #
10
#                                                                              #
11
# This program is distributed in the hope that it will be useful,              #
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
14
# GNU General Public License for more details.                                 #
15
#                                                                              #
16
# You should have received a copy of the GNU General Public License            #
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.        #
18
#                                                                              #
19
# This research/work was supported by the Cluster of Excellence Cognitive      #
20
# Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is   #
21
# funded by the German Research Foundation (DFG).                              #
22
################################################################################
23
24
#!/bin/bash
25
26 ec50c96e Thomas Schöpping
# load library
27
source "$(dirname ${BASH_SOURCE[0]})/tools/bash/setuplib.sh"
28 1da30dfc Thomas Schöpping
29 0a42f078 Thomas Schöpping
### 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 1da30dfc Thomas Schöpping
function printWelcomeText {
37 4cce70a8 Thomas Schöpping
  printf "######################################################################\n"
38
  printf "#                                                                    #\n"
39
  printf "#                  Welcome to the AMiRo-BLT setup!                   #\n"
40
  printf "#                                                                    #\n"
41
  printf "######################################################################\n"
42
  printf "#                                                                    #\n"
43 94886d84 Thomas Schöpping
  printf "# Copyright (c) 2016..2020  Thomas Schöpping                         #\n"
44 4cce70a8 Thomas Schöpping
  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 1da30dfc Thomas Schöpping
}
55
56 0a42f078 Thomas Schöpping
### print help #################################################################
57
# Prints a help text to standard out.
58
#
59
# usage:      printHelp
60
# arguments:  n/a
61
# return:     n/a
62
#
63 1da30dfc Thomas Schöpping
function printHelp {
64
  printInfo "printing help text\n"
65 0a42f078 Thomas Schöpping
  printf "usage:    $(basename ${BASH_SOURCE[0]}) [-h|--help] [-f|--stm32flash] [-s|--SerialBoot] [-e|--IDE] [-c|--compiler] [-q|--quit] [--log=<file>]\n"
66
  printf "\n"
67
  printf "options:  -h, --help\n"
68
  printf "              Print this help text.\n"
69
  printf "          -f, --stm32flash\n"
70
  printf "              Run st,32flash tool setup.\n"
71
  printf "          -s, --SerialBoot\n"
72
  printf "              Run SerialBoot tool setup.\n"
73
  printf "          -e, --IDE\n"
74
  printf "              Enter IDE setup.\n"
75
  printf "          -c, --compiler\n"
76
  printf "              Enter compiler setup.\n"
77
  printf "          -q, --quit\n"
78
  printf "              Quit the script.\n"
79
  printf "          --log=<file>\n"
80
  printf "              Specify a log file.\n"
81 1da30dfc Thomas Schöpping
}
82
83 0a42f078 Thomas Schöpping
### stm32flash tool setup ######################################################
84
# Fetches the source code for the stm32flash tool and builds the binary.
85
# If the tool was already initialized, it can be wiped and rebuilt.
86
#
87
# usage:      stm32flashSetup
88
# arguments:  n/a
89
# return:     0
90
#                 No error or warning occurred.
91
#             1
92
#                 Warning: Setup aborted by user.
93
#             -1
94
#                 Error: Unexpected user input.
95 fad4c1e7 Thomas Schöpping
#             -2
96
#                 Error: Missing dependecny.
97 0a42f078 Thomas Schöpping
#
98 1da30dfc Thomas Schöpping
function stm32flashSetup {
99 5690d84c Thomas Schöpping
  local amirobltdir=$(dirname $(realpath ${BASH_SOURCE[0]}))
100 214352ae Thomas Schöpping
  local stm32flashdir=${amirobltdir}/Host/Source/stm32flash
101 0a42f078 Thomas Schöpping
  local userdir=$(pwd)
102 1da30dfc Thomas Schöpping
103 fad4c1e7 Thomas Schöpping
  # check dependencies
104
  checkCommands git make gcc g++
105
  if [ $? -ne 0 ]; then
106
    printError "Missing dependencies detected.\n"
107
    return -2
108
  fi
109
110 1da30dfc Thomas Schöpping
  # if the stm32flash folder is not empty
111
  if [ ! -z "$(ls -A $stm32flashdir)" ]; then
112 0a42f078 Thomas Schöpping
    printWarning "$stm32flashdir is not empty. Delete and reinitialize? [y/n]\n"
113
    local userinput=""
114
    readUserInput "YyNn" userinput
115
    case "$userinput" in
116 1da30dfc Thomas Schöpping
      Y|y)
117 0a42f078 Thomas Schöpping
        printInfo "wiping ${stm32flashdir}...\n"
118
        # checkout base commit and delete all local branches
119 5690d84c Thomas Schöpping
        cd $amirobltdir
120 0a42f078 Thomas Schöpping
        git submodule update --force --checkout $stm32flashdir | tee -a $LOG_FILE
121
        cd $stm32flashdir
122
        local git_branches=($(git for-each-ref --format="%(refname)"))
123
        for branch in $git_branches ; do
124
          if [[ $branch = *"heads/"* ]]; then
125
            git branch -D ${branch##*/} | tee -a $LOG_FILE
126
          fi
127
        done
128 5690d84c Thomas Schöpping
        cd $amirobltdir
129 0a42f078 Thomas Schöpping
        # deinit stm32flash submodule and delete any remaining files
130 1da30dfc Thomas Schöpping
        git submodule deinit -f $stm32flashdir 2>&1 | tee -a $LOG_FILE
131 0a42f078 Thomas Schöpping
        rm -rf $stm32flashdir/*
132 5690d84c Thomas Schöpping
        cd $userdir
133 1da30dfc Thomas Schöpping
        ;;
134
      N|n)
135
        printWarning "stm32flash setup aborted by user\n"
136
        return 1
137
        ;;
138
      *) # sanity check (return error)
139
        printError "unexpected input: $userinput\n";
140
        return -1
141
        ;;
142
    esac
143
  fi
144
145
  # initialize submodule
146 0a42f078 Thomas Schöpping
  printInfo "initializing stm32flash submodule...\n"
147 5690d84c Thomas Schöpping
  cd $amirobltdir
148 1da30dfc Thomas Schöpping
  git submodule update --init $stm32flashdir 2>&1 | tee -a $LOG_FILE
149 66984f56 Thomas Schöpping
  while [ ${PIPESTATUS[0]} -ne 0 ]; do
150
    printWarning "initialitaion failed. Retry? [y/n]\n"
151
    local userinput=""
152
    readUserInput "YyNn" userinput
153
    case "$userinput" in
154
      Y|y)
155
        git submodule update --init $stm32flashdir 2>&1 | tee -a $LOG_FILE;;
156
      N|n)
157
        printWarning "stm32flash initialization aborted by user\n"
158 5690d84c Thomas Schöpping
        cd $userdir
159 66984f56 Thomas Schöpping
        return 1
160
        ;;
161
      *) # sanity check (return error)
162
        printError "unexpected input: $userinput\n"; return -1;;
163
    esac
164
  done
165 5690d84c Thomas Schöpping
  cd $userdir
166 1da30dfc Thomas Schöpping
167
  # build the stm32flash tool
168
  printInfo "compiling stm32flash\n"
169 214352ae Thomas Schöpping
  cd $stm32flashdir
170 1da30dfc Thomas Schöpping
  make 2>&1 | tee -a $LOG_FILE
171 214352ae Thomas Schöpping
  cd $userdir
172 1da30dfc Thomas Schöpping
173 591c0fd3 Thomas Schöpping
  # notify about udev rules
174
  printf "\n"
175
  printf "Using the AMiRo programming cable requires additional Linux udev rules (requires root).\n"
176
  printf "Please follow the instructions in ${amirobltdir}/README.txt file.\n"
177
  read -p "  Understood!"
178
179 1da30dfc Thomas Schöpping
  return 0
180
}
181
182 0a42f078 Thomas Schöpping
### SerialBoot tool setup ######################################################
183
# Builds the SerialBoot tool.
184
# If the tool was built before, it can be deleted and rebuilt.
185
#
186
# usage:      serialBootSetup
187
# arguments:  n/a
188
# return:     0
189
#                 No errort or warning occurred.
190
#             1
191
#                 Warning: Setup aborted by user.
192
#             -1
193
#                 Error: Unexpected user input.
194 e687187f Thomas Schöpping
#             -2
195
#                 Error: Missing dependecny.
196 0a42f078 Thomas Schöpping
#
197 1da30dfc Thomas Schöpping
function serialBootSetup {
198 214352ae Thomas Schöpping
  local amirobltdir=$(dirname $(realpath ${BASH_SOURCE[0]}))
199
  local serialbootdir=${amirobltdir}/Host/Source/SerialBoot
200 0a42f078 Thomas Schöpping
  local userdir=$(pwd)
201 1da30dfc Thomas Schöpping
202 fad4c1e7 Thomas Schöpping
  # check dependencies
203
  checkCommands make cmake gcc g++
204
  if [ $? -ne 0 ]; then
205
    printError "Missing dependencies detected.\n"
206
    return -2
207
  fi
208
209 1da30dfc Thomas Schöpping
  # if a build folder already exists
210
  if [ -d "${serialbootdir}/build/" ]; then
211
    printWarning "SerialBoot has been built before. Delete and rebuild? [y/n]\n"
212 0a42f078 Thomas Schöpping
    local userinput=""
213
    readUserInput "YyNn" userinput
214
    case "$userinput" in
215 1da30dfc Thomas Schöpping
      Y|y)
216 6aa90377 Thomas Schöpping
        printInfo "deleting ${serialbootdir}/build/...\n"
217
        rm -rf "${serialbootdir}/build/"
218 1da30dfc Thomas Schöpping
        ;;
219
      N|n)
220
        printWarning "SerialBoot setup aborted by user\n"
221
        return 1
222
        ;;
223
      *) # sanity check (return error)
224
        printError "unexpected input: $userinput\n";
225
        return -1
226
        ;;
227
    esac
228
  fi
229
230
  # build SerialBoot
231 0a42f078 Thomas Schöpping
  printInfo "compiling SerialBoot...\n"
232 214352ae Thomas Schöpping
  mkdir ${serialbootdir}/build
233
  cd ${serialbootdir}/build
234 1da30dfc Thomas Schöpping
  cmake .. 2>&1 | tee -a $LOG_FILE
235
  make 2>&1 | tee -a $LOG_FILE
236 0a42f078 Thomas Schöpping
  cd $userdir
237 1da30dfc Thomas Schöpping
238 591c0fd3 Thomas Schöpping
  # notify about udev rules
239
  printf "\n"
240
  printf "Using the AMiRo programming cable requires additional Linux udev rules (requires root).\n"
241
  printf "Please follow the instructions in ${amirobltdir}/README.txt file.\n"
242
  read -p "  Understood!"
243
244 1da30dfc Thomas Schöpping
  return 0
245
}
246
247 0a42f078 Thomas Schöpping
### IDE setup ##################################################################
248
# Enter the IDE setup.
249
#
250
# usage:      ideSetup
251
# arguments:  n/a
252
# return:     n/a
253
#
254 1da30dfc Thomas Schöpping
function ideSetup {
255 0a42f078 Thomas Schöpping
  printInfo "entering IDE setup\n"
256 4cce70a8 Thomas Schöpping
  printf "\n"
257 0a42f078 Thomas Schöpping
  if [ -z "$LOG_FILE" ]; then
258 9085c3a0 Thomas Schöpping
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/ide/idesetup.sh --noinfo
259 1da30dfc Thomas Schöpping
  else
260 9085c3a0 Thomas Schöpping
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/ide/idesetup.sh --LOG="$LOG_FILE" --noinfo
261 1da30dfc Thomas Schöpping
  fi
262
}
263 4cce70a8 Thomas Schöpping
264 0a42f078 Thomas Schöpping
### compiler setup #############################################################
265
# Enter the compiler setup.
266
#
267
# usage:      compilerSetup
268
# arguments:  n/a
269
# return:     n/a
270
#
271
function compilerSetup {
272 bba1e03c Thomas Schöpping
  printInfo "entering compiler setup\n"
273 0a42f078 Thomas Schöpping
  printf "\n"
274
  if [ -z "$LOG_FILE" ]; then
275 9085c3a0 Thomas Schöpping
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/compiler/compilersetup.sh --noinfo
276 0a42f078 Thomas Schöpping
  else
277 9085c3a0 Thomas Schöpping
    $(dirname $(realpath ${BASH_SOURCE[0]}))/tools/compiler/compilersetup.sh --LOG="$LOG_FILE" --noinfo
278 1da30dfc Thomas Schöpping
  fi
279 0a42f078 Thomas Schöpping
}
280 4cce70a8 Thomas Schöpping
281 0a42f078 Thomas Schöpping
### main function of this script ###############################################
282
# A setup to initialize dependencies, setup IDE projects and configure the
283
# compiler setup.
284
#
285
# usage:      see function printHelp
286
# arguments:  see function printHelp
287
# return:     0
288
#                 No error or warning occurred.
289
#
290
function main {
291 1da30dfc Thomas Schöpping
  # print welcome/info text if not suppressed
292
  if [[ $@ != *"--noinfo"* ]]; then
293
    printWelcomeText
294
  else
295
    printf "######################################################################\n"
296
  fi
297 4cce70a8 Thomas Schöpping
  printf "\n"
298 1da30dfc Thomas Schöpping
299 1446566f Thomas Schöpping
  # if --help or -h was specified, print the help text and exit
300
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
301
    printHelp
302
    printf "\n"
303
    quitScript
304
  fi
305
306 1da30dfc Thomas Schöpping
  # set log file if specified
307 0a42f078 Thomas Schöpping
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
308 1da30dfc Thomas Schöpping
    # get the parameter (file name)
309
    local cmdidx=1
310 0a42f078 Thomas Schöpping
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
311 1da30dfc Thomas Schöpping
      cmdidx=$[cmdidx + 1]
312 4cce70a8 Thomas Schöpping
    done
313 0a42f078 Thomas Schöpping
    local cmd="${!cmdidx}"
314
    local logfile=""
315
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
316
      logfile=${cmd#*=}
317
    else
318
      local filenameidx=$((cmdidx + 1))
319
      logfile="${!filenameidx}"
320
    fi
321 1da30dfc Thomas Schöpping
    # optionally force silent appending
322 0a42f078 Thomas Schöpping
    if [[ "$cmd" = "--LOG"* ]]; then
323 0dc9f2f9 Thomas Schöpping
      setLogFile --option=c --quiet "$logfile" LOG_FILE
324 1da30dfc Thomas Schöpping
    else
325 0a42f078 Thomas Schöpping
      setLogFile "$logfile" LOG_FILE
326 1da30dfc Thomas Schöpping
      printf "\n"
327
    fi
328
  fi
329
  # log script name
330
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
331
332 0a42f078 Thomas Schöpping
  # parse arguments
333
  local otherargs=()
334
  while [ $# -gt 0 ]; do
335
    if ( parseIsOption $1 ); then
336
      case "$1" in
337
        -h|--help) # already handled; ignore
338
          shift 1;;
339
        -f|--stm32flash)
340
          stm32flashSetup; printf "\n"; shift 1;;
341
        -s|--SerialBoot)
342
          serialBootSetup; printf "\n"; shift 1;;
343
        -e|--IDE)
344
          ideSetup; printf "\n"; shift 1;;
345
        -c|--compiler)
346
          compilerSetup; printf "\n"; shift 1;;
347
        -q|--quit)
348 5690d84c Thomas Schöpping
          quitScript; shift 1;;
349 0a42f078 Thomas Schöpping
        --log=*|--LOG=*) # already handled; ignore
350
          shift 1;;
351
        --log|--LOG) # already handled; ignore
352
          shift 2;;
353
        --noinfo) # already handled; ignore
354
          shift 1;;
355
        *)
356
          printError "invalid option: $1\n"; shift 1;;
357
      esac
358
    else
359
      otherargs+=("$1")
360
      shift 1
361
    fi
362 1da30dfc Thomas Schöpping
  done
363 6886c3b6 Thomas Schöpping
364 1da30dfc Thomas Schöpping
  # interactive menu
365 0a42f078 Thomas Schöpping
  while ( true ); do
366 1da30dfc Thomas Schöpping
    # main menu info prompt and selection
367 0a42f078 Thomas Schöpping
    printInfo "AMiRo-BLT setup main menu\n"
368 1da30dfc Thomas Schöpping
    printf "Please select one of the following actions:\n"
369
    printf "  [F] - get and build stm32flash tool\n"
370
    printf "  [S] - build SerialBoot tool\n"
371
    printf "  [E] - IDE project setup\n"
372 0a42f078 Thomas Schöpping
    printf "  [C] - enter compiler setup\n"
373 1da30dfc Thomas Schöpping
    printf "  [Q] - quit this setup\n"
374 0a42f078 Thomas Schöpping
    local userinput=""
375
    readUserInput "FfSsEeCcQq" userinput
376 1da30dfc Thomas Schöpping
    printf "\n"
377 6886c3b6 Thomas Schöpping
378 1da30dfc Thomas Schöpping
    # evaluate user selection
379 0a42f078 Thomas Schöpping
    case "$userinput" in
380 1da30dfc Thomas Schöpping
      F|f)
381 0a42f078 Thomas Schöpping
        stm32flashSetup; printf "\n";;
382 1da30dfc Thomas Schöpping
      S|s)
383 0a42f078 Thomas Schöpping
        serialBootSetup; printf "\n";;
384 1da30dfc Thomas Schöpping
      E|e)
385 0a42f078 Thomas Schöpping
        ideSetup; printf "\n";;
386
      C|c)
387
        compilerSetup; printf "\n";;
388 1da30dfc Thomas Schöpping
      Q|q)
389 5690d84c Thomas Schöpping
        quitScript;;
390 1da30dfc Thomas Schöpping
      *) # sanity check (exit with error)
391 0a42f078 Thomas Schöpping
        printError "unexpected argument: $userinput\n";;
392 1da30dfc Thomas Schöpping
    esac
393
  done
394 4cce70a8 Thomas Schöpping
395 1da30dfc Thomas Schöpping
  exit 0
396
}
397 4cce70a8 Thomas Schöpping
398 1da30dfc Thomas Schöpping
################################################################################
399
# SCRIPT ENTRY POINT                                                           #
400
################################################################################
401 4cce70a8 Thomas Schöpping
402 1da30dfc Thomas Schöpping
main "$@"