#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

DEB_SOURCE_PACKAGE := $(strip $(shell egrep '^Source: ' debian/control | cut -f 2 -d ':'))
DEB_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')
DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -e 's/\+dfsg.*$$//p')

FENICS_RELEASE_VERSION=$(DEB_STRIPPED_UPSTREAM_VERSION)
FENICS_MAJOR_VERSION=$(shell echo $(FENICS_RELEASE_VERSION) | sed "s/^\([^.]*\)\..*$$/\1/")
FENICS_MINOR_VERSION=$(shell echo $(FENICS_RELEASE_VERSION) | sed "s/^\([^.]*\)\.\([^.]*\)\..*$$/\2/")
FENICS_VERSION=$(FENICS_MAJOR_VERSION).$(FENICS_MINOR_VERSION)
FENICS_NEXT_VERSION=$(FENICS_MAJOR_VERSION).$(shell echo $$(( $(FENICS_MINOR_VERSION) + 1 )) )

# dolfin depends on the pybind11 version it was built against,
# if pybind11.h will be used in C++ code fragments in python scripts.
# But pybind11 follows semantic versioning, so should have backwards compatibility
# with minor and patch version updates.
# Extract pybind11 version from pybind11-dev
PYBIND11_DEB_VERSION=$(shell dpkg -s pybind11-dev | awk '/Version:/ {print $$2}')
# extract the current pybind11 version X.Y.Z (drop epoch and debian package version)
PYBIND11_UPSTREAM_VERSION=$(shell echo $(PYBIND11_DEB_VERSION) | sed "s/^.[^:]*://; s/-[^-]*$$//")
PYBIND11_X_VERSION=$(shell echo $(PYBIND11_UPSTREAM_VERSION) | sed "s/^\([^.]*\).*/\1/")
PYBIND11_X_Y_VERSION=$(shell echo $(PYBIND11_UPSTREAM_VERSION) | sed "s/^\(.*\)\.\([^.]*\)$$/\1/")
PYBIND11_VERSION=$(PYBIND11_X_Y_VERSION)
PYBIND11_NEXT_VERSION=$(shell echo $$(( $(PYBIND11_X_VERSION) + 1 )) )

# Allow test programs that use OpenMPI to run
export OMPI_MCA_plm_rsh_agent=/bin/false
export OMPI_MCA_btl_base_warn_component_unused=0

include /usr/share/mpi-default-dev/debian_defaults
ENABLE_MPI=ON
ifeq ($(findstring $(DEB_BUILD_ARCH),$(OPENMPI_ARCHITECTURES)),)
MPIEXEC_PARAMS=
else
MPIEXEC_PARAMS=--oversubscribe
endif

DOLFIN_HOME = $(CURDIR)/$(DEB_SRCDIR)
USCAN_DESTDIR := $(CURDIR)
PY3VERS := $(shell py3versions --requested debian/control | tac -s' ')
PYVERS = $(PY3VERS)

BUILDDIR = obj-$(DEB_HOST_GNU_TYPE)
BUILDDIR_64 = obj-$(DEB_HOST_GNU_TYPE)-64
TESTDIR = build-tests
TESTDIR_64 = build-tests-64

