CC = gcc
CAMLC = ocamlc
CAMLO = ocamlopt

#CDEBUGFLAGS = -g -DDEBUG
CDEBUGFLAGS = -g -Wall -DGTKTHR -DEBUG
ZFLAGS = -g -thread
ZFLAGSOPT = 

# Be sure of what you are doing if you want to change this:
OCAMLDIR = $(shell $(CAMLC) -v | grep Standard | sed -e "s/^.*: //") 

# On the other hand you may want to adapt this:

DESTDIR =
INSTALL = install -m644
INSTDIR = $(DESTDIR)$(OCAMLDIR)


# path for Ocaml C header files
OC_CFLAGS = -I $(OCAMLDIR)
# OC_CFLAGS = -I/usr/lib/ocaml -I/usr/local/lib/ocaml

##############################################################
###  You should not need to customize anything below this  ###
##############################################################

.PHONY : all all_opt clean distclean install install_opt depend examples examples_opt

include detect_gtk

ifneq (,$(OK))

CFLAGS = $(CDEBUGFLAGS) $(OC_CFLAGS) $(GLIB_CFLAGS) \
         $(shell gtk-config --cflags) $(GTKDEFINES)
LDFLAGS = "$(GLIB_LIBS) $(shell gtk-config --libs) -lunix"

CMOFILES = glib.cmo gdk.cmo gtk.cmo gtkThr.cmo gtkObj.cmo gtkEasy.cmo gtkDrawing.cmo
CMXFILES = glib.cmx gdk.cmx gtk.cmx gtkThr.cmx gtkObj.cmx gtkEasy.cmx gtkDrawing.cmx
OFILES = mlgdk_stub.o mlglib_stub.o mlgtk_stub.o

all: print_message libs examples 
all_opt: libs_opt examples_opt

libs: gtk.cma libmlgtk.a 
libs_opt: gtk.cmxa libmlgtk.a 

examples: 
	make -C examples all

examples_opt: 
	make -C examples all_opt

print_message:
	@echo $(OK)

gtk.cma: $(CMOFILES)
	$(CAMLC) -a -o $@ $(CMOFILES)

gtk.cmxa: $(CMXFILES)
	$(CAMLO) -a -o $@ $(CMXFILES)

libmlgtk.a: $(OFILES)
	$(AR) -rc $@ $(OFILES)

gtkThr.cmx: gtkThr.ml
	$(CAMLO) -thread $(ZFLAGSOPT) -c $<
clean:
	-rm -f *.cm* *.o *.a 
	make -C examples clean

distclean: 
	-rm -f .depend *.cm* *.o *.a 
	make -C examples distclean

install: libs
	$(INSTALL) gtk.cma $(INSTDIR)
	$(INSTALL) libmlgtk.a $(INSTDIR)
	$(INSTALL) *.cmi $(INSTDIR)

install_opt: libs_opt
	$(INSTALL) gtk.cmxa gtk.a $(INSTDIR)

depend: 
	ocamldep *.ml* >.depend

.depend:
	ocamldep *.ml* >.depend

.ml.cmo:
	$(CAMLC) $(ZFLAGS) -c $<
.ml.cmx:
	$(CAMLO) $(ZFLAGSOPT) -c $<
.mli.cmi:
	$(CAMLC) $(ZFLAGS) -c $<

.SUFFIXES: .ml .cmo .cmx .mli .cmi

include .depend

endif
