#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)
DPKG_EXPORT_BUILDFLAGS = 1
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Disabling tbb on s390x until Debian bug #1009361 is fixed.
ifeq ($(shell dpkg --print-architecture),s390x)
    with_tbb := -DUSE_TBB:BOOL=OFF
else
    with_tbb := -DUSE_TBB:BOOL=ON
endif

# FunctionalChaos_ishigami_sparse (cpp) segfaults on armel for now.
# TensorApproximationAlgorithm_std (cpp) segfaults on armel for now.
# DistributionFactory_std fails on armel (python) for now.
# KrigingAlgorithm_std fails on armel (python) for now.
# MetaModelValidation_std fails on armel (python) for now.
ifeq ($(shell dpkg --print-architecture),armel)
    test_discardflags := -E \\\"FunctionalChaos_ishigami_sparse|TensorApproximationAlgorithm_std|DistributionFactory_std|KrigingAlgorithm_std|MetaModelValidation_std\\\"
endif

# DistributionFactory_std fails on armhf (cpp) for now.
ifeq ($(shell dpkg --print-architecture),armhf)
    test_discardflags := -E DistributionFactory_std
endif

# DistributionFactory_std fails on arm64 (python) for now.
# SimulatedAnnealingLHS_std fails on arm64 (python) for now.
ifeq ($(shell dpkg --print-architecture),arm64)
    test_discardflags := -E \\\"DistributionFactory_std|SimulatedAnnealingLHS_std\\\"
endif

# SimulatedAnnealingLHS_std fails on ppc64el (python) for now.
ifeq ($(shell dpkg --print-architecture),ppc64el)
    test_discardflags := -E SimulatedAnnealingLHS_std
endif

# SimulatedAnnealingLHS_std fails on s390x (python) for now.
ifeq ($(shell dpkg --print-architecture),s390x)
    test_discardflags := -E SimulatedAnnealingLHS_std
endif

# SimulatedAnnealingLHS_std fails on powerpc (python) for now.
ifeq ($(shell dpkg --print-architecture),powerpc)
    test_discardflags := -E SimulatedAnnealingLHS_std
endif

# SimulatedAnnealingLHS_std fails on ppc64 (python) for now.
ifeq ($(shell dpkg --print-architecture),ppc64)
    test_discardflags := -E SimulatedAnnealingLHS_std
endif

# DistributionFactory_std fails on mips64el (python) for now.
ifeq ($(shell dpkg --print-architecture),mips64el)
    test_discardflags := -E DistributionFactory_std
endif

# For now, on i386, there is a freeze when we launch the cpp tests Cobyla_std to PostAnalyticalImportanceSampling_std.
# Same issue for the Python tests EfficientGlobalOptimization to Waarts_saddle.
# Failure of the cpp tests CovarianceModel_std to KrigingAlgorithm_funcmodel.
# Failure of the Python tests BernsteinCopulaFActory_std to AdaptiveDirectionalStratification_std.
ifeq ($(shell dpkg --print-architecture),i386)
    test_discardflags := -E \\\"Cobyla_std|FORM_sensitivity|WhittleFactory_std|ARMALikelihoodFactory_std|PostAnalyticalControlledImportanceSampling_std|PostAnalyticalImportanceSampling_std|EfficientGlobalOptimization_std|InverseRosenblattEvaluation_form|FORM_interval|MultiFORM_std|Waarts_25_quadratic_terms|Waarts_system_series|Waarts_saddle|CovarianceModel_std|BernsteinCopulaFactory_std|GeneralizedPareto_std|LinearLeastSquaresCalibration_std|NonLinearLeastSquaresCalibration_std|LinearLeastSquaresCalibration_noobs|NonLinearLeastSquaresCalibration_noobs|KrigingAlgorithm_funcmodel|BernsteinCopulaFactory_std|GeneralizedPareto_std|OrdinalSumCopula_std|Pareto_std|LinearLeastSquaresCalibration_std|NonLinearLeastSquaresCalibration_std|LinearLeastSquaresCalibration_linear|LinearLeastSquaresCalibration_noobs|NonLinearLeastSquaresCalibration_noobs|SimulatedAnnealingLHS_std|KrigingAlgorithm_funcmodel|AdaptiveDirectionalStratification_std\\\"
endif

PY3VER = $(shell py3versions -d)
PYALL = $(PY3VER)

BUILD_DATE = $(shell date --utc --date="@$(SOURCE_DATE_EPOCH)" "+%a, %d %b %Y %H:%M:%S %z")

%:
	dh $@ --buildsystem=cmake --with python3

override_dh_auto_configure: $(PYALL:python%=cmake-configure-%) \

cmake-configure-%:
	dh_auto_configure -Bbuild-python$* -- \
            $(with_tbb) \
            -Dot_configure_date:STRING='$(BUILD_DATE)' \
            -DUSE_SPHINX:BOOL=OFF \
            -DCMAKE_SKIP_INSTALL_RPATH:BOOL=ON \
            -DCMAKE_INSTALL_PREFIX:PATH=/usr \
            -DCMAKE_INSTALL_LIBDIR:PATH="/usr/lib/$(DEB_HOST_MULTIARCH)" \
            -DINSTALL_DESTDIR:PATH=$(CURDIR)/debian/tmp \
            -DUSE_COTIRE=ON -DCOTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES:STRING='-j32' \
            -DSWIG_COMPILE_FLAGS:STRING='-O1' \
            -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python$* \
            -DPYTHON_INCLUDE_DIR:PATH=$$(python$*-config --includes | sed -e 's/ .*//' -e 's/^-I//') \
            -DPYTHON_LIBRARY:PATH=$$(python$*-config --ldflags | sed -e 's/ .*//' -e 's/^-L//')/libpython$*.so

override_dh_auto_build: $(PYALL:python%=cmake-build-%)

cmake-build-%:
	$(MAKE) -C build-python$*/lib
	$(MAKE) -j1 -C build-python$*/python
	$(MAKE) -C build-python$*

override_dh_auto_install: $(PYALL:python%=cmake-install-%)

cmake-install-%:
	dh_auto_install -Bbuild-python$*
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
ifneq ($(shell dpkg --print-architecture),armel)
	eval "LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$${LD_LIBRARY_PATH}:}$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
	  CTEST_OUTPUT_ON_FAILURE=1 \
	  $(MAKE) -C build-python$* test ARGS=\"$(test_discardflags) -R pyinstallcheck\""
endif
endif

override_dh_auto_test: $(PYALL:python%=cmake-test-%)

cmake-test-%:
	# 'make test' does not build binary tests
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C build-python$* tests
	eval "CTEST_OUTPUT_ON_FAILURE=1 \
	  $(MAKE) -C build-python$* test ARGS=\"$(test_discardflags) -R cppcheck\""
endif

override_dh_installexamples-indep:
	dh_installexamples -i
	# Fixing missing interpreter in some Python scripts to comply with Debian Python policy.
	# Also substituting /usr/bin/env python with /usr/bin/python3 in shebangs, to comply with the policy.
	for F in $$(find debian/openturns-examples/usr/share/doc/openturns-examples/ -name "*.py"); do \
	        if ! grep -q '#!.*python' $$F; then \
	                sed -i '1s,^,\#!/usr/bin/python3\n,' $$F; \
	        elif grep -q '#!/usr/bin/env.*python' $$F; then \
	                sed -i 's,/usr/bin/.*$$,/usr/bin/python3,' $$F; \
	        fi; \
	done
