#
# Adapted from sample Makefile for Objective Caml
# Copyright (C) 2001 Jean-Christophe FILLIATRE
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License version 2, as published by the Free Software Foundation.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# 
# See the GNU Library General Public License version 2 for more details
# (enclosed in the file LGPL).

MYDIR := $(shell pwd)
VERSION =`cat $(MYDIR)/version.txt`
PROJECT = ddk

# where to install the binaries
prefix=@prefix@
exec_prefix=@exec_prefix@
BINDIR=@bindir@

# where to install the man page
MANDIR=@mandir@

# other variables set by ./configure
OCAMLC   = @OCAMLC@
OCAMLOPT = @OCAMLOPT@
OCAMLDEP = @OCAMLDEP@
OCAMLLEX = @OCAMLLEX@
OCAMLYACC= @OCAMLYACC@
OCAMLLIB = @OCAMLLIB@
OCAMLBEST= @OCAMLBEST@
OCAMLVERSION = @OCAMLVERSION@
OCAMLWEB = @OCAMLWEB@

EXPORT = export
TEMPLATES = templates
SRCDIR = src
DDIR = $(SRCDIR)/D
DSRDIR = $(SRCDIR)/DSR
BOOLDIR = $(SRCDIR)/BOOL

DIRS = $(SRCDIR) $(DDIR) $(DSRDIR) $(BOOLDIR)

INCLUDES = -I $(SRCDIR) -I $(DDIR) -I $(DSRDIR) -I $(BOOLDIR)
BFLAGS = -g $(INCLUDES)
OFLAGS = $(INCLUDES)

CMO =	$(SRCDIR)/version.cmo \
	$(SRCDIR)/application.cmo \
	$(SRCDIR)/ddk.cmo


DCMO =	$(CMO) \
	$(DDIR)/dast.cmo \
	$(DDIR)/dparser.cmo \
	$(DDIR)/dpp.cmo \
	$(DDIR)/dlexer.cmo \
	$(DDIR)/dinterp.cmo \
	$(DDIR)/d.cmo

DSRCMO =$(CMO) \
	$(DSRDIR)/dsrast.cmo \
	$(DSRDIR)/dsrparser.cmo \
	$(DSRDIR)/dsrpp.cmo \
	$(DSRDIR)/dsrlexer.cmo \
	$(DSRDIR)/dsrinterp.cmo \
	$(DSRDIR)/dsr.cmo

BOOLCMO =$(CMO) \
	$(BOOLDIR)/boolast.cmo \
	$(BOOLDIR)/boolparser.cmo \
	$(BOOLDIR)/boolpp.cmo \
	$(BOOLDIR)/boollexer.cmo \
	$(BOOLDIR)/boolinterp.cmo \
	$(BOOLDIR)/bool.cmo

DCMX   =$(DCMO:.cmo=.cmx)
DSRCMX =$(DSRCMO:.cmo=.cmx)
BOOLCMX=$(BOOLCMO:.cmo=.cmx)

all: $(OCAMLBEST)
	cp -f $(DDIR)/d.$(OCAMLBEST) ./D
	cp -f $(DSRDIR)/dsr.$(OCAMLBEST) ./DSR
	cp -f $(BOOLDIR)/bool.$(OCAMLBEST) ./BOOL

GENERATED = $(SRCDIR)/version.ml \
	$(DDIR)/dparser.ml \
	$(DDIR)/dparser.mli \
	$(DDIR)/dlexer.ml \
	$(DSRDIR)/dsrparser.ml \
	$(DSRDIR)/dsrparser.mli \
	$(DSRDIR)/dsrlexer.ml \
	$(BOOLDIR)/boolparser.ml \
	$(BOOLDIR)/boolparser.mli \
	$(BOOLDIR)/boollexer.ml 


byte: $(DDIR)/d.byte $(DSRDIR)/dsr.byte $(BOOLDIR)/bool.byte
opt: $(DDIR)/d.opt $(DSRDIR)/dsr.opt $(BOOLDIR)/bool.opt

$(DDIR)/d.byte: $(DCMO)
	$(OCAMLC) $(BFLAGS) -o $@ $^

$(DDIR)/d.opt: $(DCMX) 
	$(OCAMLOPT) $(OFLAGS) -o $@ $^

$(DSRDIR)/dsr.byte: $(DSRCMO)
	$(OCAMLC) $(BFLAGS) -o $@ $^

$(DSRDIR)/dsr.opt: $(DSRCMX)
	$(OCAMLOPT) $(OFLAGS) -o $@ $^

$(BOOLDIR)/bool.byte: $(BOOLCMO)
	$(OCAMLC) $(BFLAGS) -o $@ $^

$(BOOLDIR)/bool.opt: $(BOOLCMX)
	$(OCAMLOPT) $(OFLAGS) -o $@ $^

$(SRCDIR)/version.ml: Makefile
	echo "let version = \""$(VERSION)"\"" > $(SRCDIR)/version.ml
	echo "let date = \""`date`"\"" >> $(SRCDIR)/version.ml

