############################################################################
#                                                                          #
#                MLGlide 0.1: Interfacing Glide with O'Caml                #
#                                                                          #
#              Alexandre Miquel   <Alexandre.Miquel@inria.fr>              #
#                                                                          #
# Copyright (c) 1998-1999                                                  #
# This code can be re-used or modified without any restriction.            #
# This version is still under development.  There may be bugs and          #
# inefficient stuff, and I will not be responsible of what can happen      #
# to you and your environment if you use, read or do whatever you want     #
# with all this...                                                         #
#                                                                          #
#                   ... Enjoy!                                             #
#                                                                          #
############################################################################

#### Global variables ####

# Building tools
OCAMLC   = ocamlc
OCAMLOPT = ocamlopt
CC       = gcc -O2 -Wall
AR       = ar
RANLIB   = ranlib
CP       = cp
RM       = rm

# Edit this!
MLINCDIR = /usr/local/lib/ocaml
GLINCDIR = /usr/include/glide
LIBDIR   = /home/alex/lib

###########################

message:
	@echo "Choose one of:"
	@echo "  make lib       # build the \`mlglide' library"
	@echo "  make test      # build the test bytecode executable"
	@echo "  make test.opt  # build the test native executable"
	@echo "  make all       # make all these things"
	@echo "  make install   # install the library"
	@echo "  make clean     # go back before birth"
	@echo
	@echo "Current settings in the \`Makefile' are"
	@echo "  * O'Caml directory:"
	@echo "      MLINCDIR = "$(MLINCDIR)
	@echo "  * Glide directory:"
	@echo "      GLINCDIR = "$(GLINCDIR)
	@echo "  * Destination directory (for installing the library)"
	@echo "      LIBDIR   = "$(LIBDIR)
	@echo "Perhaps you want to edit some of them ?"

all: lib test test.opt

#####################
#   Generic rules   #
#####################

.SUFFIXES: .mlp .ml .mli .cmo .cmi .cma .cmx .cmxa .o .a

.c.o:
	$(CC) -I$(MLINCDIR) -I$(GLINCDIR) -c $<

.mlp.mli:
	./buildml -v SECTION=MLI < $< > $@

.mlp.ml:
	./buildml -v SECTION=ML < $< > $@

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

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

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

#########################
#   Build the library   #
#########################

lib: mlglide.cma mlglide.cmxa libmlglide.a

# Build the archive files

mlglide.cma: gr.cmo gu.cmo
	$(OCAMLC) -a -o mlglide.cma gr.cmo gu.cmo

mlglide.cmxa: gr.cmx gu.cmx
	$(OCAMLOPT) -a -o mlglide.cmxa gr.cmx gu.cmx

mlglide.a: mlglide.cmxa

libmlglide.a: stub_tbl.o stub_gr.o stub_gu.o
	$(RM) -f libmlglide.a
	$(AR) rc libmlglide.a stub_tbl.o stub_gr.o stub_gu.o
	$(RANLIB) libmlglide.a

# Dependencies
gr.mli gr.ml: gr.mlp
gu.mli gu.ml: gu.mlp
gr.cmi: gr.mli
gr.cmo: gr.cmi gr.ml
gr.cmx: gr.cmi gr.ml
gu.cmi: gr.cmi gu.mli
gu.cmo: gr.cmi gu.cmi gu.ml
gu.cmx: gu.cmi gu.ml
stub_tbl.o: stub_inc.h stub_tbl.c
stub_gr.o: stub_inc.h stub_gr.c
stub_gu.o: stub_inc.h stub_gu.c

###################################################
#   Build the `test' and `test.opt' executables   #
###################################################

MLGLIDE=mlglide.cma -ccopt -L. -cclib -lmlglide -cclib -lglide2x
MLGLIDEOPT=mlglide.cmxa -ccopt -L. -cclib -lmlglide -cclib -lglide2x

test: mlglide.cma libmlglide.a test.cmo
	$(OCAMLC) -custom $(MLGLIDE) test.cmo -o test

test.opt: mlglide.cmxa libmlglide.a test.cmx
	$(OCAMLOPT) $(MLGLIDEOPT) test.cmx -o test.opt

# Dependencies
test.cmo: lib test.ml
test.cmx: lib test.ml

###########################
#   Install the library   #
###########################

LIB_FILES=gr.cmi gu.cmi mlglide.cma mlglide.cmxa mlglide.a libmlglide.a

install: $(LIB_FILES)
	$(CP) $(LIB_FILES) $(LIBDIR)

############################
#   Clean this directory   #
############################

CLEAN_LIST=\
  *.cm[oixa] *.cmxa *.o *.a \
  gr.mli gr.ml gu.mli gu.ml \
  *~ test test.opt

clean:
	$(RM) -f $(CLEAN_LIST)
