#######################################################################
# Makefile for Go test-suite
#######################################################################

# Use to print the commands 'go build' use during build
# GOBUILDDBG=-x
LANGUAGE	= go
GO		= @GO@
GOGCC		= @GOGCC@
GCCGO		= @GCCGO@
GOVERSIONOPTION	= @GOVERSIONOPTION@
host		= @host@
SCRIPTSUFFIX	= _runme.go
GO_CYGPATH = @GO_CYGPATH@

SO = @SO@

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@
abs_top_srcdir = @abs_top_srcdir@
BOOST_CPPFLAGS = @BOOST_CPPFLAGS@

CPP_TEST_CASES = \
	go_inout \
	go_director_inout

MULTI_CPP_TEST_CASES = \
	go_subdir_import

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

INCLUDES = -I$(abs_top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)

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

.SUFFIXES: .cpptest .ctest .multicpptest

# Rules for the different types of tests
%.cpptest:
	$(setup)
	+$(swig_and_compile_cpp) GOMOD="$*"
	$(run_testcase_cpp)

%.ctest:
	$(setup)
	+$(swig_and_compile_c) GOMOD="$*"
	$(run_testcase)

%.multicpptest:
	$(setup)
	+for f in `cat $(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$*.list` ; do \
	  n=`printf "$$f" | tr -d '\r\n\t '`; \
	  $(call swig_and_compile_cpp_helper,$${n},'$(SWIGOPT)') GOMOD="$*"; \
	done
	$(run_multi_testcase)

li_windows.cpptest:
	# Does not work because go build won't build li_windows.go,
	# because file names with "windows" are only built on Windows.

go_subdir_import.multicpptest:
	$(setup)
	mkdir -p testdir/go_subdir_import gopath/go_subdir_import/src/testdir/go_subdir_import
	$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' \
	SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
	LIBS='$(LIBS)' INTERFACEPATH='$(SRCDIR)$(INTERFACEDIR)go_subdir_import_b.i' \
	INCLUDES='$(INCLUDES)' SWIGOPT='$(SWIGOPT) -outdir .' NOLINK=true \
	TARGET='$(TARGETPREFIX)go_subdir_import_b$(TARGETSUFFIX)' INTERFACEDIR='$(INTERFACEDIR)' \
	INTERFACE='testdir/go_subdir_import/go_subdir_import_b.i' \
	GOMOD="go_subdir_import" \
	$(LANGUAGE)$(VARIANT)_cpp;
	+for f in testdir/go_subdir_import/go_subdir_import_c go_subdir_import_a ; do \
	  $(call swig_and_compile_cpp_helper,$${f},'$(SWIGOPT)') GOMOD="go_subdir_import"; \
	done
	$(run_multi_testcase)

# Runs the testcase.
run_testcase = \
	if test -f "$(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX)"; then \
	  GOPATH="`pwd`/gopath/$*"; \
	  includes="$(INCLUDES) -I `pwd` "; \
	  if test -n '$(SRCDIR)'; then \
	    includes="$$includes -I `cd '$(SRCDIR)' && pwd` "; \
	  fi; \
	  if test -n "$(GO_CYGPATH)"; then \
	    n_includes=''; \
	    for n in $${includes//-I}; do \
	      if test -d "$$n"; then \
	        n_includes="$$n_includes -I `$(GO_CYGPATH) -w $$n`"; \
	      else \
	        n_includes="$$n_includes $$n"; \
	      fi; \
	    done; \
	    includes="$$n_includes"; \
	  fi; \
	  CGO_CPPFLAGS="$(CPPFLAGS) $$includes"; \
	  CGO_CFLAGS="$(CFLAGS) $$includes"; \
	  CGO_LDFLAGS="$(LDFLAGS)"; \
	  export GOPATH CGO_CPPFLAGS CGO_CFLAGS CGO_LDFLAGS; \
	  mkdir -p "gopath/$*/src/swigtests/$*"; \
	  cp gopath/$*/src/$*/* gopath/$*/src/swigtests/$*/; \
	  mkdir -p "gopath/$*/src/$*/runme"; \
	  cp $(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) gopath/$*/src/$*/runme/runme.go; \
	  (cd gopath/$*/src/$*/runme && $(COMPILETOOL) $(GO) build $(GOBUILDDBG) `if $(GOGCC); then echo -compiler=gccgo; fi` -o ../../../../../$*_runme runme.go); \
	  env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) ./$*_runme; \
	fi