NPROC := $(shell nproc)
PARALLEL = $(subst parallel=,,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
N_CPU    = $(or $(PARALLEL),$(NPROC),1)
# MPI tests are set up to run on 3 processes. Spread them leanly over the available processors (don't have more than 2 tests using one processor).
# e.g. run 1 MPI test at a time over 1-3 processors, or 2 tests at a time over 4-6 processors, or 3 tests over 7-9 processors, etc.
N_MPI := 3
N_MPI_TESTS = $(shell echo $$(( ($(N_CPU)+$(N_MPI)-1)/$(N_MPI) )) )

# some tests run slow (> 1000 sec) on all arches
ARCH_ALL_SKIP_SERIAL_TEST = demo_hyperelasticity_serial demo_cahn-hilliard_serial

# hyperelasticity fails (diverges) on all arches (pivot factors too large, not enough memory assigned)
# elastodynamics passes with dpkg-buildpackage but fails with pdebuild (on amd64). Unreliable, so skip.
ARCH_ALL_SKIP_MPI_TEST = demo_hyperelasticity_mpi demo_elastodynamics_mpi

# Some arches fail the same MPI tests
# e.g. cahn-hilliard, navier-stokes, elasticity, fails or exceeds allowed time
# So skip these tests on the failing arches
ARCH_SKIP_MPI_TEST_LIST = i386 mipsel ppc64el ia64
ARCH_SKIP_MPI_TEST = demo_cahn-hilliard_mpi demo_navier-stokes_mpi demo_elasticity_mpi

# skip more MPI tests for some specific arches
ifeq (arm64, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_MPI_TEST = demo_navier-stokes_mpi
else ifeq (mips, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_MPI_TEST = demo_cahn-hilliard_mpi demo_stokes-iterative_mpi demo_ale_mpi demo_poisson-disc_mpi
else ifeq (mipsel, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_MPI_TEST = demo_stokes-iterative_mpi demo_ale_mpi demo_poisson-disc_mpi
else ifeq (ppc64el, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_MPI_TEST = demo_poisson_mpi demo_singular-poisson_mpi demo_sym-dirichlet-bc_mpi
else ifeq (ia64, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_MPI_TEST = demo_bcs_mpi demo_biharmonic_mpi demo_built-in-meshes_mpi demo_eigenvalue_mpi
else ifeq (sparc64, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_MPI_TEST = demo_contact-vi-snes_mpi
else
  ARCH_SPECIFIC_SKIP_MPI_TEST =
endif

# m68k,mips,sparc64 need some help to pass serial tests
ifeq (m68k, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_SERIAL_TEST = demo_auto-adaptive-poisson_serial demo_elasticity_serial
else ifeq (mips, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_SERIAL_TEST = demo_cahn-hilliard_serial demo_hyperelasticity_serial
else ifeq (mipsel, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_SERIAL_TEST = demo_elastodynamics_serial demo_sym-dirichlet-bc_serial
else ifeq (sparc64, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_SERIAL_TEST = demo_contact-vi-snes_serial
else
  ARCH_SPECIFIC_SKIP_SERIAL_TEST =
endif


# skip slow tests (> 500-1000 sec to complete)
ifeq (armel, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_stokes-iterative_serial demo_auto-adaptive-navier-stokes_serial demo_elastodynamics_serial demo_sym-dirichlet-bc_serial demo_hyperelasticity_serial demo_cahn-hilliard_serial
else ifeq (armhf, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_elastodynamics_serial demo_sym-dirichlet-bc_serial demo_hyperelasticity_serial
else ifeq (mips, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_eigenvalue_serial demo_navier-stokes_serial demo_auto-adaptive-poisson_serial demo_stokes-taylor-hood_serial demo_auto-adaptive-navier-stokes_serial demo_curl-curl_serial demo_elasticity_serial demo_stokes-iterative_serial demo_eval_serial demo_sym-dirichlet-bc_serial demo_elastodynamics_serial demo_eigenvalue_mpi demo_navier-stokes_mpi demo_curl-curl_mpi demo_elasticity_mpi demo_elastodynamics_mpi demo_sym-dirichlet-bc_mpi
else ifeq (mipsel, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_cahn-hilliard_serial demo_hyperelasticity_serial
else ifeq (alpha, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_elastodynamics_serial demo_sym-dirichlet-bc_serial demo_hyperelasticity_serial
else ifeq (hppa, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_cahn-hilliard_serial demo_hyperelasticity_serial
else ifeq (m68k, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_cahn-hilliard_serial demo_hyperelasticity_serial
else ifeq (riscv64, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_elastodynamics_serial demo_sym-dirichlet-bc_serial demo_cahn-hilliard_serial demo_hyperelasticity_serial demo_cahn-hilliard_mpi demo_elastodynamics_mpi
else ifeq (sparc64, $(DEB_HOST_ARCH))
  ARCH_SKIP_SLOW_TEST = demo_cahn-hilliard_serial demo_hyperelasticity_serial demo_stokes-iterative_serial demo_cahn-hilliard_mpi demo_elastodynamics_mpi
else
  ARCH_SKIP_SLOW_TEST =
endif

empty :=
space := $(empty)$(empty)

ARCH32_LIST = armel armhf i386 mipsel powerpc x32

ARCH_SPECIFIC_SKIP_SERIAL_TEST_64 = $(ARCH_SPECIFIC_SKIP_SERIAL_TEST) demo_stokes-iterative_serial demo_curl-curl_serial
ARCH_SPECIFIC_SKIP_MPI_TEST_64 = $(ARCH_SPECIFIC_SKIP_MPI_TEST)

ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH32_LIST)$(space)))
  # these 4 tests fail in 64-bit build tests on 32-bit arches
  ARCH_SPECIFIC_SKIP_SERIAL_TEST_64 += demo_mixed-poisson_serial demo_stokes-taylor-hood_serial demo_auto-adaptive-navier-stokes_serial demo_multimesh-stokes_serial
endif
ifeq (armel, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_SERIAL_TEST_64 += demo_contact-vi-snes_serial
else ifeq (armhf, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_SERIAL_TEST_64 += demo_contact-vi-snes_serial
else ifeq (mipsel, $(DEB_HOST_ARCH))
  ARCH_SPECIFIC_SKIP_SERIAL_TEST_64 += demo_contact-vi-snes_serial demo_sym-dirichlet-bc_serial
endif

# other arches fail or time out on too many tests, so skip all their MPI tests
ARCH_SKIP_ALL_MPI_TESTS_LIST=armel armhf i386 hppa m68k powerpc

# some arches just aren't keeping up at all, and fail non-MPI tests
ARCH_SKIP_ALL_TESTS_LIST = mips64el hurd-i386 m68k sh4

# extract PETSc version from petsc-dev
PETSC_DEB_VERSION=$(shell dpkg -s petsc-dev | awk '/Version:/ {print $2}')
# extract the current PETSc version
PETSC_UPSTREAM_VERSION=$(shell pkg-config --modversion PETSc)
# "Major" version is the first number in the upstream version (major.minor.release)
PETSC_MAJOR_VERSION=$(shell echo $(PETSC_UPSTREAM_VERSION) | sed "s/^\([^.]*\)\..*$$/\1/")
# "Minor" version is the second number in the upstream version (major.minor.release)
PETSC_MINOR_VERSION=$(shell echo $(PETSC_UPSTREAM_VERSION) | sed "s/^\([^.]*\)\.\([^.]*\)\..*$$/\2/")
PETSC_VERSION=$(PETSC_MAJOR_VERSION).$(PETSC_MINOR_VERSION)
PETSC_VERSION_NEXT=$(shell echo $(PETSC_MAJOR_VERSION).$$(($(PETSC_MINOR_VERSION)+1)))

SLEPC_UPSTREAM_VERSION=$(shell pkg-config --modversion SLEPc)
# SLEPc version must match PETSc
SLEPC_VERSION=$(PETSC_VERSION)
SLEPC_VERSION_NEXT=$(PETSC_VERSION_NEXT)

PETSC_DIR=/usr/lib/petscdir/petsc$(PETSC_VERSION)/$(DEB_HOST_MULTIARCH)-real
SLEPC_DIR=/usr/lib/slepcdir/slepc$(SLEPC_VERSION)/$(DEB_HOST_MULTIARCH)-real

PETSC64_DIR=/usr/lib/petscdir/petsc64-$(PETSC_VERSION)/$(DEB_HOST_MULTIARCH)-real
SLEPC64_DIR=/usr/lib/slepcdir/slepc64-$(SLEPC_VERSION)/$(DEB_HOST_MULTIARCH)-real


DEB_CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)

ifeq (mips64el, $(DEB_HOST_ARCH))
  DEB_CXXFLAGS += -flto-partition=none
endif

CMAKE_OPTS := \
	-D CMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
	-D BUILD_SHARED_LIBS:BOOL=ON \
	-D CMAKE_SKIP_RPATH:BOOL=ON \
	-D CMAKE_INSTALL_RPATH_USE_LINK_PATH:BOOL=OFF \
	-D DOLFIN_ENABLE_TRILINOS:BOOL=OFF \
	-D DOLFIN_ENABLE_HDF5:BOOL=ON \
	-D HDF5_C_COMPILER_EXECUTABLE:FILEPATH=/usr/bin/h5pcc \
	-D DOLFIN_ENABLE_PARMETIS:BOOL=OFF \
	-D DOLFIN_ENABLE_SCOTCH:BOOL=ON \
	-D DOLFIN_ENABLE_DOCS:BOOL=OFF \
	-D DOLFIN_ENABLE_MPI:BOOL=$(ENABLE_MPI) \
	-D MPIEXEC_PARAMS:STRING="$(MPIEXEC_PARAMS)" \
	-D CMAKE_CXX_FLAGS:STRING="-fpermissive" \
	-D DOLFIN_EXTRA_CXX_FLAGS:STRING="$(DEB_CXXFLAGS)" \
	$(DOLFIN_HOME)

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

override_dh_compress:
	dh_compress -X.py -X.cpp -X.h -X.pdf -X.ufl

override_dh_auto_clean:
	rm -rf $(INSTALLATION_BUILDDIR) $(TESTDIR)
	rm -rf $(INSTALLATION_BUILDDIR_64) $(TESTDIR_64)
	dh_auto_clean

override_dh_auto_configure:
	PETSC_DIR=$(PETSC_DIR) SLEPC_DIR=$(SLEPC_DIR) dh_auto_configure --builddir=$(BUILDDIR) -- $(CMAKE_OPTS)
	PETSC_DIR=$(PETSC64_DIR) SLEPC_DIR=$(SLEPC64_DIR) dh_auto_configure --builddir=$(BUILDDIR_64) -- $(CMAKE_OPTS) -DLIB_NAME_EXT="64"

override_dh_auto_build:
	PETSC_DIR=$(PETSC_DIR) SLEPC_DIR=$(SLEPC_DIR) dh_auto_build --builddir=$(BUILDDIR)
	PETSC_DIR=$(PETSC64_DIR) SLEPC_DIR=$(SLEPC64_DIR) dh_auto_build --builddir=$(BUILDDIR_64)

override_dh_auto_install-indep:
	dh_auto_install -i --builddir=$(BUILDDIR)
	(cd doc; doxygen)
	(cd python/doc; make html)
	dh_numpy3 -i

override_dh_auto_install-arch:
	dh_auto_install -a --builddir=$(BUILDDIR) --destdir=debian/tmp
	dh_auto_install -a --builddir=$(BUILDDIR_64) --destdir=debian/tmp64
	dh_numpy3 -A
	for v in $(PYVERS); do \
	    cd python; \
	    PETSC_DIR=$(PETSC_DIR) SLEPC_DIR=$(SLEPC_DIR) \
	    PATH=$(CURDIR)/debian/tmp/usr/bin:$$PATH \
	    CXXFLAGS="$(DEB_CXXFLAGS) -isystem $(CURDIR)/debian/tmp/usr/include" VERBOSE=1 \
	    $$v setup.py build --build-base=build install --root=$(CURDIR)/debian/tmp --install-layout=deb; \
	    PETSC_DIR=$(PETSC64_DIR) SLEPC_DIR=$(SLEPC64_DIR) \
	    PATH=$(CURDIR)/debian/tmp64/usr/bin:$$PATH \
	    CXXFLAGS="$(DEB_CXXFLAGS) -isystem $(CURDIR)/debian/tmp64/usr/include" VERBOSE=1 \
	    $$v setup.py build --build-base=build-64 install --root=$(CURDIR)/debian/tmp64 --install-layout=deb; \
	done;
	chrpath -d $(CURDIR)/debian/tmp/usr/lib/python*/dist-packages/dolfin/*.so
	chrpath -d $(CURDIR)/debian/tmp64/usr/lib/python*/dist-packages/dolfin/*.so
	sed -i "s/-D_FORTIFY_SOURCE=2//g" $(CURDIR)/debian/tmp/usr/lib/$(DEB_BUILD_MULTIARCH)/pkgconfig/dolfin.pc
	sed -i "s|-DNDEBUG||g" $(CURDIR)/debian/tmp/usr/lib/$(DEB_BUILD_MULTIARCH)/pkgconfig/dolfin.pc
	sed -i "s/-D_FORTIFY_SOURCE=2//g" $(CURDIR)/debian/tmp64/usr/lib/$(DEB_BUILD_MULTIARCH)/pkgconfig/dolfin64.pc
	sed -i "s|-DNDEBUG||g" $(CURDIR)/debian/tmp64/usr/lib/$(DEB_BUILD_MULTIARCH)/pkgconfig/dolfin64.pc

# make a separate copy of the build directory while testing, or else local flags (-I$(DOLFIN_HOME)) get written into dolfin.pc
INSTALLATION_BUILDDIR = installation-build
INSTALLATION_BUILDDIR_64 = installation-build-64
override_dh_auto_test-arch:
	case " $(ARCH_SKIP_ALL_TESTS_LIST) " in \
	  *\ $(DEB_HOST_ARCH)\ *) echo "ALL tests have been disabled on $(DEB_HOST_ARCH)";; \
	  *) set -e; \
	     cp -ra $(BUILDDIR) $(INSTALLATION_BUILDDIR); \
	     cd $(BUILDDIR); \
	     make -j$(N_CPU) unittests VERBOSE=1; \
	     make -j$(N_CPU) demos VERBOSE=1; \
	     case " $(ARCH_SKIP_MPI_TEST_LIST) " in \
	       *\ $(DEB_HOST_ARCH)\ *) IGNORE_MPI_TESTS="$(ARCH_ALL_SKIP_MPI_TEST) $(ARCH_SKIP_MPI_TEST) $(ARCH_SPECIFIC_SKIP_MPI_TEST)";; \
	       *) IGNORE_MPI_TESTS="$(ARCH_ALL_SKIP_MPI_TEST) $(ARCH_SPECIFIC_SKIP_MPI_TEST)";; \
	     esac; \
	     echo "set(CTEST_CUSTOM_TESTS_IGNORE $${IGNORE_MPI_TESTS} $(ARCH_ALL_SKIP_SERIAL_TEST) $(ARCH_SPECIFIC_SKIP_SERIAL_TEST) $(ARCH_SKIP_SLOW_TEST) )" >> CTestCustom.cmake; \
	     echo "Run C++ unit tests (serial)"; \
	     LD_LIBRARY_PATH=$(DOLFIN_HOME)/$(BUILDDIR)/dolfin:$${LD_LIBRARY_PATH} ctest --output-on-failure -R unittests; \
	     echo "Run C++ regressions tests (serial)"; \
	     LD_LIBRARY_PATH=$(DOLFIN_HOME)/$(BUILDDIR)/dolfin:$${LD_LIBRARY_PATH} ctest --output-on-failure -j$(N_CPU) -R demo -R serial; \
	     echo "Run C++ regression tests (MPI)"; \
	     case " $(ARCH_SKIP_ALL_MPI_TESTS_LIST) " in \
	       *\ $(DEB_HOST_ARCH)\ *) echo "MPI tests have been disabled on $(DEB_HOST_ARCH)";; \
	       *)  LD_LIBRARY_PATH=$(DOLFIN_HOME)/$(BUILDDIR)/dolfin:$${LD_LIBRARY_PATH} ctest --output-on-failure -j$(N_MPI_TESTS) -R demo -R mpi;; \
	     esac; \
	     cd ..; \
	     mv $(BUILDDIR) $(TESTDIR); \
	     mv $(INSTALLATION_BUILDDIR) $(BUILDDIR);; \
	esac
	case " $(ARCH_SKIP_ALL_TESTS_LIST) " in \
	  *\ $(DEB_HOST_ARCH)\ *) echo "ALL 64-bit tests have been disabled on $(DEB_HOST_ARCH)";; \
	  *) set -e; \
	     cp -ra $(BUILDDIR_64) $(INSTALLATION_BUILDDIR_64); \
	     cd $(BUILDDIR_64); \
	     make -j$(N_CPU) unittests VERBOSE=1; \
	     make -j$(N_CPU) demos VERBOSE=1; \
	     case " $(ARCH_SKIP_MPI_TEST_LIST) " in \
	       *\ $(DEB_HOST_ARCH)\ *) IGNORE_MPI_TESTS="$(ARCH_ALL_SKIP_MPI_TEST) $(ARCH_SKIP_MPI_TEST) $(ARCH_SPECIFIC_SKIP_MPI_TEST_64)";; \
	       *) IGNORE_MPI_TESTS="$(ARCH_ALL_SKIP_MPI_TEST) $(ARCH_SPECIFIC_SKIP_MPI_TEST)";; \
	     esac; \
	     echo "set(CTEST_CUSTOM_TESTS_IGNORE $${IGNORE_MPI_TESTS} $(ARCH_ALL_SKIP_SERIAL_TEST) $(ARCH_SPECIFIC_SKIP_SERIAL_TEST_64) $(ARCH_SKIP_SLOW_TEST) )" >> CTestCustom.cmake; \
	     echo "Run C++ 64-bit unit tests (serial)"; \
	     LD_LIBRARY_PATH=$(DOLFIN_HOME)/$(BUILDDIR_64)/dolfin:$${LD_LIBRARY_PATH} ctest --output-on-failure -R unittests; \
	     echo "Run C++ 64-bit regressions tests (serial)"; \
	     LD_LIBRARY_PATH=$(DOLFIN_HOME)/$(BUILDDIR_64)/dolfin:$${LD_LIBRARY_PATH} ctest --output-on-failure -j$(N_CPU) -R demo -R serial; \
	     echo "Run C++ 64-bit regression tests (MPI)"; \
	     case " $(ARCH_SKIP_ALL_MPI_TESTS_LIST) " in \
	       *\ $(DEB_HOST_ARCH)\ *) echo "MPI 64-bit tests have been disabled on $(DEB_HOST_ARCH)";; \
	       *)  LD_LIBRARY_PATH=$(DOLFIN_HOME)/$(BUILDDIR_64)/dolfin:$${LD_LIBRARY_PATH} ctest --output-on-failure -j$(N_MPI_TESTS) -R demo -R mpi || /bin/true;; \
	     esac; \
	     cd ..; \
	     mv $(BUILDDIR_64) $(TESTDIR_64); \
	     mv $(INSTALLATION_BUILDDIR_64) $(BUILDDIR_64);; \
	esac

override_dh_auto_test-indep:

override_dh_install-indep:
	cd python/demo; \
	python3 generate-demo-files.py
	dh_install -i
	sed "s/set(CMAKE_IMPORT_FILE_VERSION 1)/set(CMAKE_IMPORT_FILE_VERSION 1)\n\nif(PETSC_INT_SIZE EQUAL 8)\n  set(LIB_NAME_EXT \"64\")\nelse()\n  set(LIB_NAME_EXT \$${DOLFIN_LIB_NAME_EXT})\nendif()/; \
	  s/libdolfin.so/libdolfin\$${LIB_NAME_EXT}.so/g; s/$(DEB_HOST_MULTIARCH)/\$${CMAKE_LIBRARY_ARCHITECTURE}/g" \
	  -i debian/libdolfin-dev-common/usr/share/dolfin/cmake/DOLFINTargets-relwithdebinfo.cmake
	sed "s/$(DEB_HOST_MULTIARCH)/\$${CMAKE_LIBRARY_ARCHITECTURE}/g" \
	  -i debian/libdolfin-dev-common/usr/share/dolfin/cmake/DOLFINTargets.cmake

override_dh_python3-arch:
	dh_python3 -a
	dh_numpy3 -a
	mkdir -p debian/python3-dolfin-real/$(PETSC_DIR)/lib/
	mv debian/python3-dolfin-real/usr/lib/python3  debian/python3-dolfin-real/$(PETSC_DIR)/lib
	mkdir -p debian/python3-dolfin64-real/$(PETSC64_DIR)/lib/
	mv debian/python3-dolfin64-real/usr/lib/python3  debian/python3-dolfin64-real/$(PETSC64_DIR)/lib
	rm -rf debian/python3-dolfin64-real/$(PETSC64_DIR)/lib/dist-packages/dolfinx_utils
	rm -rf debian/python3-dolfin64-real/$(PETSC64_DIR)/lib/dist-packages/fenics*

override_dh_fixperms-indep:
	dh_fixperms -i
	chmod a-x debian/dolfin-doc/usr/share/dolfin/demo-python/*/*/*.py

override_dh_sphinxdoc-indep:
	dh_sphinxdoc -i
	grep "https://cdnjs.cloudflare.com/ajax/libs/mathjax/.*/latest.js" debian/dolfin-doc/usr/share/doc/dolfin-doc/* -r --files-with-matches | xargs sed "s|src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/.*/latest.js|src=\"file:///usr/share/javascript/mathjax/unpacked/latest.js|g" -i

# set petsc:Depends to something like "libpetsc-real3.8-dev, libslepc-real3.8-dev, python-petsc4py (>= 3.8), python-petsc4py (<< 3.9), python-slepc4py (>= 3.8), python-slepc4py (<< 3.9)"
PETSC_DEV_DEPENDS="libpetsc-real$(PETSC_VERSION)-dev | libpetsc$(PETSC_UPSTREAM_VERSION)-dev, libslepc-real$(SLEPC_VERSION)-dev | libslepc$(SLEPC_UPSTREAM_VERSION)-dev"
PETSC64_DEV_DEPENDS="libpetsc64-real$(PETSC_VERSION)-dev | libpetsc64-$(PETSC_UPSTREAM_VERSION)-dev, libslepc64-real$(SLEPC_VERSION)-dev | libslepc64-$(SLEPC_UPSTREAM_VERSION)-dev"
# slepc4py version must match petsc4py (using the PETSc minor version, not the patch release)
PETSC4PY_DEPENDS_PY3=python3-petsc4py (>= $(PETSC_VERSION)), python3-petsc4py (<< $(PETSC_VERSION_NEXT)), python3-slepc4py (>= $(SLEPC_VERSION)), python3-slepc4py (<< $(SLEPC_VERSION_NEXT))
PETSC4PY_REAL_DEPENDS_PY3=python3-petsc4py-real (>= $(PETSC_VERSION)), python3-petsc4py-real (<< $(PETSC_VERSION_NEXT)), python3-slepc4py-real (>= $(SLEPC_VERSION)), python3-slepc4py-real (<< $(SLEPC_VERSION_NEXT))
PETSC4PY_64_REAL_DEPENDS_PY3=python3-petsc4py-64-real (>= $(PETSC_VERSION)), python3-petsc4py-64-real (<< $(PETSC_VERSION_NEXT)), python3-slepc4py-64-real (>= $(SLEPC_VERSION)), python3-slepc4py-64-real (<< $(SLEPC_VERSION_NEXT))
override_dh_gencontrol:
	echo "python3-petsc4py:Depends=$(PETSC4PY_DEPENDS_PY3)" >> debian/python3-dolfin.substvars
	echo "python3-petsc4py-real:Depends=$(PETSC4PY_REAL_DEPENDS_PY3)" >> debian/python3-dolfin-real.substvars
	echo "python3-petsc4py-64-real:Depends=$(PETSC4PY_64_REAL_DEPENDS_PY3)" >> debian/python3-dolfin64-real.substvars
	dh_gencontrol -- -Vpetsc:Depends=$(PETSC_DEV_DEPENDS)  -Vpetsc64:Depends=$(PETSC64_DEV_DEPENDS) \
	    -Vfenics:Upstream-Version=$(FENICS_VERSION) -Vfenics:Next-Upstream-Version=$(FENICS_NEXT_VERSION)~ \
	    -Vpybind11:Upstream-Version=$(PYBIND11_VERSION) -Vpybind11:Next-Upstream-Version=$(PYBIND11_NEXT_VERSION)

# dbgsym-migration was introduced for dolfin 2017.2 (don't update the version to a later version)
override_dh_strip:
	dh_strip --package=libdolfin$(FENICS_VERSION) -Xpython
	dh_strip --package=python3-dolfin-real -Xpython2
	dh_strip --package=libdolfin64-$(FENICS_VERSION) -Xpython
	dh_strip --package=python3-dolfin64-real

# python module so files are already stripped by setup.py (setuptools)
override_dh_dwz:
	dh_dwz -Xcpp.cpython

# https://stackoverflow.com/a/18793112/353337
override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info


.PHONY: override_dh_strip