#
# sample Makefile for Objective Caml
# Copyright (C) 2001 Jean-Christophe FILLIATRE
#
# modified 10/26/2003 by Paul Pelzl, for the purpose of building Orpie
# 
# 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=
exec_prefix=${prefix}
BINDIR=$(DESTDIR)/${exec_prefix}/bin

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

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

CURSES_LIB = -lcurses
GSL_LIB    = -lgsl -lgslcblas

INCLUDES = -I ./curses -I ./gsl -I ./units
BFLAGS   = -pp camlp4o -g $(INCLUDES) -thread
OFLAGS   = -pp camlp4o $(INCLUDES) -thread
BLFLAGS  = -custom -cclib ' $(CURSES_LIB) $(GSL_LIB) -lm'
OLFLAGS  = -cclib ' $(CURSES_LIB) $(GSL_LIB) -lm'
DEPFLAGS = -pp camlp4o

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

NAME  = orpie
NAME2 = orpie-curses-keys

all: $(OCAMLBEST)



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

CMO = big_int_str.cmo gsl_assist.cmo install.cmo operations.cmo utility.cmo rcfile.cmo \
	   version.cmo rpc_stack.cmo add.cmo sub.cmo mult.cmo div.cmo inv.cmo pow.cmo \
		solvelin.cmo rpc_calc.cmo txtin_parser.cmo txtin_lexer.cmo interface.cmo interface_draw.cmo \
		interface_main.cmo main.cmo
CMX = $(CMO:.cmo=.cmx)
CMA = nums.cma bigarray.cma str.cma unix.cma threads.cma
CMXA = $(CMA:.cma=.cmxa)

CURSES_CMO = curses/curses.cmo
CURSES_CMX = curses/curses.cmx
CURSES_COBJS = curses/ml_curses.o
CURSES_BOBJS = $(CURSES_CMO) $(CURSES_COBJS)
CURSES_OOBJS = $(CURSES_CMX) $(CURSES_COBJS)

GSL_CMO = gsl/gsl_error.cmo gsl/gsl_blas.cmo gsl/gsl_complex.cmo gsl/gsl_matrix.cmo gsl/gsl_matrix_complex.cmo \
		gsl/gsl_vector.cmo gsl/gsl_vector_complex.cmo gsl/gsl_vector_flat.cmo gsl/gsl_matrix_flat.cmo \
		gsl/gsl_vector_complex_flat.cmo gsl/gsl_matrix_complex_flat.cmo gsl/gsl_vectmat.cmo \
		gsl/gsl_permut.cmo gsl/gsl_linalg.cmo gsl/gsl_fun.cmo
GSL_CMX = $(GSL_CMO:.cmo=.cmx)
GSL_COBJS = gsl/mlgsl_error.o gsl/mlgsl_blas.o gsl/mlgsl_blas_complex.o gsl/mlgsl_complex.o \
		 gsl/mlgsl_matrix_complex.o gsl/mlgsl_matrix_double.o gsl/mlgsl_vector_double.o gsl/mlgsl_permut.o \
		 gsl/mlgsl_linalg.o gsl/mlgsl_linalg_complex.o gsl/mlgsl_fun.o gsl/mlgsl_math.o gsl/mlgsl_sf.o
GSL_BOBJS = $(GSL_COBJS) $(GSL_CMO)
GSL_OOBJS = $(GSL_COBJS) $(GSL_CMX)

UNITS_CMO = units/units.cmo units/constants.cmo
UNITS_CMX = $(UNITS_CMO:.cmo=.cmx)

TEST_CMO = big_int_str.cmo gsl_assist.cmo install.cmo operations.cmo utility.cmo rcfile.cmo \
	   version.cmo rpc_stack.cmo add.cmo sub.cmo mult.cmo div.cmo inv.cmo pow.cmo \
		solvelin.cmo rpc_calc.cmo txtin_parser.cmo txtin_lexer.cmo calc_test.cmo
TEST_CMX = $(TEST_CMO:.cmo=.cmx)

GENERATED = version.ml

# special case: generated parser depends on the .cmi file
txtin_parser.cmo: txtin_parser.ml txtin_parser.cmi
	$(OCAMLC) -c $(BFLAGS) $<

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

byte: $(NAME).byte $(NAME2).byte
opt: $(NAME).opt $(NAME2).opt
static: $(NAME).static-opt $(NAME2).static-opt

$(NAME).byte: $(CURSES_BOBJS) $(GSL_BOBJS) $(UNITS_CMO) $(CMO)
	$(OCAMLC) $(BFLAGS) $(BLFLAGS) -o $@ $(CURSES_BOBJS) $(CMA) $(GSL_BOBJS) $(UNITS_CMO) $(CMO) 

