CC = gcc -g
LD = gcc -g

EXE=
CFLAGS = \
  -L `ocamlfind -query lutin` \
  -I `ocamlfind -query lutin`

LIB= `ocamlfind query camlidl -l-format` \
		`ocamlfind query lutin-utils -i-format` \
		`ocamlfind query lutils -i-format` \
		-cclib -lcamlidl -cclib -lgmp


#LIBS = -llut4c_stubs `ocamlfind query lutils -i-format` -lgmp -lm -ldl -lstdc++ 
LUTIN=../../../bin/lutin

ifneq (,$(findstring $(HOSTTYPE),win32))
  EXE=.exe
  CFLAGS = \
    -L../../../lib -L/cygdrive/c/TEMP/MinGW/lib \
    -I../../../include \
    -Winline -Wimplicit-function-declaration 

  LIBS = -llut4c -llutils -lgmp -lws2_32 -lm -lstdc++ 
  LUTIN=../../../bin/lutin$(EXE) 
endif
ifeq ($(HOSTTYPE),mac)
	LD = g++ -g
	LIBS = -llut4c -llutils -lgmp -lm -ldl
endif

################################################################
# Calling lutin from Lustre

call_foo_loop.c:foo.c
call_foo_ext_func.c:foo.c
foo.c: foo.lut
	$(LUTIN) --2c-4lustre "call_foo" -seed 42 foo.lut -m main


call_foo.ec:
	../../../bin/lus2ec call_foo.lus call_foo

call_foo.c: call_foo.ec
	../../../bin/ec2c$(EXE)  -loop call_foo.ec

%.o : %.c
	$(CC) -c $(CFLAGS) $*.c 

foo$(EXE): foo.o call_foo.o call_foo_ext_func.o call_foo_loop.o 
	$(LD) $(CFLAGS) -o $@ $^  $(LIBS) 



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

clean:
	rm -f run *.o *~  call_foo_ext* call_foo.c call_foo.h call_foo_loop.c call_foo_loop.h foo.h foo.c *.gp *.plot *.rif *.ec foo *.res #* 

EXPDIR=`$(LUTIN) --ocaml-version`
$(EXPDIR):
	[ -d $(EXPDIR) ] || (mkdir -p $(EXPDIR) ; make utest)

test.rif : clean foo$(EXE) $(EXPDIR)
	echo "1 1 1 1 1 1 1 1 1 1 1" | ./foo$(EXE) > test.rif


test:test.rif
	rm -f test.res && diff -B -u -i  $(EXPDIR)/test.rif.exp test.rif > test.res
	[ ! -s test.res ] && make clean 


utest:test.rif
	cp test.rif $(EXPDIR)/test.rif.exp 


