#!/usr/bin/make -f

G = debian/generated
B = debian/.dh-scala
J = /usr/share/java
TCP = $(J)/sbt-test-interface.jar:$(J)/commons-lang3.jar
TESTS = core/jvm/src/test/scala core/shared/src/test/scala \
	core/shared/src/test/scala-2.13+ debian/tests/RunTests.scala

%:
	dh $@ --buildsystem=scala

# Upstream's sbt build generates the tuple and function arities.
execute_before_dh_auto_configure:
	mkdir -p $(G)/classes
	scalac-2.13 -d $(G)/classes project/codegen.scala debian/codegen.scala
	java -cp $(J)/scala-library-2.13.jar:$(G)/classes DebianCodegen $(G)
	rm -rf $(G)/classes

# Upstream's tests, compiled here and not as scala-build stanzas, which
# would need commons-lang3 under the nocheck profile too.
# $(1): Scala version, $(2): its options from sbt-typelevel.
define run_tests
	mkdir -p $(B)/tests_$(1)
	find $(TESTS) -name '*.scala' | LC_ALL=C sort > $(B)/tests_$(1).files
	scalac-$(1) $(2) -language:implicitConversions -d $(B)/tests_$(1) \
		-cp $(B)/scalacheck_$(1)/classes:$(TCP) @$(B)/tests_$(1).files
	java -cp $(J)/scala-library-$(1).jar:$(B)/scalacheck_$(1)/classes:$(TCP):$(B)/tests_$(1) \
		RunTests $(B)/tests_$(1)
endef

execute_after_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(call run_tests,2.13,-Xsource:3 -Wconf:cat=scala3-migration:s)
	$(call run_tests,3,-Xkind-projector)
endif
