# Acute Makefile
#
#*** 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.
#
#***


default: all

rebuild: clean default

# version information for distribution tarballs, display, and (perhaps) magic numbers
ACUTEVER=0.50-1

# freshocamldirs.mk is generated from freshocamldirs.mk.in, so your local
# changes to freshocamldirs.mk won't accidentally go into CVS
include freshocamldirs.mk

Y2L=../y2l/y2l
LATEX=latex
DVIPS=dvips
DVIPSOPTS=-Ppdf
GZIP=gzip
BZIP2=bzip2

# thread support in freshocaml: either -thread or -vmthread remark:
# the Tcp library requires -thread; the Atcp library should be
# compatible with -vmthread, but it is not.
THREAD_MOD=-thread

# we get around a bug in fresh which calls ocamlc from fresh-ocamlcp by mistake
FRESH_OCAML_LINKS_DIR=$(PWD)/fresh_ocaml_links_dir
FRESH_OCAML_LINKS=$(FRESH_OCAML_LINKS_DIR)/ocamlc $(FRESH_OCAML_LINKS_DIR)/ocamlprof

PROFILING?=#do 'PROFILING=1 make' for compilation with profiling
OLD_FRESH?=#do 'OLD_FRESH=1 make' for compilation with old version of fresh-ocaml

# don't do both profiling without old_fresh, this makefile isn't smart enough

ifneq ($(PROFILING),)
PATH:=$(FRESH_OCAML_LINKS_DIR):$(PATH)
FCAMLCP?=$(FRESH_BIN_PREFIX)/fresh-ocamlcp
COMPFLAGS_EXTRA=-p a
else
FCAMLCP?=$(FCAMLC)
COMPFLAGS_EXTRA=
endif

OCAMLPATH=$(FRESH_BIN_PREFIX)
FRESH_EXT=fresh-

# sort paths for testing fresh, and pass down into make inside tcp.
# note that 3.06 and 3.07 keep some files in different places; this copes with that.
ifeq ($(OLD_FRESH),)
# new fresh (3.07 alpha 1)
FRESH_BIN_PREFIX=$(TESTING_FRESH_BIN_PREFIX)
FRESH_LIB_PREFIX=$(TESTING_FRESH_LIB_PREFIX)
FRESH_SRC_PREFIX=$(TESTING_FRESH_SRC_PREFIX)
else
# old fresh (3.06)
FRESH_BIN_PREFIX=$(OLD_FRESH_BIN_PREFIX)
FRESH_LIB_PREFIX=$(OLD_FRESH_LIB_PREFIX)
FRESH_SRC_PREFIX=$(OLD_FRESH_SRC_PREFIX)
endif


FCAMLC=$(FRESH_BIN_PREFIX)/fresh-ocamlc
FCDEP=$(FRESH_BIN_PREFIX)/fresh-ocamldep
FP4=$(FRESH_BIN_PREFIX)/fresh-camlp4
FMKLIB=$(FRESH_BIN_PREFIX)/fresh-ocamlmklib -ocamlc $(FCAMLCP)



INHERIT_FRESH_PATHS_IN_TCP=1

export FRESH_SRC_PREFIX
export FRESH_LIB_PREFIX
export FCAMLC
export FCAMLCP
export FCDEP
export FP4
export FMKLIB
export INHERIT_FRESH_PATHS_IN_TCP
export PATH
export LIBEDITPATH
export OCAMLPATH
export FRESH_EXT
export NOEDITLINE
export COMPFLAGS
export COMPFLAGS_EXTRA

FCAMLYACC?=$(FRESH_BIN_PREFIX)/fresh-ocamlyacc
FCAMLLEX?=$(FRESH_BIN_PREFIX)/fresh-ocamllex

CPP=./wrap-cpp
SIMPLEPP=scripts/simplepp
INCLUDES = -I$(FRESH_BIN_PREFIX)/../lib/ocaml/caml
CFLAGS = -O -fno-defer-pop -Wall -Wno-unused $(INCLUDES)
CAMLLINKFLAGS = -cclib -L. -g $(THREAD_MOD) unix.cma threads.cma str.cma graphics.cma
LEXER_HACK_SCRIPT=lexer_hack_script
PARSER_HACK_SCRIPT_MLI=parser_hack_script_mli
PARSER_HACK_SCRIPT_ML=parser_hack_script_ml

#P4DIR=/usr/groups/netsem/lib/fresh-ocaml/camlp4

COMPFLAGS = -g $(THREAD_MOD) #unix.cma threads.cma

#sources are not generated files!

