# Usage: # $(1): Target prefix # # Generate build rules for files in variable _SRCS. Use _CFLAGS, # _CXXFLAGS, _ASFLAGS to control build flags. # Object files will be put into $BUILDDIR # E.g: # $(eval $(call COMPILE_RULES,AM_)) define COMPILE_RULES $(1)OBJS += $$(addprefix $$(BUILDDIR)/, $$(addsuffix .o, $$($(1)SRCS))) ### Rule (compile): a single `.c` -> `.c.o` (gcc) $$(filter %.c.o, $$($(1)OBJS)): $$(BUILDDIR)/%.c.o: %.c @mkdir -p $$(dir $$@) && echo + CC $$< @$$(CC) $$($(1)CFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (compile): a single `.cc` -> `.cc.o` (g++) $$(filter %.cc.o, $$($(1)OBJS)): $$(BUILDDIR)/%.cc.o: %.cc @mkdir -p $$(dir $$@) && echo + CXX $$< @$$(CXX) $$($(1)CXXFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (compile): a single `.cpp` -> `.cpp.o` (g++) $$(filter %.cpp.o, $$($(1)OBJS)): $$(BUILDDIR)/%.cpp.o: %.cpp @mkdir -p $$(dir $$@) && echo + CXX $$< @$$(CXX) $$($(1)CXXFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (compile): a single `.S` -> `.S.o` (gcc, which preprocesses and calls as) $$(filter %.S.o, $$($(1)OBJS)): $$(BUILDDIR)/%.S.o: %.S @mkdir -p $$(dir $$@) && echo + AS $$< @$$(AS) $$($(1)ASFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (`#include` dependencies): paste in `.d` files generated by gcc on `-MMD` -include $$(addprefix $$(BUILDDIR)/, $$(addsuffix .d, $$(filter %.c %.cc, $$($(1)SRCS)))) endef # Usage: # $(1): Library build path # $(2): Library variable prefix # E.g: # $(eval $(call ADD_LIBRARY,am-riscv,AM_)) define ADD_LIBRARY $(eval $(call COMPILE_RULES,$(2))) $(1): $$($(2)OBJS) @mkdir -p $$(dir $$@) @echo + AR "->" $$(patsubst $$(CURDIR)/%,%,$(1)) @$$(AR) rcs $$@ $$($(2)OBJS) endef # Usage: # $(1): Image build path # $(2): Image variable prefix # E.g: # $(eval $(call ADD_IMAGE,dummy,DUMMY_)) define ADD_IMAGE $(eval $(call COMPILE_RULES,$(2))) $(1).elf: $$($(2)OBJS) @mkdir -p $$(dir $$@) @echo + LD "->" $$(patsubst $$(CURDIR)/%,%,$(1).elf) @$$(LD) -o $$@ $$(filter-out -l%,$$($(2)LDFLAGS)) --start-group $$($(2)OBJS) $$(filter -l%,$$($(2)LDFLAGS)) --end-group $(1).bin: $(1).elf @echo + OBJCOPY "->" $$(patsubst $$(CURDIR)/%,%,$(1).bin) @$$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(1).elf $(1).bin endef