Statistics
| Branch: | Revision:

urtware / tools / ide / QtCreator / QtCreatorSetup.sh @ 1fb06240

History | View | Annotate | Download (15.682 KB)

1
################################################################################
2
# AMiRo-uRtWare is a collection of applications and configurations for the     #
3
# Autonomous Mini Robot (AMiRo).                                               #
4
# Copyright (C) 2018..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]})/../../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 QtCreator setup!                   #\n"
40
  printf "#                                                                    #\n"
41
  printf "######################################################################\n"
42
  printf "#                                                                    #\n"
43
  printf "# Copyright (c) 2018..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] [-c|--clean] [-w|--wipe] [-q|--quit] [--log=<file>]\n"
66
  printf "\n"
67
  printf "options:  -h, --help\n"
68
  printf "              Print this help text.\n"
69
  printf "          -i, --initialize\n"
70
  printf "              Initialize project files.\n"
71
  printf "          -c, --clean\n"
72
  printf "              Delete project files.\n"
73
  printf "          -q, --quit\n"
74
  printf "              Quit the script.\n"
75
}
76

    
77
### read directory where to create/delete projects #############################
78
# Read the directory where to create/delete project files from user.
79
#
80
# usage:      getProjectDir <pathvar>
81
# arguments:  <pathvar>
82
#                 Variable to store the selected path to.
83
# return:     n/a
84
#
85
function getProjectDir {
86
  printLog "reading path for project files from user...\n"
87
  local amiroappsdir=$(realpath $(dirname $(realpath ${BASH_SOURCE[0]}))/../../../)
88
  local input=""
89
  read -p "Path where to create/delete project files: " -i $amiroappsdir -e input
90
  printLog "user selected path $(realpath $input)\n"
91
  eval $1="$(realpath $input)"
92
}
93

    
94
### retrieves the ARM-NONE-EABI-GCC include directory ##########################
95
# Retrieves the include directory of the currently set arm-none-eabi-gcc.
96
#
97
# usage:      retrieveGccIncludeDir <path>
98
# arguments:  <path>
99
#                 Variable to store the path to.
100
# return:    0
101
#                 No error or warning occurred.
102
#            -1
103
#                 Error: Command 'arm-none-eabi-gcc' not found.
104
#
105
function retrieveGccIncludeDir {
106
  # retrieve binary path or link
107
  local binpath=$(which arm-none-eabi-gcc)
108
  if [ -z "$binpath" ]; then
109
    printError "command 'arm-none-eabi-gcc' not found\n"
110
    return -1
111
  else
112

    
113
    # traverse any links
114
    while [ -L "$binpath" ]; do
115
      binpath=$(readlink $binpath)
116
    done
117
    printInfo "gcc-arm-none-eabi detected: $binpath\n"
118

    
119
    # return include path
120
    eval $1=$(realpath $(dirname ${binpath})/../arm-none-eabi/include/)
121

    
122
    return 0
123
  fi
124
}
125

    
126
### intialize project files ####################################################
127
# Initailizes all project files.
128
#
129
# usage:      initProject [-p|--path=<path>] [--gcc=<path>] [-o|--out=<var>] [--gccout=<var>]
130
# arguments:  -p, --path <path>
131
#                 Path where to create the project files.
132
#             --gcc=<path>
133
#                 Path to the GCC include directory.
134
#             -o, --out <var>
135
#                 Variable to store the path to.
136
#             --gccout=<var>
137
#                 Variable to store the path to the GCC include directory to.
138
# return:     0
139
#                 No error or warning occurred.
140
#
141
function initProject {
142
  local userdir=$(pwd)
143
  local projectdir=""
144
  local gccincludedir=""
145
  local outvar=""
146
  local gccoutvar=""
147

    
148
  # parse arguments
149
  local otherargs=()
150
  while [ $# -gt 0 ]; do
151
    if ( parseIsOption $1 ); then
152
      case "$1" in
153
        -p=*|--path=*)
154
          projectdir=$(realpath "${1#*=}"); shift 1;;
155
        -p|--path)
156
          projectdir=$(realpath "$2"); shift 2;;