ML_SOURCES = \
        threadpool.ml \
	opts.ml \
           lextools.ml \
           location.ml \
	   basecon.ml \
           ast.ml \
	   baseconty.ml \
	   exceptions.ml \
           pipeline.ml debuginit.ml debug.ml evaltypes.ml econst.ml evalsupp.ml eval.ml genlib.ml \
           desugar.ml hashify.ml hashifysupp.ml \
           $(LEXER_HACK_SCRIPT).ml  util.ml linkok.ml misc.ml    \
           mylexing.ml myparsing.ml $(PARSER_HACK_SCRIPT_MLI).ml $(PARSER_HACK_SCRIPT_ML).ml  \
           parsertypes.ml parsertypesprivate.ml uri.ml parsetools.ml pretty.ml    \
           typecheck.ml tysupp.ml dump.ml \
           $(RUNTEST_ML_FILES)

ML_ISH_SOURCES = \
	   acute.mlp lexer.mllp library.mlp parser.mlyp mlilexer.mll

OTHER_SOURCES = \
	mylexing.c wrap-cpp

# to get a plausble wc number for the paper, here are the `real' sources, leaving out test
# apparatus, preprocessor gadgets, and the tcp stuff.  Count using "make wc".
HACKY_WC_SOURCES = \
	acute.ml \
        threadpool.ml \
	opts.ml \
           lextools.ml \
           location.ml \
	   basecon.ml \
           ast.ml \
	   baseconty.ml \
	   exceptions.ml \
           pipeline.ml debuginit.ml debug.ml evaltypes.ml econst.ml evalsupp.ml eval.ml genlib.ml \
           desugar.ml hashify.ml hashifysupp.ml\
           util.ml linkok.ml misc.ml    \
           mylexing.ml myparsing.ml  \
           parsertypes.ml parsertypesprivate.ml uri.ml parsetools.ml pretty.ml    \
           typecheck.ml tysupp.ml dump.ml \
	   lexer.mllp parser.mlyp \
              threadpool.mli location.mli lexer.mli library.mli \
              ast.mli dump.mli econst.mli hashify.mli evalsupp.mli eval.mli desugar.mli \
              linkok.mli mylexing.mli myparsing.mli opts.mli uri.mli parsetools.mli pretty.mli typecheck.mli tysupp.mli \
              util.mli


MLI_SOURCES = \
              threadpool.mli location.mli lexer.mli library.mli \
              ast.mli dump.mli econst.mli hashify.mli hashifysupp.mli evalsupp.mli eval.mli desugar.mli \
              linkok.mli mylexing.mli myparsing.mli opts.mli uri.mli parsetools.mli pretty.mli typecheck.mli tysupp.mli \
              util.mli $(RUNTEST_MLI_FILES)

#generated files (note that lexer.mli is not generated!)
#also generated are: libfuncs.ml but we don't put it in to avoid make problems
PARSER_LEXER_MODES = usersource compiled
COMPILED = compiled
PARSER_STEM = parser
LEXER_STEM = lexer
# calculated
PARSER_PREFIX = $(PARSER_STEM)_
LEXER_PREFIX = $(LEXER_STEM)_
PARSERS = $(PARSER_LEXER_MODES:%=$(PARSER_PREFIX)%)
LEXERS = $(PARSER_LEXER_MODES:%=$(LEXER_PREFIX)%)
PARSERS_MLY = $(PARSERS:%=%.mly)
LEXERS_MLL = $(LEXERS:%=%.mll)
PARSERS_CMO = $(PARSERS:%=%.cmo)
LEXERS_CMO = $(LEXERS:%=%.cmo)

MLLs = $(LEXERS_MLL) mlilexer.mll
MLYs = $(PARSERS_MLY)
#MLYs = $(filter-out %-tmp.mly, $(wildcard *.mly))
LEXERS_ML = $(MLLs:.mll=.ml)
PARSERS_ML_TOKML = $(MLYs:.mly=.ml) $(MLYs:.mly=_tok.ml)
PARSERS_MLI = $(MLYs:.mly=.mli)
PARSERS_ML_MLI_TOKML = $(PARSERS_ML_TOKML) $(PARSERS_MLI)
LEXER_PARSER_GENERATED_MLS = $(LEXERS_ML) $(PARSERS_ML_TOKML) acute.ml
LEXER_PARSER_GENERATED_MLIS = $(PARSERS_MLI)
LEXER_PARSER_GENERATED_FILES = $(LEXER_PARSER_GENERATED_MLS) $(LEXER_PARSER_GENERATED_MLIS)

GENLIBOBJFILES = \
                 util.cmo opts.cmo debug.cmo mylexing.cma \
                 myparsing.cmo \
                 location.cmo \
                 lextools.cmo \
                 basecon.cmo ast.cmo baseconty.cmo exceptions.cmo pretty.cmo misc.cmo lexer_usersource.cmo \
                 parsertypes.cmo \
                 parser_usersource.cmo \
		 mlilexer.cmo genlib.cmo

