#######################################################################
# Makefile for ocaml test-suite
#######################################################################

LANGUAGE     = ocaml
OCAMLP4WHERE =`$(COMPILETOOL) @CAMLP4@ -where`
OCC          =$(COMPILETOOL) @OCAMLC@
OCAMLPP      = -pp "camlp4o ./swigp4.cmo"
VARIANT      = _static
SCRIPTSUFFIX = _runme.ml

HAVE_CXX11   = @HAVE_CXX11@
HAVE_CXX14   = @HAVE_CXX14@
HAVE_CXX17   = @HAVE_CXX17@
HAVE_CXX20   = @HAVE_CXX20@
srcdir       = @srcdir@
top_srcdir   = @top_srcdir@
top_builddir = @top_builddir@

FAILING_CPP_TESTS = \
allprotected \
apply_strings \
cpp11_decltype \
cpp11_director_enums \
cpp11_strongly_typed_enumerations \
cpp_enum \
default_constructor \
director_binary_string \
director_binary_string_rev_len \
director_comparison_operators \
director_enum \
director_primitives \
director_string \
director_using_member_scopes \
enum_thorough \
smart_pointer_inherit \

FAILING_C_TESTS = \
enums \

C_TEST_CASES += \
	multivalue \

ml_runme = $(SCRIPTPREFIX)$*$(SCRIPTSUFFIX)

run_testcase = \
	if [ -f $(srcdir)/$(ml_runme) ]; then \
		if [ $(srcdir) != . ]; then \
			cp $(srcdir)/$(ml_runme) $(ml_runme); \
		fi ; \
		$(OCC) $(OCAMLPP) -c $(ml_runme) && \
		if [ -f $(top_srcdir)/Examples/test-suite/$*.list ]; then \
			$(OCC) swig.cmo -custom -g -cc '$(CXX)' -o $*_runme `cat $(top_srcdir)/Examples/test-suite/$(*).list | sed -e 's/\(.*\)/\1_wrap.o \1.cmo/g'`&& $(RUNTOOL) ./$*_runme; \
		else \
			$(OCC) swig.cmo -custom -g -cc '$(CXX)' -o $*_runme $(*).cmo $(*)_runme.cmo $(*)_wrap.o && $(RUNTOOL) ./$*_runme; \
		fi ; \
	fi ;

include $(srcdir)/../common.mk

# Overridden variables here

# Ensure testsuite remains free from SWIG warnings.
SWIGOPT += -Werror

SWIGOPT += -w524 # Suppress SWIGWARN_LANG_EXPERIMENTAL warning

# Custom tests - tests with additional commandline options
# none!

# Rules for the different types of tests
%.cpptest:
	$(setup)
	+$(swig_and_compile_cpp)
	$(run_testcase)

%.ctest:
	$(setup)
	+$(swig_and_compile_c)
	$(run_testcase)

%.multicpptest:
	+$(swig_and_compile_multi_cpp)
	$(setup)
	$(run_testcase)

swig.cmi:
	env SWIG_LIB=$(SWIG_LIB_DIR) $(SWIGEXE) -ocaml -co swig.mli
	$(OCC) -c swig.mli
swig.cmo:
	env SWIG_LIB=$(SWIG_LIB_DIR) $(SWIGEXE) -ocaml -co swig.ml
	$(OCC) -c swig.ml
swigp4.cmi:
	env SWIG_LIB=$(SWIG_LIB_DIR) $(SWIGEXE) -ocaml -co swigp4.ml
	$(OCC) -I $(OCAMLP4WHERE) -pp "camlp4o pa_extend.cmo q_MLast.cmo" -c swigp4.ml
extra_objects: swig.cmi swig.cmo swigp4.cmi

$(C_TEST_CASES:=.ctest): extra_objects
$(CPP_TEST_CASES:=.cpptest): extra_objects
$(CPP11_TEST_CASES:=.cpptest): extra_objects
$(MULTI_CPP_TEST_CASES:=.multicpptest): extra_objects

# Clean
%.clean:
	@rm -f $*_wrap.* $*.cmo $*.cmi $*.ml $*.mli $*_runme;
	@if test $(srcdir) != .; then rm -f $(ml_runme); fi

clean:
	$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile SRCDIR='$(SRCDIR)' ocaml_clean
	rm -f clientdata_prop_a.ml clientdata_prop_b.ml
	rm -f import_stl_a.ml import_stl_b.ml
	rm -f imports_a.ml imports_b.ml
	rm -f mod_a.ml mod_b.ml
	rm -f multi_import_a.ml multi_import_b.ml multi_import_d.ml
	rm -f packageoption_a.ml packageoption_b.ml packageoption_c.ml
	rm -f template_typedef_cplx2.ml