157
        --gcc=*)
158
          gccincludedir=$(realpath "${1#*=}"); shift 1;;
159
        --gcc)
160
          gccincludedir=$(realpath "$2"); shift 2;;
161
        -o=*|--out=*)
162
          outvar=${1#*=}; shift 1;;
163
        -o|--out)
164
          outvar=$2; shift 2;;
165
        --gccout=*)
166
          gccoutvar=$(realpath "${1#*=}"); shift 1;;
167
        --gccout)
168
          gccoutvar=$(realpath "$2"); shift 2;;
169
        *)
170
          printError "invalid option: $1\n"; shift 1;;
171
      esac
172
    else
173
      otherargs+=("$1")
174
      shift 1
175
    fi
176
  done
177

    
178
  # print message
179
  printInfo "creating QtCreator project files...\n"
180

    
181
  # read absolute project directory if required
182
  if [ -z "$projectdir" ]; then
183
    getProjectDir projectdir
184
  fi
185

    
186
  # retrieve absolute GCC include dir
187
  if [ -z "$gccincludedir" ]; then
188
    retrieveGccIncludeDir gccincludedir
189
  fi
190

    
191
  # move to project directory
192
  cd $projectdir
193

    
194
  # AMiRo-OS, ChibiOS, AMiRo-BLT, AMiRo-LLD and uRtWare relative root directories
195
  local urtwarerootdir=$(realpath --relative-base=$projectdir $(dirname ${BASH_SOURCE[0]})/../../..)
196

    
197
  # generate a file that contains all subdirectories as includes (but ignore hidden and documentation directories)
198
  find $gccincludedir -type d > ${projectdir}/AMiRo-Apps.includes
199
  find $amiroappsrootdir -type d | grep -E "/(apps|configurations)/.+" >> ${projectdir}/AMiRo-Apps.includes
200
  find $amiroosrootdir -type d | grep -v "/doc\|/build\|/.dep\|/hal\|/ports" >> ${projectdir}/AMiRo-Apps.includes
201
  find $amiroosrootdir -type d | grep -E "/os/hal/(include|src)" >> ${projectdir}/AMiRo-Apps.includes
202
  find $chibiosrootdir -type d | grep -E "/os/common/abstractions/cmsis_os$" >> ${projectdir}/AMiRo-Apps.includes
203
  find $chibiosrootdir -type d | grep -E "/os/common/oslib/(include|src)$" >> ${projectdir}/AMiRo-Apps.includes
204
  find $chibiosrootdir -type d | grep -E "/os/hal/(include|src)$" >> ${projectdir}/AMiRo-Apps.includes
205
  find $chibiosrootdir -type d | grep -E "/os/hal/lib/streams" >> ${projectdir}/AMiRo-Apps.includes
206
  find $chibiosrootdir -type d | grep -E "/os/hal/osal/(lib|rt)$" >> ${projectdir}/AMiRo-Apps.includes
207
  find $chibiosrootdir -type d | grep -E "/os/license$" >> ${projectdir}/AMiRo-Apps.includes
208
  find $chibiosrootdir -type d | grep -E "/os/rt/(include|src)$" >> ${projectdir}/AMiRo-Apps.includes
209
  find $chibiosrootdir -type d | grep -E "/os/various/(shell|cpp_wrappers)" >> ${projectdir}/AMiRo-Apps.includes
210
  find $chibiosrootdir -type d | grep -E "/test/(lib|rt/source/test)$" >> ${projectdir}/AMiRo-Apps.includes
211
  find $amirobltrootdir -type d | grep -E "/Target/Source/AMiRo$" >> ${projectdir}/AMiRo-Apps.includes
212
  echo "$(realpath --relative-base=$projectdir ${amirolldrootdir}/..)" >> ${projectdir}/AMiRo-Apps.includes
213
  find $amirolldrootdir -type d | grep -v "/doc" >> ${projectdir}/AMiRo-Apps.includes
214
  find $urtwarerootdir -type d | grep -v "/doc" >> ${projectdir}/AMiRo-Apps.includes
215
  # generate a file that specifies all files