ACUTEOBJFILES = \
                  util.cmo \
	          opts.cmo \
	          lextools.cmo \
                  debug.cmo \
                  mylexing.cma \
                  myparsing.cmo \
                  location.cmo \
                  basecon.cmo ast.cmo baseconty.cmo \
	          exceptions.cmo \
                  evaltypes.cmo \
	          threadpool.cmo \
                  library.cmo econst.cmo \
                  pretty.cmo  \
	          misc.cmo  \
	          uri.cmo \
                  tysupp.cmo typecheck.cmo \
                  $(LEXERS_CMO) \
                  parsertypesprivate.cmo parsertypes.cmo $(PARSERS_CMO) \
                  parsetools.cmo \
                  dump.cmo \
	          linkok.cmo \
	          evalsupp.cmo \
	          desugar.cmo \
                  hashifysupp.cmo \
		  hashify.cmo \
	          eval.cmo \
	          pipeline.cmo



ifdef NS_LOGGING
TCPFILES = tcp/ocamllib/persist.cmo tcp/ocamllib/tcp.cma tcp/nssock/libnssock.a tcp/common/libntplib.a
else
TCPFILES = tcp/ocamllib/persist.cmo tcp/ocamllib/tcp.cma tcp/nssock/libnssock.a
endif




LIBRARYLIBFILES=-I tcp/ocamllib -I tcp/nssock -custom  $(TCPFILES) \
	        wrappers/agraphics.cmo wrappers/apervasives.cmo wrappers/apervasives_small.cmo wrappers/atcp.cmo  -I wrappers
ifneq ($(NOEDITLINE),)
MAINLIBFILES=-I noeditline noeditline/editline.cmo
else
MAINLIBFILES=-I editline editline/editline_wrap.cma
endif

RUNTEST = runtest
RUNTEST_ML_FILES = $(RUNTEST)_db.ml $(RUNTEST)_pretty.ml $(RUNTEST)_acute.ml $(RUNTEST)_shell.ml $(RUNTEST)_library.ml $(RUNTEST).ml
RUNTEST_MLI_FILES = $(RUNTEST)_db.mli $(RUNTEST)_pretty.mli $(RUNTEST)_shell.mli $(RUNTEST)_library.mli
RUNTESTOBJFILES = $(RUNTEST_ML_FILES:.ml=.cmo)

DEFINITIONS_FILES = \
	definitions_lib.ac \
	definitions_lib_semiauto_small.ac \
	definitions_lib_small.ac \
	io_persist.ac \
	io_template.ac

DEFINITIONS_GEN_FILES = \
	definitions_lib_auto.ac \


SRCDIST_FILES = \
	Makefile             \
	freshocamldirs.mk.in \
	README.language-changes \
	NEW-LICENSE              \
	LICENSE-OCAML          \
	$(ML_SOURCES)        \
	$(MLI_SOURCES)	     \
	$(ML_ISH_SOURCES)    \
	$(OTHER_SOURCES)    \
	$(DEFINITIONS_FILES)

#	README.distro-src-files \


SRCDIST_LIBS_FILES = \
	char.mli             \
	digest.mli	     \
	filename.mli	     \
	graphics.mli	     \
	list.mli	     \
	persist.mli	     \
	pervasives_small.mli \
	string.mli	     \
	sys.mli		     \
	tcp.mli		     \
	tcp.slow	     \
	tcp.types	     \
	unix.mli             \
	unix.slow

SRCDIST_WRAPPERS_FILES = \
agraphics.mli    apervasives.slow \
Makefile      apervasives.ml   apervasives_small.ml \
agraphics.ml  apervasives.mli  apervasives_small.mli \
atcp.ml atcp.mli atcp.slow


SRCDIST_TCP_FILES = \
	Makefile

# for the tcp/* subdirectories, this lists all the files in the dirs
# (except CVS) after a make clean.  Perhaps nicer to express
# algorithmically, but this cheaply protects against including junk.

# SRCDIST_TCP_COMMON_FILES = \
# lexer.mll       nettypes.ml    sock.ml                              \
# Makefile         libcalls.ml     nettypes.mli   sock.mli	    \
# Makefile.win     libcalls.mli    ntpheader.c    tcpcbparserlib.ml   \
# hol2net.ml       librender.ml    ntpheader.h    tcpcbparserlib.mli  \
# hol2net.mli      librender.mli   parser.mly     tcpcbrender.ml	    \
# holparselib.ml   net2hol.ml      parserlib.ml   tcpcbrender.mli	    \
# holparselib.mli  net2hol.mli     parserlib.mli  tcpcbtypes.ml	    \
# holrender.ml     netconv.ml      render.ml      tcpcbtypes.mli	    \
# holrender.mli    netconv.mli     render.mli			    \
# holtypes.ml      netipreass.ml   renderlib.ml                       \
# holtypes.mli     netipreass.mli  renderlib.mli

SRCDIST_TCP_NSSOCK_FILES = \
README    ns_errors.c   ns_sockets_int.h                            \
Makefile      conv      ns_sockets.c  winconv                       \
Makefile.win  ns_aux.c  ns_sockets.h  winerrcodes.txt