# installation
##############

install:
	mkdir -p $(BINDIR)
	cp -f $(DDIR)/d.$(OCAMLBEST) $(BINDIR)/D
	cp -f $(DSRDIR)/dsr.$(OCAMLBEST) $(BINDIR)/DSR

# export
########

EXPORTDIR=$(PROJECT)-$(VERSION)
TAR=$(EXPORTDIR).tar

FILES = $(TEMPLATES)/*.ml version.txt \
	$(SRCDIR)/*.ml* $(DDIR)/*.ml* $(DSRDIR)/*.ml* $(BOOLDIR)/*.ml* \
	Makefile.in configure configure.in         \
	.depend README INSTALL COPYING CHANGES

#export: source binary - modified 3/05 to distribute bytecode, not binary
BINARY = $(EXPORTDIR)-byte# -$(OSTYPE)
export: source byte 
	mkdir -p $(EXPORT)/$(BINARY)
#	cp $(BINARYFILES) $(EXPORT)/$(BINARY)
	cp $(DDIR)/d.byte $(EXPORT)/$(BINARY)/d.byte
	cp $(DSRDIR)/dsr.byte $(EXPORT)/$(BINARY)/dsr.byte
	(cd $(EXPORT); tar czf $(BINARY).tar.gz $(BINARY))

source:
	mkdir -p $(EXPORT)/$(EXPORTDIR)
	tar c $(FILES) | tar x -C $(EXPORT)/$(EXPORTDIR)
	(cd $(EXPORT)/$(EXPORTDIR); rm -f $(GENERATED))
	(cd $(EXPORT) ; tar cf $(TAR) $(EXPORTDIR) ; gzip -f --best $(TAR))

student-source: source
	cp -f $(TEMPLATES)/dinterp.ml $(EXPORT)/$(EXPORTDIR)/$(DDIR)
	cp -f $(TEMPLATES)/dsrinterp.ml $(EXPORT)/$(EXPORTDIR)/$(DSRDIR)
	(cd $(EXPORT) ; tar cf $(TAR) $(EXPORTDIR) ; gzip -f --best $(TAR))

# BINARYTAR=$(BINARY).tar
# 
# BINARYFILES = 
# 
binary: $(DDIR)/d.$(OCAMLBEST) $(DSRDIR)/dsr.$(OCAMLBEST)
	mkdir -p $(EXPORT)/$(BINARY)
 #	cp $(BINARYFILES) $(EXPORT)/$(BINARY)
	cp $(DDIR)/d.$(OCAMLBEST) $(EXPORT)/$(BINARY)/D
	cp $(DSRDIR)/dsr.$(OCAMLBEST) $(EXPORT)/$(BINARY)/DSR
	(cd $(EXPORT); tar czf $(BINARY).tar.gz $(BINARY))

# generic rules
###############

.SUFFIXES: .mli .ml .cmi .cmo .cmx .mll .mly .tex .dvi .ps .html

.mli.cmi:
	$(OCAMLC) -c $(BFLAGS) $<

.ml.cmo:
	$(OCAMLC) -c $(BFLAGS) $<

.ml.o:
	$(OCAMLOPT) -c $(OFLAGS) $<

.ml.cmx:
	$(OCAMLOPT) -c $(OFLAGS) $<

.mll.ml:
	$(OCAMLLEX) $<

.mly.ml:
	$(OCAMLYACC) -v $<

.mly.mli:
	$(OCAMLYACC) -v $<

# Emacs tags
############

tags:
	find . -name "*.ml*" | sort -r | xargs \
	etags "--regex=/let[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/let[ \t]+rec[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/and[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/type[ \t]+\([^ \t]+\)/\1/" \
              "--regex=/exception[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/val[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/module[ \t]+\([^ \t]+\)/\1/"

# Makefile is rebuilt whenever Makefile.in or configure.in is modified
######################################################################

Makefile: Makefile.in config.status
	./config.status

config.status: configure
	./config.status --recheck

#configure: configure.in
#	autoconf 

# clean
#######

clean::
	for i in $(DIRS); do \
		rm -f $$i/*.cm[iox] $$i/*.o $$i/*~ $$i/*.output; \
	done
	rm -f $(GENERATED) 
	rm -f $(DDIR)/d.byte $(DDIR)/d.opt
	rm -f $(DSRDIR)/dsr.byte $(DSRDIR)/dsr.opt
	rm -f $(BOOLDIR)/bool.byte $(BOOLDIR)/bool.opt
	rm -rf $(EXPORT)
	rm -f BOOL D DSR

dist-clean distclean:: clean
	rm -f Makefile config.cache config.log config.status

# depend
########

.depend depend:: $(GENERATED) 
	rm -f .depend
	(for i in $(DIRS); do \
		$(OCAMLDEP) $(INCLUDES) $$i/*.ml $$i/*.mli; \
	 done) > .depend

include .depend








