# This file is (c) 1998-2001 Ted Faber (faber@lunabase.org) see COPYRIGHT
# for the full copyright and limitations of liabilities.

prefix = @prefix@
exec_prefix = @exec_prefix@

BINDIR = @bindir@
DEFINESDIR = @datadir@/grap
EXAMPLEDIR = @datadir@/examples/grap
DOCDIR = @datadir@/doc/grap
MANDIR = @mandir@/man1

PROGS=grap

INSTALL=@INSTALL@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
INSTALL_DATA=@INSTALL_DATA@
RM=rm -f
RMDIR=rm -rf
MKDIR=mkdir
LEX=@LEX@
YACC=@YACC@
CXX=@CXX@
LD=${CXX}
LIBOBJS=@LIBOBJS@
PREMKDEP= grep "^\#" grap.y > grap.cc ; grep "^\#" grap_lex.l > grap_lex.cc; touch y.tab.h 
POSTMKDEP=${RM} grap.cc grap_lex.cc y.tab.h
MKDEP= 	@MKDEP@
MKDEPFLAGS=@MKDEPFLAGS@;

SOURCES=grap.cc grap_lex.cc *.cc
DISTDIR=@PACKAGE_TARNAME@-@PACKAGE_VERSION@

CXXFLAGS +=@GXXFLAGS@ @DEFS@

# To suppress optimization of certain files under g++ where
# optimization is costly at compilation time and of minimial use at
# runtime.
SUPPRESS_OPT=@SUPPRESS_OPT@
NO_UNDEF=@NO_UNDEF@

OBJS=grap.o grap_lex.o grap_draw.o grap_pic.o grap_parse.o grap_tokenizer.o \
	${LIBOBJS}
CLEANFILES=y.output
VERYCLEANFILES=grap.cc grap_lex.cc y.tab.h
SPOTLESSFILES=grap.1 grap.ps grap.man

include Makefile.common

grap:	${OBJS}
	${CXX} ${CXXFLAGS} ${LDFLAGS} ${OBJS} ${LDLIBS} -o grap

.l.cc:
	${LEX} -o$@ $<

grap.cc:	grap.y
	${YACC} -d grap.y
	mv y.tab.c grap.cc

y.tab.h:	grap.cc

grap_lex.cc:	grap_lex.l y.tab.h

# Under g++ the memory overhead for optimization of grap_tokenizer is
# high.  Compilation time becomes prohibitive and the function is only
# called once per execution, so this rule disables optimization for
# that file under g++ by default.  If --optimize-grap_tokenizer is
# given to configure, no attempt to suppress optimization is made.
grap_tokenizer.o:	grap_tokenizer.cc
	${CXX} ${CXXFLAGS} ${SUPPRESS_OPT} -c grap_tokenizer.cc

# flex defines an unused static function.  This rule supresses that
# warning under g++.
grap_lex.o:	grap_lex.cc
	${CXX} ${CXXFLAGS} ${NO_UNDEF} -c grap_lex.cc

y.output:	grap.y
	${YACC} -v grap.y

grap.man:	grap.1
	groff -mdoc -Tascii grap.1 > grap.man

grap.ps:	grap.1
	groff -mdoc grap.1 > grap.ps

grap.1:	grap.doc
	sed "s#DEFINES_FILE#${DEFINESDIR}/grap.defines#g; s#EXAMPLES#${DOCDIR}/examples#g;" < grap.doc > grap.1 ;

# The || true lines allow make to continue on systems where install -d
# fails on existing directories.
install:	@INSTALL_DEPS@
	strip grap || true
	${INSTALL} -d ${BINDIR} || true
	${INSTALL} -d ${MANDIR} || true
	${INSTALL} -d ${DEFINESDIR} || true
	${INSTALL} -d ${EXAMPLEDIR} || true
	${INSTALL_PROGRAM} grap ${BINDIR}
	${INSTALL_DATA} grap.1 ${MANDIR}
	${INSTALL_DATA} grap*.defines ${DEFINESDIR}
	${INSTALL_DATA} examples/*.d examples/example.ms examples/*.result examples/Makefile ${EXAMPLEDIR}

#BSD ports may not want these installed
install-docs:
	${INSTALL} -d ${DOCDIR} || true
	${INSTALL_DATA} README CHANGES COPYRIGHT grap.man ${DOCDIR}

deinstall:
	${RM} ${BINDIR}/grap
	${RM} ${MANDIR}/grap.1
	${RM} ${DEFINESDIR}/grap*.defines ${DOCDIR}/README ${DOCDIR}/CHANGES ${DOCDIR}/COPYRIGHT ${DOCDIR}/grap.man
	${RM} ${EXAMPLEDIR}/*.d ${EXAMPLEDIR}/*.ms ${EXAMPLEDIR}/*result
	${RMDIR} ${DOCDIR}/examples
	${RMDIR} ${DOCDIR}
	${RMDIR} ${DEFINESDIR}
	${RMDIR} ${EXAMPLEDIR}

check:	grap grap.defines examples/example.ms
	cd examples && \
	if ../grap -d ../grap.defines example.ms > /dev/null; then \
		echo "pass";\
		exit 0; \
	else \
		echo "fail"; \
		exit 1; \
	fi

dist:	grap.man grap.ps

include .depend
