TOP=.
CONFIG=$(TOP)/config/default
include $(CONFIG)

sources = dwf.nw interface.nw

targets.c = dwf.c

targets.h = sse.h sse-dwf-cg.h

subdirs = tests

.PHONY: all clean realclean tar pdf programs execs
.PHONY: compile

f.tex=$(sources:%.nw=%.tex)
f.dvi=$(sources:%.nw=%.dvi)
f.pdf=$(sources:%.nw=%.pdf)

all: pdf programs

clean:
	$(RM) *.tex *.log *.dvi *.aux *.toc *.nwi *.out $(targets.c:%.c=%.o) *~
	for f in $(subdirs); do \
		make -C$$f $@; \
	done

realclean: clean
	$(RM) *.pdf $(targets.c) $(targets.h)
	for f in $(subdirs); do \
		make -C$$f $@; \
	done

tar:
	base=`basename $$PWD` ; \
	cd .. ; \
	tar -cvf - $$base | bzip2 -9 > $$base.tar.bz2

pdf: $(f.pdf)

programs: $(targets.c) $(targets.h)

$(f.pdf): %.pdf: %.dvi
	dvipdfm $<

$(f.dvi): %.dvi: %.tex
	latex $<
	latex $<
	latex $<

$(f.tex): %.tex: %.nw
	noweave -index -latex -delay $< > $@

$(targets.c): $(sources)
	notangle $(TFLAGS) -R$@ -c $< > $@ \
	|| (rm -f $@ ; exit 1)

sse.h: dwf.nw
	notangle $(TFLAGS) -R$@ -c $< > $@ \
	|| (rm -f $@ ; exit 1)

sse-dwf-cg.h: interface.nw
	notangle $(TFLAGS) -R$@ -c $< > $@ \
	|| (rm -f $@ ; exit 1)

compile: $(targets.c) $(targets.h)
	$(CC) $(CFLAGS) -c $(targets.c)

$(targets.c): $(targets.h)
