INSTALLDIR = /arch

CC = gcc
LDFLAGS = -g -lm
_CFLAGS = -g -O
CFLAGS = -Wall -Wshadow -Wwrite-strings -W -Wpointer-arith -Wcast-qual -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wnested-externs -Wconversion -Werror $(_CFLAGS)

all: code dvi-files tests symbols-stuff 
code: libydfg.a ydfg-glassbox-test ydfg-blackbox-test symbols-bb-test ydfg-disasm ydfg-test-reader ydfg-test-reader-bad ydfg-test-reader-stdin
dvi-files: ydfg-spec.dvi symbols.dvi hashtable.dvi list.dvi multiset.dvi numbering.dvi ydfg-disassembler.dvi
ps-files: ydfg-spec.ps symbols.ps hashtable.ps list.ps multiset.ps
.PRECIOUS: ydfg-spec.tex hashtable.tex list.tex multiset.tex symbols.tex
symbols-stuff: symbols.dvi symbols-tests

ifeq ($(shell uname -sr),$(shell echo SunOS 5.4))
symbols-bb-test: symbols.o symbols-bb-test.o hashtable.o random.o
else
symbols-bb-test: symbols.o symbols-bb-test.o hashtable.o
endif

symbols-bb-test.o: symbols-bb-test.c symbols.h
symbols-bb-test.c: symbols.nw
	notangle -L -R$@ $< | cpif $@
symbols.o: symbols.c symbols.h hashtable.h
symbols.h: symbols.nw
	notangle -L -R$@ $< | cpif $@

ydfg-test-reader-stdin: ydfg-test-reader-stdin.o libydfg.a
run-reader-tests-stdin: read-test-stdin-ex1 read-test-stdin-ex2 read-test-stdin-ex3 read-test-stdin-ex4 read-test-stdin-ex5 read-test-stdin-ex6  read-test-stdin-ex7 read-test-stdin-ex8 read-test-stdin-ex9  read-test-stdin-ex10    read-test-stdin-ex12
read-test-stdin-ex%: tmp-stdout%.ydfg
	diff -I'^DFG [0-9]+.[0-9] ;;;'  $(patsubst tmp-stdout%.ydfg,graph-examples/ex%.ydfg,$<) $<
tmp-stdout%.ydfg:  graph-examples/ex%.ydfg ydfg-test-reader-stdin
	cat $< | ydfg-test-reader-stdin | cat > $@


ydfg-test-reader: ydfg-test-reader.o libydfg.a
#do-test-reader is a script that does the comparison
run-reader-tests: test-bad-inputs
run-reader-tests: read-test-ex1 read-test-ex2 read-test-ex3 read-test-ex4 read-test-ex5 read-test-ex6 read-test-ex7
run-reader-tests: read-test-t1 read-test-t2 read-test-t3 read-test-t4 read-test-t5 read-test-t6 read-test-t7 read-test-t8 read-test-t9 read-test-t10 read-test-t11 read-test-t12 read-test-t16
#Note that this requires gnu diff for the -I argument.
#Note that gnu diff man pages do not exist.  See the info tree.
read-test-ex%: ydfg-test-reader
	rm -f $(patsubst read-test-ex%,tmp-ex%.ydfg,$@)
	ydfg-test-reader $(patsubst read-test-ex%,graph-examples/ex%.ydfg,$@) $(patsubst read-test-ex%,tmp-ex%.ydfg,$@)
	diff -I'^DFG [0-9]+.[0-9] ;;;'  $(patsubst read-test-ex%,graph-examples/ex%.ydfg,$@) $(patsubst read-test-ex%,tmp-ex%.ydfg,$@)
read-test-t%: ydfg-test-reader
	rm -f $(patsubst read-test-t%,tmp-t%.ydfg,$@) $(patsubst read-test-t%,tmp-t%l.ydfg,$@)
	ydfg-test-reader $(patsubst read-test-t%,graph-examples/t%.ydfg,$@) $(patsubst read-test-t%,tmp-t%.ydfg,$@)
	ydfg-test-reader $(patsubst read-test-t%,graph-examples/t%l.ydfg,$@) $(patsubst read-test-t%,tmp-t%l.ydfg,$@)
	diff -I'^DFG [0-9]+.[0-9] ;;;'  $(patsubst read-test-t%,graph-examples/t%.ydfg,$@) $(patsubst read-test-t%,tmp-t%.ydfg,$@)
	diff -I'^DFG [0-9]+.[0-9] ;;;'  $(patsubst read-test-t%,graph-examples/t%l.ydfg,$@) $(patsubst read-test-t%,tmp-t%l.ydfg,$@)

