-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-uno.sh
130 lines (109 loc) · 3.62 KB
/
build-uno.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
#!/usr/bin/env bash
WORKING_DIR="$(pwd)"
OUT_DIR="${WORKING_DIR}/out"
rm -rf "$OUT_DIR" && mkdir "$OUT_DIR"
COMMON_FLAGS=(
"-Wall"
"-Os"
"-ffunction-sections"
"-fdata-sections"
"-finline-functions"
"-flto=full"
"-fmerge-all-constants"
"-fshort-enums"
"-integrated-as"
"-fvisibility=hidden"
)
ARCH_FLAGS=(
"-mmcu=atmega328p"
"-DF_CPU=16000000L"
"-DARDUINO=10808"
"-DARDUINO_AVR_UNO"
"-DARDUINO_ARCH_AVR"
)
CPP_FLAGS=(
"-std=gnu++11"
"-fpermissive"
"-fno-threadsafe-statics"
"-fno-exceptions"
"-fno-rtti"
)
C_FLAGS=(
"-std=gnu11"
"-fno-fat-lto-objects"
)
AS_FLAGS=(
"-x"
"assembler-with-cpp"
)
#LINK_FLAGS
#rm -rf sysroot
#wget "https://github.com/ZakKemble/avr-gcc-build/releases/download/v14.1.0-1/avr-gcc-14.1.0-x64-linux.tar.bz2"
#tar -xf avr-gcc-14.1.0-x64-linux.tar.bz2 && rm -rf avr-gcc-14.1.0-x64-linux.tar.bz2
#mv avr-gcc-14.1.0-x64-linux sysroot
SYSROOT_DIR="${WORKING_DIR}/sysroot"
ARCH_FLAGS+=(
"--sysroot=${SYSROOT_DIR}"
"-I${SYSROOT_DIR}/avr/include"
)
#rm -rf "${WORKING_DIR}/core/arduino/avr"
#git clone "https://github.com/ClangBuiltArduino/core_arduino-avr.git" "${WORKING_DIR}/core/arduino/avr" --depth=1
AVR_CORE_DIR="${WORKING_DIR}/core/arduino/avr"
ARCH_FLAGS+=(
"-I${AVR_CORE_DIR}/cores/arduino"
"-I${AVR_CORE_DIR}/variants/standard"
)
job_c() {
local variant="${1}"
local outfile="$(basename $2).o"
mkdir -p "${OUT_DIR}/${variant}"
echo "CC: ${OUT_DIR}/${variant}/${outfile}"
clang --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${C_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
}
job_cpp() {
local variant="${1}"
local outfile="$(basename $2).o"
mkdir -p "${OUT_DIR}/${variant}"
echo "CPP: ${OUT_DIR}/${variant}/${outfile}"
local original_common_flags=("${COMMON_FLAGS[@]}")
if [[ $3 == "nolto" ]]; then
COMMON_FLAGS+=("-fno-lto")
fi
clang++ --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${CPP_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
COMMON_FLAGS=("${original_common_flags[@]}")
}
job_s() {
local variant="${1}"
local outfile="$(basename $2).o"
mkdir -p "${OUT_DIR}/${variant}"
echo "CC: ${OUT_DIR}/${variant}/${outfile}"
clang --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${AS_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
}
compile_and_link_core() {
for c_file in "${AVR_CORE_DIR}"/cores/arduino/*.c; do
if [[ -f "$c_file" ]]; then
job_c "core" "$c_file"
fi
done
for cpp_file in "${AVR_CORE_DIR}"/cores/arduino/*.cpp; do
if [[ -f "$cpp_file" ]]; then
if echo "$cpp_file" | grep -q "cores/arduino/HardwareSerial0.cpp"; then
echo "nolto: $cpp_file"
job_cpp "core" "$cpp_file" "nolto"
else
job_cpp "core" "$cpp_file"
fi
fi
done
for s_file in "${AVR_CORE_DIR}"/cores/arduino/*.S; do
if [[ -f "$s_file" ]]; then
job_s "core" "$s_file"
fi
done
llvm-ar rc "${OUT_DIR}/libArduinoCore.a" "${OUT_DIR}/core/"*
llvm-ranlib "${OUT_DIR}/libArduinoCore.a"
}
compile_and_link_core
mkdir -p "${OUT_DIR}/main"
job_cpp "main" "$(pwd)/main.cpp"
clang++ --target=avr -v -o firmware.elf "${ARCH_FLAGS[@]}" -Os -Wl,--strip-debug -Wl,--plugin-opt=-import-instr-limit=5 -Wl,--print-gc-sections -flto=full -finline-functions -ffunction-sections -fdata-sections -fpermissive -fno-exceptions -fno-threadsafe-statics -fno-rtti -L"${SYSROOT_DIR}/lib" -Wl,--gc-sections "${OUT_DIR}/main/main.cpp.o" "${OUT_DIR}/libArduinoCore.a" -fuse-linker-plugin