LINKER= $(CC)
EXE=

DEBUG=
DEBUG=-D_DEBUG 
CFLAGS = \
  -L../../../lib \
  -I../../../include -L$(shell ocamlc -where) \
  -L$(shell ocamlc -where)/../lutin  \
  -I$(shell ocamlc -where)/../lutin \
  -L../../../_build/lutin/src \
	 $(DEBUG) 

LIBS = lut4c.a liblut4c_stubs.a -lgmp -lm -ldl -lstdc++ 
LIBS = ../../../_build/lutin/src/lut4c.a ../../../_build/lutin/src/liblut4c_stubs.a \
	 -lasmrun -lgmp -lm -ldl 

LIBS = ../../../_build/lutin/src/lut4c.a ../../../_build/lutin/src/lutin.a   -lgmp -lm -ldl  -lstdc++ 

#gcc -o test -L. -L"`ocamlc -where`" test.c -Wl,--whole-archive -lhello_world -Wl,--no-whole-archive -lasmrun -lm -ldl


#  -lcamlrun

LUT2C=../../../bin/lutin$(EXE) --2c-4c -seed 42
LUT2CSOCK=../../../bin/lutin$(EXE) --2c-4c-socks 127.0.0.1 -seed 42
CALLVIASOCKET=../../../bin/call-via-socket -addr 127.0.0.1 -port 2004
LUTIN=../../../bin/lutin$(EXE)  -seed 42 -only-outputs 

ifeq ($(HOSTTYPE),mac)
	LINKER=g++ -g
	LIBS = -lluc4c_nc -llucky_nc -lgmp -lm -ldl 
endif

ifneq (,$(findstring win32,$(HOSTTYPE)))
  EXE=.exe
  LINKER=$(CC) 
  CFLAGS = \
    -L../../../lib  \
    -I../../../include -D_WIN32 -D_WINSOCK  $(DEBUG) \
    -Winline -Wimplicit-function-declaration 

  LIBS = -lluc4c_nc -llucky_nc -lgmp -lws2_32 -lm -lstdc++ -lole32 
  LIBSSOCK =  -lws2_32  -lole32
  LUT2C=../../../bin/lutin.exe --2c-4c
  LUT2CSOCK=../../../bin/lutin.exe --2c-4c-socks  127.0.0.1 -seed 42
endif
ifeq ($(HOSTTYPE),cygwin)
  EXE=.exe
  CFLAGS = \
    -L../../../lib  \
    -I../../../include \
    -Winline -Wimplicit-function-declaration 

  LIBS = -lluc4c_nc -llucky_nc -lgmp -lws2_32 -lm -lstdc++
  LUT2C=../../../bin/lutin --2c-4c-socks  127.0.0.1 -seed 42
endif


################################################################
# Calling lucky from C

foo.c: foo.lut
	$(LUT2C) foo.lut

foo.o : foo.c
	$(CC) -c $(CFLAGS) foo.c

call_foo.o: call_foo.c
	$(CC) -c $(CFLAGS) call_foo.c


foo$(EXE): foo.o call_foo.o 
	$(LINKER) $(CFLAGS)  -o $@ $^   $(LIBS) 



################################################################
# Calling lucky from C via sockets

call_foo_sock.o: call_foo_sock.c
	$(CC) -c $(CFLAGS) call_foo_sock.c

sock: 
	$(LUT2CSOCK) foo.lut
	make foo.o


foo-sock$(EXE): sock foo.o call_foo_sock.o 
	$(LINKER) $(CFLAGS)  -o $@  foo.o call_foo_sock.o  $(LIBSSOCK) 

################################################################x

clean:
	rm -f run *.o *~ foo foo-sock foo*.h foo*.c  foo-sock.c *.gp *.plot *.rif *.log

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

test1.rif : clean foo$(EXE) $(EXPDIR)
	./foo$(EXE) > test1.rif

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


utest: utest1 utest2

utest1: test1.rif
	./foo$(EXE) > test1.rif
	cp test1.rif $(EXPDIR)/test1.rif.exp 



test2: clean foo-sock$(EXE) $(EXPDIR)
	./foo-sock$(EXE) > test2.rif & 
	$(CALLVIASOCKET) "$(LUTIN) foo.lut" 
	rm -f test2.res && diff -B -u -i  $(EXPDIR)/test2.rif.exp test2.rif > test2.res || true
	cat test2.res
	[ ! -s test2.res ] && make clean 


utest2:clean foo-sock$(EXE)
	./foo-sock$(EXE) > test2.rif & 
	$(CALLVIASOCKET) "$(LUTIN) foo.lut" 
	cp test2.rif $(EXPDIR)/test2.rif.exp 

test: test1 

LTOP=../../../bin/lurettetop
LURETTETOP=$(LTOP)    \
	   --test-length 100 --thick-draw 1 \
	  --draw-inside 0 --draw-edges 0 --draw-vertices 0 --draw-all-vertices  \
	  --step-mode Inside --local-var  --no-sim2chro  --seed 3 \
	  --do-not-show-step 

PORT=2001
xxx:
	$(LURETTETOP)   --dbg -v 3 \
	  -rp "sut:socket:127.0.0.1:$(PORT)"  \
	  -rp "env:lutin:foo.lut:env"
xxx2:
		../../../bin/call-via-socket -addr 127.0.0.1 -port $(PORT) "lutin  -m main foo.lut -rif xxx.rif" 

xxx3:foo-sock 
		./foo-sock 
