#*** Copyright 2002-2004 The Netsem Team
#
#    * Steve Bishop
#    * Michael Compton
#    * Matthew Fairbairn
#    * Michael Norrish
#    * Andrei Serjantov
#    * Peter Sewell
#    * Michael Smith
#    * Keith Wansbrough
#
#All rights reserved.
#
#This file is distributed under the terms of the GNU Lesser General
#Public License, with the special exception on linking described in
#file NEW-LICENSE.
#
#***
#
#*** Copyright 2002-2004 The Acute Team
#
#  Allen-Williams, Mair
#  Bishop, Steven
#  Fairbairn, Matthew
#  Habouzit, Pierre [*]
#  Leifer, James [*]
#  Sewell, Peter
#  Sjberg, Vilhelm
#  Steinruecken, Christian
#  Vafeiadis, Viktor
#  Wansbrough, Keith
#  Zappa Nardelli, Francesco [*]
#  Institut National de Recherche en Informatique et en Automatique (INRIA)
#
#  Contributions of authors marked [*] are copyright INRIA.
#
#All rights reserved.
#
#This file is distributed under the terms of the GNU Lesser General
#Public License, with the special exception on linking described in
#file NEW-LICENSE.
#
#***

NSSOCKLIB=../nssock/libnssock.a

# Calls and returns are logged only if NS_LOGGING is defined:
#NS_LOGGING=1

# empty for no, 1 for yes
#FRESH?=
NATIVE?=$(if $(FRESH),,1)
fresh=$(if $(FRESH),fresh-,)

#ifeq (,$(FRESH))
OCAMLPREFIX=$(OCAMLPATH)/../lib/ocaml
#else
#OCAMLPREFIX=/home/kw217/Scratch/Fast/lib/$(fresh)ocaml
#endif

OCAMLC=$(OCAMLPATH)/$(FRESH_EXT)ocamlc
OCAMLOPT=$(OCAMLPATH)/$(FRESH_EXT)ocamlc
#OCAMLOPT=$(OCAMLPATH)/$(FRESH_EXT)ocamlopt
OCAMLDEP=$(OCAMLPATH)/$(FRESH_EXT)ocamldep
OCAMLMKLIB=$(OCAMLPATH)/$(FRESH_EXT)ocamlmklib -ocamlc $(OCAMLC) -ocamlopt $(OCAMLOPT)
OCAMLCOPT=-I $(OCAMLPREFIX)/caml -I $(OCAMLPREFIX)
RM=rm -f

CFILES=camlsupport.c cst2constr.c errmsg.c unixsupport.c \
	tcpc.c
CAMLFILES=tcp.ml persist.ml
CAMLIFACES=tcp.mli persist.mli

ifdef NS_LOGGING
LIBPATHSNTP=-cclib -L../common -cclib -lntplib
COPTS=-cclib -DNS_LOGGING
else
LIBPATHSNTP=
COPTS=
endif

LIBPATHS=-cclib -L../nssock -cclib -lnssock $(LIBPATHSNTP)

all: $(CAMLIFACES:.mli=.cmi) libtcp.a
#$(if $(NATIVE),libtcpnative.a,)

libtcp.a: $(CAMLFILES:.ml=.cmo) $(CFILES:.c=.o) $(NSSOCKLIB)
	$(OCAMLMKLIB) -o tcp $(CAMLFILES:.ml=.cmo) $(CFILES:.c=.o) \
			    $(NSSOCKLIB)

libtcpnative.a: $(CAMLFILES:.ml=.cmx) $(CFILES:.c=.o) $(NSSOCKLIB)
	$(OCAMLMKLIB) -o tcpnative $(CAMLFILES:.ml=.cmx) $(CFILES:.c=.o) $(LIBPATHS)

.PHONY: depend
depend : .depend

.depend: $(wildcard *.ml) $(wildcard *.mli)
	$(OCAMLDEP) $^ >$@-tmp
	mv $@-tmp $@

clean:
	$(RM) *.[oa] *.cm[ioax] *.cmxa *.so

realclean: clean
	$(RM) .depend

.SUFFIXES: .c .o .ml .cmo .mli .cmi .cmx
.ml.cmx:
	$(OCAMLOPT) -c $(OCAMLCOPT) $<
.c.o:
	$(OCAMLC) -custom -c $(OCAMLCOPT) $(COPTS) $<
.mli.cmi:
	$(OCAMLC) -c $(OCAMLCOPT) $<
.ml.cmo:
	$(OCAMLC) -custom -c $(OCAMLCOPT) $<

ifdef MAKECMDGOALS
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),realclean)
-include .depend
endif
endif
else
-include .depend
endif
