#!/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)

# Getting the number of jobs for the Debian build to transmit it to CMake.
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	test_makeflags = -j$(NUMJOBS)
endif

# 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

# Discarding the following flaky tests on armel.
ifeq ($(shell dpkg --print-architecture),armel)
    test_discardflags := -E \\\"FunctionalChaos_.*|KrigingAlgorithm_std|MetaModelValidation_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_SITE_PACKAGES:PATH="lib/$(PY3VER)/site-packages" \
            -DPYTHON_LIBRARY:PATH=$$(python$*-config --ldflags | sed -e 's/ .*//' -e 's/^-L//')/libpython$*.so
	# Also configuring a build for a static library.
	dh_auto_configure -Bbuild-static -- \
            $(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' \
            -DBUILD_PYTHON:BOOL=OFF \
            -DBUILD_SHARED_LIBS:BOOL=OFF

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

cmake-build-%:
	$(MAKE) $(test_makeflags) -C build-python$*/lib
	$(MAKE) -j1 -C build-python$*/python
	$(MAKE) $(test_makeflags) -C build-python$*
	# Building the static library.
	$(MAKE) $(test_makeflags) -C build-static/lib

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

cmake-install-%:
	dh_auto_install -Bbuild-python$*
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	eval "LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$${LD_LIBRARY_PATH}:}$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
	  CTEST_OUTPUT_ON_FAILURE=1 \
	  $(MAKE) $(test_makeflags) -C build-python$* test ARGS=\"$(test_makeflags) $(test_discardflags) -R pyinstallcheck\""
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) $(test_makeflags) -C build-python$* tests
	eval "CTEST_OUTPUT_ON_FAILURE=1 \
	  $(MAKE) -C build-python$* test ARGS=\"$(test_makeflags) $(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
