amiro-blt / tools / compiler / GCC / gccsetup.sh @ f7d2c786
History | View | Annotate | Download (33.905 KB)
1 | 0a42f078 | Thomas Schöpping | ################################################################################ |
---|---|---|---|
2 | # AMiRo-BLT is an bootloader and toolchain designed for the Autonomous Mini # |
||
3 | # Robot (AMiRo) platform. # |
||
4 | 449d916a | Thomas Schöpping | # Copyright (C) 2016..2019 Thomas Schöpping et al. # |
5 | 0a42f078 | 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 | ################################################################################ |
||
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 | 3719a40a | Thomas Schöpping | printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n" |
110 | printf "\n" |
||
111 | 0a42f078 | Thomas Schöpping | 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 | 0dc9f2f9 | Thomas Schöpping | # Possible values are 'a', 'c', 'r' and 'n'. |
173 | # - a: append (starts with a separator) |
||
174 | # - c: continue (does not insert a seperator) |
||
175 | 0a42f078 | Thomas Schöpping | # - 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 | 0dc9f2f9 | Thomas Schöpping | a|c) |
231 | 0a42f078 | Thomas Schöpping | if [ $quiet = false ]; then |
232 | printInfo "appending log to $filepath\n" |
||
233 | fi |
||
234 | 0dc9f2f9 | Thomas Schöpping | if [ $option != c ]; then |
235 | printf "\n" >> $filepath |
||
236 | printf "######################################################################\n" >> $filepath |
||
237 | printf "\n" >> $filepath |
||
238 | fi |
||
239 | 0a42f078 | Thomas Schöpping | ;; |
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 | fad4c1e7 | Thomas Schöpping | ### check whether commands are available ####################################### |
267 | # Checks whether the specified commands are available and can be executed. |
||
268 | # |
||
269 | # usage: checkCommand [<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 | 0a42f078 | Thomas Schöpping | ################################################################################ |
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 GCC setup! #\n" |
||
315 | printf "# #\n" |
||
316 | printf "######################################################################\n" |
||
317 | printf "# #\n" |
||
318 | 449d916a | Thomas Schöpping | printf "# Copyright (c) 2016..2019 Thomas Schöpping #\n" |
319 | 0a42f078 | Thomas Schöpping | 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] [-i|--install] [-c|--change] [-q|--quit] [--log=<file>]\n" |
||
341 | printf "\n" |
||
342 | printf "options: -h, --help\n" |
||
343 | printf " Print this help text.\n" |
||
344 | printf " -i, --install\n" |
||
345 | printf " Install another version.\n" |
||
346 | 316a2b34 | Thomas Schöpping | printf " -u, --uninstall\n" |
347 | printf " Unistall a version.\n" |
||
348 | 0a42f078 | Thomas Schöpping | printf " -c, --change\n" |
349 | printf " Change the default version.\n" |
||
350 | printf " -q, --quit\n" |
||
351 | printf " Quit the script.\n" |
||
352 | printf " --log=<file>\n" |
||
353 | printf " Specify a log file.\n" |
||
354 | } |
||
355 | |||
356 | ### detect installed versions ################################################## |
||
357 | # Detect all installed version of arm-none-eabi-gcc, if any. |
||
358 | # |
||
359 | 316a2b34 | Thomas Schöpping | # usage: detectInstalledVersions <binarray> <current> [<current_idx>] |
360 | 0a42f078 | Thomas Schöpping | # arguments: <binarray> |
361 | # Array variable to store all detected binary paths to. |
||
362 | # <current> |
||
363 | # Variable to store the currently active binary to. |
||
364 | 316a2b34 | Thomas Schöpping | # <current_idx> |
365 | # Index of the curretly selected version in the output array (<binarray>). |
||
366 | 0a42f078 | Thomas Schöpping | # return: n/a |
367 | # |
||
368 | function detectInstalledVersions { |
||
369 | local armgcc_command=$(command -v arm-none-eabi-gcc) |
||
370 | local armgcc_commanddir=${HOME}/gcc-none-eabi |
||
371 | local armgcc_currentbin="" |
||
372 | local armgcc_installdir=${HOME}/gcc-none-eabi |
||
373 | local armgcc_bins=() |
||
374 | local armgcc_bincnt=0 |
||
375 | |||
376 | # check for already installed versions |
||
377 | if [ -n "$armgcc_command" ]; then |
||
378 | # follow the link to the actual binary |
||
379 | armgcc_commanddir=$(dirname $armgcc_command) |
||
380 | armgcc_currentbin=$armgcc_command |
||
381 | while [ -L $armgcc_currentbin ]; do |
||
382 | 316a2b34 | Thomas Schöpping | # differentiate between relative and absolute paths |
383 | if [[ $(readlink $armgcc_currentbin) = /* ]]; then |
||
384 | armgcc_currentbin=$(readlink $armgcc_currentbin) |
||
385 | else |
||
386 | armgcc_currentbin=$(realpath $(dirname $armgcc_currentbin)/$(readlink $armgcc_currentbin)) |
||
387 | fi |
||
388 | 0a42f078 | Thomas Schöpping | done |
389 | # the installation location is assumed to be two directories up |
||
390 | 316a2b34 | Thomas Schöpping | armgcc_installdir=$(realpath $(dirname ${armgcc_currentbin})/../..) |
391 | 0a42f078 | Thomas Schöpping | # list all detected instalations |
392 | for dir in $(ls -d ${armgcc_installdir}/*/); do |
||
393 | if [ -f ${dir}/bin/arm-none-eabi-gcc ]; then |
||
394 | armgcc_bins[$armgcc_bincnt]=${dir}bin/arm-none-eabi-gcc |
||
395 | armgcc_bincnt=$((armgcc_bincnt + 1)) |
||
396 | fi |
||
397 | done |
||
398 | |||
399 | 316a2b34 | Thomas Schöpping | # set the output variables |
400 | eval "$1=(${armgcc_bins[*]})" |
||
401 | eval $2="$armgcc_currentbin" |
||
402 | if [ -n "$3" ]; then |
||
403 | for (( bin=0; bin<${#armgcc_bins[@]}; ++bin )); do |
||
404 | if [ ${armgcc_bins[bin]} = "$armgcc_currentbin" ]; then |
||
405 | eval $3=$bin |
||
406 | fi |
||
407 | done |
||
408 | fi |
||
409 | else |
||
410 | eval "$1=()" |
||
411 | eval $2="" |
||
412 | if [ -n "$3" ]; then |
||
413 | eval $3="" |
||
414 | fi |
||
415 | fi |
||
416 | 0a42f078 | Thomas Schöpping | } |
417 | |||
418 | ### install new version ######################################################## |
||
419 | # Fetches an installation package from the internet, installs it and expands |
||
420 | # the $PATH environment variable (via .bashrc) if required. |
||
421 | # |
||
422 | # usage: installNewVersion [-i|--install=<path>] [-l|--link=<path>] |
||
423 | # argumenst: -i, --install <path> |
||
424 | # Path where to install the new version to. |
||
425 | # -l, --link <path> |
||
426 | # Path where to create according links. |
||
427 | # return: 0 |
||
428 | # No error or warnign occurred. |
||
429 | # 1 |
||
430 | # Warning: Installation aborted by user. |
||
431 | 2880bac8 | Thomas Schöpping | # -1 |
432 | # Error: specified URL can not be reached. |
||
433 | 0a42f078 | Thomas Schöpping | # |
434 | function installNewVersion { |
||
435 | local installbasedir=${HOME}/gcc-arm-embedded |
||
436 | local linkdir="/usr/bin" |
||
437 | |||
438 | # parse arguments |
||
439 | local otherargs=() |
||
440 | while [ $# -gt 0 ]; do |
||
441 | if ( parseIsOption $1 ); then |
||
442 | case "$1" in |
||
443 | -i=*|--install=*) |
||
444 | installbasedir=$(realpath "${1#*=}"); shift 1;; |
||
445 | -i|--install) |
||
446 | installbasedir="$2"; shift 2;; |
||
447 | -l=*|--link=*) |
||
448 | linkdir=$(realpath "${1#*=}"); shift 1;; |
||
449 | -l|--link) |
||
450 | linkdir="$2"; shift 2;; |
||
451 | *) # sanity check (exit with error) |
||
452 | printError "invalid option: $1\n"; shift 1;; |
||
453 | esac |
||
454 | else |
||
455 | otherargs+=("$1") |
||
456 | shift 1 |
||
457 | fi |
||
458 | done |
||
459 | |||
460 | # read download URL form user |
||
461 | printLog "read installation url from user\n" |
||
462 | local armgcc_downloadurl="" |
||
463 | 316a2b34 | Thomas Schöpping | while [ -z "$armgcc_downloadurl" ]; do |
464 | 0a42f078 | Thomas Schöpping | read -p "Download link for the installation file: " -e armgcc_downloadurl |
465 | 316a2b34 | Thomas Schöpping | if [ -z "$armgcc_downloadurl" ]; then |
466 | printWarning "installation aborted by user\n" |
||
467 | return 1 |
||
468 | fi |
||
469 | 2880bac8 | Thomas Schöpping | # check whether url is valid |
470 | wget --spider -r "$armgcc_downloadurl" &>/dev/null |
||
471 | if [ $? -ne 0 ]; then |
||
472 | printError "'$armgcc_downloadurl' can not be reached\n" |
||
473 | return -1 |
||
474 | 0a42f078 | Thomas Schöpping | fi |
475 | done |
||
476 | printLog "user selected $armgcc_downloadurl\n" |
||
477 | |||
478 | # if the file already exists, ask the user if it should be downloaded again |
||
479 | 2880bac8 | Thomas Schöpping | local armgcc_tarball=$(basename $(wget --spider -r "$armgcc_downloadurl" 2>&1 | \ |
480 | grep "^--" | \ |
||
481 | tail -n 1 | \ |
||
482 | awk '{print $NF}')) |
||
483 | if [ -f "$armgcc_tarball" ]; then |
||
484 | 0a42f078 | Thomas Schöpping | printWarning "$armgcc_tarball already exists. Delete and redownload? [y/n]\n" |
485 | local userinput="" |
||
486 | readUserInput "YyNn" userinput |
||
487 | case "$userinput" in |
||
488 | Y|y) |
||
489 | rm "$armgcc_tarball" |
||
490 | 2880bac8 | Thomas Schöpping | wget "$armgcc_downloadurl" -O "$armgcc_tarball" | tee -a $LOG_FILE |
491 | 0a42f078 | Thomas Schöpping | ;; |
492 | N|n) |
||
493 | ;; |
||
494 | *) # sanity check (exit with error) |
||
495 | printError "unexpected argument: $userinput\n";; |
||
496 | esac |
||
497 | else |
||
498 | 2880bac8 | Thomas Schöpping | wget "$armgcc_downloadurl" -O "$armgcc_tarball" | tee -a $LOG_FILE |
499 | 0a42f078 | Thomas Schöpping | fi |
500 | |||
501 | # extract tarball |
||
502 | printInfo "extracting ${armgcc_tarball}...\n" |
||
503 | tar -jxf "$armgcc_tarball" | tee -a $LOG_FILE |
||
504 | local compilerdir=`tar --bzip2 -tf ${armgcc_tarball} | sed -e 's@/.*@@' | uniq` |
||
505 | |||
506 | 72294488 | Thomas Schöpping | # install gcc arm embedded |
507 | 0a42f078 | Thomas Schöpping | printLog "read installation directory from user\n" |
508 | local installdir="" |
||
509 | read -p "Installation directory: " -i ${installbasedir}/${compilerdir} -e installdir |
||
510 | printLog "user selected $installdir\n" |
||
511 | dab959cd | Thomas Schöpping | linkdir=$(dirname ${installdir}) |
512 | 0a42f078 | Thomas Schöpping | printLog "read link directory\n" |
513 | read -p "Link directory: " -i $linkdir -e linkdir |
||
514 | printLog "user selected $linkdir\n" |
||
515 | # if the installation path already exists, ask user to overwrite |
||
516 | if [ -d "$installdir" ]; then |
||
517 | printWarning "$installdir already exists. Overwrite? [y/n]\n" |
||
518 | local userinput="" |
||
519 | readUserInput "YyNn" userinput |
||
520 | case "$userinput" in |
||
521 | Y|y) |
||
522 | ;; |
||
523 | N|n) |
||
524 | printWarning "installation aborted by user\n" |
||
525 | return 1 |
||
526 | ;; |
||
527 | *) # sanity check (exit with error) |
||
528 | printError "invalid option: $userinput\n";; |
||
529 | esac |
||
530 | # make sure the whole ínstallation path exists |
||
531 | else |
||
532 | while [ ! -d $(dirname "$installdir") ]; do |
||
533 | local dir=$(dirname "$installdir") |
||
534 | while [ ! -d $(dirname "$dir") ]; do |
||
535 | dir=$(dirname "$dir") |
||
536 | done |
||
537 | echo "mkdir $dir" |
||
538 | mkdir "$dir" |
||
539 | done |
||
540 | fi |
||
541 | # copy the extracted compiler folder |
||
542 | cp -fR "$compilerdir" "$installdir" |
||
543 | # make sure whole link path exists |
||
544 | while [ ! -d "$linkdir" ]; do |
||
545 | local dir="$linkdir" |
||
546 | while [ ! -d $(dirname "$linkdir") ]; do |
||
547 | dir=$(dirname "$dir") |
||
548 | done |
||
549 | mkdir "$dir" |
||
550 | done |
||
551 | # create / overwrite links |
||
552 | 56360b33 | Thomas Schöpping | local linkpath=$(realpath --relative-base=$linkdir ${installdir}/bin/) |
553 | ls ${installdir}/bin/ | xargs -i ln -sf ${linkpath}/{} ${linkdir}/{} |
||
554 | 0a42f078 | Thomas Schöpping | printInfo "default version set to $(arm-none-eabi-gcc -dumpversion)\n" |
555 | |||
556 | # append the link directory to the PATH environment variable if required |
||
557 | if [[ ! "$linkdir" = *"$PATH"* ]]; then |
||
558 | local bashrc_file=${HOME}/.bashrc |
||
559 | local bashrc_identifier="##### AMiRo ENVIRONMENT CONFIGURATION #####" |
||
560 | local bashrc_note="# DO NOT EDIT THESE LINES MANUALLY!" |
||
561 | local bashrc_entry="export PATH=\$PATH:$linkdir" |
||
562 | |||
563 | # find and edit old entry, or append a new one to the file |
||
564 | local bashrc_idlines=$(grep -x -n "$bashrc_identifier" "$bashrc_file" | cut -f1 -d:) # string of line numbers |
||
565 | bashrc_idlines=(${bashrc_idlines//"\n"/" "}) # array of line numbers |
||
566 | case ${#bashrc_idlines[@]} in |
||
567 | |||
568 | # append a new entry to the BASHRC_FILE |
||
569 | 0) |
||
570 | # make sure the last line is empty |
||
571 | if [[ ! $(tail -1 $bashrc_file) =~ ^[\ \t]*$ ]]; then |
||
572 | printf "\n" >> $bashrc_file |
||
573 | fi |
||
574 | # append text to file |
||
575 | sed -i '$a'"$bashrc_identifier\n$bashrc_note\n$bashrc_entry\n$bashrc_identifier\n" $bashrc_file |
||
576 | # print note |
||
577 | printInfo "Your $bashrc_file has been updated. You need to source it to apply the changes in your environment.\n" |
||
578 | read -p " Understood!" |
||
579 | ;; |
||
580 | |||
581 | # extend the old entry |
||
582 | 2) |
||
583 | # don't do anything if the line is already present |
||
584 | local bashrc_entrylines=$(grep -x -n "$bashrc_entry" $bashrc_file | cut -f1 -d:) # string of line numbers |
||
585 | bashrc_entrylines=(${bashrc_entrylines//"\n"/" "}) # array of line numbers |
||
586 | if [[ ${#bashrc_entrylines[@]} = 0 ]]; then |
||
587 | # insert the entry before the closing identifier |
||
588 | sed -i "${bashrc_idlines[1]}"'i'"$bashrc_entry" $bashrc_file |
||
589 | # print note |
||
590 | printInfo "Your $bashrc_file has been updated. You need to source it to apply the changes in your environment.\n" |
||
591 | read -p " Understood!" |
||
592 | elif [[ ${#bashrc_entrylines[@]} -eq 1 && ( ${bashrc_entrylines[0]} -lt ${bashrc_idlines[0]} || ${bashrc_entrylines[0]} -gt ${bashrc_idlines[1]} ) ]]; then |
||
593 | # print an error that there is an entry at the wrong place |
||
594 | printError "corrupted entry in your $bashrc_file detected\n" |
||
595 | printf "The following entry was found at the wrong place:\n" |
||
596 | printf "\n" |
||
597 | printf "$bashrc_entry\n" |
||
598 | printf "\n" |
||
599 | printf "To fix this, delete the line and rerun this setup.\n" |
||
600 | read -p " Understood!" |
||
601 | elif [[ ${#bashrc_entrylines[@]} -gt 1 ]]; then |
||
602 | # print an error that there are multiple entries |
||
603 | printError "corrupted entry in your $bashrc_file detected\n" |
||
604 | printf "There are multiple identical entries in your $bashrc_file file.\n" |
||
605 | printf "To fix it, make sure that it contains the following line exactly once:\n" |
||
606 | printf "\n" |
||
607 | printf "$bashrc_entry\n" |
||
608 | printf "\n" |
||
609 | read -p " Understood!" |
||
610 | fi |
||
611 | ;; |
||
612 | |||
613 | # error state (corrupted entry detected) |
||
614 | *) |
||
615 | printError "unable to append link directory to \$PATH variable\n" |
||
616 | printf "There seems to be a broken entry in your $bashrc_file file.\n" |
||
617 | printf "To fix it, make sure that the following line appears exactly twice and encloses your AMiRo related settings:\n" |
||
618 | printf "\n" |
||
619 | printf "$bashrc_identifier\n" |
||
620 | printf "\n" |
||
621 | read -p " Understood!" |
||
622 | ;; |
||
623 | esac |
||
624 | fi |
||
625 | |||
626 | # clean up the current directory |
||
627 | rm "$armgcc_tarball" |
||
628 | rm -rf "$compilerdir" |
||
629 | |||
630 | return 0 |
||
631 | } |
||
632 | |||
633 | 316a2b34 | Thomas Schöpping | ### uninstall a version ######################################################## |
634 | # Select an installed version and uninstall it from the system. |
||
635 | # |
||
636 | # usage: uninstallVersion <versions> <current_idx> <linkdir> |
||
637 | # arguments: <version> |
||
638 | # Array of available versions (full path to binary). |
||
639 | # <current_idx> |
||
640 | # Index of the currently selected version in the array. |
||
641 | # <linkdir> |
||
642 | # Path where to delete old links. |
||
643 | # return: 0 |
||
644 | # No error or warning occurred. |
||
645 | # 1 |
||
646 | # Warning: Installation aborted by user. |
||
647 | # -1 |
||
648 | # Error: An exception occurred. |
||
649 | # |
||
650 | function uninstallVersion { |
||
651 | local versions=("${!1}") |
||
652 | local current_idx="$2" |
||
653 | local linkdir="$3" |
||
654 | |||
655 | # check whether at least two installations were detected |
||
656 | if [ ${#versions[@]} -eq 0 ]; then |
||
657 | printError "no installation detected\n" |
||
658 | return -1 |
||
659 | else |
||
660 | # print all available versions |
||
661 | printInfo "choose the installation to uninstall to or type 'A' to abort:\n" |
||
662 | for (( cnt=0; cnt<${#versions[@]}; ++cnt )); do |
||
663 | if [ $cnt -eq $current_idx ]; then |
||
664 | printf "*%3u: %s\n" $(($cnt + 1)) ${versions[$cnt]} |
||
665 | else |
||
666 | printf " %3u: %s\n" $(($cnt + 1)) ${versions[$cnt]} |
||
667 | fi |
||
668 | done |
||
669 | |||
670 | # read user selection |
||
671 | printLog "read user slection\n" |
||
672 | local userinput="" |
||
673 | while [ -z $userinput ] ; do |
||
674 | read -p "your selection: " -e userinput |
||
675 | printLog "user selection: $userinput\n" |
||
676 | if [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#versions[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; then |
||
677 | printWarning "Please enter an integer between 1 and ${#versions[@]} or 'A' to abort.\n" |
||
678 | userinput="" |
||
679 | fi |
||
680 | if [ ${#versions[@]} -gt 1 ] && [ $((userinput - 1)) -eq $current_idx ]; then |
||
681 | printWarning "Unable to uninstall currently selected version (as long as there are others).\n" |
||
682 | userinput="" |
||
683 | fi |
||
684 | done |
||
685 | |||
686 | if [[ "$userinput" =~ ^[Aa]$ ]]; then |
||
687 | printWarning "aborted by user\n" |
||
688 | return 1 |
||
689 | else |
||
690 | local idx=$((userinput - 1)) |
||
691 | printf "\n" |
||
692 | # prompt selected and aks user for confirmation |
||
693 | printInfo "${versions[$idx]} will be removed. Continue? [y/n]\n" |
||
694 | readUserInput "YyNn" userinput |
||
695 | case "$userinput" in |
||
696 | Y|y) |
||
697 | ;; |
||
698 | N|n) |
||
699 | printWarning "uninstallation process aborted by user\n" |
||
700 | return 1 |
||
701 | ;; |
||
702 | *) # sanity check (exit with error) |
||
703 | printError "invalid option: $userinput\n" |
||
704 | return -1 |
||
705 | ;; |
||
706 | esac |
||
707 | # find and delete any links pointing to the version to be deleted |
||
708 | for link in `find $linkdir -maxdepth 1 -type l`; do |
||
709 | local l=$link |
||
710 | # follow the link to the actual binary |
||
711 | while [ -L $l ]; do |
||
712 | # differentiate between relative and absolute paths |
||
713 | if [[ $(readlink $l) = /* ]]; then |
||
714 | l=$(readlink $l) |
||
715 | else |
||
716 | l=$(realpath $(dirname $l)/$(readlink $l)) |
||
717 | fi |
||
718 | done |
||
719 | # delete the link if it points to the version to be uninstalled |
||
720 | if [ $(dirname $l) == $(dirname ${versions[$idx]}) ]; then |
||
721 | rm $link |
||
722 | fi |
||
723 | done |
||
724 | # delete the version directory (assumed to be one directory up) |
||
725 | rm -rf $(realpath $(dirname ${versions[$idx]})/..) |
||
726 | printInfo "${versions[$idx]} has been removed.\n" |
||
727 | fi |
||
728 | fi |
||
729 | |||
730 | return 0 |
||
731 | } |
||
732 | |||
733 | 0a42f078 | Thomas Schöpping | ### change default version ##################################################### |
734 | # Change the default arm-none-eabi-gcc version. |
||
735 | # |
||
736 | 316a2b34 | Thomas Schöpping | # usage: changeDefaultVersion <versions> <linkdir> |
737 | 0a42f078 | Thomas Schöpping | # argumenst: <versions> |
738 | # Array of available versions (full path to binary). |
||
739 | # <linkdir> |
||
740 | # Path where to delete old and create new links. |
||
741 | # return: 0 |
||
742 | # No error or warnign occurred. |
||
743 | # -1 |
||
744 | # Error: no installation detected. |
||
745 | # |
||
746 | function changeDefaultVersion { |
||
747 | local versions=("${!1}") |
||
748 | local linkdir="$2" |
||
749 | |||
750 | # check whether an installation was detected |
||
751 | if [ ${#versions[@]} -eq 0 ]; then |
||
752 | printError "no installation detected\n" |
||
753 | return -1 |
||
754 | else |
||
755 | # print all available versions |
||
756 | printInfo "choose the installation to switch to or type 'A' to abort:\n" |
||
757 | for (( cnt=0; cnt<${#versions[@]}; ++cnt )); do |
||
758 | printf " %2u: %s\n" $(($cnt + 1)) ${versions[$cnt]} |
||
759 | done |
||
760 | |||
761 | # read user selection |
||
762 | printLog "read user slection\n" |
||
763 | local userinput="" |
||
764 | while [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#versions[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; do |
||
765 | read -p "your selection: " -e userinput |
||
766 | printLog "user selection: $userinput\n" |
||
767 | if [[ ! "$userinput" =~ ^[0-9]+$ ]] || [ ! "$userinput" -gt 0 ] || [ ! "$userinput" -le ${#versions[@]} ] && [[ ! "$userinput" =~ ^[Aa]$ ]]; then |
||
768 | printWarning "Please enter an integer between 1 and ${#versions[@]} or 'A' to abort.\n" |
||
769 | fi |
||
770 | done |
||
771 | |||
772 | if [[ "$userinput" =~ ^[Aa]$ ]]; then |
||
773 | printWarning "aborted by user\n" |
||
774 | else |
||
775 | local idx=$((userinput - 1)) |
||
776 | # find and delete old links |
||
777 | rm `find $linkdir -maxdepth 1 -type l | grep -Ev "*[0-9]\.[0-9]\.[0-9]"` |
||
778 | 56360b33 | Thomas Schöpping | # create new links with relative or absolute paths |
779 | 0a42f078 | Thomas Schöpping | local bindir=$(dirname ${versions[$idx]}) |
780 | 56360b33 | Thomas Schöpping | local linkpath=$(realpath --relative-base=$linkdir $bindir) |
781 | ls $bindir | xargs -i ln -sf $linkpath/{} $linkdir/{} |
||
782 | 0a42f078 | Thomas Schöpping | printInfo "default version set to $(arm-none-eabi-gcc -dumpversion)\n" |
783 | fi |
||
784 | fi |
||
785 | |||
786 | return 0 |
||
787 | } |
||
788 | |||
789 | ### main function of this script ############################################### |
||
790 | # The IDE setup lets the user select an IDE of choice. |
||
791 | # As of now, only QtCreator is supported. |
||
792 | # |
||
793 | # usage: see function printHelp |
||
794 | # arguments: see function printHelp |
||
795 | # return: 0 |
||
796 | # No error or warning occurred. |
||
797 | # |
||
798 | function main { |
||
799 | # print welcome/info text if not suppressed |
||
800 | if [[ $@ != *"--noinfo"* ]]; then |
||
801 | printWelcomeText |
||
802 | else |
||
803 | printf "######################################################################\n" |
||
804 | fi |
||
805 | printf "\n" |
||
806 | |||
807 | 1446566f | Thomas Schöpping | # if --help or -h was specified, print the help text and exit |
808 | if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then |
||
809 | printHelp |
||
810 | printf "\n" |
||
811 | quitScript |
||
812 | fi |
||
813 | |||
814 | 0a42f078 | Thomas Schöpping | # set log file if specified |
815 | if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then |
||
816 | # get the parameter (file name) |
||
817 | local cmdidx=1 |
||
818 | while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do |
||
819 | cmdidx=$[cmdidx + 1] |
||
820 | done |
||
821 | local cmd="${!cmdidx}" |
||
822 | local logfile="" |
||
823 | if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then |
||
824 | logfile=${cmd#*=} |
||
825 | else |
||
826 | local filenameidx=$((cmdidx + 1)) |
||
827 | logfile="${!filenameidx}" |
||
828 | fi |
||
829 | # optionally force silent appending |
||
830 | if [[ "$cmd" = "--LOG"* ]]; then |
||
831 | 0dc9f2f9 | Thomas Schöpping | setLogFile --option=c --quiet "$logfile" LOG_FILE |
832 | 0a42f078 | Thomas Schöpping | else |
833 | setLogFile "$logfile" LOG_FILE |
||
834 | printf "\n" |
||
835 | fi |
||
836 | fi |
||
837 | # log script name |
||
838 | printLog "this is $(realpath ${BASH_SOURCE[0]})\n" |
||
839 | |||
840 | # detect installed versions and inform user |
||
841 | local installedversions=() |
||
842 | local currentversion="" |
||
843 | 316a2b34 | Thomas Schöpping | local currentversionidx="n/a" |
844 | detectInstalledVersions installedversions currentversion currentversionidx |
||
845 | 0a42f078 | Thomas Schöpping | case "${#installedversions[@]}" in |
846 | 0) |
||
847 | printInfo "no installation has been detected\n";; |
||
848 | 1) |
||
849 | printInfo "1 installation has been detected:\n";; |
||
850 | *) |
||
851 | printInfo "${#installedversions[@]} installations have been detected:\n";; |
||
852 | esac |
||
853 | for (( idx=0; idx<${#installedversions[@]}; ++idx )); do |
||
854 | if [ ${installedversions[$idx]} = "$currentversion" ]; then |
||
855 | printInfo " * ${installedversions[$idx]}\n" |
||
856 | else |
||
857 | printInfo " ${installedversions[$idx]}\n" |
||
858 | fi |
||
859 | done |
||
860 | printf "\n" |
||
861 | |||
862 | # parse arguments |
||
863 | local otherargs=() |
||
864 | while [ $# -gt 0 ]; do |
||
865 | if ( parseIsOption $1 ); then |
||
866 | case "$1" in |
||
867 | -h|--help) # already handled; ignore |
||
868 | shift 1;; |
||
869 | 316a2b34 | Thomas Schöpping | -i|--install) |
870 | 0a42f078 | Thomas Schöpping | if [ -z "$currentversion" ]; then |
871 | installNewVersion |
||
872 | else |
||
873 | 316a2b34 | Thomas Schöpping | installNewVersion --install=$(realpath $(dirname $currentversion)/../..) --link=$(realpath $(dirname $currentversion)/../..) |
874 | fi |
||
875 | detectInstalledVersions installedversions currentversion currentversionidx |
||
876 | printf "\n"; shift 1;; |
||
877 | -u|--uninstall) |
||
878 | if [ ! -z "$currentversion" ]; then |
||
879 | uninstallVersion installedversions[@] $currentversionidx $(realpath $(dirname $currentversion)/../..) |
||
880 | detectInstalledVersions installedversions currentversion currentversionidx |
||
881 | else |
||
882 | printError "no installation detected\n" |
||
883 | 0a42f078 | Thomas Schöpping | fi |
884 | printf "\n"; shift 1;; |
||
885 | -c|--change) |
||
886 | 316a2b34 | Thomas Schöpping | if [ ! -z "$currentversion" ]; then |
887 | changeDefaultVersion installedversions[@] $(realpath $(dirname $currentversion)/../..) |
||
888 | else |
||
889 | printError "no installation detected\n" |
||
890 | fi |
||
891 | printf "\n"; shift 1;; |
||
892 | 0a42f078 | Thomas Schöpping | -q|--quit) |
893 | quitScript; shift 1;; |
||
894 | --log=*|--LOG=*) # already handled; ignore |
||
895 | shift 1;; |
||
896 | --log|--LOG) # already handled; ignore |
||
897 | shift 2;; |
||
898 | --noinfo) # already handled; ignore |
||
899 | shift 1;; |
||
900 | *) |
||
901 | printError "invalid option: $1\n"; shift 1;; |
||
902 | esac |
||
903 | else |
||
904 | otherargs+=("$1") |
||
905 | shift 1 |
||
906 | fi |
||
907 | done |
||
908 | |||
909 | # interactive menu |
||
910 | while ( true ); do |
||
911 | # main menu info prompt and selection |
||
912 | printInfo "GCC setup main menu\n" |
||
913 | printf "Please select one of the following actions:\n" |
||
914 | printf " [I] - install another version\n" |
||
915 | 316a2b34 | Thomas Schöpping | printf " [U] - uninstall a version\n" |
916 | 0a42f078 | Thomas Schöpping | printf " [C] - change default version\n" |
917 | printf " [Q] - quit this setup\n" |
||
918 | local userinput="" |
||
919 | 316a2b34 | Thomas Schöpping | readUserInput "IiUuCcQq" userinput |
920 | 0a42f078 | Thomas Schöpping | printf "\n" |
921 | |||
922 | # evaluate user selection |
||
923 | case "$userinput" in |
||
924 | I|i) |
||
925 | if [ -z "$currentversion" ]; then |
||
926 | installNewVersion |
||
927 | else |
||
928 | 316a2b34 | Thomas Schöpping | installNewVersion --install=$(realpath $(dirname $currentversion)/../..) --link=$(realpath $(dirname $currentversion)/../..) |
929 | fi |
||
930 | detectInstalledVersions installedversions currentversion currentversionidx |
||
931 | printf "\n";; |
||
932 | U|u) |
||
933 | if [ ! -z "$currentversion" ]; then |
||
934 | uninstallVersion installedversions[@] $currentversionidx $(realpath $(dirname $currentversion)/../..) |
||
935 | detectInstalledVersions installedversions currentversion currentversionidx |
||
936 | else |
||
937 | printError "no installation detected\n" |
||
938 | 0a42f078 | Thomas Schöpping | fi |
939 | printf "\n";; |
||
940 | C|c) |
||
941 | 316a2b34 | Thomas Schöpping | if [ ! -z "$currentversion" ]; then |
942 | changeDefaultVersion installedversions[@] $(realpath $(dirname $currentversion)/../..) |
||
943 | else |
||
944 | printError "no installation detected\n" |
||
945 | fi |
||
946 | printf "\n";; |
||
947 | 0a42f078 | Thomas Schöpping | Q|q) |
948 | quitScript;; |
||
949 | *) # sanity check (exit with error) |
||
950 | printError "unexpected argument: $userinput\n";; |
||
951 | esac |
||
952 | done |
||
953 | |||
954 | exit 0 |
||
955 | } |
||
956 | |||
957 | ################################################################################ |
||
958 | # SCRIPT ENTRY POINT # |
||
959 | ################################################################################ |
||
960 | |||
961 | main "$@" |