#!/usr/bin/make -f

include /usr/share/dpkg/buildflags.mk

COMMON_MAKE_OPTIONS = NO_LAPACKE=1 NO_AFFINITY=1 NO_WARMUP=1 CFLAGS="$(CPPFLAGS) $(CFLAGS)" FFLAGS="$(FFLAGS)"
ENABLE_DYNAMIC_ARCHS := amd64 i386 kfreebsd-amd64 kfreebsd-i386
ifneq (,$(findstring $(DEB_HOST_ARCH),$(ENABLE_DYNAMIC_ARCHS)))
	DYNAMIC_ARCH_ARG="DYNAMIC_ARCH=1"
	TARGET_GENERIC_ARG="TARGET=GENERIC"
endif

ifeq ($(DEB_HOST_ARCH),armhf)
	# We cannot use the ARMv7 profile on armhf, because it requires a 32-register FP unit.
	# See kernel/arm/KERNEL.ARMv7: it loads some *_vfpv3.S files, which use 32 registers.
	# Also, it FTBFS if GCC flag -mvfpv3 is removed (see arm-gcc-flags.patch), because GCC
	# refuses asm files with 32 FP registers in that case.
	# Issue discussed in https://github.com/xianyi/OpenBLAS/issues/388
	TARGET_GENERIC_ARG="TARGET=ARMV6"
endif

ifeq ($(DEB_HOST_ARCH),powerpc)
	# There is no dynamic arch selection on PowerPC. To avoid selecting a
	# target based on the buildd hardware, we enforce a generic-enough
	# target.
	TARGET_GENERIC_ARG="TARGET=PPCG4"
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	DEBUG_ARG="DEBUG=1"
endif

%:
	dh $@

override_dh_auto_build:
	if test "$(TARGET)" = ""; then 		\
			echo "Support multiple architectures"; \
			$(MAKE) $(COMMON_MAKE_OPTIONS) $(DYNAMIC_ARCH_ARG) $(TARGET_GENERIC_ARG) $(DEBUG_ARG) NUM_THREADS=64;		\
	fi

	if test "$(TARGET)" = "custom"; then 		\
			echo "Leave the detection to GotoBLAS / OpenBLAS"; \
			$(MAKE) $(COMMON_MAKE_OPTIONS) $(DEBUG_ARG); 								\
	fi

	if test "$(TARGET)" != "" -a "$(TARGET)" != "custom"; then 		\
			echo "Use the value '$(TARGET)' provided by the user";	\
			$(MAKE) $(COMMON_MAKE_OPTIONS) TARGET=$(TARGET) $(DEBUG_ARG);									\
	fi

	make -C interface shared-blas-lapack

override_dh_auto_install:
	make install DESTDIR=debian/tmp/ PREFIX=/usr

custom:
	dch --local='+custom'  "custom build on: `uname -a`"
	LANG=C debian/rules TARGET=custom build binary

# We want packages linked with -lblas to depend on any BLAS alternative, and we
# want packages linked with -lopenblas to depend specifically on
# libopenblas-base.
#
# Such a setting is not supported by dh_makeshlibs, so we ship a hand-crafted
# shlibs file.
override_dh_makeshlibs:
	dh_makeshlibs
	cp debian/libopenblas-base.shlibs debian/libopenblas-base/DEBIAN/shlibs

override_dh_gencontrol:
	dh_gencontrol -- -VBuilt-Using="`dpkg-query -W -f='$${source:Package} (= $${source:Version})' liblapack-pic`"

get-orig-source:
	-uscan --upstream-version 0
