Refactor build rules in rules.mk for better tracking and incremental builds (#2)

* Refactor build rules in rules.mk to use a compile rule template for source files, improving maintainability and readability. Update dependency file generation to ensure correct paths.

* refactor: move emscripten targets to rules.mk and fix build issues
fix: correct .PHONY declaration and dependency handling in rules.mk
This commit is contained in:
Daniel Bergman 2025-07-01 01:06:41 +02:00 committed by GitHub
parent 382265d086
commit dd9e89b9a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,19 +1,21 @@
CFLAGS += $(INCLUDES) $(DEFINES)
OBJS = $(addprefix $(BUILD)/, $(notdir %/$(subst .c,.o, $(SRCS))))
OBJS = $(addprefix $(BUILD)/, $(notdir $(subst .c,.o, $(SRCS))))
SUBMODULES = tinyusb
COBRA = cobra -f
ifndef EMSCRIPTEN
# Hardware build targets
ifeq ($(DFU), 1)
all: $(BUILD)/$(BIN).elf $(BUILD)/$(BIN).hex $(BUILD)/$(BIN).bin $(BUILD)/$(BIN).dfu size
else
all: $(BUILD)/$(BIN).elf $(BUILD)/$(BIN).hex $(BUILD)/$(BIN).bin $(BUILD)/$(BIN).uf2 size
endif
else
# Emscripten build targets
all: $(BUILD)/$(BIN).elf $(BUILD)/$(BIN).html
endif
$(BUILD)/$(BIN).elf: $(OBJS)
@ -42,7 +44,16 @@ $(BUILD)/$(BIN).dfu: $(BUILD)/$(BIN).elf
@echo DFUCONV $@
@$(DFU_CONV) $^ $@
.phony: $(SUBMODULES)
# Emscripten HTML target
$(BUILD)/$(BIN).html: $(OBJS)
@echo HTML $@
@$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $@ \
-s ASYNCIFY=1 \
-s EXPORTED_RUNTIME_METHODS=lengthBytesUTF8,printErr \
-s EXPORTED_FUNCTIONS=_main \
--shell-file=./watch-library/simulator/shell.html
.PHONY: $(SUBMODULES) all clean size analyze install directory
$(SUBMODULES):
:
@ -65,10 +76,15 @@ install:
@$(UF2) -D $(BUILD)/$(BIN).uf2
endif
# Define a compile rule template
define compile_rule
$(BUILD)/$(notdir $(1:.c=.o)): $(1) | $(SUBMODULES) directory
@echo CC $$@
@$(CC) $(CFLAGS) $$< -c -o $$@
endef
$(BUILD)/%.o: | $(SUBMODULES) directory
@echo CC $@
@$(CC) $(CFLAGS) $(filter %/$(subst .o,.c,$(notdir $@)), $(SRCS)) -c -o $@
# Generate a rule for each source file
$(foreach src,$(SRCS),$(eval $(call compile_rule,$(src))))
directory:
@$(MKDIR) -p $(BUILD)
@ -84,6 +100,11 @@ clean:
analyze:
@$(COBRA) basic $(INCLUDES) $(DEFINES) $(SRCS)
DEPFILES := $(SRCS:%.c=$(BUILD)/%.d)
DEPFILES := $(addprefix $(BUILD)/, $(notdir $(SRCS:%.c=%.d)))
# Generate dependency files
$(BUILD)/%.d: %.c | directory
@$(CC) $(CFLAGS) -MM -MT $(BUILD)/$(notdir $(<:.c=.o)) $< > $@
-include $(wildcard $(DEPFILES))