-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDebugDueBuilder.sh
executable file
·2069 lines (1585 loc) · 68.9 KB
/
DebugDueBuilder.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Copyright (c) 2014-2022 R. Diez - Licensed under the GNU AGPLv3 - see below for more information.
set -o errexit
set -o nounset
set -o pipefail
# set -x # Enable tracing of this script.
user_config ()
{
DEFAULT_TOOLCHAIN_DIR="$HOME/SomeDir/DebugDueToolchain"
DEFAULT_PATH_TO_OPENOCD="$HOME/SomeDir/openocd-0.10.0-bin/bin/openocd"
# This setting only matters when using the 'bossac' tool.
DEFAULT_PATH_TO_BOSSAC="bossac"
# This setting only matters when using the 'bossac' tool.
PROGRAMMING_USB_VIRTUAL_SERIAL_PORT="/dev/serial/by-id/usb-Arduino__www.arduino.cc__Arduino_Due_Prog._Port_7523230323535180A120-if00"
DEFAULT_DEBUG_ADAPTER="DebugDue"
# This setting only matters for the 'DebugDue' adapter. This is the location of the
# 'native' USB virtual serial port of the Arduino Due that is acting as a JTAG adapter.
# OpenOCD will be told that this is where to find the (emulated) Bus Pirate.
DEBUGDUE_SERIAL_PORT="/dev/serial/by-id/usb-Arduino_Due_JTAG_Adapter_DebugDue1-if00"
DEFAULT_PROJECT="DebugDue"
DEFAULT_BUILD_TYPE="debug"
DEFAULT_DEBUGGER_TYPE="gdb"
DEFAULT_BUILD_OUTPUT_BASE_SUBDIR="BuildOutput"
DEFAULT_BUILD_OUTPUT_BASE_DIR="$(readlink --verbose --canonicalize -- "$DEFAULT_BUILD_OUTPUT_BASE_SUBDIR")"
}
declare -r SCRIPT_NAME="${BASH_SOURCE[0]##*/}" # This script's filename only, without any path components.
declare -r EXIT_CODE_SUCCESS=0
declare -r EXIT_CODE_ERROR=1
abort ()
{
echo >&2 && echo "Error in script \"$0\": $*" >&2
exit $EXIT_CODE_ERROR
}
is_var_set ()
{
if [ "${!1-first}" == "${!1-second}" ]; then return 0; else return 1; fi
}
str_starts_with ()
{
# $1 = string
# $2 = prefix
# From the bash manual, "Compound Commands" section, "[[ expression ]]" subsection:
# "Any part of the pattern may be quoted to force the quoted portion to be matched as a string."
# Also, from the "Pattern Matching" section:
# "The special pattern characters must be quoted if they are to be matched literally."
if [[ $1 == "$2"* ]]; then
return 0
else
return 1
fi
}
quote_and_append_args ()
{
local -n VAR="$1"
shift
local STR
# Shell-quote all arguments before joining them into a single string.
printf -v STR "%q " "$@"
# Remove the last character, which is one space too much.
STR="${STR::-1}"
if [ -z "$VAR" ]; then
VAR="$STR"
else
VAR+=" $STR"
fi
}
delete_dir_if_exists ()
{
# $1 = dir name
if [ -d "$1" ]
then
echo "Deleting directory \"$1\" ..."
rm -rf -- "$1"
# Sometimes under Windows/Cygwin, directories are not immediately deleted,
# which may cause problems later on.
if [ -d "$1" ]; then abort "Cannot delete directory \"$1\"."; fi
fi
}
create_dir_if_not_exists ()
{
# $1 = dir name
if ! test -d "$1"
then
echo "Creating directory \"$1\" ..."
mkdir --parents -- "$1"
fi
}
delete_file_if_exists ()
{
# $1 = file name
if [ -f "$1" ]
then
echo "Deleting file \"$1\" ..."
rm -f -- "$1"
# Sometimes under Windows/Cygwin, directories are not immediately deleted,
# which may cause problems later on. I am not sure is the same is true for files.
if [ -f "$1" ]; then abort "Cannot delete file \"$1\"."; fi
fi
}
get_uptime ()
{
local PROC_UPTIME_STR # Variable declared on a separate line, or it would mask any errors on the right part of the assignment.
PROC_UPTIME_STR="$(</proc/uptime)"
local PROC_UPTIME_COMPONENTS
# Split on blanks.
IFS=$' \t' read -r -a PROC_UPTIME_COMPONENTS <<< "$PROC_UPTIME_STR"
if [ ${#PROC_UPTIME_COMPONENTS[@]} -ne 2 ]; then
abort "Invalid /proc/uptime format."
fi
CURRENT_UPTIME="${PROC_UPTIME_COMPONENTS[0]}"
}
# WARNING: 32-bit versions of Bash will not be able to cope with elapsed times greater than 248 days.
generate_elapsed_time_msg ()
{
# Function argument $1 is the elapsed time in hundredths of seconds.
local -i ELAPSED_TIME="$1"
local sign
if [ "$ELAPSED_TIME" -lt 0 ]
then
ELAPSED_TIME=$((-ELAPSED_TIME))
sign="-"
else
sign=""
fi
local -i hundredths_of_seconds=$(( ELAPSED_TIME % 100 ))
local -i total_seconds=$(( ELAPSED_TIME / 100 ));
local -i seconds=$total_seconds
local -i weeks=0;
local -i days=0;
local -i hours=0;
local -i minutes=0;
if [ $seconds -gt 0 ]
then
minutes=$(( seconds / 60 ))
seconds=$(( seconds % 60 ))
fi
if [ $minutes -gt 0 ]
then
hours=$(( minutes / 60 ))
minutes=$(( minutes % 60 ))
fi
if [ $hours -gt 0 ]
then
days=$(( hours / 24 ))
hours=$(( hours % 24 ))
fi
if [ $days -gt 0 ]
then
weeks=$(( days / 7 ))
days=$(( days % 7 ))
fi
local res
printf -v res "%d.%02d s" $seconds $hundredths_of_seconds;
if [ $(( minutes + hours + days + weeks )) -gt 0 ]
then
printf -v res "%d min %s" "$minutes" "$res"
fi
if [ $(( hours + days + weeks )) -gt 0 ]
then
if [ $hours -eq 1 ]; then
local hour_str="hour"
else
local hour_str="hours"
fi
printf -v res "%d %s %s" "$hours" "$hour_str" "$res"
fi
if [ $(( days + weeks )) -gt 0 ]
then
if [ $days -eq 1 ]; then
local day_str="day"
else
local day_str="days"
fi
printf -v res "%d %s %s" "$days" "$day_str" "$res"
fi
if [ $weeks -gt 0 ]
then
if [ $weeks -eq 1 ]; then
local week_str="week"
else
local week_str="weeks"
fi
printf -v res "%d %s %s" "$weeks" "$week_str" "$res"
fi
ELAPSED_TIME_MSG="$sign$res"
# Every now and then, the user may want to compare elapsed times.
# For example, the last change may make the build 10% faster.
# It is hard to compare elapsed times if they have minutes, hours and so on.
# Therefore, append to the message the total number of seconds.
if [ $total_seconds -ge 60 ]
then
local tmp
printf -v tmp "%d.%02d s" $total_seconds $hundredths_of_seconds
ELAPSED_TIME_MSG+=" ($tmp)"
fi
}
display_help ()
{
cat - <<EOF
$SCRIPT_NAME - Copyright (c) 2014-2022 R. Diez - Licensed under the GNU AGPLv3
Overview:
This script builds/runs/etc. the DebugDue project. You would normally run
the script from your development environment (Emacs, Vim, Eclipse, ...).
Syntax:
$SCRIPT_NAME <switches...>
Information switches:
--help Displays this help text.
--license Prints license information.
All steps below are optional. The step number gives the order in which
the steps are run if requested.
Step 1, clean operation and configuration options:
--clean Deletes the -obj and -bin directories for the given build type
and the 'configure' script, if they exist, so that the
next build will start from scratch.
--enable-configure-cache Enables the cache file when invoking
autoconf's 'configure' script. This will save some time
when rebuilding from scratch.
You should not use a cache file if you are changing configure.ac
or some other autoconf source file in this project. You should also
drop the cache after changing your system's configuration,
such as after installing a new software package.
The local cache file (if any) will be dropped if you run this script
without enabling the cache.
--configure-cache-filename="filename"
This option triggers --enable-configure-cache, but, instead
of using a local cache file, the given one will be used.
The caller is then responsible for the lifetime of the supplied
cache file, as this script will never drop it.
--enable-ccache Uses 'ccache', which can possibly reduce compilation times.
You can only enable ccache when configuring the project
for the first time (or after cleaning it).
There are also some caveats, see this script's source code
for details about ccache.
Step 2, build operations:
--build Runs "make" for the default target. Generates the autoconf
files beforehand if necessary.
--atmel-software-framework="<path>" Directory where the ASF is installed.
Only needed if the project requires it.
--show-build-commands Show the full compilation commands during the build.
--disassemble Generate extra information files from the just-built ELF file:
complete disassembly, list of objects sorted by size,
sorted list of strings (with 'strings' command), readelf dump.
--make-arg=ARG Pass an extra argument to 'make'. This is primarily intended
for make variables. For example: --make-arg CPPFLAGS=-Dmysymbol=1
You can specify --make-arg several times.
--build-output-base-dir="<path>" Where the build output will land.
Defaults to '$DEFAULT_BUILD_OUTPUT_BASE_SUBDIR'.
The default is not to build anything. If you then debug your firmware,
make sure that the existing binary matches the code on the target.
That is, do not forget to program it first on the target.
Step 3, program operations:
--program-over-jtag Transfers the firmware over JTAG to the target device.
--program-with-bossac Transfers the firmware with 'bossac' to the target device.
--verify After programming, verify that the firmware was written correctly.
--cache-programmed-file Programming a new binary takes time. If the binary has not
changed, this option skips programming.
Warning: This assumes exclusive access to a single device.
If some other tool reprograms the Arduino Due, or it
is swapped out for a different Arduino Due, it will confuse
the build script. In this case, either re-run without
this option or delete the cached binary file to force
reprogramming.
--path-to-bossac="path/to/bossac" The default is "bossac", which only works
if is is on the PATH. Under Ubuntu/Debian,
the package to install is called 'bossa-cli'.
--debug-adapter=xxx What debug adapter to use:
DebugDue, Flyswatter2 or Olimex-ARM-USB-OCD-H.
Step 4, debug operations:
--debug Starts the firmware under the debugger (GDB connected to
OpenOCD over JTAG).
--debugger-type="<type>" Debugger types are "gdb" and "ddd" (a graphical
interface to GDB).
--debug-from-the-start Breaks as soon as possible after starting the firmware.
--add-breakpoint="function name or line position like Main.cpp:123"
--openocd-path="openocd-0.10.0/bin/openocd" Path to the OpenOCD executable.
Global options:
--project="<project name>" Specify 'DebugDue' (the default), 'EmptyFirmware' or 'QemuFirmware'.
--toolchain-dir="<path>"
--build-type="<type>" Build types are "debug" and "release".
Examples:
First of all, you may want to edit routine user_config() in this script
in order to set the default options according to your system.
This way, you do not have to specify any global configuration switches
on each run.
Every time you make a change in the source code, you would normally run:
$SCRIPT_NAME --build --build-type="debug"
At some point in time, you want to debug your firmware with:
$SCRIPT_NAME --build --build-type="debug" --program-over-jtag --debug
Exit status: 0 means success. Any other value means error.
Feedback: Please send feedback to rdiezmail-arduino at yahoo.de
EOF
}
display_license()
{
cat - <<EOF
Copyright (c) 2014-2022 R. Diez
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License version 3 for more details.
You should have received a copy of the GNU Affero General Public License version 3
along with this program. If not, see L<http://www.gnu.org/licenses/>.
EOF
}
check_only_one ()
{
local ERR_MSG="$1"
shift
local -i COUNT=0
for arg do
if $arg; then
COUNT=$((COUNT+1))
fi
done
if [ $COUNT -gt 1 ]; then
abort "$ERR_MSG"
fi
}
do_clean ()
{
echo "Cleaning the project's output directories..."
delete_dir_if_exists "$PROJECT_OBJ_DIR"
delete_dir_if_exists "$PROJECT_BIN_DIR"
delete_file_if_exists "$CONFIGURE_SCRIPT_PATH"
}
do_autogen_if_necessary ()
{
if ! [ -f "$CONFIGURE_SCRIPT_PATH" ]; then
echo "File \"$CONFIGURE_SCRIPT_PATH\" does not exist, running the autotools..."
pushd "$PROJECT_SRC_DIR" >/dev/null
./autogen.sh
popd >/dev/null
if ! [ -f "$CONFIGURE_SCRIPT_PATH" ]; then
abort "File \"$CONFIGURE_SCRIPT_PATH\" is not where it is expected to be."
fi
echo "Finished running the autotools."
fi
}
do_configure_if_necessary ()
{
local MAKEFILE_PATH="$PROJECT_OBJ_DIR/Makefile"
if ! [ -f "$MAKEFILE_PATH" ]; then
echo "File \"$MAKEFILE_PATH\" does not exist, running the configure step..."
pushd "$PROJECT_OBJ_DIR" >/dev/null
local CONFIG_CMD=""
quote_and_append_args CONFIG_CMD "CONFIG_SHELL=/bin/bash"
quote_and_append_args CONFIG_CMD "$CONFIGURE_SCRIPT_PATH"
if $ENABLE_CONFIGURE_CACHE_SPECIFIED; then
echo "Using configure cache file \"$CONFIGURE_CACHE_FILENAME\"."
quote_and_append_args CONFIG_CMD "--cache-file=$CONFIGURE_CACHE_FILENAME"
else
# If the cache file to use comes as a command-line argument, then the user
# is responsible for the cache file's lifetime.
# This script will only delete its local, default cache file
# if it has not been told to use it.
delete_file_if_exists "$DEFAULT_CONFIGURE_CACHE_FILENAME"
fi
quote_and_append_args CONFIG_CMD "--prefix=$PROJECT_BIN_DIR"
if [[ $BUILD_TYPE = debug ]]; then
quote_and_append_args CONFIG_CMD "--enable-debug=yes"
# echo "Creating a debug build..."
else
quote_and_append_args CONFIG_CMD "--enable-debug=no"
# echo "Creating a release build..."
fi
if [ -n "$ASF_DIR" ]; then
quote_and_append_args CONFIG_CMD "--with-atmel-software-framework=$ASF_DIR"
fi
quote_and_append_args CONFIG_CMD "--with-project=$PROJECT_NAME"
quote_and_append_args CONFIG_CMD "--host=$TARGET_ARCH"
# I have not figured out yet how to get the value passed as --host to configure.ac ,
# so I am passing it again in a separate command-line option.
quote_and_append_args CONFIG_CMD "--with-target-arch=$TARGET_ARCH"
# Use GCC's wrappers for 'ar' and 'ranlib'. Otherwise, when using the binutils versions directly,
# they will complain about a missing plug-in to process object files compiled for LTO.
# I reported this issue to the Autoconf project:
# sr #110475: ranlib: plugin needed to handle lto object
# https://savannah.gnu.org/support/index.php?110475
quote_and_append_args CONFIG_CMD "AR=$TARGET_ARCH-gcc-ar"
quote_and_append_args CONFIG_CMD "RANLIB=$TARGET_ARCH-gcc-ranlib"
if $ENABLE_CCACHE_SPECIFIED; then
# You probably do not want to turn ccache on unconditionally. The price of a cache miss
# in a normal compilation can be as high as 20 %. There are many more disk writes during
# compilation, so pressure increases on the system's disk cache.
#
# Therefore, ccache only helps if you recompile often with the same results.
# For example, if you rebuild many times from scratch during testing of
# your application's build script. And you are always using the same compiler.
#
# It also helps if your changes end up generating the same preprocessor output
# for many of the recompiled files. For example, if you amend just comments,
# or you change something under #ifdef DEBUG in a header file included by many
# source files, but you are compiling a release build at the moment.
#
# Using ccache also means more admin work. You should check every now and then
# whether your cache hits are high enough. Otherwise, you may have to increase
# your global cache size, or you will actually be losing performance.
#
# Beware that ccache is not completely reliable: adding a new header file
# may change the compilation results, and ccache may not realise.
# This corner case is documented in ccache's user manual.
CCACHE_NAME="ccache"
if type "$CCACHE_NAME" >/dev/null 2>&1 ; then
quote_and_append_args CONFIG_CMD "CC=$CCACHE_NAME $TARGET_ARCH-gcc"
quote_and_append_args CONFIG_CMD "CXX=$CCACHE_NAME $TARGET_ARCH-g++"
else
abort "Tool '$CCACHE_NAME' not found."
fi
fi
echo "$CONFIG_CMD"
eval "$CONFIG_CMD"
if ! [ -f "$MAKEFILE_PATH" ]; then
abort "File \"$MAKEFILE_PATH\" is not where it is expected to be."
fi
popd >/dev/null
echo "Finished running the configure step."
fi
}
check_whether_compiler_is_present ()
{
local COMPILER_NAME="$TARGET_ARCH-gcc"
# If you don't get the PATH right, the ./configure script will not find the right compiler,
# and the error message you'll get much further down is not immediately obvious.
# Therefore, check beforehand that we do find the right compiler.
if ! type "$COMPILER_NAME" >/dev/null 2>&1 ;
then
abort "Could not find compiler \"$COMPILER_NAME\", did you get the toolchain path right? I am using: $TOOLCHAIN_DIR"
fi
if false; then
echo "Compiler \"$COMPILER_NAME\" exists. The version is:"
"$COMPILER_NAME" -v
fi
}
process_command_line_argument ()
{
case "$OPTION_NAME" in
help)
display_help
exit $EXIT_CODE_SUCCESS
;;
license)
display_license
exit $EXIT_CODE_SUCCESS
;;
clean) CLEAN_SPECIFIED=true;;
verify) VERIFY_SPECIFIED=true;;
enable-configure-cache) ENABLE_CONFIGURE_CACHE_SPECIFIED=true;;
configure-cache-filename)
ENABLE_CONFIGURE_CACHE_SPECIFIED=true
if [[ $OPTARG = "" ]]; then
abort "Option --configure-cache-filename has an empty value."
fi
CONFIGURE_CACHE_FILENAME="$OPTARG"
;;
build) BUILD_SPECIFIED=true;;
enable-ccache) ENABLE_CCACHE_SPECIFIED=true;;
disassemble) DISASSEMBLE_SPECIFIED=true;;
program-over-jtag) PROGRAM_OVER_JTAG_SPECIFIED=true;;
program-with-bossac) PROGRAM_WITH_BOSSAC_SPECIFIED=true;;
cache-programmed-file) CACHE_PROGRAMMED_FILE_SPECIFIED=true;;
debug) DEBUG_SPECIFIED=true;;
debug-from-the-start) DEBUG_FROM_THE_START_SPECIFIED=true;;
show-build-commands) SHOW_BUILD_COMMANDS=true;;
path-to-bossac)
if [[ $OPTARG = "" ]]; then
abort "The --path-to-bossac option has an empty value."
fi
PATH_TO_BOSSAC="$OPTARG"
;;
toolchain-dir)
if [[ $OPTARG = "" ]]; then
abort "The --toolchain-dir option has an empty value."
fi
TOOLCHAIN_DIR="$OPTARG"
;;
build-output-base-dir)
if [[ $OPTARG = "" ]]; then
abort "The --build-output-base-dir option has an empty value."
fi
BUILD_OUTPUT_BASE_DIR="$OPTARG"
;;
atmel-software-framework)
if [[ $OPTARG = "" ]]; then
abort "The --atmel-software-framework option has an empty value."
fi
ASF_DIR="$OPTARG"
;;
build-type)
if [[ $OPTARG = "" ]]; then
abort "The --build-type option has an empty value."
fi
BUILD_TYPE="$OPTARG"
;;
debugger-type)
if [[ $OPTARG = "" ]]; then
abort "The --debugger-type option has an empty value."
fi
DEBUGGER_TYPE="$OPTARG"
;;
add-breakpoint)
if [[ $OPTARG = "" ]]; then
abort "The --add-breakpoint option has an empty value."
fi
BREAKPOINTS+=("$OPTARG")
;;
openocd-path)
if [[ $OPTARG = "" ]]; then
abort "The --openocd-path option has an empty value."
fi
PATH_TO_OPENOCD="$OPTARG"
;;
debug-adapter)
if [[ $OPTARG = "" ]]; then
abort "Option --debug-adapter has an empty value."
fi
DEBUG_ADAPTER="$OPTARG"
;;
project)
PROJECT="$OPTARG"
;;
make-arg)
if [[ $OPTARG = "" ]]; then
abort "The --make-arg option has an empty value."
fi
EXTRA_MAKE_ARGS+=("$OPTARG")
;;
*) # We should actually never land here, because parse_command_line_arguments() already checks if an option is known.
abort "Unknown command-line option \"--${OPTION_NAME}\".";;
esac
}
parse_command_line_arguments ()
{
# The way command-line arguments are parsed below was originally described on the following page:
# http://mywiki.wooledge.org/ComplexOptionParsing
# But over the years I have rewritten or amended most of the code myself.
if false; then
echo "USER_SHORT_OPTIONS_SPEC: $USER_SHORT_OPTIONS_SPEC"
echo "Contents of USER_LONG_OPTIONS_SPEC:"
for key in "${!USER_LONG_OPTIONS_SPEC[@]}"; do
printf -- "- %s=%s\\n" "$key" "${USER_LONG_OPTIONS_SPEC[$key]}"
done
fi
# The first colon (':') means "use silent error reporting".
# The "-:" means an option can start with '-', which helps parse long options which start with "--".
local MY_OPT_SPEC=":-:$USER_SHORT_OPTIONS_SPEC"
local OPTION_NAME
local OPT_ARG_COUNT
local OPTARG # This is a standard variable in Bash. Make it local just in case.
local OPTARG_AS_ARRAY
while getopts "$MY_OPT_SPEC" OPTION_NAME; do
case "$OPTION_NAME" in
-) # This case triggers for options beginning with a double hyphen ('--').
# If the user specified "--longOpt" , OPTARG is then "longOpt".
# If the user specified "--longOpt=xx", OPTARG is then "longOpt=xx".
if [[ "$OPTARG" =~ .*=.* ]] # With this --key=value format, only one argument is possible.
then
OPTION_NAME=${OPTARG/=*/}
OPTARG=${OPTARG#*=}
OPTARG_AS_ARRAY=("")
if ! test "${USER_LONG_OPTIONS_SPEC[$OPTION_NAME]+string_returned_if_exists}"; then
abort "Unknown command-line option \"--$OPTION_NAME\"."
fi
# Retrieve the number of arguments for this option.
OPT_ARG_COUNT=${USER_LONG_OPTIONS_SPEC[$OPTION_NAME]}
if (( OPT_ARG_COUNT != 1 )); then
abort "Command-line option \"--$OPTION_NAME\" does not take 1 argument."
fi
process_command_line_argument
else # With this format, multiple arguments are possible, like in "--key value1 value2".
OPTION_NAME="$OPTARG"
if ! test "${USER_LONG_OPTIONS_SPEC[$OPTION_NAME]+string_returned_if_exists}"; then
abort "Unknown command-line option \"--$OPTION_NAME\"."
fi
# Retrieve the number of arguments for this option.
OPT_ARG_COUNT=${USER_LONG_OPTIONS_SPEC[$OPTION_NAME]}
if (( OPT_ARG_COUNT == 0 )); then
OPTARG=""
OPTARG_AS_ARRAY=("")
process_command_line_argument
elif (( OPT_ARG_COUNT == 1 )); then
OPTARG="${!OPTIND}"
OPTARG_AS_ARRAY=("")
process_command_line_argument
else
OPTARG=""
# OPTARG_AS_ARRAY is not standard in Bash. I have introduced it to make it clear that
# arguments are passed as an array in this case. It also prevents many Shellcheck warnings.
OPTARG_AS_ARRAY=("${@:OPTIND:OPT_ARG_COUNT}")
if [ ${#OPTARG_AS_ARRAY[@]} -ne "$OPT_ARG_COUNT" ]; then
abort "Command-line option \"--$OPTION_NAME\" needs $OPT_ARG_COUNT arguments."
fi
process_command_line_argument
fi;
((OPTIND+=OPT_ARG_COUNT))
fi
;;
*) # This processes only single-letter options.
# getopts knows all valid single-letter command-line options, see USER_SHORT_OPTIONS_SPEC above.
# If it encounters an unknown one, it returns an option name of '?'.
if [[ "$OPTION_NAME" = "?" ]]; then
abort "Unknown command-line option \"$OPTARG\"."
else
# Process a valid single-letter option.
OPTARG_AS_ARRAY=("")
process_command_line_argument
fi
;;
esac
done
shift $((OPTIND-1))
ARGS=("$@")
}
add_make_parallel_jobs_flag ()
{
local SHOULD_ADD_PARALLEL_FLAG=true
if is_var_set "MAKEFLAGS"
then
if false; then
echo "MAKEFLAGS: $MAKEFLAGS"
fi
# The following string search is not 100 % watertight, as MAKEFLAGS can have further arguments at the end like " -- VAR1=VALUE1 VAR2=VALUE2 ...".
if [[ $MAKEFLAGS =~ --jobserver-fds= || $MAKEFLAGS =~ --jobserver-auth= ]]
then
# echo "Called from a makefile with parallel jobs enabled."
SHOULD_ADD_PARALLEL_FLAG=false
fi
fi
if $SHOULD_ADD_PARALLEL_FLAG; then
local MAKE_J_VAL
MAKE_J_VAL="$(( $(getconf _NPROCESSORS_ONLN) + 1 ))"
quote_and_append_args MAKE_CMD "-j" "$MAKE_J_VAL"
# Option "--output-sync" requires GNU Make version 4.0 (released in 2013) or newer. If you have an older GNU Make, comment the following line out.
#
# Note that you should be using GNU Make 4.3 or later, because older GNU Make versions have issues with parallel builds:
# A change to how pipe waiting works promises to speed up parallel kernel builds - always a kernel developer's favorite
# workload - but can also trigger a bug with old versions of GNU Make.
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0ddad21d3e99
#
# I have noticed that this option seems to be ignored by the GNU Make version 4.2.1 that comes with Ubuntu 20.04.2.
# I tested on the same system with a self-compiled GNU Make 4.3, and "--output-sync" worked fine.
#
# Unfortunately, option "--output-sync" leads to long periods of time with no output, followed by large bursts of output.
# It is annoying, but it is actually the only sane way of generating a build log when building in parallel.
# And you want to build in parallel on today's multicore computers.
#
# Do not add this option if you will not be building in parallel, because the user may want to see any progress messages
# straight away. An example is running a makefile to download some files: you will probably not want to enable (or
# you may want to disable) downloading in parallel, in order to prevent overloading the network, but then you will want
# to see without delay the download progress messages that tools like 'curl' can output.
quote_and_append_args MAKE_CMD "--output-sync=recurse"
fi
}
do_build ()
{
pushd "$PROJECT_OBJ_DIR" >/dev/null
local MAKE_CMD=""
quote_and_append_args MAKE_CMD "make"
if false; then
# Possible flags:
# a for all
# b for basic debugging
# v for more verbose basic debugging
# i for showing implicit rules
# j for details on invocation of commands
# m for debugging while remaking makefiles.
local DEBUG_FLAGS="a"
quote_and_append_args MAKE_CMD "--debug=$DEBUG_FLAGS"
fi
# Normally, the build commands are not shown, see AM_SILENT_RULES in configure.ac .
# Passing "V=1" in CPPFLAGS is not enough, you need to remove "-s" too.
if $SHOW_BUILD_COMMANDS; then
quote_and_append_args MAKE_CMD "V=1"
else
quote_and_append_args MAKE_CMD "-s"
fi
EXTRA_CPPFLAGS=""
# If you are building from within emacs, GCC will not automatically turn the diagnostics colours on
# because it is not running on a real console. You can overcome this by enabling colours in emacs'
# build output window and then setting the following variable to 'true'.
# You do not actually need to enable this flag, you can just set CPPFLAGS before running this script.
local FORCE_GCC_DIAGNOSTICS_COLOR=false
if $FORCE_GCC_DIAGNOSTICS_COLOR; then
EXTRA_CPPFLAGS+="-fdiagnostics-color=always "
fi
# Show the path of all files #include'd during compilation. It often helps when debugging preprocessor problems.
# You do not actually need to enable this flag, you can just set CPPFLAGS before running this script.
local SHOW_INCLUDED_FILES=false
if $SHOW_INCLUDED_FILES; then
EXTRA_CPPFLAGS+="-H "
fi
# Generate the assembly files from the source files.
# Due to the command-line arguments passed to GCC, .s output files end up actually with
# file extension .o, like object files are named.
# Unfortunately, this option does not generate the real object files, so the build does not complete.
# Use -save-temps instead, see file configure.ac .
# I have verified that option -fverbose-asm does generate extra comments in
# the assembly files (which actually have file extension .o, see above).
local GENERATE_ASSEMBLY_FILES=false
if $GENERATE_ASSEMBLY_FILES; then
EXTRA_CPPFLAGS+="-S -fverbose-asm "
fi
if [[ $EXTRA_CPPFLAGS != "" ]]; then
# The user's CPPFLAGS comes at the end, so that the user always has the last word.
quote_and_append_args MAKE_CMD "CPPFLAGS=$EXTRA_CPPFLAGS${CPPFLAGS:-}"
fi
quote_and_append_args MAKE_CMD "--no-builtin-rules"
add_make_parallel_jobs_flag
local EXTRA_ARG
for EXTRA_ARG in "${EXTRA_MAKE_ARGS[@]}"; do
quote_and_append_args MAKE_CMD "$EXTRA_ARG"
done
# After all 'make' options, append any targets the user requested.
if $DISASSEMBLE_SPECIFIED; then
quote_and_append_args MAKE_CMD "disassemble"
fi
echo "$MAKE_CMD"
eval "$MAKE_CMD"
local PROG_SIZE
PROG_SIZE="$(stat -c%s "$BIN_FILEPATH")"
printf "Resulting binary: \"$BIN_FILEPATH\", size: %'d bytes.\\n" "$PROG_SIZE"
popd >/dev/null
}
add_openocd_cmd ()
{
# We could write this routine like quote_and_append_args, but keep in mind that Bash and Tcl escaping
# is different. For example, the '[' character in command "set fifo [open filename a]"
# must not be escaped in Tcl.
# When running .tcl files, OpenOCD does not print the function results,
# but when running commands with --command, it does.
# The following makes every line return an empty list, which then prints nothing,
# effectively suppressing printing the function result.
# I have not found a better way yet to achieve this.
local -r SUPPRESS_PRINTING_RESULT=true
local QUOTED
if $SUPPRESS_PRINTING_RESULT; then
local -r TCL_SUPPRESS_PRINTING_RESULT_SUFFIX="; list"
quote_and_append_args OPEN_OCD_CMD "--command" "$1 $TCL_SUPPRESS_PRINTING_RESULT_SUFFIX"
else
quote_and_append_args OPEN_OCD_CMD "--command" "$1"
fi
}
add_openocd_cmd_echo ()
{
local QUOTED
printf -v QUOTED "%q" "$1"
add_openocd_cmd "echo $QUOTED"
}
do_bossac ()
{
# Tool 'bossac' does not seem to give an intuitive error message if the port
# is not there at all (at least for versions up to 1.3a). The error message
# is the same whether the port is not present, or whether it is,
# but the SAM-BA bootloader is not running there.
# Therefore, manually check beforehand that the port is actually there,
# in order to generate a more helpful error message if it is not.
if [ ! -e "$PROGRAMMING_USB_VIRTUAL_SERIAL_PORT" ]; then
abort "The Arduino Due's 'programming' USB virtual port is not at location \"$PROGRAMMING_USB_VIRTUAL_SERIAL_PORT\""
fi
if ! type "$PATH_TO_BOSSAC" >/dev/null 2>&1 ;
then
abort "Could not find tool \"$PATH_TO_BOSSAC\"."
fi
local PREFIX="/dev/"
local -i PREFIX_LEN="${#PREFIX}"
if ! str_starts_with "$PROGRAMMING_USB_VIRTUAL_SERIAL_PORT" "$PREFIX"; then
abort "Tool 'bossac' expects the port location to start with \"$PREFIX\", but it does not. The port location is: \"$PROGRAMMING_USB_VIRTUAL_SERIAL_PORT\"."
fi
if $SAME_FILE_THEREFORE_SKIP_PROGRAMMING; then
echo "Skipping programming, as it is the same binary file as the last time around. In order to force programming, delete the cached file:"
echo " $CACHED_PROGRAMMED_FILE_FILENAME"
return