read-test-t11: ydfg-test-reader
	rm -f $(patsubst read-test-t%,tmp-t%.ydfg,$@) $(patsubst read-test-t%,tmp-t%l.ydfg,$@)
	ydfg-test-reader $(patsubst read-test-t%,graph-examples/t%.ydfg,$@) $(patsubst read-test-t%,tmp-t%.ydfg,$@)
	diff -I'^DFG [0-9]+.[0-9] ;;;'  $(patsubst read-test-t%,graph-examples/t%.ydfg,$@) $(patsubst read-test-t%,tmp-t%.ydfg,$@)>$(patsubst read-test-t%,tmp-%.diff,$@);diff read-test-t11.expected.diff $(patsubst read-test-t%,tmp-%.diff,$@)

read-test-t16: ydfg-test-reader
	ydfg-test-reader-bad  graph-examples/t16.ydfg tmp-t15-bad.ydfg
	echo "An abort on the previous line is OK"

test-bad-inputs: ydfg-test-reader
	ydfg-test-reader-bad graph-examples/ex11-bad.ydfg tmp-ex11-bad.ydfg
	echo "An abort on the previous line is OK"

hashtable.o: hashtable.c hashtable.h
hashtable.c hashtable.h hashtable.tex: hashtable.nw
hashtable.dvi: hashtable.tex  inchmar.sty
hashtable-test.c: hashtable.nw
	notangle -L -R$@ $< | cpif $@
hashtable-test.o: hashtable-test.c hashtable.h
hashtable-test: hashtable-test.o hashtable.o

numbering.h: numbering.nw
	notangle -L -R$@ $< | cpif $@
numbering.c: numbering.nw
	notangle -L -R$@ $< | cpif $@
numbering.o: numbering.c numbering.h hashtable.h
sexpr.o: sexpr.c sexpr.h
list.dvi multiset.dvi stacks-and-queues.dvi symbols.dvi: inchmar.sty
sexpr-identity-test.c: ydfg-spec.nw
	notangle -L -R$@ $< | cpif $@
sexpr-identity-test.o: sexpr-identity-test.c sexpr.h symbols.h numbering.h
sexpr-identity-test: sexpr-identity-test.o sexpr.o symbols.o hashtable.o numbering.o
run-sexpr-tests: run-sexpr-identity-test-1
run-sexpr-identity-test-%: sexpr-identity-test 
	rm -f $(patsubst run-sexpr-identity-test-%,sexpr-test-%.output,$@)
	sexpr-identity-test < $(patsubst run-sexpr-identity-test-%,sexpr-test-%.lisp,$@) > $(patsubst run-sexpr-identity-test-%,sexpr-test-%.output,$@)
	diff $(patsubst run-sexpr-identity-test-%,sexpr-test-%.lisp,$@) $(patsubst run-sexpr-identity-test-%,sexpr-test-%.output,$@)

list.o: list.c list.h
list.c list.h list.tex: list.nw

stacks-and-queues.o: stacks-and-queues.c stacks-and-queues.h list.h

# The glassbox test literally includes the whole ydfg.c file, so we don't need ydfg.o
ydfg-glassbox-test: ydfg-glassbox-test.o libydfg.a
ydfg-glassbox-test.o: ydfg-glassbox-test.c ydfg.h multiset.h hashtable.h list.h sexpr.h ydfg-test.h
ydfg-blackbox-test: ydfg-blackbox-test.o libydfg.a
ydfg-blackbox-test.o: ydfg-blackbox-test.c ydfg.h
ydfg.h: ydfg-spec.nw
	notangle -L -R$@ $< | cpif $@
ydfg-test.h: ydfg-spec.nw
	notangle -L -R$@ $< | cpif $@
