# Time-stamp: <Mon Apr 04 2005 15:53:20 hwloidl>
#
# Targets:
#  cons-<name> ... do the consumer for <name>.cmlt
#  conx-<name> ... as above but don't launch isabelle
#  prod-<name> ... do the producer for <name>.cmlt
#  cert-<name> .., generate and check the certificate for <name> in batch mode
#
#  build-<name> ... generate a .jar file
#  run-<name> ... run the .jar file with an implicit check via premain method
#
# The producer is kicked-off automatically if con[sx] is called and the
# consumer generated files don't exist yet.
# Targets for producing intermediate files are as expected
#  <name>.gr
#  <name>.class
#  <name>Certificate.thy
#
# To pass flags to camelot or gf on per-name basis set
#  <name>_CFLAGS
#  <name>_GFLAGS
# (see examples below)
#
# ---------------------------------------------------------------------------

# Setup ------------------------------------------------------------------

MRGDIR ?= $(HOME)/mrg-working
PROGDIR=$(MRGDIR)/progs
TESTDIR=..

CAMELOT=$(PROGDIR)/Camelot/src/camelot
CAMELOT_FLAGS=-olhs 4 -lfd -n -C -thy 
# For JVM demo
CAM_FLAGS=-J -C -lfd -n -olhs 4

CERT=$(FILE)Certificate.thy

HEAP=SLACK
# only used for batch jobs
HEAP_DIR=/tmp
HEAP_WITH_PATH=$(HEAP_DIR)/$(HEAP)
# style for thy generation
THY_STYLE=vcg

# URL of dir for downloading HEAP image
URL=ftp://ftp.macs.hw.ac.uk/pub/gph/hwloidl

MRGISABELLE ?= emacs
ISABELLE=$(MRGISABELLE)
ISABELLE_BATCH=isabelle
#ISABELLE=Isabelle
#ISABELLE_FLAGS="-l $(HEAP)"

GF=$(PROGDIR)/Grail/gf/src/gf
GF_FLAGS=-t $(THY_STYLE) -C

CERT_NOTES=
PROGS_CERTIFIABLE=$(subst .cmlt,,$(shell ls *.cmlt))

MRGJAVA = ../../bin/MRGjava

# Prg specific settings -------------------------------------------------------

Twice_CFLAGS=-b

INPUT_InsSortM = 4 3 9 1 7 2

# rules ------------------------------------------------------------------

.PHONY: %.Certificate.thy prod-% cons-% build-% jar-% run-%

build-%: %.jar

%.jar:	%.cmlt
	 $(CAMELOT) $(CAM_FLAGS) $(subst .jar,,$@)

run-%: %.jar
	$(MRGJAVA)  $< $(INPUT_$(subst .jar,,$<)) 


%.class:	%.cmlt
	$(CAMELOT) $(CAMELOT_FLAGS) $($(subst .class,,$@)_CFLAGS) $<

%.gr:	%.cmlt
	$(CAMELOT) -g $(CAMELOT_FLAGS) $($(subst .class,,$@)_CFLAGS) $<

%Certificate.thy: %.class
	@echo "Built certificate: $@"

prod-%:  %.jar
	@echo "Finished with producer for: $@"

cons-%: CERT=$*Certificate.thy
#cons-%: LAYOUT="`$(TESTDIR)/grab_pragmas.sh 1 $(CERT)`"
cons-%: LAYOUT=$(shell $(TESTDIR)/grab_pragmas.sh 1 $(CERT))
cons-%: TAG=$(shell $(TESTDIR)/grab_pragmas.sh 2 $(CERT))
cons-%: %Certificate.thy
	$(GF) -L $(LAYOUT) -T $(TAG) $(GF_FLAGS) $($(subst Certificate.thy,,$<)_GFLAGS) $*
	@echo "Name: $*; Certificate: $<; Layout: $(LAYOUT); TAG: $(TAG)"
	@if [ "x$(DRY_RUN)"=="x" ] ; then $(ISABELLE) $(ISABELLE_FLAGS) $*_Consumer1.thy  ;  else echo "dry run" ; fi

#conx-%: DRY_RUN=1
#conx-%: cons-%
#	$(GF) -L $(LAYOUT) -T $(TAG) $(GF_FLAGS) $($(subst Certificate.thy,,$<)_GFLAGS) $*
#	@echo "Name: $*; Certificate: $<; Layout: $(LAYOUT); TAG: $(TAG)"
#	ifeq "$(DRY_RUN)" ""
#	$(ISABELLE) $(ISABELLE_FLAGS) $*_Consumer1.thy  
#	else 
#	@echo "dry run"
#	endif

$(HEAP_WITH_PATH):
	@echo "Downloading image from $(URL)"
	wget --output-document=$@ $(URL)/$(HEAP)

cert-all: $(HEAP_WITH_PATH)
	@echo "Certifying all programs: $(PROGS_CERTIFIABLE) $(CERT_NOTES)"
	@-for p in $(PROGS_CERTIFIABLE) ; do $(MAKE) DRY_RUN=1 -s cert-$$p ; done

cert-%: $(HEAP_WITH_PATH)
cert-%:	%.class
	@echo "Certifying the inferred heap consumption of $(subst .class,.cmlt,$<) ..."
	$(GF) -L $(LAYOUT) -T $(TAG) $(GF_FLAGS) $($(subst Certificate.thy,,$<)_GFLAGS) $*
	@echo "Name: $*; Certificate: $<; Layout: $(LAYOUT); TAG: $(TAG)"
	@echo "use_thy \"$(subst .class,,$<)_Consumer2\";" > ROOT.ML
	$(ISABELLE_BATCH) -r $(HEAP_WITH_PATH) DUMMY < ROOT.ML
	@if [ -f ROOT.ML ] ; then rm ROOT.ML ; fi


cleanup-%: %.constraints %.lfd
	rm -f $^

clean-%: cleanup-% %.class %Certificate.thy %.thy %_Consumer1.thy %_Consumer2.thy %_TACTIC.thy
	rm -f $^

# Edinburgh-style setup
producer:
	$(MRGDIR)/progs/Camelot/src/camelot -D -lfd -n -thy $(FILE).cmlt
	rm -f *.constraints *.lfd *~

consumer:
	$(MRGDIR)/progs/Grail/gf/src/gf -L $(L) -T $(T) -t vcg $(FILE).class
	$(ISABELLE) $(ISABELLE_FLAGS) $(FILE)_Consumer1.thy &
	@echo "Cert: $(Q) ; Layout: $(LAYOUT) ; TAG: $(TAG)"


clean:
	rm -f *.class *.thy *.constraints *.lfd *~ *.jar valdecs.pkl MANIFEST
