From 698ad5d0f7b6f5147d01fd4c0a352d21bc6d5899 Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Sat, 21 Dec 2024 10:14:05 +0530 Subject: [PATCH 1/8] HardwareSerial: Define no-op serialEvent() functions for clang GCC automatically resolves weakly defined empty symbols to null if not overridden, but Clang doesn't resolve it this way. This causes undefined reference errors during linking. To address this, define empty no-op serialEvent() functions that can be overridden later since its weakly defined, ensuring compatibility with Clang and fixing the build issue. ref: https://github.com/avr-llvm/clang/issues/9 Signed-off-by: Dakkshesh --- cores/arduino/HardwareSerial.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index 561127f9f..2523865ef 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -44,21 +44,33 @@ #if defined(HAVE_HWSERIAL0) void serialEvent() __attribute__((weak)); bool Serial0_available() __attribute__((weak)); + + void serialEvent() { } + bool Serial0_available() { return false; } #endif #if defined(HAVE_HWSERIAL1) void serialEvent1() __attribute__((weak)); bool Serial1_available() __attribute__((weak)); + + void serialEvent1() { } + bool Serial1_available() { return false; } #endif #if defined(HAVE_HWSERIAL2) void serialEvent2() __attribute__((weak)); bool Serial2_available() __attribute__((weak)); + + void serialEvent2() { } + bool Serial2_available() { return false; } #endif #if defined(HAVE_HWSERIAL3) void serialEvent3() __attribute__((weak)); bool Serial3_available() __attribute__((weak)); + + void serialEvent3() { } + bool Serial3_available() { return false; } #endif void serialEventRun(void) From 96cc1dbd93c963f399443be82ad50cc4e6dd4347 Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Tue, 4 Feb 2025 23:53:50 +0530 Subject: [PATCH 2/8] platform: Rebrand and adapt for ClangBuiltArduino Signed-off-by: Dakkshesh --- platform.txt | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/platform.txt b/platform.txt index df5ddb712..fa2f25b26 100644 --- a/platform.txt +++ b/platform.txt @@ -5,8 +5,8 @@ # For more info: # https://arduino.github.io/arduino-cli/latest/platform-specification/ -name=Arduino AVR Boards -version=1.8.6 +name=ClangBuiltArduino AVR Boards +version=1.0.0 # AVR compile variables # --------------------- @@ -18,23 +18,23 @@ compiler.warning_flags.more=-Wall compiler.warning_flags.all=-Wall -Wextra # Default "compiler.path" is correct, change only if you want to override the initial value -compiler.path={runtime.tools.avr-gcc.path}/bin/ -compiler.c.cmd=avr-gcc -compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -compiler.c.elf.flags={compiler.warning_flags} -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -compiler.c.elf.cmd=avr-gcc -compiler.S.flags=-c -g -x assembler-with-cpp -flto -MMD -compiler.cpp.cmd=avr-g++ -compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -compiler.ar.cmd=avr-gcc-ar +compiler.path={runtime.tools.cba-llvm.path}/bin/ +compiler.c.cmd=clang-wrapper +compiler.c.flags=--target=avr -c -g -Os {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects +compiler.c.elf.flags=--target=avr {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -Os -g -flto -fuse-linker-plugin -ffunction-sections -fdata-sections -Wl,--gc-sections -fuse-ld={runtime.tools.cba-avr-bfd.path}/bin/ld.bfd +compiler.c.elf.cmd=clang++ +compiler.S.flags=--target=avr --sysroot={runtime.tools.cba-avr-sysroot.path} -c -g -x assembler-with-cpp -flto -MMD +compiler.cpp.cmd=clang++-wrapper +compiler.cpp.flags=--target=avr -c -g -Os {compiler.warning_flags} --skip-lto=HardwareSerial0.cpp --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto +compiler.ar.cmd=llvm-ar compiler.ar.flags=rcs -compiler.objcopy.cmd=avr-objcopy +compiler.objcopy.cmd=llvm-objcopy compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 compiler.elf2hex.flags=-O ihex -R .eeprom -compiler.elf2hex.cmd=avr-objcopy +compiler.elf2hex.cmd=llvm-objcopy compiler.ldflags= compiler.libraries.ldflags= -compiler.size.cmd=avr-size +compiler.size.cmd=llvm-size # This can be overridden in boards.txt build.extra_flags= From 39845d371f511c0e59f2675983d353255b6eb603 Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Wed, 5 Feb 2025 11:51:26 +0530 Subject: [PATCH 3/8] extras: pack.release: Few touches Signed-off-by: Dakkshesh --- extras/pack.release.bash | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/extras/pack.release.bash b/extras/pack.release.bash index a2999d505..cf17bb39c 100755 --- a/extras/pack.release.bash +++ b/extras/pack.release.bash @@ -26,8 +26,10 @@ THIS_SCRIPT_NAME=`basename $0` rm -f avr-$VERSION.tar.bz2 cd .. -tar --transform "s|$FOLDERNAME|$FOLDERNAME-$VERSION|g" --exclude=extras/** --exclude=.git* --exclude=.idea -cjf avr-$VERSION.tar.bz2 $FOLDERNAME +tar --transform "s|$FOLDERNAME|$FOLDERNAME-$VERSION|g" --exclude=extras/** --exclude=.git* --exclude=.idea -cjf cba-avr-$VERSION.tar.bz2 $FOLDERNAME cd - -mv ../avr-$VERSION.tar.bz2 . +mv ../cba-avr-$VERSION.tar.bz2 . +echo "SHA256: $(sha256sum cba-avr-${VERSION}.tar.bz2)" +echo "size: $(stat -c%s cba-avr-${VERSION}.tar.bz2)" From 9602bbb2523eedbe1e8c7459659e3eb00781b000 Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Wed, 12 Feb 2025 00:00:32 +0530 Subject: [PATCH 4/8] cores: HardwareSerial: Fix serialEventRun() undefined references Signed-off-by: Dakkshesh --- cores/arduino/HardwareSerial.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 6ff29d0b9..3e12e779f 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -156,6 +156,6 @@ class HardwareSerial : public Stream #define HAVE_HWSERIAL3 #endif -extern void serialEventRun(void) __attribute__((weak)); +extern void serialEventRun(void); #endif From fce71849c59309a16a596b0a3f2f3793dad95460 Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Wed, 12 Feb 2025 00:02:29 +0530 Subject: [PATCH 5/8] platform: Skip lto for wiring.c - Causes some weird issues, will investigate it later. Signed-off-by: Dakkshesh --- platform.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform.txt b/platform.txt index fa2f25b26..bb5351b27 100644 --- a/platform.txt +++ b/platform.txt @@ -20,7 +20,7 @@ compiler.warning_flags.all=-Wall -Wextra # Default "compiler.path" is correct, change only if you want to override the initial value compiler.path={runtime.tools.cba-llvm.path}/bin/ compiler.c.cmd=clang-wrapper -compiler.c.flags=--target=avr -c -g -Os {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects +compiler.c.flags=--target=avr -c -g -Os {compiler.warning_flags} --skip-lto=wiring.c --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects compiler.c.elf.flags=--target=avr {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -Os -g -flto -fuse-linker-plugin -ffunction-sections -fdata-sections -Wl,--gc-sections -fuse-ld={runtime.tools.cba-avr-bfd.path}/bin/ld.bfd compiler.c.elf.cmd=clang++ compiler.S.flags=--target=avr --sysroot={runtime.tools.cba-avr-sysroot.path} -c -g -x assembler-with-cpp -flto -MMD From 56c46b69d8d1d7de7b19add34c0d8f9e8e525ec9 Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Wed, 12 Feb 2025 00:05:17 +0530 Subject: [PATCH 6/8] platform: Fix llvm-objcopy and use new clang-wrapper flags Signed-off-by: Dakkshesh --- platform.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/platform.txt b/platform.txt index bb5351b27..268502cb5 100644 --- a/platform.txt +++ b/platform.txt @@ -21,15 +21,15 @@ compiler.warning_flags.all=-Wall -Wextra compiler.path={runtime.tools.cba-llvm.path}/bin/ compiler.c.cmd=clang-wrapper compiler.c.flags=--target=avr -c -g -Os {compiler.warning_flags} --skip-lto=wiring.c --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -compiler.c.elf.flags=--target=avr {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -Os -g -flto -fuse-linker-plugin -ffunction-sections -fdata-sections -Wl,--gc-sections -fuse-ld={runtime.tools.cba-avr-bfd.path}/bin/ld.bfd -compiler.c.elf.cmd=clang++ +compiler.c.elf.flags=--target=avr {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -Os -g -flto -fuse-linker-plugin -ffunction-sections -fdata-sections -Wl,--gc-sections --bfd-dir={runtime.tools.cba-avr-bfd.path} --llvmgold-dir={runtime.tools.cba-llvmgold.path} +compiler.c.elf.cmd=clang++-wrapper compiler.S.flags=--target=avr --sysroot={runtime.tools.cba-avr-sysroot.path} -c -g -x assembler-with-cpp -flto -MMD compiler.cpp.cmd=clang++-wrapper compiler.cpp.flags=--target=avr -c -g -Os {compiler.warning_flags} --skip-lto=HardwareSerial0.cpp --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto compiler.ar.cmd=llvm-ar compiler.ar.flags=rcs compiler.objcopy.cmd=llvm-objcopy -compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 +compiler.objcopy.eep.flags=-O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load compiler.elf2hex.flags=-O ihex -R .eeprom compiler.elf2hex.cmd=llvm-objcopy compiler.ldflags= From 5d88ddef86ce75fffadc195aab2200f86403719d Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Sat, 29 Mar 2025 22:32:46 +0530 Subject: [PATCH 7/8] platform: Add `-mno-relax` for final link As reported in [1] and verified in [2] adding -mno-relax seems to fix the LED blinking issues without needing to disable LTO for the affected files. Use this as a temporary workaround until the issue is fixed. Remove the usage of clang-wrapper for c and cpp cmds since its not required anymore. [1]: https://github.com/llvm/llvm-project/issues/133579 [2]: https://github.com/llvm/llvm-project/issues/127651#issuecomment-2763720447 Signed-off-by: Dakkshesh --- platform.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/platform.txt b/platform.txt index 268502cb5..5a9dfd76f 100644 --- a/platform.txt +++ b/platform.txt @@ -19,13 +19,13 @@ compiler.warning_flags.all=-Wall -Wextra # Default "compiler.path" is correct, change only if you want to override the initial value compiler.path={runtime.tools.cba-llvm.path}/bin/ -compiler.c.cmd=clang-wrapper -compiler.c.flags=--target=avr -c -g -Os {compiler.warning_flags} --skip-lto=wiring.c --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects -compiler.c.elf.flags=--target=avr {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -Os -g -flto -fuse-linker-plugin -ffunction-sections -fdata-sections -Wl,--gc-sections --bfd-dir={runtime.tools.cba-avr-bfd.path} --llvmgold-dir={runtime.tools.cba-llvmgold.path} +compiler.c.cmd=clang +compiler.c.flags=--target=avr -c -g -Os {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu11 -ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects +compiler.c.elf.flags=--target=avr -mno-relax {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -Os -g -flto -fuse-linker-plugin -ffunction-sections -fdata-sections -Wl,--gc-sections --bfd-dir={runtime.tools.cba-avr-bfd.path} --llvmgold-dir={runtime.tools.cba-llvmgold.path} compiler.c.elf.cmd=clang++-wrapper compiler.S.flags=--target=avr --sysroot={runtime.tools.cba-avr-sysroot.path} -c -g -x assembler-with-cpp -flto -MMD -compiler.cpp.cmd=clang++-wrapper -compiler.cpp.flags=--target=avr -c -g -Os {compiler.warning_flags} --skip-lto=HardwareSerial0.cpp --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto +compiler.cpp.cmd=clang++ +compiler.cpp.flags=--target=avr -c -g -Os {compiler.warning_flags} --sysroot={runtime.tools.cba-avr-sysroot.path} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fno-rtti -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -MMD compiler.ar.cmd=llvm-ar compiler.ar.flags=rcs compiler.objcopy.cmd=llvm-objcopy From 230866559e4e826b3c3a2103db7047fe6e763ecf Mon Sep 17 00:00:00 2001 From: Dakkshesh Date: Mon, 31 Mar 2025 10:39:38 +0530 Subject: [PATCH 8/8] core: main: Dont check for `serialEventRun` as its always defined Signed-off-by: Dakkshesh --- cores/arduino/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index 434cd403c..e5b9248c0 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -44,7 +44,7 @@ int main(void) for (;;) { loop(); - if (serialEventRun) serialEventRun(); + serialEventRun(); } return 0;