[antlr-interest] make rules for *.g -> outputs

Sebastian Kaliszewski sk at z.pl
Sat Dec 4 08:19:18 PST 2004


Dnia pią 3. grudzień 2004 19:35, Martin Probst napisał:
> Hi,
> I had to write Makefiles too. I wrote a rule that the generated files
> depend on the grammar:
> MyParser.cpp MyParser.hpp MyLexer.cpp [...]: mygrammar.g
> 	(antlr action ...)
> MyParser.o: MyParser.cpp MyParser.hpp MyParserTokenTypes.hpp
> (...)
> This lead to the annoying result that if the grammar was changed make
> expected every file to change - but the didn't (e.g. only a small change
> in Parser - MyLexer.cpp won't be touched). So make kept calling the
> antlr target again and again for every file, even if they wouldn't be
> altered.
> So I came up with two solutions: either touch all the grammars products
> after the antlr run or use a ".mygrammar_was_parsed" file (touching it
> after antlr run), make it depend on the grammar and the object files
> depend on that magic file.
> Both ways lead to unnecessary compiles of source files :-(
> Either I misunderstood something with Makefiles (I'm not really a Guru
> with them) or there is no other solution ... at least a change always
> resulted in a rebuild and dependencies were kind of correct.


Below is (an edited) snippet from my makefile to do what you described
above. Set properly 'SRC' to all the source files begin compiled, change
'GENSRC' files to those actually produced by your grammar (only .cpp
files, not .hpp!). Set all the marcos like 'ANTLR', 'CXX', 'LINK', etc. to
be properly defined for your development environment and also properly
set compilation as well as linking options ('CXXFLAGS' and 'LINKOPT') as
needed. Then just substitute 'exe' with your target, substitute 'grammar'
with your grammar, add your rules for other languages (if you have any
other language files in your project).
You should not specify any C++ header (.hpp) files anywhere, nor specify
generic .hpp rules. This makefile autmatically generates properly all the
dependencies -- %.d : %.cpp rule & include directives at the end do the
whole magic.
This thing requires gnu make GCC (or option sompatible product) as well
as *nixish toolset (sed, diff, touch and sleep are used). It also should
be run from sh compatible (bash is good) shell (maybe it works form csh
but I did't check).

8<--------------------------------
#! /usr/bin/make
#
# Generic makefile for ANTLR based C++ project
# Released to public domain by S. Kaliszewski in Dec 2004
#

SRC := 		all the cpp source files
GENSRC :=	MyParser.cpp MyLexer.cpp MyTreeParser.cpp

ANTLR :=		cantlr
CXX := 		g++
LINK := 		g++
DIFF :=		diff -NEbBq
DEL := 		rm -f --
CP := 		cp --
ECHO := 		echo
TOUCH :=	touch
SLEEP :=		sleep
SED :=		sed

CXXFLAGS :=	-pipe -O3 -DNDEBUG -ansi -pedantic
LINKOPT :=	-lantlr -lm



exe : $(GENSRC:.cpp=.o) $(SRC:.cpp=.o)
	$(LINK)  -o $@ $+ $(LINKOPT)

%.o : %.d
	$(CXX) $(CXXFLAGS) -c -o $@ ${<:.d=.cpp}

%.d : %.cpp
	@set -e; $(CXX) -MM $(CXXFLAGS) $< \
	| $(SED) 's!\($*\)\.o[ :]*!\1.o $@ : !g' > $@; [ -s $@ ] 2>/dev/null || $(DEL) $@

grammar.g.dd : grammar.g
	-@$(foreach FILE, $(GENSRC), $(DEL) $(FILE).old >/dev/null 2>&1 ; true; )
	-@$(foreach FILE, $(GENSRC), [ -f $(FILE:.cpp=o) ] && [ $(FILE) -ot $(FILE:.cpp=o) ] \
          && $(CP) $(FILE) $(FILE).old && $(ECHO) Copying $(FILE) to $(FILE).old; true; )
	$(ANTLR) $<
	-@$(SLEEP) 1
	-@$(foreach FILE, $(GENSRC), { $(DIFF) $(FILE) $(FILE).old >/dev/null 2>&1 \
          && $(TOUCH) $(FILE:.cpp=.d) && $(TOUCH) $(FILE:.cpp=.o); } \
          || { $(DEL) $(FILE:.cpp=.o) \
          ; $(ECHO) Found changes in $(FILE) since last $(FILE:.cpp=.o) compilation; }; true; )
	-@$(foreach FILE, $(GENSRC), $(DEL) $(FILE).old >/dev/null 2>&1 ; true; )
	-@$(SLEEP) 1
	@$(TOUCH) $@

$(GENSRC) : grammar.g.dd

include $(GENSRC:.cpp=.d)
include $(SRC:.cpp=.d)

8<----------------------------


Hope it's useful

rgds
-- 
Sebastian Kaliszewski


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list