Warn HN: watch out for built-in make rules
Since the whole thing is logically a module, I decided to call the files:
parser.l parser.y parser.c
The .l has the lexer, .y the parser, and the .c file some supporting common code.In the Makefile, the rules were like this:
OBJS := [...] y.tab.o lex.yy.o parser.o
y.tab.c: parser.y
$(MY_YACC) ...
lex.yy.c: parser.y
$(MY_LEX) ...
Things were coming along fine, until I updated the parser.y file so that it was newer than parser.c, and ran make.Little did I know that make has a built in rule which looks something like this:
# make .c file from .y file
.y.c:
$(YACC) $(YACCFLAGS) $< > $@
Of course, parser.o depends on parser.c, so has to be rebuilt. But parser.c, through the built-in rule, depends on the newer parser.y.To my dismay, the build failed with warnings about missing and duplicate symbols. What?
I went through the make output, and saw the following:
yacc parser.y > parser.c
I rubbed my eyes, shook my head, but it was still there: the module of supporting C routines is gone, clobbered by the generated parser that should only go into y.tab.c.I reported this to the GNU Make bug database and was informed that a .y to .c rule is a POSIX requirement, believe it or not, so they can do nothing.
You heard it: POSIX stupidly requires make to have a built-in rule which has .c files as its target.
After that I stopped trusting built-in rules. Who knows what else is in there? And how it varies between different make programs and their versions? Even if the contents are otherwise harmless today, maybe that will change in the future if some new, potentially dangerous item is added again.
The way you get rid of all of them is an empty SUFFIXES rule:
.SUFFIXES:
Then you can write just rules you need yourself, in plain sight.