gossamer/rules.mk
Daniel Bergman dd9e89b9a0
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
2025-06-30 19:06:41 -04:00

111 lines
2.6 KiB
Makefile

CFLAGS += $(INCLUDES) $(DEFINES)
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)
@echo LD $@
@$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
$(BUILD)/$(BIN).hex: $(BUILD)/$(BIN).elf
@echo OBJCOPY $@
@$(OBJCOPY) -O ihex $^ $@
$(BUILD)/$(BIN).bin: $(BUILD)/$(BIN).elf
@echo OBJCOPY $@
@$(OBJCOPY) -O binary $^ $@
ifeq ($(CHIP),samd51)
$(BUILD)/$(BIN).uf2: $(BUILD)/$(BIN).bin
@echo UF2CONV $@
@$(UF2) $^ -b0x4000 -co $@
else
$(BUILD)/$(BIN).uf2: $(BUILD)/$(BIN).bin
@echo UF2CONV $@
@$(UF2) $^ -co $@
endif
$(BUILD)/$(BIN).dfu: $(BUILD)/$(BIN).elf
@echo DFUCONV $@
@$(DFU_CONV) $^ $@
# 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):
:
# git submodule update --init
ifeq ($(DFU), 1)
install:
@if [ ! -f $(BUILD)/$(BIN).dfu ]; then \
echo "\033[0;31m\033[1mError: Firmware not found. Please run 'make BOARD=<board_name>' first to build the firmware.\033[0m"; \
exit 1; \
fi
@$(DFU_UTIL) -D $(BUILD)/$(BIN).dfu
@echo "\033[0;97m\033[1mRemember to press RESET to run the newly installed code!\033[0m"
else
install:
@if [ ! -f $(BUILD)/$(BIN).uf2 ]; then \
echo "\033[0;31m\033[1mError: Firmware not found. Please run 'make BOARD=<board_name>' first to build the firmware.\033[0m"; \
exit 1; \
fi
@$(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
# Generate a rule for each source file
$(foreach src,$(SRCS),$(eval $(call compile_rule,$(src))))
directory:
@$(MKDIR) -p $(BUILD)
size: $(BUILD)/$(BIN).elf
@echo size:
@$(SIZE) -t $^
clean:
@echo clean
@-rm -rf $(BUILD)
analyze:
@$(COBRA) basic $(INCLUDES) $(DEFINES) $(SRCS)
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))