#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

# Enable hardening build flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all reproducible=+fixfilepath
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

# to help makeshlibs
UPSTREAM_VERSION := $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+.*//')

FLEXIBLAS_CMAKE_COMMON := -DTESTS=ON -DEXAMPLES=OFF -DSYSCONFDIR=/etc \
  -DLAPACK_API_VERSION=$(shell dpkg -s liblapack-dev | sed '/^Version:/!d;s/.* \([^-]*\).*/\1/;s/^[0-9]*://')
FLEXIBLAS_BLAS32 := $(shell dpkg -L libblas-dev | grep 'libblas.so$$')
FLEXIBLAS_LAPACK32 := $(shell dpkg -L liblapack-dev | grep 'liblapack.so$$')
FLEXIBLAS_LAPACKE32 := $(shell dpkg -L liblapacke-dev | grep 'liblapacke.so$$')
ifeq (64,$(DEB_HOST_ARCH_BITS))
FLEXIBLAS_BLAS64 := $(shell dpkg -L libblas64-dev | grep 'libblas64.so$$')
FLEXIBLAS_LAPACK64 := $(shell dpkg -L liblapack64-dev | grep 'liblapack64.so$$')
FLEXIBLAS_LAPACKE64 := $(shell dpkg -L liblapacke64-dev | grep 'liblapacke64.so$$')
endif

ifneq (64,$(DEB_HOST_ARCH_BITS))
export DH_OPTIONS = \
    -Nlibflexiblas64-3 -Nlibflexiblas64-bin -Nlibflexiblas64-dev \
    -Nlibflexiblas64-api3 -Nlibflexiblas64-api-dev \
    -Nlibflexiblas64-hook-profile \
    -Nlibflexiblas64-openblas-serial \
    -Nlibflexiblas64-openblas-openmp \
    -Nlibflexiblas64-openblas-pthread \
    -Nlibflexiblas64-blis-serial \
    -Nlibflexiblas64-blis-openmp \
    -Nlibflexiblas64-blis-pthread
endif

export FLEXIBLAS_VERBOSE=1

%:
	dh $@ --buildsystem=cmake

override_dh_auto_clean:
	dh_auto_clean --builddirectory build-32
ifeq (64,$(DEB_HOST_ARCH_BITS))
	dh_auto_clean --builddirectory build-64
endif

override_dh_auto_configure:
	# Mute blhc false positives
	@echo "blhc: ignore-line-regexp: .*User Specified CMake Arguments.*"
	@echo "blhc: ignore-line-regexp: .*env -- DEB_PYTHON_INSTALL_LAYOUT=deb.*"
	@echo "blhc: ignore-line-regexp: .*/.chk(aa|ee)\.F -o CMakeFiles/.*"
	dh_auto_configure --buildsystem=cmake --builddirectory=build-32 -- \
	    $(FLEXIBLAS_CMAKE_COMMON) -DINTEGER8=OFF \
	    -DSYS_BLAS_LIBRARY=$(FLEXIBLAS_BLAS32) \
	    -DSYS_CBLAS_LIBRARY=$(FLEXIBLAS_BLAS32) \
	    -DSYS_LAPACKE_LIBRARY=$(FLEXIBLAS_LAPACKE32) \
	    -DSYS_LAPACK_LIBRARY=$(FLEXIBLAS_LAPACK32)
ifeq (64,$(DEB_HOST_ARCH_BITS))
	dh_auto_configure --buildsystem=cmake --builddirectory=build-64 -- \
	    $(FLEXIBLAS_CMAKE_COMMON) -DINTEGER8=ON \
	    -DSYS_BLAS_LIBRARY=$(FLEXIBLAS_BLAS64) \
	    -DSYS_CBLAS_LIBRARY=$(FLEXIBLAS_BLAS64) \
	    -DSYS_LAPACKE_LIBRARY=$(FLEXIBLAS_LAPACKE64) \
	    -DSYS_LAPACK_LIBRARY=$(FLEXIBLAS_LAPACK64)
endif

override_dh_auto_build:
	dh_auto_build --builddirectory build-32 --parallel
ifeq (64,$(DEB_HOST_ARCH_BITS))
	dh_auto_build --builddirectory build-64 --parallel
endif

override_dh_auto_test:
	dh_auto_test --buildsystem=cmake --builddirectory=build-32
ifeq (64,$(DEB_HOST_ARCH_BITS))
	dh_auto_test --buildsystem=cmake --builddirectory=build-64
endif

override_dh_auto_install:
	dh_auto_install --builddirectory build-32 --destdir debian/tmp
ifeq (64,$(DEB_HOST_ARCH_BITS))
	dh_auto_install --builddirectory build-64 --destdir debian/tmp
endif

override_dh_makeshlibs:
	dh_makeshlibs -- -v$(UPSTREAM_VERSION)
