#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
# ulmBLAS compiles with -O3 (its OPTFLAGS come before CXXFLAGS)
export DEB_CXXFLAGS_MAINT_STRIP = -O2
# Ubuntu: -Bsymbolic-functions would bind the library's calls of xerbla_
# to its own, and a program could no longer replace it, as is the
# convention of the BLAS (Basic Linear Algebra Subprograms)
export DEB_LDFLAGS_MAINT_STRIP = -Wl,-Bsymbolic-functions
# the C sources (dispatch.c, ...) are compiled without CPPFLAGS
export DEB_CFLAGS_MAINT_APPEND = $(shell \
    DEB_BUILD_MAINT_OPTIONS="$(DEB_BUILD_MAINT_OPTIONS)" \
    dpkg-buildflags --get CPPFLAGS)

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

LIBDIR = /usr/lib/$(DEB_HOST_MULTIARCH)
# the directory of the alternatives (libblas.so.3, libblas.so, blas.pc)
# and, on amd64, of the backends of the dispatching library
SUBDIR = $(LIBDIR)/ulmblas
# priority of the alternatives libblas.so.3 and libblas.so: below OpenBLAS
# (90-100) and BLIS (BLAS-like Library Instantiation Software) (70-80),
# above the reference BLAS (10)
UAPRI = 50

export ULMBLAS_VERSION = $(DEB_VERSION_UPSTREAM)
export ULMBLAS_LIBDIR = $(LIBDIR)
export ULMBLAS_BACKENDDIR = $(SUBDIR)
export ULMBLAS_INCLUDEDIR = /usr/include/$(DEB_HOST_MULTIARCH)/ulmblas
export ULMBLAS_RPATH = no
export ULMBLAS_STATIC_LIBSTDCXX = no
# no libulmlapack: Debian's liblapack3 runs on any libblas.so.3
ULMBLAS_FC = none
ifeq ($(DEB_HOST_ARCH),amd64)
# the dispatching library with the SONAME libblas.so.3 in SUBDIR (and
# libcblas.so.3, which Debian does not use)
export ULMBLAS_SONAMES = yes
endif

NPROC = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
ULMBLAS_MAKE = $(MAKE) $(if $(NPROC),-j$(NPROC))

%:
	dh $@

override_dh_auto_build:
	ULMBLAS_STEP=build FC=$(ULMBLAS_FC) MAKE="$(ULMBLAS_MAKE)" \
	    sh interfaces/install-shared.sh /usr
ifneq ($(DEB_HOST_ARCH),amd64)
	# one library without backends (arm64): its objects linked a second
	# time, with the SONAME (shared object name) libblas.so.3
	$(ULMBLAS_MAKE) -C interfaces/blas/shared SHARED_NAME=blas \
	    SHARED_VERSION=3 STATIC_LIBSTDCXX= OUTDIR=$(CURDIR)/debian/build
endif

override_dh_auto_install:
	DESTDIR=$(CURDIR)/debian/tmp FC=$(ULMBLAS_FC) MAKE="$(ULMBLAS_MAKE)" \
	    sh interfaces/install-shared.sh /usr
ifeq ($(DEB_HOST_ARCH),amd64)
	rm -f debian/tmp$(SUBDIR)/libcblas.so.3
else
	install -D -m 0644 debian/build/libblas.so.3 \
	    debian/tmp$(SUBDIR)/libblas.so.3
endif
	ln -s libblas.so.3 debian/tmp$(SUBDIR)/libblas.so
	mkdir -p debian/tmp$(SUBDIR)/pkgconfig
	sed -e 's,@LIBDIR@,$(LIBDIR),' \
	    -e 's,@INCLUDEDIR@,/usr/include/$(DEB_HOST_MULTIARCH),' \
	    -e 's,@VERSION@,$(DEB_VERSION_UPSTREAM),' \
	    debian/blas-ulmblas.pc.in \
	    > debian/tmp$(SUBDIR)/pkgconfig/blas-ulmblas.pc

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	FC=gfortran sh packaging/check.sh
endif

override_dh_auto_clean:
	$(MAKE) clean
	rm -rf build debian/build libulmblas* libblas.so.3 libcblas.so.3
	rm -f debian/libulmblas0.postinst debian/libulmblas0.prerm \
	      debian/libulmblas-dev.postinst debian/libulmblas-dev.prerm

# symbols for libulmblas.so.0 only: libblas.so.3 of the alternative has the
# entry "libblas 3 libblas3 | libblas.so.3" of debian/libulmblas0.shlibs
# (as the other providers), the backends are private
override_dh_makeshlibs:
	dh_makeshlibs -X/ulmblas/

execute_before_dh_installdeb:
	for f in libulmblas0.postinst libulmblas0.prerm \
	         libulmblas-dev.postinst libulmblas-dev.prerm; do \
	    sed -e 's,@MULTIARCH@,$(DEB_HOST_MULTIARCH),g' \
	        -e 's,@UAPRI@,$(UAPRI),g' debian/$$f.in > debian/$$f; \
	done