$(NAME2).byte: $(CURSES_BOBJS) utility.cmo curses-keys.cmo
	$(OCAMLC) $(BFLAGS) $(BLFLAGS) -o $@ $(CURSES_BOBJS) $(CMA) utility.cmo curses-keys.cmo

$(NAME).opt: $(CURSES_OOBJS) $(GSL_OOBJS) $(UNITS_CMX) $(CMX)
	$(OCAMLOPT) $(OFLAGS) -o $@ $(CURSES_OOBJS) $(CMXA) $(GSL_OOBJS) $(UNITS_CMX) $(CMX) $(OLFLAGS)

$(NAME2).opt: $(CURSES_OOBJS) utility.cmx curses-keys.cmx
	$(OCAMLOPT) $(OFLAGS) -o $@ $(CURSES_OOBJS) $(CMXA) utility.cmx curses-keys.cmx $(OLFLAGS)

# static build seems to require mashing all the C objects into a library
$(NAME).static-opt: $(UNITS_CMX) $(CMX) $(CURSES_OOBJS) $(GSL_OOBJS) my-static-libs
	$(OCAMLOPT) $(OFLAGS) -I . -ccopt -static -cclib '-lmycurses -lmygsl $(CURSES_LIB) $(GSL_LIB) -lm' -o $@ $(CMXA) \
	curses.cmx $(GSL_CMX) $(UNITS_CMX) $(CMX)
	strip $(NAME).static-opt

$(NAME2).static-opt: $(CURSES_OOBJS) utility.cmx curses-keys.cmx my-static-libs
	$(OCAMLOPT) $(OFLAGS) -I . -ccopt -static -cclib '-lmycurses $(CURSES_LIB) -lm' -o $@ $(CMXA) curses.cmx \
	utility.cmx curses-keys.cmx
	strip $(NAME2).static-opt

