Skip to content

Commit 7a4fabe

Browse files
authored
Create build-uno.sh
Made LTO to work
1 parent 784129a commit 7a4fabe

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

build-uno.sh

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env bash
2+
3+
WORKING_DIR="$(pwd)"
4+
OUT_DIR="${WORKING_DIR}/out"
5+
6+
rm -rf "$OUT_DIR" && mkdir "$OUT_DIR"
7+
8+
COMMON_FLAGS=(
9+
"-Wall"
10+
"-Os"
11+
"-ffunction-sections"
12+
"-fdata-sections"
13+
"-finline-functions"
14+
"-flto=full"
15+
"-fmerge-all-constants"
16+
"-fshort-enums"
17+
"-integrated-as"
18+
"-fvisibility=hidden"
19+
)
20+
21+
ARCH_FLAGS=(
22+
"-mmcu=atmega328p"
23+
"-DF_CPU=16000000L"
24+
"-DARDUINO=10808"
25+
"-DARDUINO_AVR_UNO"
26+
"-DARDUINO_ARCH_AVR"
27+
)
28+
29+
CPP_FLAGS=(
30+
"-std=gnu++11"
31+
"-fpermissive"
32+
"-fno-threadsafe-statics"
33+
"-fno-exceptions"
34+
"-fno-rtti"
35+
)
36+
37+
C_FLAGS=(
38+
"-std=gnu11"
39+
"-fno-fat-lto-objects"
40+
)
41+
42+
AS_FLAGS=(
43+
"-x"
44+
"assembler-with-cpp"
45+
)
46+
47+
#LINK_FLAGS
48+
49+
#rm -rf sysroot
50+
#wget "https://github.com/ZakKemble/avr-gcc-build/releases/download/v14.1.0-1/avr-gcc-14.1.0-x64-linux.tar.bz2"
51+
#tar -xf avr-gcc-14.1.0-x64-linux.tar.bz2 && rm -rf avr-gcc-14.1.0-x64-linux.tar.bz2
52+
#mv avr-gcc-14.1.0-x64-linux sysroot
53+
SYSROOT_DIR="${WORKING_DIR}/sysroot"
54+
55+
ARCH_FLAGS+=(
56+
"--sysroot=${SYSROOT_DIR}"
57+
"-I${SYSROOT_DIR}/avr/include"
58+
)
59+
60+
#rm -rf "${WORKING_DIR}/core/arduino/avr"
61+
#git clone "https://github.com/ClangBuiltArduino/core_arduino-avr.git" "${WORKING_DIR}/core/arduino/avr" --depth=1
62+
AVR_CORE_DIR="${WORKING_DIR}/core/arduino/avr"
63+
64+
ARCH_FLAGS+=(
65+
"-I${AVR_CORE_DIR}/cores/arduino"
66+
"-I${AVR_CORE_DIR}/variants/standard"
67+
)
68+
69+
job_c() {
70+
local variant="${1}"
71+
local outfile="$(basename $2).o"
72+
mkdir -p "${OUT_DIR}/${variant}"
73+
echo "CC: ${OUT_DIR}/${variant}/${outfile}"
74+
75+
clang --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${C_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
76+
}
77+
78+
job_cpp() {
79+
local variant="${1}"
80+
local outfile="$(basename $2).o"
81+
mkdir -p "${OUT_DIR}/${variant}"
82+
echo "CPP: ${OUT_DIR}/${variant}/${outfile}"
83+
84+
local original_common_flags=("${COMMON_FLAGS[@]}")
85+
if [[ $3 == "nolto" ]]; then
86+
COMMON_FLAGS+=("-fno-lto")
87+
fi
88+
clang++ --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${CPP_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
89+
COMMON_FLAGS=("${original_common_flags[@]}")
90+
}
91+
92+
job_s() {
93+
local variant="${1}"
94+
local outfile="$(basename $2).o"
95+
mkdir -p "${OUT_DIR}/${variant}"
96+
echo "CC: ${OUT_DIR}/${variant}/${outfile}"
97+
98+
clang --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${AS_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
99+
}
100+
101+
compile_and_link_core() {
102+
for c_file in "${AVR_CORE_DIR}"/cores/arduino/*.c; do
103+
if [[ -f "$c_file" ]]; then
104+
job_c "core" "$c_file"
105+
fi
106+
done
107+
for cpp_file in "${AVR_CORE_DIR}"/cores/arduino/*.cpp; do
108+
if [[ -f "$cpp_file" ]]; then
109+
if echo "$cpp_file" | grep -q "cores/arduino/HardwareSerial0.cpp"; then
110+
echo "nolto: $cpp_file"
111+
job_cpp "core" "$cpp_file" "nolto"
112+
else
113+
job_cpp "core" "$cpp_file"
114+
fi
115+
fi
116+
done
117+
for s_file in "${AVR_CORE_DIR}"/cores/arduino/*.S; do
118+
if [[ -f "$s_file" ]]; then
119+
job_s "core" "$s_file"
120+
fi
121+
done
122+
llvm-ar rc "${OUT_DIR}/libArduinoCore.a" "${OUT_DIR}/core/"*
123+
llvm-ranlib "${OUT_DIR}/libArduinoCore.a"
124+
}
125+
126+
compile_and_link_core
127+
mkdir -p "${OUT_DIR}/main"
128+
job_cpp "main" "$(pwd)/main.cpp"
129+
130+
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

0 commit comments

Comments
 (0)