#---------------------------------------
# Directories
#---------------------------------------

SRCDIR = $(shell pwd)
#
# Installation directory prefix

# XXX MODIFY IT IF NECESSARY !!!
PREFIX =$(LURETTE_PATH)


# XXX MODIFY IT IF NECESSARY !!!
PREFIX =$(shell pwd)/../../..

# C include and lib directories
INCDIR = $(PREFIX)/include
LIBDIR = $(PREFIX)/lib
BINDIR = $(PREFIX)/bin
#
# Where to find necessary files
#
# LuckyDraw
# XXX MODIFY IT IF NECESSARY !!!
LUCKYDRAW_LIBDIR = $(PREFIX)/lib
CAMLLIBDIR = $(shell ocamlc -where)
#$(CAML_INSTALL)/lib/ocaml

#---------------------------------------
# CAML part
#---------------------------------------
OCAMLC = ocamlc.opt 
OCAMLOPT = ocamlopt.opt
OCAMLDEP = ocamldep
OCAMLLEX = ocamllex.opt
OCAMLYACC = ocamlyacc
OCAMLINC = -I $(LUCKYDRAW_LIBDIR)
OCAMLFLAGS = -g
OCAMLOPTFLAGS = -inline 20

CAMLIDL = camlidl
M4 = m4


#---------------------------------------
# C part
#---------------------------------------

CC = gcc
ICFLAGS = \
-I$(LUCKYDRAW_LIBDIR) \
-I$(CAMLLIBDIR) \
-Winline -Wimplicit-function-declaration 
XCFLAGS = 

CFLAGS = -g $(ICFLAGS) $(XCFLAGS)

#---------------------------------------
# Rules
#---------------------------------------

# Global rules
all: essai

#---------------------------------------
# essai
#---------------------------------------

essai: essai.o luckyDrawC_stubs.o luckyDrawC.o
	g++  $(CFLAGS) -o $@ $^ -L$(LUCKYDRAW_LIBDIR) -lluckyDraw_stubs  -lluckyDraw -lbdd_stubs -lpolkag_caml -lpolkag -lgmp -lnums  -lunix -lcamlstr -lcamlidl -L$(CAMLLIBDIR) -lasmrun -lm -ldl
essai.o: essai.c
	$(CC) $(CFLAGS) -c -o $@ $<

#---------------------------------------
# lib
#---------------------------------------

luckyDrawC_stubs.o: luckyDrawC_stubs.c luckyDrawC_stubs.h
	$(CC) $(CFLAGS) -c -o $@ $<

luckyDrawC.o: luckyDrawC_caml.cmx 
	$(OCAMLOPT) -output-obj -o $@ nums.cmxa unix.cmxa str.cmxa $(LUCKYDRAW_LIBDIR)/bdd.cmxa $(LUCKYDRAW_LIBDIR)/polka.cmxa $(LUCKYDRAW_LIBDIR)/luckyDraw.cmxa $^

luckyDrawC_caml.cmx: luckyDrawC_caml.ml
	$(OCAMLOPT) $(OCAMLINC) -c -o $@ $^

luckyDrawC_stubs.c: luckyDrawC_stubs.c.m4
	$(M4) $^ >$@

#---------------------------------------
# lib
#---------------------------------------

MAIN=essai
make test: clean $(MAIN)
	./$(MAIN) | grep -v "The random engine is initialized with the seed" > $(MAIN).out &&  \
	rm -f test.res && diff -u -i $(MAIN).out.exp $(MAIN).out \
	    > test.res
	[ ! -s test.res ] && make clean 

make utest:
	cp $(MAIN).out $(MAIN).out.exp

#---------------------------------------
# cleaning
#---------------------------------------
 clean:
	/bin/rm -f *.cmi *.cmo *.cmx *.o luckyDrawC_stubs.c *~ essai $(MAIN).out


