help-make
[Top][All Lists]
Advanced

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

Re: "Advanced Auto-Dependency Generation" and lex/yacc (again)


From: Philip Guenther
Subject: Re: "Advanced Auto-Dependency Generation" and lex/yacc (again)
Date: Sat, 28 Jan 2006 17:06:23 -0700 (MST)

--------
James P Williams writes:
>In my case, however, I'm trying to implement this in a general way
>that can be #included by Makefiles from many different applications.
>Also, files other than lex.c files can #include yacc.h.  So the
>dependency graph could look like this.
>
>   prog.exe: goo.o lex.o yacc.o
>
>   goo.o: goo.c yacc.h
>   lex.o: lex.c yacc.h
>   yacc.o: yacc.c
>
>   lex.c: lex.l
>   yacc.c yacc.h: yacc.y
>
>And just because there's a lex.l doesn't mean there's an associated yacc.y.
>
>How can I teach make these dependencies in a general way without
>having to write them by hand, using "Advanced Auto-Dependencies"?
>I don't see a way to detect the goo.o/yacc.h dependency because it
>requires that yacc be run first.

You're correct that yacc.h must be generated before goo.o can be
built or its dependencies automatically calculated.  However, once
yacc.h exists, whether it's up to date or not, the automatic
dependency generation process will work correctly.  So, you just
need some way to tell make that yacc.h must exist before attempting
to build any .o files.  This sort of dependency can be expressed
using an order-only prerequisite:

        %.o: | yacc.h

That tells make that if yacc.h doesn't exist, it must be created
before make tries to build any .o files.  However, that dependency
by itself will _not_ force all the .o files to be rebuilt every
time the yacc.h file is updated


You asked about how to do this in a general way:
>In my case, however, I'm trying to implement this in a general way
>that can be #included by Makefiles from many different applications.

The generalization will depend on your exact rules for running yacc,
but here's an example set of rules you can ponder:


# Are there any yacc input files?  If so, add rules for them
YACC_FILES := $(wildcard *.y)
ifneq (${YACC_FILES},)

# Force use of the -d flag and use the -o flag to make yacc output
# directly to the desired .tab.[ch] names.  We use ${@:.h=.c} instead
# of just $@ so that the name of the .tab.c file is passed even if
# this rule is triggered by the .tab.h target
%.tab.c %.tab.h: %.y
        ${YACC} ${YFLAGS} -d -o ${@:.h=.c} $<

# Require that the .tab.h file(s) exist before any .o files are built
%.o: | ${YACC_FILES:.y=.tab.h}

endif


Philip Guenther





reply via email to

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