From dd590b9f42092f3316f807fb4f5e1c6bbf1cc067 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 23 Dec 2025 13:09:00 +0100 Subject: [PATCH] rework Makefile One issue is that dependencies are not properly generated, this will be implemented later. --- Makefile | 81 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index dfbcf23..daf4022 100644 --- a/Makefile +++ b/Makefile @@ -1,44 +1,59 @@ SHELL = /bin/sh -.SUFFIXES: +.SUFFIXES: # Disable implicit rules. -CC = avr-gcc -STRIP = avr-strip +CC = avr-gcc +CFLAGS := $(CFLAGS) -mmcu=avr128da28 -Os -std=gnu99 -Wall -Wextra -Wpedantic -Wno-pointer-arith +CPPFLAGS := $(CPPFLAGS) -Iinclude -DNDEBUG -CPPFLAGS = -DNDEBUG -CFLAGS = -Os +ASFLAGS := $(ASFLAGS) -mmcu=avr128da28 -Os +LDFLAGS := $(LDFLAGS) -nostdlib -flto +LDLIBS := $(LDLIBS) -lgcc -CPPFLAGS += -Iinclude -CFLAGS += -mmcu=avr128da28 -std=gnu99 -MMD -MP -LDFLAGS += -mmcu=avr128da28 -Wall -Wextra -Wpedantic -Wno-pointer-arith -LDFLAGS += -nostdlib -nostartfiles -flto +# Find source files +SRC := $(shell find src/ -name '*.[cS]' -print) +DIR := $($(sort $(foreach f,$(SRC),$(dir $f))):%=obj/%) bin/ -SRC := $(wildcard src/*.c src/*/*.c src/*/*/*.c src/*/*/*/*.c src/*/*/*/*/*.c src/*/*/*/*/*/*.c src/*/*/*/*/*/*/*.c src/*/*/*/*/*/*/*/*.c) -SRC += $(wildcard src/*.S src/*/*.S src/*/*/*.S src/*/*/*/*.S src/*/*/*/*/*.S src/*/*/*/*/*/*.S src/*/*/*/*/*/*/*.S src/*/*/*/*/*/*/*/*.S) -OBJ := $(SRC:%=obj/%.o) +# Configure common functions for logging +msg-as = $(info [AS] $(1)) +msg-cc = $(info [CC] $(1)) +msg-clean = $(info [CLEAN] $(1)) +msg-ld = $(info [LD] $(1)) +msg-tar = $(info [TAR] $(1)) +msg-xxd = $(info [XXD] $(1)) +# Set Q to @ to silence commands being printed, if --no-silent has not been set +ifeq (0, $(words $(findstring --no-silent,$(MAKEFLAGS)))) +Q=@ +endif -.PHONY: all clean install-strip info -all: info bin/a.hex -clean:; @-$(RM) -rv bin/ obj/ -info: - $(info CC: $(CC)) - $(info CFLAGS: $(CFLAGS)) - $(info CPPFLAGS: $(CPPFLAGS)) - $(info LDFLAGS: $(LDFLAGS)) - $(info LDLIBS: $(LDLIBS)) +.PHONY: $(DIR) all clean +all: bin/a.out bin/a.hex +clean:; $(Q)-$(RM) -rv bin/ obj/ -bin/a.out: $(OBJ) - $(info [LD] $(CC) $@) - @mkdir -p $(@D) - @$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS) +# Links object files to a binary +bin/a.out: $(SRC:%=obj/%.o) + $(Q)$(call msg-ld,$@) + $(Q)$(CC) $(LDFLAGS) $(LDLIBS) -o $@ $^ -bin/a.hex: bin/a.out - $(info [STRIP] $(STRIP) $@) - @$(STRIP) -o $@ $< +# Links object files to a stripped binary +bin/a.hex: $(SRC:%=obj/%.o) + $(Q)$(call msg-ld,$@) + $(Q)$(CC) -s $(LDFLAGS) $(LDLIBS) -o $@ $^ -obj/%.o: % - $(info [CC] $(CC) $@) - @mkdir -p $(@D) - @$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $< +# Assembles preprocessed assembly into object files +obj/%.S.d: %.S; $Q$(CC) -MM $(ASFLAGS) $(CPPFLAGS) -MF $@ $< +obj/%.S.o: %.S + $(Q)$(call msg-as,$@) + $(Q)$(CC) -c $(ASFLAGS) $(CPPFLAGS) -o $@ $< --include $(OBJ:%.o=%.d) +# Compiles C sources into object files +obj/%.c.d: %.c; $Q$(CC) -MM $(CFLAGS) $(CPPFLAGS) -MF $@ $< +obj/%.c.o: %.c %.c.d + $(Q)mkdir $(@D) + $(Q)$(call msg-cc,$@) + $(Q)$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $< + +# Include dependencies, ignoring any errors that may occur when doing so. +ifeq (0, $(words $(findstring $(MAKECMDGOALS), clean))) +# -include $(SRC:%=obj/%.d) +endif