Crap, devkitPro's Makefile Template
I really, really got tired of taking care of it. It causes many problems with the dirty hack, “calling itself”. But you know, it’s actually used in many projects.
make -C $(BUILD) -f $(CURDIR)/Makefile
Why is the template used? The major feature of the template is to output objects in a directory different from the one where source files are. The feature is provided by automake or something kind of that, but we want to do that only with Makefile. How can we do that? Google often solves any problems, but in this case, I couldn’t find a solution. So, I’ll write code by myself.
Fortunately, the manual of GNU make provides a hint.
Consider an example where your targets are to be placed in a separate directory
OBJDIR := objdir
OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
$(OBJDIR)/%.o : %.c
$(COMPILE.c) $(OUTPUT_OPTION) $<
all: $(OBJS)
$(OBJS): | $(OBJDIR)
$(OBJDIR):
mkdir $(OBJDIR)
It looks nice, but it can’t generate multiple directories. To solve the problem, I use foreach, eval, and dir.
$(foreach f,$(BUILD) $(PATCHES) $(PAYLOAD_OBJS),$(eval $f : | $(dir $f)))
%/:
mkdir -p $@
Probably you don’t need an explanation for foreach. In eval, each content will be evaluated as a independent sentence. dir will return the directory with “/” suffix. The meaning is clear: it allows to make a generic rule.
Although (GNU) make is difficult to use, it is really powerful. The world of make may be broader than you think.