# prelude                             -*- makefile-gmake -*-
# GNUmakefile

# TODO DELETE the or
OCAMLMAKEFILE = $(or $(shell opam var ocaml-makefile:lib),.)/OCamlMakefile
ARCHIVE = ~/web/hg
ARCHIVE_EXCLUDES += TESTING
MAKEFLAGS += -j --no-print-directory

SOURCES = a1.ml a1.mli ia.ml ia.mli prelude_time.ml prelude.ml
RESULT = prelude
# these packages are only used in unit tests
OPAMS_TEST = ctypes {with-test} ctypes-foreign {with-test} ocamlbuild {with-test} ounit2 {with-test} qcheck {with-test} qtest {with-test}
OPAMS = camlp-streams $(OPAMS_TEST)
# LIB_PACK_NAME = $(RESULT)
PACKS = camlp-streams str unix
PACKS_TEST = $(filter-out ocamlbuild,$(shell erg pkg -n '$(PACKS) $(patsubst ctypes-foreign,ctypes.foreign,$(OPAMS_TEST))')) threads
ifeq ($(LIB_PACK_NAME),)
  LIBINSTALL_FILES = $(wildcard *.[oa]) $(wildcard *.cm*) $(sort $(SOURCES))
else
  LIBINSTALL_FILES = $(wildcard $(RESULT).[ao]) $(wildcard $(RESULT).cm*) $(sort $(SOURCES))
endif

.PHONY: default all
default: bcl
all: bcl ncl

.PHONY: install
install: libuninstall libinstall
.PHONY: uninstall
uninstall: libuninstall

TEST = test
$(TEST).ml: $(SOURCES)
	qtest -o $@ extract $^

OCAMLBUILD = ocamlbuild -use-ocamlfind -classic-display -j 8
TRASH += qtest.targets.log $(TEST) $(TEST).ml
.PHONY: check test
check test: clean $(TEST).ml
	$(OCAMLBUILD) $(patsubst %,-pkg %,$(PACKS)) -lflag -linkall $(RESULT).cmxa
	qtest -o $(TEST).ml extract a1.ml ia.ml prelude_time.ml prelude.ml
	$(OCAMLBUILD) -cflags -warn-error,+26 $(patsubst %,-pkg %,$(PACKS_TEST)) $(TEST).native
	./$(TEST).native
	$(OCAMLBUILD) -clean

clean::
	-$(OCAMLBUILD) -clean

# OCamlMakefile is sufficient to build and install prelude from opam
include $(OCAMLMAKEFILE)
# so no error if erg is not installed
# ifneq "$(shell opam var erg:lib || echo -n)" ""
#   -include $(shell opam var erg:lib)/Makefile.erg
# endif
# so we don't even ship this development makefile in the tarfile
# include Makefile.dldc
