# Copyright Notices

# This software was developed for use by the U.S. Government as
# represented by the Administrator of the National Aeronautics and
# Space Administration. No copyright is claimed in the United States
# under 17 U.S.C. 105.

# This software may be used, copied, and provided to others only as
# permitted under the terms of the contract or other agreement under
# which it was acquired from the U.S. Government.  Neither title to nor
# ownership of the software is hereby transferred.  This notice shall
# remain on all copies of the software.

# $Id: Makefile,v 1.45 2003/09/29 16:59:19 wedgingt Exp $
# Makefile for the Europa project's parser

include ../commonMakeOptions.imake

# YYERROR_VERBOSE tells bison (and yacc?) to produce more details when a parse error occurs.
# YYDEBUG enables code related to debugging.
OTHERFLAG = -DYYERROR_VERBOSE -DYYDEBUG=1

SRCS = ddlModelParser.cc ddl-lexer.cc

OBJECTS = $(SRCS:%.cc=%.o)

DDL_LIBRARY=libddl-parser

all:	$(OBJECTS)
	echo $@ > $@

# No tests or test code, so ...
test-code: all
	echo $@ > $@

tests: test-code
	echo $@ > $@

$(DDL_LIBRARY).a: $(DDL_LIBRARY).a($(OBJECTS))
	$(RANLIB) $@

$(DDL_LIBRARY).a(%.o): %.o
	$(AR) $@ $?

$(DDL_LIBRARY).so: $(OBJECTS)
	$(LD) $(LDFLAG) -o $@ $^

ddl-lexer.o:	ddl-lexer.cc ddlModelParser.h

# Invoke sed to replace yy prefix to avoid clashes with other parsers
ddl-lexer-deb.cc:	ddl-lexer.l Makefile .yy_letters
	$(LEX) -s -d -o$@.temp ddl-lexer.l
	$(SED) -e 's/yy/$(YY_LETTERS)/g' < $@.temp > $@.tmp
	mv $@.tmp $@

ddl-lexer-deb.o:	ddl-lexer-deb.cc ddl-lexer.o

# The first sed changes any error messages to the same format as g++'s so Emacs recognizes them.
# Also, invoke sed to replace yy prefix to avoid clashes with other parsers
ddlModelParser.h ddlModelParser.cc:	ddlModelParser.y Makefile .yy_letters
	$(YACC) -v -d ddlModelParser.y 2>&1 | \
	    $(SED) -e 's/("//' -e 's/", line /:/' -e 's/) error//'
	$(SED) -e 's/yy/$(YY_LETTERS)/g' \
		-e '/__attribute__/s/__attribute__/\/\/__attribute__/' \
		< ddlModelParser.tab.c > ddlModelParser.cc.tmp
	$(SED) -e 's/yy/$(YY_LETTERS)/g' < ddlModelParser.tab.h > ddlModelParser.h.tmp
	mv ddlModelParser.cc.tmp ddlModelParser.cc
	mv ddlModelParser.h.tmp ddlModelParser.h

# Invoke sed to replace yy prefix to avoid clashes with other parsers
ddl-decs.hh: ddl-decs.hh.dd .yy_letters
	$(SED) -e 's/yy/$(YY_LETTERS)/g' < $@.dd > $@.tmp
	mv $@.tmp $@

clean:
	rm -f *.o ddl-parser.h ddl-parser.cc ddl-lexer.cc ir.out
	rm -f ddl-lexer-deb.cc ddl-parser.output depend all *.a *.so ddl-lexer-deb.cc.temp
	rm -f ddlModelParser.h ddlModelParser.cc ddlModelParser.output test-code tests
	rm -f ddlModelParser.tab.c ddlModelParser.tab.h ddl-lexer.cc.dd *.tmp
	rm -f .yy_letters .yy_letters.tmp ddl-decs.hh

.SUFFIXES:	.l .y

# Invoke sed to replace yy prefix to avoid clashes with other parsers
%.cc: %.l Makefile .yy_letters
	$(LEX) -o$@.dd $(shell basename $@ .cc).l
	$(SED) -e 's/yy/$(YY_LETTERS)/g' < $@.dd > $@.tmp
	mv $@.tmp $@

# Record the YY_LETTERS value used to force recompiling when it changes.
.yy_letters: .ALWAYS
	echo '$(YY_LETTERS)' > $@.tmp
	-cmp -s $@.tmp $@ || cp $@.tmp $@
	-cmp -s $@.tmp $@ && rm $@.tmp

.ALWAYS:

depend: Makefile ddl-lexer-deb.cc ddlModelParser.h $(SRCS) ddl-decs.hh ../Includes/version.hh
	$(DEPEND)
	test -s $@

include depend
