Files
avr-test/Makefile
Quinn dd590b9f42 rework Makefile
One issue is that dependencies are not properly generated, this will be
implemented later.
2025-12-23 13:09:00 +01:00

60 lines
1.7 KiB
Makefile

SHELL = /bin/sh
.SUFFIXES: # Disable implicit rules.
CC = avr-gcc
CFLAGS := $(CFLAGS) -mmcu=avr128da28 -Os -std=gnu99 -Wall -Wextra -Wpedantic -Wno-pointer-arith
CPPFLAGS := $(CPPFLAGS) -Iinclude -DNDEBUG
ASFLAGS := $(ASFLAGS) -mmcu=avr128da28 -Os
LDFLAGS := $(LDFLAGS) -nostdlib -flto
LDLIBS := $(LDLIBS) -lgcc
# Find source files
SRC := $(shell find src/ -name '*.[cS]' -print)
DIR := $($(sort $(foreach f,$(SRC),$(dir $f))):%=obj/%) bin/
# 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: $(DIR) all clean
all: bin/a.out bin/a.hex
clean:; $(Q)-$(RM) -rv bin/ obj/
# Links object files to a binary
bin/a.out: $(SRC:%=obj/%.o)
$(Q)$(call msg-ld,$@)
$(Q)$(CC) $(LDFLAGS) $(LDLIBS) -o $@ $^
# Links object files to a stripped binary
bin/a.hex: $(SRC:%=obj/%.o)
$(Q)$(call msg-ld,$@)
$(Q)$(CC) -s $(LDFLAGS) $(LDLIBS) -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 $@ $<
# 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