sexpr.h: ydfg-spec.nw
	notangle -L -R$@ $< | cpif $@
ydfg.o: ydfg.c ydfg.h multiset.h hashtable.h list.h sexpr.h numbering.h symbols.h
ydfg-spec.tex: ydfg-spec.nw
ydfg-spec.dvi: ydfg-spec.tex ydfg-ex1.eps inchmar.sty
	gmake ydfg-spec.bbl
	dolatex ydfg-spec

ydfg-disasm.c: ydfg-disassembler.nw
	notangle -L -R$@ $< | cpif $@
ydfg-disasm: ydfg-disasm.c libydfg.a

# I don't actually want ydfg.bbl to be remade just because ydfg.tex or ydfg-ex1.eps
# But I do need it.
ydfg-spec.bbl:
	latex ydfg-spec
	bibtex ydfg-spec

multiset.o: multiset.h multiset.c list.h
ydfg-disassembler.o: ydfg-disassembler.h ydfg-disassembler.nw ydfg.h numbering.h ydfg.h hashtable.h symbols.h multiset.h list.h sexpr.h

%.ps: %.dvi
	dvips $< -o $@
%.dvi: %.tex
	dolatex $(patsubst %.tex,%,$<)
%.c: %.nw
	notangle -L -R$@ $< | cpif $@
%.h: %.nw
	notangle -L -R$@ $< | cpif $@
%.tex: %.nw
	noweave -delay $< > $@
%.eps: %.fig
	fig2dev -L ps -p portrait $< $@
%.pstex: %.fig
	fig2dev -L pstex $< $@
%.pstex_t: %.fig
	fig2dev -L pstex_t -p $(patsubst %.fig,%.pstex,$<) $< $@

random.o:
	mkdir tmp
	(cd tmp;ar x  /usr/ucblib/libucb.a)
	mv tmp/random.o .
	rm -rf tmp
%.actualroots: %.nw
	noroots $<|sort>$@
%.testactualroots: %.actualroots
	diff $< $(patsubst %.actualroots,%.expectedroots,$<)
testroots: symbols.testactualroots hashtable.testactualroots ydfg-spec.testactualroots

showroots: symbols.actualroots hashtable.actualroots
	noroots ydfg.nw   |sort>ydfg.actualroots
	cat ydfg.actualroots	
tests: testroots run-sexpr-tests hashtable-tests ydfg-tests symbols-tests 

symbols-tests: symbols-bb-test
	symbols-bb-test
ydfg-tests: ydfg-glassbox-test ydfg-blackbox-test run-reader-tests run-reader-tests-stdin
	ydfg-glassbox-test
	ydfg-blackbox-test
hashtable-tests: hashtable-test
	hashtable-test

libydfg.a: libydfg.a(ydfg.o list.o multiset.o hashtable.o symbols.o sexpr.o stacks-and-queues.o ydfg-disassembler.o numbering.o)

install: install-libs install-h-files install-bins

INSTALL=cp -u
# on granite do: "make install INSTALL=cp"
install-libs: libydfg.a
	$(INSTALL) $^ $(INSTALLDIR)/lib
install-h-files: hashtable.h list.h multiset.h numbering.h sexpr.h stacks-and-queues.h symbols.h ydfg-disassembler.h ydfg.h 
	$(INSTALL) $^ $(INSTALLDIR)/include
install-bins: ydfg-disasm
	$(INSTALL) $^ $(INSTALLDIR)/bin
clean:
	rm -f *~
	rm -f tmp-ex*.ydfg tmp-stdout*.ydfg
	rm -f ydfg-test-reader ydfg-test-reader-stdin ydfg-disasm sexpr-identity-test sexpr-identity-test.c sexpr-test-1.output
	rm -f numbering.h numbering.c
	rm -f hashtable.c hashtable.h hashtable.o hashtable.tex
	rm -f hashtable-test.o hashtable-test.c hashtable-test
	rm -f stacks-and-queues.c stacks-and-queues.h stacks-and-queues.o
	rm -f sexpr.o sexpr.h
	rm -f list.c list.h list.o list.tex
	rm -f multiset.c multiset.h multiset.o multiset.tex
	rm -f symbols.c symbols.h symbols.o symbols.tex
	rm -f symbols-bb-test.c symbols-bb-test.o symbols-bb-test
	rm -f ydfg.h 
	rm -f        ydfg-blackbox-test.o ydfg-glassbox-test.o ydfg.o
	rm -f        ydfg-blackbox-test   ydfg-glassbox-test
	rm -f ydfg-ex1.eps
	rm -f *.actualroots
	rm -f *.o *.dvi *.aux *.log *.blg
	rm -f libydfg.a


