CC = gcc
CAMLC = ocamlc
CAMLO = ocamlopt
OCAMLMKLIB=ocamlmklib

#CDEBUGFLAGS = -g -Wall -DGTKTHR -DDEBUG
CDEBUGFLAGS = -g -Wall -DGTKTHR 
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/^.*: //") 
OCAMLDIR = `$(CAMLC) -where`

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

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


# 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 = $(shell gtk-config --libs)

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) $(OFILES)
	$(OCAMLMKLIB) -o gtk -oc mlgtk $(CMOFILES) $(OFILES) $(LDFLAGS)

gtk.cmxa: $(CMXFILES)
	$(OCAMLMKLIB) -o gtk -oc mlgtk $(CMXFILES) $(OFILES) $(LDFLAGS)

#libmlgtk.a: $(OFILES)
#	$(OCAMLMKLIB) -o gtk -oc mlgtk $(OFILES) $(LDFLAGS)

gtkThr.cmx: gtkThr.ml
	$(CAMLO) -thread $(ZFLAGSOPT) -c $<

mli.docs:
	if [ -x mli ]; then rm -rf mli; fi && mkdir mli
	-for i in `ls --color=never *.ml`; do	\
		ocamlc -i -c -o /dev/null $$i >mli/"$$i"i;	\
	done
	cp -f *.mli mli

clean:
	-rm -f *.cm* *.o *.a 
	make -C examples clean
	-rm -rf mli
	-rm -f dllmlgtk.so

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

install: libs
	$(INSTALL) gtk.cma $(INSTDIR)
	$(INSTALL) dllmlgtk.so $(INSTDIR)
	$(INSTALL) *.cmi $(INSTDIR)

install_opt: libs_opt
	$(INSTALL) gtk.cmxa gtk.a $(INSTDIR)
	$(INSTALL) libmlgtk.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
