#
# 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).

# where to install the binaries
prefix=/usr/local
exec_prefix=${prefix}
BINDIR=${exec_prefix}/bin

# where to install the man page
MANDIR=${prefix}/share/man

# other variables set by ./configure
OCAMLC   = ocamlc.opt
OCAMLOPT = ocamlopt.opt
OCAMLDEP = ocamldep
OCAMLLEX = ocamllex.opt
OCAMLYACC= ocamlyacc
OCAMLLIB = /usr/lib/ocaml/3.09.2
OCAMLBEST= opt
OCAMLVERSION = 3.09.2
OCAMLWEB = ocamlweb
OCAMLWIN32 = no
EXE = 

INCLUDES = 
BFLAGS = -g $(INCLUDES)
OFLAGS = $(INCLUDES) -unsafe

# main target
#############

NAME = puf

all: $(OCAMLBEST)

# bytecode and native-code compilation
######################################

CMO = tarjan.cmo puf1.cmo puf2.cmo puf3.cmo puf4.cmo puf5.cmo \
      pa1.cmo puf6.cmo ptarjan.cmo pa2.cmo pa3.cmo pa4.cmo pt1.cmo \
      pptarjan.cmo pa.cmo bench.cmo
CMX = $(CMO:.cmo=.cmx)

GENERATED = version.ml

byte: bench.byte
opt: bench.opt

bench.byte: $(CMO)
	$(OCAMLC) $(BFLAGS) -o $@ unix.cma $^

bench.opt: $(CMX)
	$(OCAMLOPT) $(OFLAGS) -o $@ unix.cmxa $^

VERSION=0.1

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

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

install:
	mkdir -p $(BINDIR)
	cp -f $(NAME).$(OCAMLBEST) $(BINDIR)/$(NAME)$(EXE)
	mkdir -p $(MANDIR)/man1
	cp -f $(NAME).1 $(MANDIR)/man1

# documentation
###############

DOCFILES=manual.ps manual.html

doc: $(DOCFILES)

# export
########

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

FTP = $$HOME/ftp/$(NAME)
WWW = $$HOME/WWW/$(NAME)

FILES = *.ml* $(NAME).1 manual.tex	     \
	Makefile.in configure configure.in   \
	.depend README INSTALL COPYING GPL CHANGES

export: source binary export-doc
	cp README COPYING GPL CHANGES $(FTP)

source: 
	mkdir -p export/$(EXPORTDIR)
	cp $(FILES) export/$(EXPORTDIR)
	cd export ; tar cf $(TAR) $(EXPORTDIR) ; gzip -f --best $(TAR)
	cp export/$(TAR).gz $(FTP)

export-doc: $(DOCFILES)
	gzip -c manual.ps > $(FTP)/manual.ps.gz
	cp manual.html $(WWW)

BINARY = $(EXPORTDIR)-$(OSTYPE)
BINARYTAR=$(BINARY).tar

BINARYFILES = README INSTALL COPYING GPL $(NAME).1

binary: $(NAME).$(OCAMLBEST) $(DOCFILES)
	mkdir -p export/$(BINARY)
	cp $(BINARYFILES) $(DOCFILES) export/$(BINARY)
	cp $(NAME).$(OCAMLBEST) export/$(BINARY)/$(NAME)
	(cd export; tar czf $(BINARY).tar.gz $(BINARY))
	cp export/$(BINARY).tar.gz $(FTP)

# literate programming
######################

SOURCES = 

$(NAME).tex: $(SOURCES)
	$(OCAMLWEB) -o $@ $^

# 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 $<

.tex.dvi:
	latex $< && latex $<

.dvi.ps:
	dvips $< -o $@ 

.tex.html:
	hevea $<

# 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::
	rm -f *.cm[iox] *.o *~
	rm -f $(GENERATED) parser.output
	rm -f $(NAME).byte $(NAME).opt
	rm -f *.aux *.log $(NAME).tex $(NAME).dvi $(NAME).ps

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

# depend
########

.depend depend:: $(GENERATED)
	rm -f .depend
	$(OCAMLDEP) $(INCLUDES) *.ml *.mli > .depend

include .depend