216
  echo -n "" > ${projectdir}/AMiRo-Apps.files
217
  for path in `cat ${projectdir}/AMiRo-Apps.includes`; do
218
    find $path -maxdepth 1 -type f \( ! -iname ".*" \) |
219
      grep -Ev "^.*/arm-none-eabi/.*$" |
220
      grep -E  "^.*(\.s|\.S|\.h|\.c|\.hpp|\.cpp|\.tpp|\.ld)$" |
221
      grep -Ev "^${amirobltrootdir}/Target/Source/AMiRo/helper.*$" >> ${projectdir}/AMiRo-Apps.files;
222
  done
223
  # generate a default project configuration file if none exists so far
224
  if [ ! -f ${projectdir}/AMiRo-Apps.config ]; then
225
    echo "// Add predefined macros for your project here. For example:" > ${projectdir}/AMiRo-Apps.config
226
    echo "// #define YOUR_CONFIGURATION belongs here" >> ${projectdir}/AMiRo-Apps.config
227
  fi
228
  # generate a default .creator file if none exists so far
229
  if [ ! -f ${projectdir}/AMiRo-Apps}.creator ]; then
230
    echo "[general]" > ${projectdir}/AMiRo-Apps.creator
231
  fi
232

    
233
  # go back to user directory
234
  cd $userdir
235

    
236
  # fill the output variables
237
  if [ ! -z "$outvar" ]; then
238
    eval $outvar="$projectdir"
239
  fi
240
  if [ ! -z "$gccoutvar" ]; then
241
    eval $gccoutvar="$gccincludedir"
242
  fi
243

    
244
  return 0
245
}
246

    
247
### delete project files #######################################################
248
# Deletes all project files and optionally .user files, too.
249
#
250
# usage:      deleteProject [-p|--path=<path>] [-o|--out=<var>] [-w|-wipe]
251
# arguments:  -p, --path <path>
252
#                 Path where to delete the project files.
253
#             -o, --out <var>
254
#                 Variable to store the path to.
255
#             -w, --wipe
256
#                 Delete .user files as well.
257
# return:
258
#  -  0: no error
259
#  -  1: warning: function aborted by user
260
#  - -1: error: unexpected user input
261
function deleteProject {
262
  local projectdir=""
263
  local outvar=""
264
  local wipe=false
265

    
266
  # parse arguments
267
  local otherargs=()
268
  while [ $# -gt 0 ]; do
269
    if ( parseIsOption $1 ); then
270
      case "$1" in
271
        -p=*|--path=*)
272
          projectdir=$(realpath "${1#*=}"); shift 1;;
273
        -p|--path)
274
          projectdir=$(realpath "$2"); shift 2;;
275
        -o=*|--out=*)
276
          outvar=${1#*=}; shift 1;;
277
        -o|--out)
278
          outvar=$2; shift 2;;
279
        -w|--wipe)
280
          wipe=true; shift 1;;
281
        *)
282
          printError "invalid option: $1\n"; shift 1;;
283
      esac
284
    else
285
      otherargs+=("$1")
286
      shift 1
287
    fi
288
  done
289

    
290
  # print message
291
  if [ $wipe != true ]; then
292
    printInfo "deleting all QtCreator project files (*.includes, *.files, *.config, and *.creator)\n"
293
  else
294
    printInfo "deleting all QtCreator project files (*.includes, *.files, *.config, *.creator, and *.user)\n"
295
  fi
296

    
297
  # read project directory if required
298
  if [ -z "$projectdir" ]; then
299
    getProjectDir projectdir
300
  fi
301

    
302
  # remove all project files
303
  rm ${projectdir}/AMiRo-Apps.includes 2>&1 | tee -a $LOG_FILE
304
  rm ${projectdir}/AMiRo-Apps.files 2>&1 | tee -a $LOG_FILE
305
  rm ${projectdir}/AMiRo-Apps.config 2>&1 | tee -a $LOG_FILE
306
  rm ${projectdir}/AMiRo-Apps.creator 2>&1 | tee -a $LOG_FILE
307

    
308
  if [ $wipe == true ]; then
