.PHONY: all install uninstall reinstall clean doc test bench

OCAMLBUILD := ocamlbuild -use-ocamlfind -classic-display
OCAMLFIND  := ocamlfind
DOCDIR     := doc
MAIN       := PPrintTest
TO_BUILD   := PPrintLib.cma PPrintLib.cmxa

all:
	$(OCAMLBUILD) $(TO_BUILD)

install: all
	$(OCAMLFIND) install pprint META \
		$(patsubst %,_build/%,$(TO_BUILD)) \
		_build/PPrintLib.a _build/*.cmx _build/*.cmi

# [make uninstall] attempts to uninstall, but succeeds even if uninstallation
# fails (probably because the package was not installed in the first place).
uninstall:
	ocamlfind remove pprint || true

reinstall: uninstall
	@ $(MAKE) install

clean:
	rm -f *~
	rm -rf doc
	$(OCAMLBUILD) -clean

doc: all
	@rm -rf $(DOCDIR)
	@mkdir $(DOCDIR)
	ocamlfind ocamldoc \
	  -html \
	  -I _build \
	  -d $(DOCDIR) \
	  -charset utf8 \
	  PPrintRenderer.ml PPrintEngine.mli PPrintCombinators.mli PPrintOCaml.mli PPrint.ml

test: all
	$(OCAMLBUILD) $(MAIN).native
	./$(MAIN).native

bench: all
	$(OCAMLBUILD) -tag use_unix PPrintBench.native
	time ./PPrintBench.native
