##########################################################################
#                                 Cmmtest                                #
#                                                                        #
#   Anirudh Kumar, IIT Kanpur & INRIA Paris-Rocquencourt                 #
#   Robin Morisset, ENS & INRIA Paris-Rocquencourt                       #
#   Pankaj Pawan, IIT Kanpur & INRIA Paris-Rocquencourt                  #
#   Francesco Zappa Nardelli, INRIA Paris-Rocquencourt                   #
#                                                                        #
#  The Cmmtest tool is copyright 2012, 2013 Institut National de         #
#  Recherche en Informatique et en Automatique (INRIA).                  #
#                                                                        #
#  Redistribution and use in source and binary forms, with or without    #
#  modification, are permitted provided that the following conditions    #
#  are met:                                                              #
#  1. Redistributions of source code must retain the above copyright     #
#  notice, this list of conditions and the following disclaimer.         #
#  2. Redistributions in binary form must reproduce the above copyright  #
#  notice, this list of conditions and the following disclaimer in the   #
#  documentation and/or other materials provided with the distribution.  #
#  3. The names of the authors may not be used to endorse or promote     #
#  products derived from this software without specific prior written    #
#  permission.                                                           #
#                                                                        #
#  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS    #
#  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     #
#  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE    #
#  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY       #
#  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL    #
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE     #
#  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS         #
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER  #
#  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR       #
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN   #
#  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                         #
#                                                                        #
##########################################################################

# make builds
#
# - iteropt (which generates the traces for a given program)
#           (caution: relies on a bunch of external scripts WHICH SCRIPTS?)
#
# - modify_pintrace.ml (an auxiliary program used by trace instrumentation)
#
# - cmmtest (the testing tool)

VERSION = 0.2.10702

SOURCES = 							        \
   util.ml config.ml types.ml dwarfparse.ml raw_trace_parse.ml          \
   irreplay.ml analyse2.ml modify_pintrace.ml top.ml

#DIST_FOLDERS =                  \
	/arm-emulator /creduce-2.0.1 /csmith-2.1.0 /csmith_with_locks         \
	/pin-interceptor /pin_replay /scripts                                 \
	/pin-2.10-45467-gcc.3.4.6-ia32_intel64-linux.tar.gz

# compare.ml

ZARITHDIR = zarith-1.2.1
LIB     = str.cmxa unix.cmxa $(ZARITHDIR)/zarith.cmxa

ML      = $(filter %.ml, $(SOURCES))
CMO     = $(patsubst %.ml, %.cmo, $(ML))
CMX     = $(patsubst %.cmo, %.cmx, $(CMO))

#PREFIX = ../../compiler_testing/
#DISTRIB = $(patsubst /%, $(PREFIX)%, $(DIST_FOLDERS))

OCAMLOPT = ocamlopt -inline 9 -unsafe -I $(ZARITHDIR)
#OCAMLOPT = ocamlopt
OCAMLDEP = ocamldep

all: dwarfparse_app zarith cmmtest spawn inspect install_cmmtest

install_cmmtest: install_cmmtest.ml
	ocamlc unix.cma str.cma -o install_cmmtest install_cmmtest.ml

inspect: inspect.ml
	ocamlopt -o inspect str.cmxa inspect.ml

iteropt: iteropt.ml
	ocamlc -o iteropt iteropt.ml

dwarfparse_app: util.cmx dwarfparse_app.ml
	ocamlopt -g -o dwarfparse_app str.cmxa unix.cmxa util.cmx dwarfparse_app.ml

spawn: spawn.ml
	ocamlopt -o spawn str.cmxa unix.cmxa spawn.ml

# trace_c_file: util.ml trace_c_file.ml
# 	$(OCAMLOPT) -o trace_c_file -I $(LIB) util.ml trace_c_file.ml

# modify_pintrace: util.ml modify_pintrace.ml
# 	$(OCAMLOPT) -g -o modify_pintrace $(LIB) 		\
#            util.ml modify_pintrace.ml

zarith:
	cd zarith-1.2.1; ./configure; make

cmmtest: $(CMX) 
	$(OCAMLOPT) -annot -g -o cmmtest $(LIB) $(CMX)

distrib: include.txt
	mkdir -p cmmtest_distribution_$(VERSION)
	for FILE in `cat include.txt`; do mkdir -p cmmtest_distribution_$(VERSION)/`dirname $$FILE`; cp ../$$FILE cmmtest_distribution_$(VERSION)/`dirname $$FILE`; done
	mv cmmtest_distribution_$(VERSION)/creduce-git-250613/ cmmtest_distribution_$(VERSION)/creduce-cmmtest/
	tar -cvzf cmmtest_distribution_$(VERSION).tgz cmmtest_distribution_$(VERSION)
	rm -rf cmmtest_distribution_$(VERSION)

web: distrib
	cp cmmtest_distribution_$(VERSION).tgz ../README.txt ~/web/projects/cmmtest/
	unison web

# general rules

%.cmo: %.ml
	$(OCAMLC) -c -g -annot $<

%.cmx: %.ml
	$(OCAMLOPT) -c -g -annot $<

# cleanup

clean:
	rm -f *~ *.cmi *.cmo *.cmx *.o *.annot
	cd zarith-1.2.1; make clean
	rm -f analyse analyse.opt iteropt install_cmmtest cmmtest
	rm -f cmmtest_distribution_$(VERSION).tgz

# dependencies

depend:
	$(OCAMLDEP) $(SOURCES) $(SOURCES_MLI) > .depend
.depend: Makefile $(SOURCES) $(SOURCES_MLI)
	$(OCAMLDEP) $(SOURCES) $(SOURCES_MLI) > .depend

include .depend
