#
# This file is part of Barista.
# Copyright (C) 2007-2014 Xavier Clerc.
#
# Barista is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Barista is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

include ../Makefile.config

CAMLP4_BYTE=$(PATH_OCAML_PREFIX)/bin/camlp4of
CAMLP4_NATIVE=$(PATH_OCAML_PREFIX)/bin/camlp4of.opt
CMO=barista_pp.cmo
CMXS=barista_pp.cmxs
PREPROCESSOR_BYTE=_build/src/$(CMO)
PREPROCESSOR_NATIVE=_build/src/$(CMXS)
OCAMLBUILD_ENV=WARNINGS=$(WARNINGS) PATH_OCAML_PREFIX=$(PATH_OCAML_PREFIX)
OCAMLBUILD=$(PATH_OCAML_PREFIX)/bin/ocamlbuild
OCAMLBUILD_FLAGS=-classic-display -no-links -pp "$(PATH_OCAML_PREFIX)/bin/camlp4of"

default: FORCE
	@echo 'Available targets:'
	@echo '  compile          compiles syntax extension'
	@echo '  test             runs all tests'
	@echo '  clean            deletes compiled and result files'

compile: FORCE
	$(OCAMLBUILD_ENV) $(OCAMLBUILD) $(OCAMLBUILD_FLAGS) $(CMO)
	$(OCAMLBUILD_ENV) $(OCAMLBUILD) $(OCAMLBUILD_FLAGS) $(CMXS)

test: FORCE
	@for file in `ls tests/*.ml tests/*.mli`; do \
	  echo "testing $$file (byte)"; \
	  $(CAMLP4_BYTE) $(PREPROCESSOR_BYTE) $$file -o $$file.result-byte; \
	  diff -q $$file.reference $$file.result-byte && rm $$file.result-byte || true; \
	  echo "testing $$file (native)"; \
	  $(CAMLP4_NATIVE) $(PREPROCESSOR_NATIVE) $$file -o $$file.result-native; \
	  diff -q $$file.reference $$file.result-native && rm $$file.result-native || true; \
	done

clean: FORCE
	rm -f tests/*.result-*
	$(OCAMLBUILD) $(OCAMLBUILD_FLAGS) -clean

FORCE:
