#
# 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=@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@
OCAMLWIN32 = @OCAMLWIN32@
EXE = @EXE@

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

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

NAME = project

all: $(OCAMLBEST)

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

CMO =
CMX = $(CMO:.cmo=.cmx)

GENERATED = version.ml

byte: $(NAME).byte
opt: $(NAME).opt

$(NAME).byte: $(CMO)
	$(OCAMLC) $(BFLAGS) -o $@ $^

$(NAME).opt: $(CMX)
	$(OCAMLOPT) $(OFLAGS) -o $@ $^

VERSION=0.1

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

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

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

install: install-indep
	cp -f $(NAME).$(OCAMLBEST) $(BINDIR)/$(NAME)$(EXE)

install-byte: install-indep
	cp -f $(NAME).byte $(BINDIR)/$(NAME)$(EXE)

install-opt: install-indep
	cp -f $(NAME).opt $(BINDIR)/$(NAME)$(EXE)

# 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
