fixup! Makefile: start moving to macro based Makefile target generation

This commit is contained in:
xinyangli 2024-12-10 18:12:00 +08:00
parent 54ee5d6c31
commit fa7d9c5eb0
Signed by: xin
SSH key fingerprint: SHA256:UU5pRTl7NiLFJbWJZa+snLylZSXIz5rgHmwjzv8v4oE

View file

@ -4,32 +4,33 @@
#
# Generate build rules for files in variable <PREFIX>_SRCS. Use <PREFIX>_CFLAGS,
# <PREFIX>_CXXFLAGS, <PREFIX>_ASFLAGS to control build flags.
# Object files will be put into $BUILDDIR
# E.g:
# $(eval $(call COMPILE_RULES,AM_))
define COMPILE_RULES
AM_OBJS := $(addprefix $(DST_DIR)/, $(addsuffix .o, $(AM_SRCS)))
$(1)OBJS := $$(addprefix $$(BUILDDIR)/, $$(addsuffix .o, $$($(1)SRCS)))
### Rule (compile): a single `.c` -> `.c.o` (gcc)
$$(filter %.c.o, $$($(1)OBJS)): $$(DST_DIR)/%.c.o: %.c
$$(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)): $$(DST_DIR)/%.cc.o: %.cc
$$(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)): $$(DST_DIR)/%.cpp.o: %.cpp
$$(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)): $$(DST_DIR)/%.S.o: %.S
$$(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 $$(DST_DIR)/, $$(addsuffix .d, $$(filter %.c %.cc, $$(AM_SRCS))))
-include $$(addprefix $$(BUILDDIR)/, $$(addsuffix .d, $$(filter %.c %.cc, $$($(1)SRCS))))
endef
# Usage: