help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Interesting Make rule using static patterns


From: Smith, Matthew X
Subject: Interesting Make rule using static patterns
Date: Fri, 15 Dec 2006 15:47:51 -0800

I've run across an interesting Make rule using static patterns, define/eval/call, and dependencies.  I think I understand how it works, but I'd like to make sure.  The thing that throws me for a loop is the lack of a prerequisite pattern.

This is using make Cygwin 3.80, and it all works fine, I'm just trying to understand *how* this works fine.

[ ----------------------------------]
SOURCEFILES = foo.cpp bar.cpp
OBJS = foo.o bar.o
OBJ_FOLDER = temp/obj

$(OBJS): %.o:
        $(CC) …. -MD … -c $< -o $@

define RuleCppToObj
$$(OBJ_FOLDER)\$((subst .cpp,.o,$(1)): $(1)
endef

$(foreach FILE,$(SOURCEFILES),$(eval $(call RuleCppToObj,$(FILE))))

DEPENDENCIES = $(SOURCEFILES:.cpp=.d)
DEP_PATH = depend
DEP_FILES = $(addprefix $(DEP_PATH),$(DEPENDENCIES))
-include $(DEP_FILES)
[ ----------------------------------]
Sample dependency file: foo.d

foo.o: foo.cpp foo.h
[ ----------------------------------]

Near as I can figure, and I'll concentrate only on building foo.o:

The static pattern rule defines a rule which builds anything named foo using the specified commands.  If there wasn't anything else, since we don't have any prerequisites, if the file exists, once that file exists, it won't build anything.  So we have to add...

The $(foreach…) command creates a rule which builds foo.o from foo.cpp.  Now this is where I have a question.  Does the fact that we have a foo.o target through the static pattern mean that it simply appends the foo.cpp dependency to the (initially empty) list of prerequisites?  What role does the %o have in this then?

Next it then goes through and reads in any existing dependency files for the project, which contains the actual list of prerequisites that are used to build the target.  Again, they would just be appended to the list of prerequisites.

The $(foreach …) command is then the default command in the case that the dependencies don't exist.

*whew*

Do I have that right?  I don't know why, but that lack of prerequisites in the static pattern just makes my brain grind to a halt.  I also don't understand why when I take the %o: out of the static pattern rule, the whole thing falls apart.  Does the static pattern actually define something akin to a macro on how to build each of the targets independently? 

Thanks in advance...

Matt Smith





reply via email to

[Prev in Thread] Current Thread [Next in Thread]