run_testcase_cpp = \
	if test -f $(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX); then \
	  GOPATH="`pwd`/gopath/$*"; \
	  includes="$(INCLUDES) -I `pwd` "; \
	  if test -n '$(SRCDIR)'; then \
	    includes="$$includes -I `cd '$(SRCDIR)' && pwd` "; \
	  fi; \
	  if test -n "$(GO_CYGPATH)"; then \
	    n_includes=''; \
	    includes="$$includes $(BOOST_CPPFLAGS)"; \
	    for n in $${includes//-I}; do \
	      if test -d "$$n"; then \
	        n_includes="$$n_includes -I `$(GO_CYGPATH) -w $$n`"; \
	      else \
	        n_includes="$$n_includes $$n"; \
	      fi; \
	    done; \
	    includes="$$n_includes"; \
	  fi; \
	  CGO_CPPFLAGS="$(CPPFLAGS) $$includes"; \
	  CGO_CXXFLAGS="$(CXXFLAGS) $$includes"; \
	  CGO_LDFLAGS="$(LDFLAGS)"; \
	  export GOPATH CGO_CPPFLAGS CGO_CXXFLAGS CGO_LDFLAGS; \
	  mkdir -p "gopath/$*/src/swigtests/$*"; \
	  cp gopath/$*/src/$*/* gopath/$*/src/swigtests/$*/; \
	  mkdir -p "gopath/$*/src/$*/runme"; \
	  cp $(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) gopath/$*/src/$*/runme/runme.go; \
	  (cd gopath/$*/src/$*/runme && $(COMPILETOOL) $(GO) build $(GOBUILDDBG) `if $(GOGCC); then echo -compiler=gccgo; fi` -o ../../../../../$*_runme runme.go); \
	  env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) ./$*_runme; \
	fi

run_multi_testcase = \
	if test -f $(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX); then \
	  mkdir -p "gopath/$*/src/$*"; \
	  cp "$(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX)" "gopath/$*/src/$*"; \
	  GOPATH="`pwd`/gopath/$*"; \
	  includes="$(INCLUDES) -I `pwd` "; \
	  if test -n '$(SRCDIR)'; then \
	    includes="$$includes -I `cd '$(SRCDIR)' && pwd` "; \
	  fi; \
	  if test -n "$(GO_CYGPATH)"; then \
	    n_includes=''; \
	    includes="$$includes $(BOOST_CPPFLAGS)"; \
	    for n in $${includes//-I}; do \
	      if test -d "$$n"; then \
	        n_includes="$$n_includes -I `$(GO_CYGPATH) -w $$n`"; \
	      else \
	        n_includes="$$n_includes $$n"; \
	      fi; \
	    done; \
	    includes="$$n_includes"; \
	  fi; \
	  CGO_CPPFLAGS="$(CPPFLAGS) $$includes"; \
	  CGO_CXXFLAGS="$(CXXFLAGS) $$includes"; \
	  CGO_LDFLAGS="$(LDFLAGS)"; \
	  export GOPATH CGO_CPPFLAGS CGO_CXXFLAGS CGO_LDFLAGS; \
	  mkdir -p "gopath/$*/src/swigtests/$*"; \
	  cp gopath/$*/src/$*/* gopath/$*/src/swigtests/$*/; \
	  (cd gopath/$*/src/$* && \
	    $(COMPILETOOL) $(GO) build $(GOBUILDDBG) `if $(GOGCC); then echo -compiler=gccgo; fi` -o ../../../../$*_runme) && \
	  env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH $(RUNTOOL) ./$*_runme; \
	fi

%.clean:
	@rm -rf $*.go $*_gc.c $*_wrap.* $*_runme $*.gox $*.a

clean:
	$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile SRCDIR='$(SRCDIR)' go_clean
	rm -f clientdata_prop_a.go clientdata_prop_a.gox
	rm -f clientdata_prop_b.go clientdata_prop_b.gox
	rm -f import_share_a.go import_share_a.gox
	rm -f import_share_b.go import_share_b.gox
	rm -f import_share_c.go import_share_c.gox
	rm -f import_stl_a.go import_stl_a.gox
	rm -f import_stl_b.go import_stl_b.gox
	rm -f import_stl_c.go import_stl_c.gox
	rm -f imports_a.go imports_a.gox imports_b.go imports_b.gox
	rm -f mod_a.go mod_a.gox mod_b.go mod_b.gox
	rm -f multi_import_a.go multi_import_a.gox
	rm -f multi_import_b.go multi_import_b.gox
	rm -f multi_import_d.go multi_import_d.gox
	rm -f packageoption_a.go packageoption_a.gox
	rm -f packageoption_b.go packageoption_b.gox
	rm -f packageoption_c.go packageoption_c.gox
	rm -f template_typedef_cplx2.go template_typedef_cplx2.gox
	rm -rf go_subdir_import_a.go go_subdir_import_a.gox testdir
	rm -rf gopath
