Skip to content

Commit 49badef

Browse files
authored
upload initial script
1 parent b1bbeb1 commit 49badef

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

build-mega.sh

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
"-O3"
11+
"-ffunction-sections"
12+
"-fdata-sections"
13+
"-flto"
14+
)
15+
16+
ARCH_FLAGS=(
17+
"-mmcu=atmega2560"
18+
"-DF_CPU=16000000L"
19+
"-DARDUINO=10808"
20+
"-DARDUINO_AVR_MEGA2560"
21+
"-DARDUINO_ARCH_AVR"
22+
)
23+
24+
CPP_FLAGS=(
25+
"-std=gnu++11"
26+
"-fpermissive"
27+
"-fno-threadsafe-statics"
28+
"-fno-exceptions"
29+
"-fno-rtti"
30+
)
31+
32+
C_FLAGS=(
33+
"-std=gnu11"
34+
"-fno-fat-lto-objects"
35+
)
36+
37+
AS_FLAGS=(
38+
"-x"
39+
"assembler-with-cpp"
40+
)
41+
42+
#LINK_FLAGS
43+
44+
#rm -rf sysroot
45+
#wget "https://github.com/ZakKemble/avr-gcc-build/releases/download/v14.1.0-1/avr-gcc-14.1.0-x64-linux.tar.bz2"
46+
#tar -xf avr-gcc-14.1.0-x64-linux.tar.bz2 && rm -rf avr-gcc-14.1.0-x64-linux.tar.bz2
47+
#mv avr-gcc-14.1.0-x64-linux sysroot
48+
SYSROOT_DIR="${WORKING_DIR}/sysroot"
49+
50+
ARCH_FLAGS+=(
51+
"--sysroot=${SYSROOT_DIR}"
52+
"-I${SYSROOT_DIR}/avr/include"
53+
)
54+
55+
#rm -rf "${WORKING_DIR}/core/arduino/avr"
56+
#git clone "https://github.com/ClangBuiltArduino/core_arduino-avr.git" "${WORKING_DIR}/core/arduino/avr" --depth=1
57+
AVR_CORE_DIR="${WORKING_DIR}/core/arduino/avr"
58+
59+
ARCH_FLAGS+=(
60+
"-I${AVR_CORE_DIR}/cores/arduino"
61+
"-I${AVR_CORE_DIR}/variants/mega"
62+
)
63+
64+
job_c() {
65+
local variant="${1}"
66+
local outfile="$(basename $2).o"
67+
mkdir -p "${OUT_DIR}/${variant}"
68+
echo "CC: ${OUT_DIR}/${variant}/${outfile}"
69+
clang --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${C_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
70+
}
71+
72+
job_cpp() {
73+
local variant="${1}"
74+
local outfile="$(basename $2).o"
75+
mkdir -p "${OUT_DIR}/${variant}"
76+
echo "CPP: ${OUT_DIR}/${variant}/${outfile}"
77+
clang++ --target=avr "${ARCH_FLAGS[@]}" "${COMMON_FLAGS[@]}" "${CPP_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
78+
}
79+
80+
job_s() {
81+
local variant="${1}"
82+
local outfile="$(basename $2).o"
83+
mkdir -p "${OUT_DIR}/${variant}"
84+
echo "CC: ${OUT_DIR}/${variant}/${outfile}"
85+
clang --target=avr "${ARCH_FLAGS[@]}" "${AS_FLAGS[@]}" -o "${OUT_DIR}/${variant}/${outfile}" -c "$2"
86+
}
87+
88+
compile_and_link_core() {
89+
for c_file in "${AVR_CORE_DIR}"/cores/arduino/*.c; do
90+
if [[ -f "$c_file" ]]; then
91+
job_c "core" "$c_file"
92+
fi
93+
done
94+
for cpp_file in "${AVR_CORE_DIR}"/cores/arduino/*.cpp; do
95+
if [[ -f "$cpp_file" ]]; then
96+
job_cpp "core" "$cpp_file"
97+
fi
98+
done
99+
for s_file in "${AVR_CORE_DIR}"/cores/arduino/*.S; do
100+
if [[ -f "$s_file" ]]; then
101+
job_s "core" "$s_file"
102+
fi
103+
done
104+
llvm-ar rc "${OUT_DIR}/libArduinoCore.a" "${OUT_DIR}/core/"*
105+
llvm-ranlib "${OUT_DIR}/libArduinoCore.a"
106+
}
107+
108+
compile_and_link_core
109+
mkdir -p "${OUT_DIR}/main"
110+
job_cpp "main" "$(pwd)/main.cpp"
111+
112+
clang++ --target=avr -v -o firmware.elf "${ARCH_FLAGS[@]}" -flto -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-ld=lld -fuse-linker-plugin

0 commit comments

Comments
 (0)