# SRCDIST_TCP_OCAMLEXT_FILES = \
# parser_hack_script.ml  platformc.c  threadparsing.ml                \
# Makefile      parsescript            rawsock.ml   threadparsing.mli \
# Makefile.win  platform.ml            rawsock.mli                    \
# README        platform.mli           rawsockc.c

SRCDIST_TCP_OCAMLLIB_FILES = \
cst2constr.c        unixsupport.c \
Makefile        cst2constr.h        tcp.ml            unixsupport.h \
Makefile.win    errmsg.c            winerrmsg.c			    \
README          libfunmappings.txt  tcp.mli           winsupport.c  \
camldirs.mk.in  persist.ml          				    \
camlsupport.c   persist.mli         tcpc.c                          \
camlsupport.h         test.ml
#     prnsupport.h   prnsupport.c


SRCDIST_EDITLINE_FILES = \
	Makefile  editline.ml  editlinec.c

SRCDIST_NOEDITLINE_FILES = \
	Makefile  editline.ml

SRCDIST_SCRIPTS_FILES = \
	dumptests \
	dumptests_prefix.tex \
	dumptests_separator.tex \
	dumptests_suffix.tex \
	README.simplepp \
	simplepp
#	simplepp-tests


BINDIST_FILES = \
	README.distrob \
	README.language-changes \
	NEW-LICENSE \
	LICENSE-OCAML \
	acute \
	runtest \
	$(DEFINITIONS_FILES) \
	$(DEFINITIONS_GEN_FILES)




DIST_EXAMPLE_FILES = \
	go6 \
	blockhead.ac \
	minesweep.ac \
	util.ac \
	tcp.ac \
	local_channel.ac \
	distributed_channel.ac \
	npi.ac \
	npi-mig.ac \
	npi-recv.ac \
	bounce.ac

DIST_EXAMPLE_AMBIENTS_FILES = \
	ambients-compiler.ml  ambients.ac  test2.ma \
	Makefile  ambients-go           tcp.ac       util.ac \
	README    ambients-trees.ac     test1.ma


DIST_TESTS_DB_FILES = \
	paper.ac




acute-bin: acute-$(ACUTEVER)-bin.tar.gz


install-acute-bin: acute-$(ACUTEVER)-bin.tar.gz
	cp -f acute-$(ACUTEVER)-bin.tar.gz ../../../public_html/acute/distro

acute-%-bin.tar: acute runtest definitions_lib_auto.ac Makefile
	rm -rf acute-$-bin{,.tar{,.gz,.bz2}}     # make sure an old one doesn't confuse the issue
	mkdir -p acute-$*-bin
	cp $(BINDIST_FILES) acute-$*-bin
	touch ld.conf
	mv ld.conf acute-$*-bin
	mkdir -p acute-$*-bin/examples
	cd examples; cp $(DIST_EXAMPLE_FILES) ../acute-$*-bin/examples
	mkdir -p acute-$*-bin/examples/ambients
	cd examples/ambients; cp $(DIST_EXAMPLE_AMBIENTS_FILES) ../../acute-$*-bin/examples/ambients
	mkdir -p acute-$*-bin/tests_db
	cd tests_db; cp $(DIST_TESTS_DB_FILES) ../acute-$*-bin/tests_db
#	for dir in $(DIST_DIRS); do mkdir -p acute-$*-bin/$$dir; cp $$dir/* acute-$*-bin/$$dir/; rm -rf acute-$*-bin/$$dir/CVS; done
	tar cvf $@ acute-$*-bin
#	rm -rf acute-$*-bin     # leave around for now to check it

acute-bin-clean:
	-rm -rf acute-*-bin{,.tar{,.gz,.bz2}}





acute-src: acute-$(ACUTEVER)-src.tar.gz


install-acute-src:
	rm -f acute-$(ACUTEVER)-src.tar.gz
	make acute-$(ACUTEVER)-src.tar.gz
	cp -f acute-$(ACUTEVER)-src.tar.gz ../../../public_html/acute/distro

acute-%-src.tar:
#
# ensure an old one doesn't confuse the issue
#
	rm -rf acute-$*-src{,.tar{,.gz,.bz2}}
#
# make directory for distro
#
	mkdir -p acute-$*-src
#
# construct README.distro-src
#
	date > tmp.datestamp
	cat tmp.datestamp README.distros.p | \
	sed -e "s/ACUTEVER/$(ACUTEVER)/g" | \
	sed -e "s/DEFINITIONS_FILES/$(DEFINITIONS_FILES)/g" | \
	sed -e "s/ML_SOURCES/$(ML_SOURCES)/g"  | \
	sed -e "s/ML_ISH_SOURCES/$(ML_ISH_SOURCES)/g" > acute-$*-src/README.distro-src
#	rm tmp.datestamp
#
# construct relevant part of directory structure and copy stuff in
#
	cp $(SRCDIST_FILES) acute-$*-src
	mkdir -p acute-$*-src/libs
	cd libs; cp $(SRCDIST_LIBS_FILES) ../acute-$*-src/libs
	mkdir -p acute-$*-src/wrappers
	cd wrappers; cp $(SRCDIST_WRAPPERS_FILES) ../acute-$*-src/wrappers
	mkdir -p acute-$*-src/editline
	cd editline; cp $(SRCDIST_EDITLINE_FILES) ../acute-$*-src/editline
	mkdir -p acute-$*-src/noeditline
	cd noeditline; cp $(SRCDIST_NOEDITLINE_FILES) ../acute-$*-src/noeditline
	mkdir -p acute-$*-src/scripts
	cd scripts; cp $(SRCDIST_SCRIPTS_FILES) ../acute-$*-src/scripts
	mkdir -p acute-$*-src/tcp
	cd tcp; cp $(SRCDIST_TCP_FILES) ../acute-$*-src/tcp
#	mkdir -p acute-$*-src/tcp/common
#	cd tcp/common; cp $(SRCDIST_TCP_COMMON_FILES) ../../acute-$*-src/tcp/common
	mkdir -p acute-$*-src/tcp/nssock
	cd tcp/nssock; cp $(SRCDIST_TCP_NSSOCK_FILES) ../../acute-$*-src/tcp/nssock
#	mkdir -p acute-$*-src/tcp/ocamlext
#	cd tcp/ocamlext; cp $(SRCDIST_TCP_OCAMLEXT_FILES) ../../acute-$*-src/tcp/ocamlext
	mkdir -p acute-$*-src/tcp/ocamllib
	cd tcp/ocamllib; cp $(SRCDIST_TCP_OCAMLLIB_FILES) ../../acute-$*-src/tcp/ocamllib
	mkdir -p acute-$*-src/examples
	cd examples; cp $(DIST_EXAMPLE_FILES) ../acute-$*-src/examples
	mkdir -p acute-$*-src/examples/ambients
	cd examples/ambients; cp $(DIST_EXAMPLE_AMBIENTS_FILES) ../../acute-$*-src/examples/ambients
	mkdir -p acute-$*-src/tests_db
	cd tests_db; cp $(DIST_TESTS_DB_FILES) ../acute-$*-src/tests_db
#
# tar
#
	tar cvf $@ acute-$*-src
#	rm -rf acute-$*-src     # leave around for now to check it

acute-src-clean:
	-rm -rf acute-*-src{,.tar{,.gz,.bz2}}



%.gz : %
	$(GZIP) $<

%.bz2 : %
	$(BZIP2) $<

# all is all you need!
all: tcpall wrappersall editlineall $(FRESH_OCAML_LINKS) acute $(RUNTEST) permissions

# relicense is not included in all as not included in distro

tcpall:
	@if [ -d tcp ]; then echo "Blahblah:$(FCAMLCP)"; $(MAKE) -C tcp ; else echo "ERROR: subdirectory 'tcp' not found! Call checkout again."; false; fi

tcpdepend:
	@if [ -d tcp ]; then echo "Blahblah:$(FCAMLCP)"; $(MAKE) -C tcp depend ; else echo "ERROR: subdirectory 'tcp' not found! Call checkout again."; false; fi


editlineall:
ifneq ($(NOEDITLINE),)
	@if [ -d noeditline ]; then $(MAKE) -C noeditline ; else echo "ERROR: subdirectory noeditline not found!"; false; fi
else
	@if [ -d editline ]; then $(MAKE) -C editline ; else echo "ERROR: subdirectory editline not found!"; false; fi
endif

.PHONY: tcp-clean
tcp-clean:
	-( $(MAKE) -C tcp clean )

editline-clean:
ifneq ($(NOEDITLINE),)
	-( $(MAKE) -C noeditline clean)
else
	-( $(MAKE) -C editline clean )
endif

.PHONY: wrappersall wrappers-clean wrappers-ocaml-clean
wrappersall: $(FRESH_OCAML_LINKS)
	$(MAKE) -C wrappers
wrappers-clean:
	-$(MAKE) -C wrappers clean
wrappers-ocaml-clean:
	-$(MAKE) -C wrappers-ocaml clean

$(FRESH_OCAML_LINKS_DIR):
	mkdir -p $@
$(FRESH_OCAML_LINKS): $(FRESH_OCAML_LINKS_DIR)
	ln -sf $(FRESH_BIN_PREFIX)/fresh-`basename "$@"` $@

genlib: $(GENLIBOBJFILES)
	$(FCAMLCP) $(CAMLLINKFLAGS) -o $@ $+

$(RUNTEST): $(ACUTEOBJFILES) $(RUNTESTOBJFILES)
	$(FCAMLCP) $(CAMLLINKFLAGS) -o $@ $(LIBRARYLIBFILES) $(MAINLIBFILES) $+

# also depends on the tcp files
acute: $(ACUTEOBJFILES) acute.cmo
	$(FCAMLCP) $(CAMLLINKFLAGS) -o $@ $(LIBRARYLIBFILES) $(MAINLIBFILES) $+

%.ml : %.mlp
	$(CPP) -DACUTEVER=\"$(ACUTEVER)\" $< >$@
# also depends on the tcp files
library.ml : libfuncs.ml

# todo: maybe simplify the arguments here
library.cmo : library.ml library.cmi libfuncs.ml  eval.cmi basecon.cmi ast.cmi baseconty.cmi
	$(FCAMLC) -c $(COMPFLAGS)  $(LIBRARYLIBFILES) -impl $<

libfuncs.ml definitions_lib_auto.ac: genlib libs/tcp.types $(wildcard libs/*.mli wrappers/*.mli)
	./genlib definitions_lib_auto.ac libfuncs.ml definitions_lib_auto_doc.txt

MYLEXINGOBJFILES=mylexing.o

libmylexing.a : $(MYLEXINGOBJFILES)
	ar -crs $@ $+

%.o : %.c
	$(CC) -c $(CFLAGS) $<

mylexing.cma : libmylexing.a mylexing.cmo
	$(FCAMLCP) -a -o $@ -custom mylexing.cmo -cclib -lmylexing

#.SUFFIXES: .ml .mli .mll .mly
#showvars:
#	@echo MLLs = $(MLLs)
#	@echo MLYs = $(MLYs)
#	@echo LEXER_PARSER_GENERATED_FILES = $(LEXER_PARSER_GENERATED_FILES)


stem_of_mli_sources = $(MLI_SOURCES:.mli=) $(LEXER_PARSER_GENERATED_MLIS:.mli=)
stem_of_ml_sources = $(ML_SOURCES:.ml=) $(LEXER_PARSER_GENERATED_MLS:.ml=)
ml_without_mli_sources = $(filter-out $(stem_of_mli_sources),$(stem_of_ml_sources))
ml_and_mli_sources = $(filter $(stem_of_mli_sources),$(stem_of_ml_sources))
#mli_without_ml_sources =  $(filter-out $(stem_of_ml_sources),$(stem_of_mli_sources))

cmo_of_ml_and_mli_sources = $(ml_and_mli_sources:=.cmo)
cmi_of_mli_sources = $(stem_of_mli_sources:=.cmi)
cmo_of_ml_without_mli_sources = $(ml_without_mli_sources:=.cmo)
cmi_of_ml_without_mli_sources = $(ml_without_mli_sources:=.cmi)

fcompile = $(FCAMLCP) -c $(COMPFLAGS) $(COMPFLAGS_EXTRA) $(MAINLIBFILES)

$(cmo_of_ml_and_mli_sources) : %.cmo : %.ml %.cmi
	$(fcompile) $<

$(cmi_of_mli_sources) : %.cmi : %.mli
	$(fcompile) $<

$(cmi_of_ml_without_mli_sources) : %.cmi : %.ml
	$(fcompile) $<

$(cmo_of_ml_without_mli_sources) : %.cmo : %.ml
	$(fcompile) $<

# %.cmo: %.ml
# 	$(FCAMLCP) -c $(COMPFLAGS) $(COMPFLAGS_EXTRA) $(MAINLIBFILES) $<

# %.cmi: %.mli
# 	$(FCAMLCP) -c $(COMPFLAGS) $(COMPFLAGS_EXTRA) $(MAINLIBFILES) $<

# %.cmo %.cmi: %.ml %.mli
# 	$(FCAMLCP) -c $(COMPFLAGS) $(COMPFLAGS_EXTRA) $(MAINLIBFILES) $*.mli
# 	$(FCAMLCP) -c $(COMPFLAGS) $(COMPFLAGS_EXTRA) $(MAINLIBFILES) $*.ml

$(LEXER_HACK_SCRIPT) : $(LEXER_HACK_SCRIPT).cmo
	$(FCAMLC) -o $@ $(CAMLLINKFLAGS) $<

$(PARSER_HACK_SCRIPT_ML) : $(PARSER_HACK_SCRIPT_ML).cmo
	$(FCAMLC) -o $@ $(CAMLLINKFLAGS) $<

$(PARSER_HACK_SCRIPT_MLI) : $(PARSER_HACK_SCRIPT_MLI).cmo
	$(FCAMLC) -o $@ $(CAMLLINKFLAGS) $<

CLEANUP = rm -f $**-tmp*
REPLACETMP = perl -p -e 's/$*-tmp/$*/g;'

#$(LEXERS_ML)

%.ml : %.mll $(LEXER_HACK_SCRIPT)
        #  we do the ln to get around the -o bug in ocamllex
	( ln -f $*.mll $*-tmp.mll  && \
	$(FCAMLLEX) $*-tmp.mll   && \
	$(REPLACETMP) < $*-tmp.ml > $*.ml-tmp    && \
	./$(LEXER_HACK_SCRIPT) <$*.ml-tmp >$*.ml2-tmp && \
	mv -f $*.ml2-tmp $*.ml && \
	$(CLEANUP) ; )  || { EC="$$?" && $(CLEANUP) && exit "$$EC"; }

#$(PARSERS_ML_MLI_TOKML)

%.ml %.mli %_tok.ml : %.mly $(PARSER_HACK_SCRIPT_MLI) $(PARSER_HACK_SCRIPT_ML)
	@( set -e ; \
	COMMAND="$(FCAMLYACC) -b $*-tmp -v $*.mly"                ; \
	echo "$$COMMAND"                                          ; \
	YACCRC=0                                                  ; \
	{ YACCWARNINGS=`eval "$$COMMAND" 2>&1` || YACCRC="$$?"; } ; \
	echo "$$YACCWARNINGS" >&2                                 ; \
	if test z"$$YACCRC" \!= z0; then exit "$$YACCRC"; fi            ; \
	if echo "$$YACCWARNINGS" | grep -q "is undefined"; then exit 1; fi ; \
	mv -f $*-tmp.output $*.output                             ; \
        $(REPLACETMP) < $*-tmp.ml > $*.ml-tmp                     ; \
	$(REPLACETMP) < $*-tmp.mli > $*.mli-tmp                    ; \
	./$(PARSER_HACK_SCRIPT_ML) $*_tok < $*.ml-tmp >$*.ml2-tmp       ; \
	./$(PARSER_HACK_SCRIPT_MLI) $*_tok < $*.mli-tmp >$*.mli2-tmp    ; \
	mv -f $*.ml2-tmp $*.ml                                 ; \
	mv -f $*.mli2-tmp $*.mli                               ; \
	$(CLEANUP) ; )  || { EC="$$?" && $(CLEANUP) && exit "$$EC"; }

$(PARSERS_MLY) : $(PARSER_PREFIX)%.mly : $(PARSER_STEM).mlyp
	$(SIMPLEPP) -D $* $(PARSER_STEM).mlyp > $@-tmp
	mv $@-tmp $@

$(LEXERS_MLL) : $(LEXER_PREFIX)%.mll : $(LEXER_STEM).mllp
	$(SIMPLEPP) -D $* $(LEXER_STEM).mllp > $@-tmp
	mv $@-tmp $@

%.mly-y2l : %.mly
	cat $< | tr -d \' | grep -v '%start' | grep -v '%type' | sed -e 's/%prec [^ ]*//' > $@

%.tex : %.mly-y2l parser-tokens.y2l $(Y2L)
	$(Y2L) $< -O2 -tparser-tokens.y2l > $@

.PRECIOUS: %.mly-y2l


grammar.ps : grammar.tex parser_usersource.tex parser_compiled.tex
	$(LATEX) grammar.tex
	$(DVIPS) $(DVIPSOPTS) grammar.dvi

relicense : relicense.ml
	$(FCAMLC) -o $@ str.cma unix.cma $<

#
# re-create user configuration if missing:
#
freshocamldirs.mk :
	@echo ''
	@echo Re-creating $@ from template...
	@grep -v "^##" freshocamldirs.mk.in > $@
	@echo You may need to edit the just-created local freshocamldirs.mk,
	@echo to specify the paths to your FreshOCaml install, and then run make again.
	@echo ''
	@echo 'Please ignore the following "Error 42":'
	@exit 42  # force abort of make


blockhead:
	cd wrappers-ocaml && make
	cp -f blockhead.ac  tmp-blockhead.ml
	chmod ugo-w tmp-blockhead.ml
	ocamlc -o blockhead graphics.cma  -I wrappers-ocaml  wrappers-ocaml/agraphics.cmo tmp-blockhead.ml


wc :
	wc $(HACKY_WC_SOURCES)


.PHONY: permissions
permissions:
#	-@chmod g+w * .depend 2>/dev/null
#	-@chmod a+x twocaml
#	-@chmod a+x systest

.PHONY: depend
depend: .depend tcpdepend

DEPENDS = $(LEXER_PARSER_GENERATED_FILES) $(MLI_SOURCES) $(ML_SOURCES)
.depend: freshocamldirs.mk $(DEPENDS)
	$(FCDEP) $(DEPENDS) >$@-tmp
	mv $@-tmp $@

.PHONY: localclean
localclean:
	-rm -f .depend *.cmo *.cmi *.cma *.o *.a $(LEXER_PARSER_GENERATED_FILES) parser*.output $(PARSERS_MLY) $(LEXERS_MLL) $(LEXER_HACK_SCRIPT) $(PARSER_HACK_SCRIPT_MLI)  $(PARSER_HACK_SCRIPT_ML) genlib repl libfuncs.ml *-tmp* definitions_lib_auto.ac ocamlprof.dump acute $(RUNTEST)
	-rm -rf $(FRESH_OCAML_LINKS_DIR)
	-rm -f blockhead
	-rm -f library.ml
	-rm -f parser*.tex *.dvi grammar.ps *.mly-y2l
	-rm -f README.distros

.PHONY: clean
clean: tcp-clean wrappers-clean wrappers-ocaml-clean localclean editline-clean acute-src-clean acute-bin-clean


# we don't build try to include .depend (thereby forcing it to be built) when cleaning up
ifdef MAKECMDGOALS
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),tcp-clean)
ifneq ($(MAKECMDGOALS),localclean)
ifneq ($(MAKECMDGOALS),$(LEXER_HACK_SCRIPT))
ifneq ($(MAKECMDGOALS),$(PARSER_HACK_SCRIPT_ML))
ifneq ($(MAKECMDGOALS),$(PARSER_HACK_SCRIPT_MLI))
-include .depend
endif
endif
endif
endif
endif
endif
else
-include .depend
endif

tests-paper:
	./runtest_to_html.pl -r "-k" -f paper -d tests_html/


tests_sem:
	./runtest  -G "p11_even" > ../sem/paper_sem_examples/tmp_p11_even.ac
	./acute -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 ../sem/paper_sem_examples/tmp_p11_even.ac   > ../sem/paper_sem_examples/sem_p11_even.tex 2>&1
#
	./runtest  -G "p11_n" > ../sem/paper_sem_examples/tmp_p11_n.ac
	./acute -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0  -definitionslib definitions_lib.ac ../sem/paper_sem_examples/tmp_p11_n.ac   > ../sem/paper_sem_examples/sem_p11_n.tex 2>&1
#
	./runtest  -G "p12" > ../sem/paper_sem_examples/tmp_p12.ac
	./acute -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 ../sem/paper_sem_examples/tmp_p12.ac   > ../sem/paper_sem_examples/sem_p12.tex 2>&1
#
	./runtest  -G "p13a" > ../sem/paper_sem_examples/tmp_p13a.ac
	./acute -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 -norun ../sem/paper_sem_examples/tmp_p13a.ac   > ../sem/paper_sem_examples/sem_p13a.tex 2>&1
#
	./runtest  -G "p15" > ../sem/paper_sem_examples/src_p15.ac
	./acute -nonunitthread -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 -norun ../sem/paper_sem_examples/src_p15.ac   > ../sem/paper_sem_examples/sem_p15.tex 2>&1
#
	./runtest  -G "p_dchan3_common" > ../sem/paper_sem_examples/tmp_p_dchan3_common.ac
	./acute -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 ../sem/paper_sem_examples/tmp_p_dchan3_common.ac   > ../sem/paper_sem_examples/sem_p_dchan3_common.tex 2>&1
#
	./runtest  -G "p11_even_marshal_inside" > ../sem/paper_sem_examples/tmp_p11_even_marshal_inside.ac
	./acute -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 -norun ../sem/paper_sem_examples/tmp_p11_even_marshal_inside.ac   > ../sem/paper_sem_examples/sem_p11_even_marshal_inside.tex 2>&1
	./runtest  -G "p_dchan2_common" > ../sem/paper_sem_examples/tmp_p_dchan2_common.ac
	./acute -noshowpasses -noshowtimes  -dumptex -lithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 ../sem/paper_sem_examples/tmp_p_dchan2_common.ac   > ../sem/paper_sem_examples/sem_p_dchan2_common.tex 2>&1
#
# horrid plumbing hackery to get the includesource to work in the right place
	./runtest -t "p11_even"
	cp tests_db/p11_even.aco ../sem/paper_sem_examples/p11_even.aco
	./runtest  -G "pwith1" > ../sem/paper_sem_examples/tmp_pwith1.ac
	./acute -noshowpasses -noshowtimes  -dumptex -nolithash -norttc -nomttc -notypecheckcompiled -suffixall -dumpfinal 0 -norun ../sem/paper_sem_examples/tmp_pwith1.ac   > ../sem/paper_sem_examples/sem_pwith1.tex 2>&1
#
	./runtest -c "-marshaltex -hack_optimise" -t p1a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p1a.tex
	./runtest -c "-marshaltex -hack_optimise" -t p3a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p3a.tex
	./runtest -c "-marshaltex -hack_optimise" -t p4a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p4a.tex
	./runtest -c "-marshaltex -hack_optimise" -t p5a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p5a.tex
	./runtest -c "-marshaltex -hack_optimise" -t p6a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p6a.tex
	./runtest -c "-marshaltex -hack_optimise" -t p8a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p8a.tex
	./runtest -c "-marshaltex -hack_optimise" -t p10a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p10a.tex
	./runtest -c "-marshaltex -hack_optimise" -t p_thunkify1a && cp tmp-acute-store ../sem/paper_sem_examples/sem_mv_p_thunkify1a.tex

# DO NOT DELETE
