From a4ef1ae736cd58375affed966cf1399fe8df5774 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Mon, 19 May 2025 09:22:31 +1000 Subject: [PATCH] gcc15 AVR compilation fixes (#25238) --- Makefile | 1 + builddefs/build_keyboard.mk | 1 + builddefs/build_test.mk | 1 + builddefs/support.mk | 11 +++++++++++ platforms/avr/platform.mk | 13 ++++++++----- 5 files changed, 22 insertions(+), 5 deletions(-) create mode 100644 builddefs/support.mk diff --git a/Makefile b/Makefile index c68ee7418a4..f1e3bf856f2 100644 --- a/Makefile +++ b/Makefile @@ -59,6 +59,7 @@ ifeq ($(ROOT_DIR),) endif include paths.mk +include $(BUILDDEFS_PATH)/support.mk TEST_OUTPUT_DIR := $(BUILD_DIR)/test ERROR_FILE := $(BUILD_DIR)/error_occurred diff --git a/builddefs/build_keyboard.mk b/builddefs/build_keyboard.mk index c2c47c00fb8..640dedc31e0 100644 --- a/builddefs/build_keyboard.mk +++ b/builddefs/build_keyboard.mk @@ -11,6 +11,7 @@ endif .DEFAULT_GOAL := all include paths.mk +include $(BUILDDEFS_PATH)/support.mk include $(BUILDDEFS_PATH)/message.mk # Helper to add defines with a 'QMK_' prefix diff --git a/builddefs/build_test.mk b/builddefs/build_test.mk index d0de63c6f58..0c5c98e2a3b 100644 --- a/builddefs/build_test.mk +++ b/builddefs/build_test.mk @@ -7,6 +7,7 @@ endif OPT = g include paths.mk +include $(BUILDDEFS_PATH)/support.mk include $(BUILDDEFS_PATH)/message.mk TARGET=test/$(TEST_OUTPUT) diff --git a/builddefs/support.mk b/builddefs/support.mk new file mode 100644 index 00000000000..7ef7b9b041e --- /dev/null +++ b/builddefs/support.mk @@ -0,0 +1,11 @@ +# Helper to determine if a compiler option is supported +# Args: +# $(1) = option to test, if successful will be output +# $(2) = option to use if $(1) is not supported +# $(3) = additional arguments to pass to the compiler during the test, but aren't contained in the output +cc-option = $(shell \ + if { echo 'int main(){return 0;}' | $(CC) $(1) $(3) -o /dev/null -x c /dev/null >/dev/null 2>&1; }; \ + then echo "$(1)"; else echo "$(2)"; fi) + +# Helper to pass comma character to make functions (use with `$(,)` to pass in `$(call ...)` arguments) +, := , diff --git a/platforms/avr/platform.mk b/platforms/avr/platform.mk index a625f2e5d01..f0187457b3f 100644 --- a/platforms/avr/platform.mk +++ b/platforms/avr/platform.mk @@ -12,9 +12,10 @@ HEX = $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature EEP = $(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) BIN = -ifeq ("$(shell echo "int main(){}" | $(CC) --param=min-pagesize=0 -x c - -o /dev/null 2>&1)", "") -COMPILEFLAGS += --param=min-pagesize=0 -endif +COMPILEFLAGS += $(call cc-option,--param=min-pagesize=0) + +# Fix ICE's: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116389 +COMPILEFLAGS += $(call cc-option,-mlra) COMPILEFLAGS += -funsigned-char COMPILEFLAGS += -funsigned-bitfields @@ -25,10 +26,12 @@ COMPILEFLAGS += -fshort-enums COMPILEFLAGS += -mcall-prologues COMPILEFLAGS += -fno-builtin-printf -# Linker relaxation is only possible if -# link time optimizations are not enabled. +# On older compilers, linker relaxation is only possible if link time optimizations are not enabled. ifeq ($(strip $(LTO_ENABLE)), no) COMPILEFLAGS += -mrelax +else + # Newer compilers may support both, so quickly check before adding `-mrelax`. + COMPILEFLAGS += $(call cc-option,-mrelax,,-flto=auto) endif ASFLAGS += $(AVR_ASFLAGS)