my-static-libs:
	ar cr libmycurses.a curses/*.o
	ar cr libmygsl.a gsl/*.o

# calculator testing program
test.opt: $(CURSES_OOBJS) $(GSL_OOBJS) $(UNITS_CMX) $(TEST_CMX)
	$(OCAMLOPT) $(OFLAGS) -o $@ $(CURSES_OOBJS) $(CMXA) $(GSL_OOBJS) $(UNITS_CMX) $(TEST_CMX) $(OLFLAGS)


VERSION=1.4.3

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


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

install-indep: 
	mkdir -p $(BINDIR)
	if test "$(prefix)" = "/usr"; then \
		mkdir -p $(DESTDIR)/etc; \
  	install -m 644 orpierc $(DESTDIR)/etc; \
	else \
		mkdir -p $(DESTDIR)/$(prefix)/etc; \
		install -m 644 orpierc $(DESTDIR)/$(prefix)/etc; \
	fi
	mkdir -p $(MANDIR)/man1
	mkdir -p $(MANDIR)/man5
	install -m 644 doc/orpie.1 $(MANDIR)/man1/orpie.1
	install -m 644 doc/orpie-curses-keys.1 $(MANDIR)/man1/orpie-curses-keys.1
	install -m 644 doc/orpierc.5 $(MANDIR)/man5/orpierc.5

install: install-indep
	install -m 755 $(NAME).$(OCAMLBEST) $(BINDIR)/$(NAME)$(EXE)
	install -m 755 $(NAME2).$(OCAMLBEST) $(BINDIR)/$(NAME2)$(EXE)

install-byte: install-indep
	install -m 755 $(NAME).byte $(BINDIR)/$(NAME)$(EXE)
	install -m 755 $(NAME2).byte $(BINDIR)/$(NAME2)$(EXE)

install-opt: install-indep
	install -m 755 $(NAME).opt $(BINDIR)/$(NAME)$(EXE)
	install -m 755 $(NAME2).opt $(BINDIR)/$(NAME2)$(EXE)

install-static: install-indep
	install -m 755 -D $(NAME).static-opt $(BINDIR)/$(NAME)$(EXE)
	install -m 755 -D $(NAME2).static-opt $(BINDIR)/$(NAME2)$(EXE)

uninstall:
	rm -f $(BINDIR)/$(NAME)$(EXE)
	rm -f $(BINDIR)/$(NAME2)$(EXE)
	if test "$(prefix)" = "/usr"; then\
	rm -f /etc/orpierc; \
	else rm -f $(DESTDIR)/$(prefix)/etc/orpierc; \
	fi
	rm -f $(MANDIR)/man1/orpie.1
	rm -f $(MANDIR)/man1/orpie-curses-keys.1
	rm -f $(MANDIR)/man5/orpierc.5


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

.SUFFIXES:


# GSL build rules
gsl-opt: $(GSL_OOBJS)
gsl-byte: $(GSL_BOBJS)

GSL_INCLUDES = -I ./gsl
GSL_BFLAGS = -g $(GSL_INCLUDES) -thread
GSL_OFLAGS = $(GSL_INCLUDES) -thread
GSL_CFLAGS = -Wall -fPIC -DPIC 

gsl/%.cmi : gsl/%.mli
	$(OCAMLC) -c $(GSL_BFLAGS) $<

gsl/%.cmo : gsl/%.ml
	$(OCAMLC) -c $(GSL_BFLAGS) $<

gsl/%.o : gsl/%.ml
	$(OCAMLOPT) -c $(GSL_OFLAGS) $<

gsl/%.cmx : gsl/%.ml
	$(OCAMLOPT) -c $(GSL_OFLAGS) $<

gsl/%.o : gsl/%.c
	cd gsl && $(OCAMLC) -ccopt "$(GSL_CFLAGS)" -c $*.c


# curses build rules
curses-opt: $(CURSES_OOBJS)
curses-byte: $(CURSES_BOBJS)

curses/curses.ml:	curses/curses.ml.in curses/functions.c
	$(MAKE) -C curses

curses/functions.c:	curses/functions.c.in
	$(MAKE) -C curses

CURSES_INCLUDES = -I ./curses
CURSES_BFLAGS = -g $(CURSES_INCLUDES) -thread
CURSES_OFLAGS = $(CURSES_INCLUDES) -thread
CURSES_CFLAGS = -Wall -fPIC -DPIC 

curses/%.cmi : curses/%.mli
	$(OCAMLC) -c $(CURSES_BFLAGS) $<

curses/%.cmo : curses/%.ml
	$(OCAMLC) -c $(CURSES_BFLAGS) $<

curses/%.o : curses/%.ml
	$(OCAMLOPT) -c $(CURSES_OFLAGS) $<

curses/%.cmx : curses/%.ml
	$(OCAMLOPT) -c $(CURSES_OFLAGS) $<

curses/%.o : curses/%.c
	cd curses && $(OCAMLC) -ccopt "$(CURSES_CFLAGS)" -c $*.c


# Units build rules
gsl-opt: $(UNITS_CMX)
gsl-byte: $(UNITS_CMO)

UNITS_INCLUDES = -I ./units
UNITS_BFLAGS = -g $(UNITS_INCLUDES) -thread
UNITS_OFLAGS = $(UNITS_INCLUDES) -thread

units/%.cmi : units/%.mli
	$(OCAMLC) -c $(UNITS_BFLAGS) $<

units/%.cmo : units/%.ml
	$(OCAMLC) -c $(UNITS_BFLAGS) $<

units/%.o : units/%.ml
	$(OCAMLOPT) -c $(UNITS_OFLAGS) $<

units/%.cmx : units/%.ml
	$(OCAMLOPT) -c $(UNITS_OFLAGS) $<


# generic build rules for toplevel directory
%.cmi : %.mli
	$(OCAMLC) -c $(BFLAGS) $<

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

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

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

%.ml : %.mll
	$(OCAMLLEX) $<

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

%.mli : %.mly
	$(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/"

# vi tags
#########

vtags:
	otags -vi -o tags *.ml


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

partly-clean::
	rm -f *.cm[iox] *.o *.a *~ txtin*.ml txtin*.mli *.output
	rm -f $(GENERATED) parser.output
	rm -f $(NAME).byte $(NAME).opt $(NAME2).byte $(NAME2).opt $(NAME).static-opt $(NAME2).static-opt
	rm -f *.aux *.log $(NAME).tex $(NAME).dvi $(NAME).ps

gsl-clean::
	rm -f gsl/*.cm[iox] gsl/*.o *~

curses-clean::
	rm -f curses/*.cm[iox] curses/*.o *~

units-clean::
	rm -f units/*.cm[iox] units/*.o *~

clean:: partly-clean gsl-clean curses-clean units-clean

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


# depend
########

depend::
	$(MAKE) txtin_lexer.ml
	$(MAKE) txtin_parser.ml
	$(OCAMLDEP) -pp camlp4o $(INCLUDES) *.ml *.mli > depend
	$(OCAMLDEP) -pp camlp4o $(INCLUDES) units/*.ml units/*.mli >> depend
	$(OCAMLDEP) -pp cpp -I ./curses curses/*.ml curses/*.mli >> depend
	$(OCAMLDEP) -I ./gsl gsl/*.ml gsl/*.mli >> depend


include depend


# arch-tag: DO_NOT_CHANGE_bdb62873-ffd0-4d79-819e-880467e18f28 


syntax highlighted by Code2HTML, v. 0.9.1