#!/usr/bin/make -f
# Copyright 2008-2010 Sylvestre Ledru <sylvestre@debian.org>

include /usr/share/cdbs/1/rules/debhelper.mk

# NB: the following numbers come from xprint_enums (compilable from the build
# directory with "make xprint_enums); they can change across major releases
#
# First number in ARCHS:
# - 0 means Unknown CPU: leave ATLAS find out; used for archs where a more specific choice is not available
# - 8 means IBMz9: for s390x
# - 11 means artificial architecture called x86x87, providing you with portable (but slow!) architectural defaults: for *i386
#   See http://math-atlas.sourceforge.net/atlas_install/node28.html
# - 13 means generic libraries for P4 and later, with SSE2: for *amd64
#   See: http://math-atlas.sourceforge.net/atlas_install/node30.html
# - 36 means IA64Itan: for ia64
# - 38 means UltraSparc I (USI): for sparc
# - 46 means ARMv7: for armhf (but not for armel, which is ARM >= v4)
# - 47 means GENERIC: the same than 0 (UNKNOWN), except that it does not try autodetection
#   See debian/patches/generic.diff
# Second number in ARCHS:
# - 1 means no instruction set extension
# - 384 means SSE1+SSE2 (always available on amd64)
ifeq ($(DEB_HOST_ARCH),s390x)
ARCHS=base_8_1
else ifeq ($(DEB_HOST_ARCH_CPU),i386)
ARCHS=base_11_1
else ifeq ($(DEB_HOST_ARCH_CPU),amd64)
ARCHS=base_13_384
else ifeq ($(DEB_HOST_ARCH),ia64)
ARCHS=base_36_1
else ifeq ($(DEB_HOST_ARCH),sparc)
ARCHS=base_38_1
else ifeq ($(DEB_HOST_ARCH),armhf)
ARCHS=base_46_1
else
ARCHS=base_47_1
endif

# Pointer bitwidth
MODE_BITWIDTH = $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)

DEB_SHLIBDEPS_INCLUDE_libatlas3-base := $(CURDIR)/debian/libatlas3-base/usr/lib/atlas-base/:$(CURDIR)/debian/libatlas3-base/usr/lib/atlas-base/atlas/

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

GFORTRAN_LIB_PATH=`$(CC) -print-search-dirs|grep ^install:|awk '{print $$2}'`

# Flags common to both the generic and custom packages
COMMON_CONFIG_PARAMS := --prefix="$(DEB_DESTDIR)" 	\
	--incdir="$(DEB_DESTDIR)/usr/include/"			\
	--libdir="$(DEB_DESTDIR)/usr/lib/$$targetName"		\
	--cc="${CC}" 									\
	--cflags="${CFLAGS}" 							\
	-D c -DWALL 									\
	-b $(MODE_BITWIDTH) 							\
	-Fa alg '-fPIC' 				\
	-Ss f77lib "-L$(GFORTRAN_LIB_PATH) -lgfortran -lgcc_s -lpthread"  \
	-Ss pmake '$(MAKE)'					\
	-Ss flapack /usr/lib/liblapack_pic.a 	\
	-A $$atlasArch									\
	-V $$atlasISA 									\
	-v 2

# See http://math-atlas.sourceforge.net/errata.html#armhardfp
ifeq ($(DEB_HOST_ARCH),armhf)
COMMON_CONFIG_PARAMS += -D c -DATL_ARM_HARDFP=1
endif

# Flags used only for the generic package
GENERIC_CONFIG_PARAMS := \
	-t 0 \
	-Ss ADdir ../../../debian/archdefs/$(DEB_HOST_ARCH_CPU) \
	-Si cputhrchk 0

# The archdefs provided by ATLAS are unsuitable for armhf (see details in
# armhf.diff patch)
#
# Hence we replace them by new ones without the problematic flags. They are
# located under debian/archdefs/arm, and used for the generic package.
#
# The following rules ensure that these new archdefs are also used for the
# custom package.
use-my-armhf-archdef:
		test -f CONFIG/ARCHS/ARMv732.tar.bz2.old || (mv CONFIG/ARCHS/ARMv732.tar.bz2 CONFIG/ARCHS/ARMv732.tar.bz2.old && cp debian/archdefs/arm/ARMv732.tar.bz2 CONFIG/ARCHS/ARMv732.tar.bz2)
		test -f CONFIG/ARCHS/ARMv732NEON.tar.bz2.old || (mv CONFIG/ARCHS/ARMv732NEON.tar.bz2 CONFIG/ARCHS/ARMv732NEON.tar.bz2.old && cp debian/archdefs/arm/ARMv732NEON.tar.bz2 CONFIG/ARCHS/ARMv732NEON.tar.bz2)

restore-armhf-archdef:
		(test -f CONFIG/ARCHS/ARMv732.tar.bz2.old && mv CONFIG/ARCHS/ARMv732.tar.bz2.old CONFIG/ARCHS/ARMv732.tar.bz2) || true
		(test -f CONFIG/ARCHS/ARMv732NEON.tar.bz2.old && mv CONFIG/ARCHS/ARMv732NEON.tar.bz2.old CONFIG/ARCHS/ARMv732NEON.tar.bz2) || true

# Build a custom package optimized for the current arch
custom: custom-stamp
.PHONY: custom
custom-stamp: use-my-armhf-archdef
		rm -f configure-stamp

		ncpu=$$(LANG=C cpufreq-info | grep "analyzing CPU" -c);\
		cpu=0;\
		while test $$cpu -lt $$ncpu ; do\
		if test $$(cpufreq-info -p 2>&1 > /dev/null; echo $$?) \
			-eq 0 -a -z "$$(LANG=C cpufreq-info -p -c $$cpu | grep performance)" ; then \
			echo "frequency governor on cpu=$$cpu is not set to 'performance'"; \
			echo "run: 'sudo cpufreq-set -g performance -c cpu#' for each cpu"; \
			echo "aborting atlas build"; \
			exit 1; \
		fi; \
		cpu=$$((cpu+1)) ;\
		done

		# Here, the trick is pretty simple:
		# * We don't want to build all the optimized version of Atlas. Just the
		#   one for the current CPU
		# * We override ARCHS by base__
		# * Base is the name of the package (libatlas3-base)
		# * __ is provided to have the right expression
		# * the regexp will consider that atlasArch and atlasISA are empty
		#   therefor, leaving the choice of the optimization to Atlas
		# * After that, we fall back to the normal build procedure by calling
		#   debian/rules itself (yep, recursively)

		# Remove all the other archs in this case
		dch --local='+custom'  "custom build on: `uname -a`"
		LANG=C debian/rules ARCHS=base__ binary
		touch $@

common-configure-arch common-configure-indep:: configure-stamp
configure-stamp:
		dh_testdir

		set -e;											\
		targetName=`echo $(ARCHS)|sed -e "s|\(.*\)_.*_.*|atlas-\1|g"`; \
		atlasArch=`echo $(ARCHS)|sed -e "s|.*_\(.*\)_.*|\1|g"`; \
		atlasISA=`echo $(ARCHS)|sed -e "s|.*_.*_\(.*\)|\1|g"`; \
		mkdir -p build/$$targetName;					\
		cd build/$$targetName; 							\
		if test "$(ARCHS)" != "base__"; then 		\
		../../configure $(COMMON_CONFIG_PARAMS) $(GENERIC_CONFIG_PARAMS); \
		else						\
		../../configure $(COMMON_CONFIG_PARAMS);	\
		fi;						\
		echo "Configure done. targetName = $$targetName / atlasArch = $$atlasArch / atlasISA = $$atlasISA";								\
		if test ! -s Make.inc; then echo "Configure failed: Make.inc not found"; exit 1; fi;											\
		cat Make.inc |grep ARCH|head -1|awk '{print $$3}'; \
		echo "=============================================="; \
		cd -
		touch $@

common-build-arch common-build-indep:: build-stamp
build-stamp:
		dh_testdir
		set -e; 												\
		targetName=`echo $(ARCHS)|sed -e "s|\(.*\)_.*_.*|atlas-\1|g"`; \
		cd build/$$targetName;									\
		$(MAKE) build;											\
		$(MAKE) check;											\
		test -f build/$$targetName/lib/libptcblas.a && $(MAKE) ptcheck;										\
		echo "==============================================";	\
		cd ../../../
		# Do not build the help when building the custom package
		if test "$(ARCHS)" != "base__"; then \
			make -C TexDoc atlas_contrib.pdf atlas_devel.pdf atlas_install.pdf cblasqref.pdf  f77blasqref.pdf lapackqref.pdf; \
		fi
		touch $@

clean:: clean-work
clean-work: restore-armhf-archdef
		dh_testdir
		dh_testroot
		rm -rf build check 
		rm -f configure-stamp build-stamp
		rm -f TexDoc/*.pdf
		[ ! -f Makefile ] || $(MAKE) distclean
.PHONY: clean-work

common-install-arch common-install-indep::
		dh_testdir
		dh_testroot

		set -e;													\
		targetName=`echo $(ARCHS)|sed -e "s|\(.*\)_.*_.*|atlas-\1|g"`; \
		cd build/$$targetName;									\
		echo "make install of $$targetName to $(DEB_DESTDIR)";	\
		$(MAKE) DESTDIR=$(DEB_DESTDIR) install;					\
		echo "=============================================";	\
		cd -

install/libatlas-dev::
	if test -d $(CURDIR)/debian/libatlas-dev/usr/include/; then \
		rm -rf $(CURDIR)/debian/libatlas-dev/usr/include/;  \
	fi
	mkdir -p $(CURDIR)/debian/libatlas-dev/usr/include/
	mv $(CURDIR)/debian/tmp/usr/include/*.h $(CURDIR)/debian/tmp/usr/include/atlas/
	mv $(CURDIR)/debian/tmp/usr/include/atlas $(CURDIR)/debian/libatlas-dev/usr/include/

install/libatlas-doc::
	if test "$(ARCHS)" != "base__"; then \
		mkdir -p $(CURDIR)/debian/libatlas-doc/usr/share/doc/libatlas-doc/; \
		cp -R $(CURDIR)/doc/* $(CURDIR)/TexDoc/*.pdf $(CURDIR)/debian/libatlas-doc/usr/share/doc/libatlas-doc/; \
	fi

# We want packages using libblas/liblapack to depend on any BLAS/LAPACK
# alternative, and we want packages depending on ATLAS-specific libraries
# (e.g. libatlas.so) to depend specifically on libatlas3-base.
#
# Such a setting is not supported by dh_makeshlibs, so we ship a hand-crafted
# shlibs file.
binary-predeb/libatlas3-base::
	cp debian/libatlas3-base.shlibs debian/libatlas3-base/DEBIAN/shlibs

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