#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

include /usr/share/dpkg/default.mk
export DEB_HOST_MULTIARCH

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')

VERSION := $(DEB_STRIPPED_UPSTREAM_VERSION)
MAJOR_VERSION := $(shell echo $(DEB_STRIPPED_UPSTREAM_VERSION) | sed "s/^\([^.]*\)\..*$$/\1/")
SOVERSION := $(MAJOR_VERSION)

XTENSOR_DEB_VERSION=$(shell dpkg -s libxtensor-dev | awk '/Version:/ {print $$2}')
XTENSOR_VERSION=$(shell echo $(XTENSOR_DEB_VERSION) | sed "s/^.[^:]*://; s/-[^-]*$$//")

XSIMD_DEB_VERSION=$(shell dpkg -s libxsimd-dev | awk '/Version:/ {print $$2}')
XSIMD_VERSION=$(shell echo $(XSIMD_DEB_VERSION) | sed "s/^.[^:]*://; s/-[^-]*$$//")

PY3VERS := $(shell py3versions -v --requested debian/control | tac -s' ')
PY3VER_DEFAULT := $(shell py3versions -dv)

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

# libxsimd-dev is available on some arches such as i386, m68k,
# but no xsimd instructions are actually available which causes problems
# with the basix build (warnings are treated as errors)
# So explicitly switch xsimd support off except for arches where it is known to work.
XSIMD_ARCH_LIST := amd64 arm64 kfreebsd
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(XSIMD_ARCH_LIST)$(space)))
  XSIMD_CONFIG = -DXTENSOR_USE_XSIMD=ON
else
  XSIMD_CONFIG = -DXTENSOR_USE_XSIMD=OFF
endif

#ifneq (,$(findstring $(DEB_HOST_ARCH),mipsel))
  PYTEST_OPTIONS = -k "not numba"
#endif


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


override_dh_auto_clean:
	rm -rf cpp/basix/version.h
	rm -rf doc/cpp/html doc/cpp/latex
	rm -rf .pybuild
	rm -rf python/build
	rm -rf python/fenics_basix.egg-info
	rm -rf doc/python/build
	rm -rf test/.pytest_cache  test/__pycache__  python/testing_build
	dh_auto_clean

override_dh_auto_configure:
	dh_auto_configure --sourcedir=cpp -- $(XSIMD_CONFIG)

override_dh_auto_build-indep: export http_proxy=127.0.0.1:9
override_dh_auto_build-indep: export https_proxy=127.0.0.1:9
override_dh_auto_build-indep:
	dh_auto_build --sourcedir=cpp -i
	cd doc/cpp; doxygen

execute_after_dh_auto_install:
	LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
	  CMAKE_PREFIX_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake \
	  pybuild --dir=$(CURDIR)/python --system=distutils --name=basix \
	  --configure-args "setopt --command=build_ext --option=extra_cmake_args --set-value='$(XSIMD_CONFIG)'"

execute_before_dh_install-indep:
	LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) PYTHONPATH=$(CURDIR)/debian/python3-basix/usr/lib/python$(PY3VER_DEFAULT)/dist-packages:${PYTHONPATH} $(MAKE) -C doc/python html
	sed 's|href="https://fenicsproject.org/docs/basix/main/cpp/"|href="file:/usr/share/doc/basix-doc/cpp/index.html"|' -i doc/python/build/html/index.html

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
execute_before_dh_python3:
	cd test; LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) PYTHONPATH=$(CURDIR)/debian/python3-basix/usr/lib/python$(PY3VER_DEFAULT)/dist-packages:${PYTHONPATH} pytest-3 $(PYTEST_OPTIONS)
endif

override_dh_gencontrol:
	dh_gencontrol -- -Vxtensor:Upstream-Version=$(XTENSOR_VERSION) -Vxsimd:Upstream-Version=$(XSIMD_VERSION)
