# Makefile -- build rules for BOCK
# Written by Charles Briscoe-Smith; refer to the file LEGAL for details.

srcdir = @srcdir@
VPATH = @srcdir@

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
docdir = $(prefix)/doc
datadir = @datadir@
mandir = @mandir@
manext = 1
man1dir = $(mandir)/man$(manext)

default: do-checks bock

@SET_MAKE@

      CC = @CC@
   CCDEP = $(CC) -MM -MG
  CFLAGS = -Wall -Wmissing-prototypes @CFLAGS@
CPPFLAGS = -I@srcdir@ @DEFS@
    LIBS = @LIBS@ #-lefence
     LEX = @LEX@
  LEXLIB = @LEXLIB@
    YACC = @YACC@
   PERL5 = @PERL5@

.DELETE_ON_ERROR:

.PRECIOUS: javalex.c

BOCKOBJS = xmalloc.o stringpool.o error.o \
           javalex.o javagrammar.o parsetree.o \
           packagetree.o codegen.o transform.o bock.o

do-checks:
	$(MAKE) -C ctests check
	touch do-checks

all: bock test

install:
	install -d $(bindir)
	install -s -m 755 bock $(bindir)/bock
	install -m 755 jcomp $(bindir)/jcomp
	install -d $(datadir)/bock/lib
	install -m 644 lib/* $(datadir)/bock/lib
#	set -e; \
#	docdir=`cat install-docdir`; \
#	for x in lib/*; do \
#		sed "2,3s=the file LEGAL=$$docdir/bock/copyright=" \
#			< $$x > $(datadir)/bock/$$x; \
#	done
#	install -m 644 native.c $(datadir)/bock
	install -d $(man1dir)
	install -m 644 bock.1 $(man1dir)/bock.$(manext)
	gzip -9n $(man1dir)/bock.$(manext)
	install -m 644 jcomp.1 $(man1dir)/jcomp.$(manext)
	gzip -9n $(man1dir)/jcomp.$(manext)
	install -d $(docdir)/bock
	install -m 644 debian/changelog $(docdir)/bock
	install -m 644 BUGS $(docdir)/bock
	gzip -9n $(docdir)/bock/*
	install -m 644 README $(docdir)/bock
	install -m 644 COPYING $(docdir)/bock
	install -m 644 LEGAL $(docdir)/bock/copyright

check: Test Test2
	./Test > Test.output 2>&1
	diff Test.output Test.output.expected
	./Test2 > Test2.output 2>&1
	diff Test2.output Test2.output.expected
	$(MAKE) -C tests.java check
	@echo '***' Tests successful.

Test: Test.o
	$(CC) -o $@ Test.o $(LIBS)
Test2: Test2.o
	$(CC) -o $@ Test2.o $(LIBS)

Test.c: Test.java bock lib/*
	./bock -o Test.c -s Test Test.java lib/*
Test2.c: Test2.java bock lib/*
	./bock -o Test2.c -s Test2 Test2.java lib/*

bock: $(BOCKOBJS)
	$(CC) -o $@ $(BOCKOBJS) $(LEXLIB) $(LIBS)
	echo '$(docdir)' >install-docdir

javagrammar.y.h: javagrammar.c
javagrammar.c: javagrammar.y
	$(YACC) -d $(srcdir)/javagrammar.y
	mv y.tab.c javagrammar.c
	mv y.tab.h javagrammar.y.h

javalex.o: javagrammar.o types.h
javagrammar.o: types.h

parsetree.c: parsetree.c.in parsetree.c-gen.pl
	$(PERL5) $(srcdir)/parsetree.c-gen.pl $(srcdir)/parsetree.c.in >parsetree.c

clean:
	$(RM) *.o *.class bock install-docdir Test Test.c Test.output
	$(RM) Test2 Test2.c Test2.output javagrammar.output core
distclean: clean
	$(MAKE) -C ctests distclean
	$(MAKE) -C tests.java distclean
	$(RM) Makefile.old config.cache config.log config.status
	$(RM) depend do-checks Makefile jcomp
maintainer-clean: distclean
	$(RM) parsetree.c javalex.c javagrammar.c javagrammar.y.h *.h

%.h: %.c
	@echo 'Generating $@...'
	@echo "/* Automatically generated by Makefile on `date` */" >$@
	@echo '#ifndef SEEN_$*_h' >>$@
	@echo '#define SEEN_$*_h' >>$@
	@echo '#include "$<"' >>$@
	@echo '#endif' >>$@

depend: parsetree.c #javagrammar.c javalex.c
	@echo 'Calculating dependencies...'
	@for file in `$(CCDEP) $(srcdir)/*.c|fmt -1|sed 's/.*\///'|grep '\.h$$'`; \
	do if test ! -r $$file; then \
	echo "Generating $$file..."; \
	echo "/* Automatically generated by Makefile on `date` */" >$$file; \
	echo "#ifndef SEEN_`basename $$file .h`_h" >>$$file; \
	echo "#define SEEN_`basename $$file .h`_h" >>$$file; \
	echo '#include "'"`basename $$file .h`.c"'"' >>$$file; \
	echo "#endif" >>$$file; \
	fi; done
	@$(CCDEP) $(srcdir)/*.c >depend

parsetree.h: parsetree.c

# GNU Make will automatically rebuild 'depend' if it doesn't exist
include depend