309
    rm ${projectdir}/AMiRo-Apps.creator.user 2>&1 | tee -a $LOG_FILE
310
  fi
311

    
312
  # store the path to the output variable, if required
313
  if [ ! -z "$outvar" ]; then
314
    eval $outvar="$projectdir"
315
  fi
316

    
317
  return 0
318

    
319
}
320

    
321
### main function of this script ###############################################
322
# Creates, deletes and wipes QtCreator project files for the three AMiRo base modules.
323
#
324
# usage:      see function printHelp
325
# arguments:  see function printHelp
326
# return:     0
327
#                 No error or warning ocurred.
328
#
329
function main {
330
# print welcome/info text if not suppressed
331
  if [[ $@ != *"--noinfo"* ]]; then
332
    printWelcomeText
333
  else
334
    printf "######################################################################\n"
335
  fi
336
  printf "\n"
337

    
338
  # if --help or -h was specified, print the help text and exit
339
  if [[ $@ == *"--help"* || $@ == *"-h"* ]]; then
340
    printHelp
341
    printf "\n"
342
    quitScript
343
  fi
344

    
345
  # set log file if specified
346
  if [[ $@ == *"--log"* ]] || [[ $@ == *"--LOG"* ]]; then
347
    # get the parameter (file name)
348
    local cmdidx=1
349
    while [[ ! "${!cmdidx}" = "--log"* ]] && [[ ! "${!cmdidx}" = "--LOG"* ]]; do
350
      cmdidx=$[cmdidx + 1]
351
    done
352
    local cmd="${!cmdidx}"
353
    local logfile=""
354
    if [[ "$cmd" = "--log="* ]] || [[ "$cmd" = "--LOG="* ]]; then
355
      logfile=${cmd#*=}
356
    else
357
      local filenameidx=$((cmdidx + 1))
358
      logfile="${!filenameidx}"
359
    fi
360
    # optionally force silent appending
361
    if [[ "$cmd" = "--LOG"* ]]; then
362
      setLogFile --option=c --quiet "$logfile" LOG_FILE
363
    else
364
      setLogFile "$logfile" LOG_FILE
365
      printf "\n"
366
    fi
367
  fi
368
  # log script name
369
  printLog "this is $(realpath ${BASH_SOURCE[0]})\n"
370

    
371
  # parse arguments
372
  local otherargs=()
373
  while [ $# -gt 0 ]; do
374
    if ( parseIsOption $1 ); then
375
      case "$1" in
376
        -h|--help) # already handled; ignore
377
          shift 1;;
378
        -c|--clean)
379
          deleteProjects; printf "\n"; shift 1;;
380
        -q|--quit)
381
          quitScript; shift 1;;
382
        --log=*|--LOG=*) # already handled; ignore
383
          shift 1;;
384
        --log|--LOG) # already handled; ignore
385
          shift 2;;
386
        --noinfo) # already handled; ignore
387
          shift 1;;
388
        *)
389
          printError "invalid option: $1\n"; shift 1;;
390
      esac
391
    else
392
      otherargs+=("$1")
393
      shift 1
394
    fi
395
  done
396

    
397
  # interactive menu
398
  while ( true ); do
399
    # main menu info prompt and selection
400
    printInfo "QtCreator setup main menu\n"
401
    printf "Please select one of the following actions:\n"
402
    printf "  [I] - initialize project files\n"
403
    printf "  [C] - clean project files\n"
404
    printf "  [Q] - quit this setup\n"
405
    local userinput=""
406
    readUserInput "IiCcWwQq" userinput
407
    printf "\n"
408

    
409
    # evaluate user selection
410
    case "$userinput" in
411
      I|i)
412
        initProject; printf "\n";;
413
      C|c)
414
        deleteProject; printf "\n";;
415
      Q|q)
416
        quitScript;;
417
      *) # sanity check (exit with error)
418
        printError "unexpected argument: $userinput\n";;
419
    esac
420
  done
421

    
422
  exit 0
423
}
424

    
425
################################################################################
426
# SCRIPT ENTRY POINT                                                           #
427
################################################################################
428

    
429
main "$@"