rework Makefile

One issue is that dependencies are not properly generated, this will be
implemented later.
This commit is contained in:
2025-12-23 13:09:00 +01:00
parent 198dbfca60
commit dd590b9f42

View File

@@ -1,44 +1,59 @@
SHELL = /bin/sh SHELL = /bin/sh
.SUFFIXES: .SUFFIXES: # Disable implicit rules.
CC = avr-gcc CC = avr-gcc
STRIP = avr-strip CFLAGS := $(CFLAGS) -mmcu=avr128da28 -Os -std=gnu99 -Wall -Wextra -Wpedantic -Wno-pointer-arith
CPPFLAGS := $(CPPFLAGS) -Iinclude -DNDEBUG
CPPFLAGS = -DNDEBUG ASFLAGS := $(ASFLAGS) -mmcu=avr128da28 -Os
CFLAGS = -Os LDFLAGS := $(LDFLAGS) -nostdlib -flto
LDLIBS := $(LDLIBS) -lgcc
CPPFLAGS += -Iinclude # Find source files
CFLAGS += -mmcu=avr128da28 -std=gnu99 -MMD -MP SRC := $(shell find src/ -name '*.[cS]' -print)
LDFLAGS += -mmcu=avr128da28 -Wall -Wextra -Wpedantic -Wno-pointer-arith DIR := $($(sort $(foreach f,$(SRC),$(dir $f))):%=obj/%) bin/
LDFLAGS += -nostdlib -nostartfiles -flto
SRC := $(wildcard src/*.c src/*/*.c src/*/*/*.c src/*/*/*/*.c src/*/*/*/*/*.c src/*/*/*/*/*/*.c src/*/*/*/*/*/*/*.c src/*/*/*/*/*/*/*/*.c) # Configure common functions for logging
SRC += $(wildcard src/*.S src/*/*.S src/*/*/*.S src/*/*/*/*.S src/*/*/*/*/*.S src/*/*/*/*/*/*.S src/*/*/*/*/*/*/*.S src/*/*/*/*/*/*/*/*.S) msg-as = $(info [AS] $(1))
OBJ := $(SRC:%=obj/%.o) 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 .PHONY: $(DIR) all clean
all: info bin/a.hex all: bin/a.out bin/a.hex
clean:; @-$(RM) -rv bin/ obj/ clean:; $(Q)-$(RM) -rv bin/ obj/
info:
$(info CC: $(CC))
$(info CFLAGS: $(CFLAGS))
$(info CPPFLAGS: $(CPPFLAGS))
$(info LDFLAGS: $(LDFLAGS))
$(info LDLIBS: $(LDLIBS))
bin/a.out: $(OBJ) # Links object files to a binary
$(info [LD] $(CC) $@) bin/a.out: $(SRC:%=obj/%.o)
@mkdir -p $(@D) $(Q)$(call msg-ld,$@)
@$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS) $(Q)$(CC) $(LDFLAGS) $(LDLIBS) -o $@ $^
bin/a.hex: bin/a.out # Links object files to a stripped binary
$(info [STRIP] $(STRIP) $@) bin/a.hex: $(SRC:%=obj/%.o)
@$(STRIP) -o $@ $< $(Q)$(call msg-ld,$@)
$(Q)$(CC) -s $(LDFLAGS) $(LDLIBS) -o $@ $^
obj/%.o: % # Assembles preprocessed assembly into object files
$(info [CC] $(CC) $@) obj/%.S.d: %.S; $Q$(CC) -MM $(ASFLAGS) $(CPPFLAGS) -MF $@ $<
@mkdir -p $(@D) obj/%.S.o: %.S
@$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $< $(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