# $Log: Makefile,v $
# Revision 1.54  1999/01/15 22:06:41  bradley
# Found a slight problem in the makefile.  I'll release this as 0.2.2 anyway.
#
# Revision 1.53  1997/08/30 13:33:30  bradley
# Udate
#
# Revision 1.52  1997/08/07 11:48:34  bradley
# Make sure an error message occurs if the file is empty.
#
# Revision 1.51  1997/07/28 16:48:31  bradley
# Allow edgenames with offset 0 to be passed to cons_dfgnode().
# (Such edgenames are effectively ignored.)
#
# Revision 1.50  1997/06/18 16:20:28  bradley
# Merge sexpr's spec into ydfg-spec.nw and make sexpr.c be the code.
# (It seems better to have the spec under noweb, but the actual code is just straight C.)
#
# Revision 1.49  1997/06/18 11:39:31  bradley
# Create INSTALL and do some minor changes.
#
# Revision 1.48  1997/04/06 08:20:13  bradley
# Get the bad-case to test properly
#
# Revision 1.47  1997/04/06 08:08:01  bradley
# Fixup makefile
#
# Revision 1.46  1997/04/06 08:06:29  bradley
# Turn optimizer back on
#
# Revision 1.45  1997/04/06 08:04:50  bradley
# Fix a bug in the writer that was outputing n_edges instead of n_edgeclasses.
# Also, as of the last version the reader noticed the error.
#
# Revision 1.44  1997/03/28 22:05:05  bradley
# Clean up a lot:  There is still some bug with the testers if you compile things with -O.
#  - There was a bug in the alist constructor (it threw away everything but the first and last element of the alist)
#  - The input routines were fishy.
#
# Revision 1.43  1997/03/27 15:56:14  bradley
# Try to move the assertions out of the ydfg.h file.
#
# Revision 1.42  1997/03/27 14:48:24  bradley
# Get rid of all the fscanf()'s (especially the ones that did an fscanf of a string, potentially allowing
# buffer overflow.)
#
# Revision 1.41  1997/03/17 05:42:56  bradley
# Try to get the details right...
#
# Revision 1.40  1997/03/17 01:45:30  bradley
# Get some minor details right.
#
# Revision 1.39  1997/03/17 01:40:11  bradley
# A major reorganization of the ydfg.
#
# Revision 1.38  1997/03/12 18:00:05  bradley
# Fix up the installer code.
#
# Revision 1.37  1997/03/12 17:57:51  bradley
# Put Dongqi's symbol table stuff into the numbering module.
# Clean up the usage of the write-table too.
#
# Revision 1.36  1997/03/12 16:06:25  bradley
# Improve the modularity, by making the numbering (obj2num) separate
# from the ydfg module.  This also makes the ydfg-disassembler able to
# stand on its own separately from the ydfg module.
#
# Revision 1.35  1997/03/12 14:51:24  bradley
# Fix up formatting to get rid of overfull hbox errors and to get the
# page breaks into better shape.
#
# Reorganize the input/output section to be all input together and all
# output together.  Add flames about how badly the i/o section is done
# (both bradley and dongqi are at fault.)
#
# Fix the makefile so that the symbols tests are only run once.
#
# Revision 1.34  1997/03/03 06:54:10  bradley
# Demonstrate that the ydfg reader does not work when given input from stdin.
#
# Revision 1.33  1997/03/03 06:44:08  bradley
# Fix the makefile
#
# Revision 1.32  1997/03/03 06:41:55  bradley
# Get the regression tests to work again.
#
# Revision 1.31  1997/03/03 05:01:41  bradley
# Make the code cleaner with respect to more compiler warnings.
#
# Revision 1.30  1997/02/28 01:58:05  bradley
# Make a hack to make it work better on solaris.
#
# Revision 1.29  1996/09/17 17:45:52  bradley
# Make the dissassembler available as a separate program.
#
# Revision 1.28  1996/09/05 13:40:55  bradley
# Make it compile on granite as well as pepes by using a conditional.
# Solaris (#$%*!) does not make the random() function available.  Instead
#  it says to use, rand(), which on every other unix implementation is broken.
#
# Revision 1.27  1996/09/05 13:32:05  bradley
# move the sexpr reader from ydfg to sexpr.
# Cleanup the version of the sexpr reader that dong wrote.
# Make sexpr_read correctly read integers.
# Do a test for the sexpr-reader.
# Now ydfg-tests runs ok.
#
# Revision 1.26  1996/09/05 10:30:27  bradley
# Makefie hacking
#
# Revision 1.25  1996/08/29 21:05:54  bradley
# Fix a bug in the hashtables, and make the tests do more testing.
#
# Revision 1.24  1996/07/24 14:46:47  bradley
# Add ex6
#
# Revision 1.23  1996/07/24 14:21:52  bradley
# Get rid of gratuitous including of #include<> in sexpr.h (move it sexpr.c)
# Make an "install" option to install.
#
# Revision 1.22  1996/07/24 07:07:02  bradley
# Get rid of -Wredundant-decls in Makefile since Linux apparently has
# many redundant decls in <stdio.h>
#
# Get rid of random.o since Linux is not broken the way Solaris is.  In
# future, I'll just create random.o separately on Solaris.
#
# Changed test_reader into ydfg-test-reader and got rid of the mention
# of dong's home directory.
#
# Get rid of reader since it didn't do anything that ydfg-test-reader
# doesn't do.
#
# Added run-reader-tests to the tests, and actually perform the diff to
# see if things worked out.  Note that this requires gnu diff because we
# use the -I argument.  Note also that gnu diff man pages do not exist.
# See the info tree.
#
# Make the ydfg-reader-test print to stderr instead of stdout when
# things go wrong, and check to see if various errors happen.  Also
# return a somewhat meaningful result (0 for no error.)  Also, add a new
# error which is that the output file already exists.  (I don't want to
# accidently clobber a real file.)
#
# Clean up symbols.nw and multiset.nw to use latex2e instead of
# latex2.09 and to use nowebb.sty instead of noweb.sty
#
# Revision 1.21  1996/07/19 15:36:21  bradley
# Don't put "%" comment characters at the beginning of log entries.
#
# Revision 1.20  1996/07/19 15:29:15  dong
# Updated makefile to integrate the rules to generate reader.c and
# ydfg-test-reader.c. reader.c is just a mudule that contains all
# the functions pertaining to the reader; while ydfg-test-reader.c
# is a test program to verify the correctness of the reader.
#
# Revision 1.19  1996/06/25 17:46:16  bradley
# Add inchmar.sty to dependencies for things.
#
# Revision 1.18  1996/06/24 21:24:45  bradley
# Revise DFG print format to make it easier to print.
#
# Also got rid of some old dead code (dfg.c, dfg.h, and test-nullproc.c)
#
# Revision 1.17  1996/06/11 21:32:11  bradley
# Make multiple edges in an edgeclass work.
#
# Revision 1.16  1996/04/29 13:19:42  bradley
# Add stacks-and-queues.nw
# Add a "free" operator to cons cells.
# Add operations to map over inputs and outputs of ydfg.
# Add accessors for the opcode and arguments to ydfg.
# Add a nicer dissassembler.  It is still not the best it could be.
#
# Revision 1.15  1996/04/19 12:40:15  bradley
# Fix things up so that ydfg.bbl is not remade all the time.
#
# Revision 1.14  1996/04/19 12:24:35  bradley
# Move some useful hash functions into hashtable.
#
# Revision 1.13  1996/04/19 03:50:51  bradley
# make the arguments into sexprs (adding sexpr abstract type.)
#
# Revision 1.12  1996/04/16 17:53:27  bradley
# Make clean now works, and cvsignore is happier now.
#
