##########################################################################
#                                                                        #
#  This file is part of Frama-C.                                         #
#                                                                        #
#  Copyright (C) 2013-2014                                               #
#    CEA (Commissariat à l'énergie atomique et aux énergies              #
#         alternatives)                                                  #
#                                                                        #
#  You may redistribute it and/or modify it under the terms of the GNU   #
#  Lesser General Public License as published by the Free Software       #
#  Foundation, version 2.1.                                              #
#                                                                        #
#  It is distributed in the hope that it will be useful, but WITHOUT     #
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY    #
#  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General      #
#  Public License for more details.                                      #
#                                                                        #
#  See the GNU Lesser General Public License version 2.1 for more        #
#  details (enclosed in the file LICENSE).                               #
#                                                                        #
##########################################################################

prefix = $(shell opam config var prefix 2>/dev/null || echo /usr/local)
bindir = $(prefix)/bin

OCAMLC = ocamlc
OCAMLOPT = ocamlopt
OCAMLDEP = ocamldep
INSTALL = install

PROGRAM = lreplay
OBJECTS = csv data strUtils foreignCodeUtils foreignCode coverage labelActions lreplay
LIBS = str unix

BEST = opt

OBJECTS_CMO = $(addsuffix .cmo,$(OBJECTS))
OBJECTS_CMX = $(addsuffix .cmx,$(OBJECTS))
LIBS_CMA = $(addsuffix .cma,$(LIBS))
LIBS_CMXA = $(addsuffix .cmxa,$(LIBS))

OCAMLC_COMPILE = $(OCAMLC) -c
OCAMLOPT_COMPILE = $(OCAMLOPT) -c

OCAMLC_LINK = $(OCAMLC) $(LIBS_CMA)
OCAMLOPT_LINK = $(OCAMLOPT) $(LIBS_CMXA)

all: $(PROGRAM)

$(PROGRAM): %: %.$(BEST)
	cp $< $@

%.opt: $(OBJECTS_CMX)
	$(OCAMLOPT_LINK) $^ -o $@
%.byte: $(OBJECTS_CMX)
	$(OCAMLC_LINK) $^ -o $@

%.cmo: %.ml
	$(OCAMLC_COMPILE) $<
%.cmi: %.mli
	$(OCAMLC_COMPILE) $<
%.cmx: %.ml
	$(OCAMLOPT_COMPILE) $<

foreignCode.ml: runtime.h detection.h
	(\
	  echo -n 'let runtime_h = ForeignCodeUtils.lazy_create_file "'; cat runtime.h | sed 's/["\\]/\\&/g'; echo '" "lreplay_runtime" ".h";;'; \
	  echo -n 'let detection_h= ForeignCodeUtils.lazy_create_file "'; cat detection.h | sed 's/["\\]/\\&/g'; echo '" "lreplay_detection" ".h";;' \
	) > $@
	@$(OCAMLDEP) *.ml > .depend

foreignCode.cmo: foreignCodeUtils.cmi
foreignCode.cmx: foreignCodeUtils.cmi

clean:
	$(RM) *.cm[iox] *.o foreignCode.ml *~

distclean: clean
	$(RM) $(PROGRAM) *.opt *.byte .depend

install:
	$(INSTALL) -d $(bindir)
	$(INSTALL) $(PROGRAM) $(bindir)

uninstall:
	$(RM) $(bindir)/$(PROGRAM)

.depend: *.ml *.mli
	$(OCAMLDEP) $^ > .depend


-include .depend

# vim: noet ts=4 sw=4:
