# Edit and uncomment the following line
OCAMLLEX_SRC = ~/lex308

# The directory where the wlex binary goes
INSTALL_BIN  = `which ocamlc | xargs dirname`

PACKAGE_NAME = wlexing
LIB = wlexing


all: wlex runtime
all.opt: wlex runtime.opt

wlex: wlex.patch
	( \
	rm -Rf wlex.src; \
	cp -R $(OCAMLLEX_SRC) wlex.src; \
	cd wlex.src; \
	make clean; \
	if gpatch </dev/null; then \
	  gpatch -Np1 < ../wlex.patch; \
	else \
	  patch -Np1 < ../wlex.patch; \
	fi; \
	make ocamllex CAMLC=ocamlc CAMLYACC=ocamlyacc CAMLLEX=ocamllex; \
	cp ocamllex ../wlex)


install_wlex: wlex
	cp wlex $(INSTALL_BIN)/wlex

uninstall_wlex:
	rm -f $(INSTALL_BIN)/wlex

OBJECTS = lex_engines.cmo
XOBJECTS = lex_engines.cmx
COBJECTS = wlexing_c.o
INTERF = lex_engines.cmi

lex_engines.cmo: lex_engines.cmi
lex_engines.cmx: lex_engines.cmi

LIB_OBJECTS = $(OBJECTS) $(XOBJECTS) $(COBJECTS)

lib$(LIB).a: $(COBJECTS)
	ocamlmklib $(OCAMLMKLIB_OPTS) -verbose -o $(LIB) $(COBJECTS)

$(LIB).cma: $(OBJECTS)
	ocamlc -a -o $(LIB).cma -dllib -l$(LIB) -cclib -l$(LIB) $(OBJECTS)

$(LIB).cmxa: $(XOBJECTS)
	ocamlopt -a -o $(LIB).cmxa -cclib -l$(LIB) $(XOBJECTS)


runtime: $(INTERF) lib$(LIB).a $(LIB).cma
runtime.opt: $(INTERF) lib$(LIB).a $(LIB).cmxa
runtime.all: runtime runtime.opt

install_runtime:
	@test -f wlexing.cma -o -f wlexing.cmxa || (echo "Do 'make runtime' , 'make runtime.opt' or 'make runtime.all' first"; exit 2)
	ocamlfind install $(PACKAGE_NAME) META $(wildcard *.cmi) $(wildcard *.a) $(wildcard *.so) $(wildcard *.cma) $(wildcard *.cmxa)

uninstall_runtime:
	ocamlfind remove wlexing

install: install_wlex install_runtime

uninstall: uninstall_wlex uninstall_runtime

tester: runtime wlex
	(cd test; make)

#create_patch:
#	LC_ALL=C TZ=UTC0 diff -Naur lex wlex.src > wlex.patch; true

clean:
	rm -f *.cmi *.cmo *.cmx *.a *.o *.cma *.cmxa *.so *~
	rm -Rf wlex.src wlex
	(cd test; make clean)

.SUFFIXES: .cmo .cmi .cmx .ml .mli .c .o

.ml.cmo:
	ocamlc -c $<

.ml.cmx:
	ocamlopt -inline 40 -c $<

.mli.cmi:
	ocamlc -c $<

.c.o:
	ocamlc -verbose -c $<

