-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy-versatilepb-build
executable file
·88 lines (76 loc) · 2.77 KB
/
easy-versatilepb-build
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
#!/usr/bin/env bash
if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$#" -lt 1 ]; then
echo './easy-versatilepb-build INPUT OUTPUT{.elf, .list, .bin}'
echo ' -h, --help, * )'
echo ' Print usage information.'
exit
fi
SCRIPT=$(realpath "$0")
SCRIPT_PATH=$(dirname "$SCRIPT")
LINKER="$SCRIPT_PATH/src/linker.ld"
BOOTSTRAP="$SCRIPT_PATH/src/bootstrap.s"
SVC_HANDLER="$SCRIPT_PATH/src/svc_handler.c"
temp_dir=$(mktemp -d); # Temporary files directory
mcpu='arm926ej-s'; # ARM9 CPU for use with versatilepb QEMU machine
inputfile="$1"
inputfile_nodir=$(basename "$inputfile")
inputfile_base="${inputfile_nodir%.*}"
inputfile_ext="${inputfile_nodir##*.}"
outputs=${@:2}
function clean_exit {
rm -r "$temp_dir"
exit $1
}
# Ensure input file was supplied
if [ -z "$inputfile" ]; then
echo 'Please supply input source code file.' 1>&2
clean_exit 1
fi
# Ensure output file(s) were supplied
if [ "$#" -lt 2 ]; then
echo 'No output file(s) specified.' 1>&2
clean_exit 2
fi
# Compile/Assemble input file source code
case "$inputfile_ext" in
c)
arm-none-eabi-gcc -c -g -mcpu="$mcpu" "$inputfile" -o "$temp_dir/input.o" || \
( echo "Could not compile input file '$inputfile'" 1>&2; false ) || clean_exit 3
;;
s)
arm-none-eabi-as --warn --fatal-warnings -g -mcpu="$mcpu" "$inputfile" -o "$temp_dir/input.o" || \
( echo "Could not assemble input file '$inputfile'" 1>&2; false ) || clean_exit 3
;;
*)
echo "Unrecognised source code file extension '.$inputfile_ext'" >&2
clean_exit 4
esac
arm-none-eabi-as --warn --fatal-warnings -g -mcpu="$mcpu" "$BOOTSTRAP" -o "$temp_dir/bootstrap.o" && \
arm-none-eabi-gcc -c -Wall -O2 -nostdlib -nostartfiles -ffreestanding -g -mcpu="$mcpu" "$SVC_HANDLER" -o "$temp_dir/svc_handler.o" && \
arm-none-eabi-ld "$temp_dir/bootstrap.o" "$temp_dir/svc_handler.o" "$temp_dir/input.o" -T "$LINKER" -o "$temp_dir/$inputfile_base.elf" && \
arm-none-eabi-objdump -D "$temp_dir/$inputfile_base.elf" > "$temp_dir/$inputfile_base.list" && \
arm-none-eabi-objcopy "$temp_dir/$inputfile_base.elf" -O binary "$temp_dir/$inputfile_base.bin" || \
( echo "Compilation Failed." 1>&2; false ) || clean_exit 5
for o in $outputs
do
o_nodir=$(basename "$o")
o_ext="${o_nodir##*.}"
case $o_ext in
elf)
mv "$temp_dir/$inputfile_base.elf" "$o"
;;
list)
mv "$temp_dir/$inputfile_base.list" "$o"
;;
bin)
mv "$temp_dir/$inputfile_base.bin" "$o"
;;
*)
echo "Unrecognised output file format / extension, must be .elf, .list, or .bin." 1>&2
clean_exit 6
;;
esac
done
echo "Compilation Successful: $outputs"
# Cleanup
clean_exit 0