#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

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

SO:=3

# xerblas_ and LAPACK_xerblas must be overrideable by applications
# --dynamic-list does not work with gold, so drop the flag completely
BUILDFLAGS_ENV:=DEB_LDFLAGS_MAINT_STRIP="-Wl,-Bsymbolic-functions"
LDFLAGS:=$(shell $(BUILDFLAGS_ENV) dpkg-buildflags --get LDFLAGS)

F77:=gfortran
FFLAGS:=$(shell dpkg-buildflags --get FFLAGS)
FFLAGS_NOOPT:=$(shell DEB_BUILD_OPTIONS=noopt dpkg-buildflags --get FFLAGS)

CC:=/usr/bin/cc
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)

ifeq ($(DEB_HOST_ARCH),alpha)
  FFLAGS += -mieee
  FFLAGS_NOOPT += -mieee
endif

# On arches with extended precision, this flag is used
# for BLAS when compiling i{c,z}amax.f and the BLAS testsuite,
# in order to avoid test failures.
# See also debian/patches/icamax-izamax-extended-precision.patch
ifneq (,$(filter $(DEB_HOST_ARCH_CPU),i386 m68k))
  FFLAGS_FLOAT_STORE := -ffloat-store
  export FFLAGS_FLOAT_STORE
endif

FFLAGS_BLAS_TESTSUITE := $(FFLAGS) $(FFLAGS_FLOAT_STORE)

# Using the -frecursive flag is the solution recommended by upstream for #693269
# However, we don't want to use -frecursive for the LAPACK testsuite, it leads to
#  segfaults on mips{,el} (xeigtstz allocates too much space on the stack)
FFLAGS_LAPACK_TESTSUITE := $(FFLAGS)
FFLAGS += -frecursive
FFLAGS_NOOPT += -frecursive

# Support parallel build
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

%:
	dh $@ --parallel

make.inc: INSTALL/make.inc.gfortran
	sed -e "s,\(^CC  *=\).*,\1 /usr/bin/cc,1" \
	    -e "s,\(^NOOPT  *=\).*,\1 \$$(PICOPTS) $(FFLAGS_NOOPT),1" \
	    -e "s,\(^OPTS  *=\).*,\1 \$$(PICOPTS) $(FFLAGS),1" \
	    -e "s/\(^LOADOPTS  *=\).*/\1 $(LDFLAGS)/1" \
	    -e "s,\(^CFLAGS  *=\).*,\1 $(CPPFLAGS) \$$(PICOPTS) $(CFLAGS),1" \
	    < $< > $@
	# Build deprecated routines (see #810143)
	echo "BUILD_DEPRECATED=1" >> $@

override_dh_auto_clean: make.inc
	$(MAKE) cleanall
	rm -rf debian/*_stamp debian/*_results lib*.so*
	rm -rf debian/test/ debian/time/
	rm -f TESTING/tmp TESTING/testing_results.txt
	rm -f LAPACKE/include/lapacke_mangling.h
	rm -f CBLAS/include/cblas_mangling.h
	rm -rf DOCS/explore-html/ DOCS/man/
	rm -f output_err
	rm -f lapack-netlib.pc LAPACKE/lapacke.pc BLAS/blas-netlib.pc
	rm -f make.inc

override_dh_auto_configure: make.inc
	# Create lapack-netlib.pc
	sed -e "s%@LAPACK_VERSION@%$(DEB_VERSION_UPSTREAM)%" \
	    -e "s%@prefix@%/usr%"  \
	    -e "s%@libdir@%/usr/lib%" < lapack.pc.in > lapack-netlib.pc

	# Create lapacke.pc
	sed -e "s%@LAPACK_VERSION@%$(DEB_VERSION_UPSTREAM)%" \
	    -e "s%@prefix@%/usr%"  \
	    -e "s%@libdir@%/usr/lib%" < LAPACKE/lapacke.pc.in > LAPACKE/lapacke.pc

	# Create blas-netlib.pc
	sed -e "s%@LAPACK_VERSION@%$(DEB_VERSION_UPSTREAM)%" \
	    -e "s%@prefix@%/usr%"  \
	    -e "s%@libdir@%/usr/lib%" < BLAS/blas.pc.in > BLAS/blas-netlib.pc

override_dh_auto_build-arch:
	# Build pic libraries
	$(MAKE) cleanlib
	$(MAKE) blaslib cblaslib lapacklib tmglib lapackelib PICOPTS=-fPIC
	mv librefblas.a librefblas_pic.a
	mv libcblas.a libcblas_pic.a
	mv liblapack.a liblapack_pic.a
	mv libtmglib.a libtmglib_pic.a
	mv liblapacke.a liblapacke_pic.a

	# Build libblas.so.*
	ar d librefblas_pic.a xerbla.o # We want to use the xerbla.o from libcblas_pic.a
	mkdir -p tmp
	cd tmp && ar x ../librefblas_pic.a && ar x ../libcblas_pic.a
	$(F77) -shared -Wl,-soname=libblas.so.$(SO) -o libblas.so.$(DEB_VERSION_UPSTREAM) tmp/*.o $(LDFLAGS)
	rm -rf tmp
	ln -s libblas.so.$(DEB_VERSION_UPSTREAM) libblas.so.$(SO)
	ln -s libblas.so.$(DEB_VERSION_UPSTREAM) libblas.so

	# Build liblapack.so.*
	mkdir -p tmp
	cd tmp && ar x ../liblapack_pic.a
	$(F77) -shared -Wl,-soname=liblapack.so.$(SO) -o liblapack.so.$(DEB_VERSION_UPSTREAM) tmp/*.o  -L. -lblas $(LDFLAGS)
	rm -rf tmp
	ln -s liblapack.so.$(DEB_VERSION_UPSTREAM) liblapack.so.$(SO)
	ln -s liblapack.so.$(DEB_VERSION_UPSTREAM) liblapack.so

	# Build libtmglib.so.*
	mkdir -p tmp
	cd tmp && ar x ../libtmglib_pic.a
	$(F77) -shared -Wl,-soname=libtmglib.so.$(SO) -o libtmglib.so.$(DEB_VERSION_UPSTREAM) tmp/*.o  -L. -lblas -llapack $(LDFLAGS)
	rm -rf tmp
	ln -s libtmglib.so.$(DEB_VERSION_UPSTREAM) libtmglib.so.$(SO)
	ln -s libtmglib.so.$(DEB_VERSION_UPSTREAM) libtmglib.so

	# Build liblapacke.so.*
	mkdir -p tmp
	cd tmp && ar x ../liblapacke_pic.a
	$(CC) -shared -Wl,-soname=liblapacke.so.$(SO) -o liblapacke.so.$(DEB_VERSION_UPSTREAM) tmp/*.o  -L. -lblas -llapack -ltmglib $(LDFLAGS)
	rm -rf tmp
	ln -s liblapacke.so.$(DEB_VERSION_UPSTREAM) liblapacke.so.$(SO)
	ln -s liblapacke.so.$(DEB_VERSION_UPSTREAM) liblapacke.so

	# Build static versions
	$(MAKE) cleanlib
	$(MAKE) blaslib cblaslib lapacklib tmglib lapackelib

	# Build libblas.a
	ar d librefblas.a xerbla.o # We want to use the xerbla.o from libcblas.a
	mkdir -p tmp
	cd tmp && ar x ../librefblas.a && ar x ../libcblas.a
	ar r libblas.a tmp/*.o
	rm -rf tmp

	# Build the test programs, in order to avoid FTBFS if DEB_BUILD_OPTIONS contains nocheck
	$(MAKE) OPTS="$(FFLAGS_BLAS_TESTSUITE)" BLASLIB="../../libblas.so" -C BLAS/TESTING -f Makeblat1
	$(MAKE) OPTS="$(FFLAGS_BLAS_TESTSUITE)" BLASLIB="../../libblas.so" -C BLAS/TESTING -f Makeblat2
	$(MAKE) OPTS="$(FFLAGS_BLAS_TESTSUITE)" BLASLIB="../../libblas.so" -C BLAS/TESTING -f Makeblat3
	$(MAKE) OPTS="$(FFLAGS_BLAS_TESTSUITE)" BLASLIB="../../libblas.so" CBLASLIB="" -C CBLAS/testing
	$(MAKE) OPTS="$(FFLAGS_LAPACK_TESTSUITE)" BLASLIB="../../libblas.so" LAPACKLIB="liblapack.so" TMGLIB="libtmglib.so" -C TESTING xeigtstc xeigtstd xeigtsts xeigtstz xlintstc xlintstd xlintsts xlintstz xlintstds xlintstrfc xlintstrfd xlintstrfs xlintstrfz xlintstzc

override_dh_auto_build-indep:
	$(MAKE) html
	# Remove useless doxygen-generated files
	find DOCS/explore-html/ \( -name '*.map' -or -name '*.md5' \) -delete

	$(MAKE) man

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test-arch:
	# Run BLAS tests, output the result, and stop if one test fails
	LD_LIBRARY_PATH=$$(pwd):$$LD_LIBRARY_PATH $(MAKE) blas_testing
	cat BLAS/*.out
	! grep -q -i fail BLAS/*.out

	# Run CBLAS tests, output the result, and stop if one test fails
	LD_LIBRARY_PATH=$$(pwd):$$LD_LIBRARY_PATH $(MAKE) cblas_testing
	cat CBLAS/testing/*.out
	! grep -q -i fail CBLAS/testing/*.out

	# Run LAPACK tests, but don't stop if one test fails
	# Only a summary is displayed; full test results in TESTING/testing_results.txt
	LD_LIBRARY_PATH=$$(pwd):$$LD_LIBRARY_PATH $(MAKE) lapack_testing
else
override_dh_auto_test-arch:
	touch TESTING/testing_results.txt
endif

override_dh_makeshlibs:
	dh_makeshlibs -pliblapack3 -V "liblapack3 | liblapack.so.3" -n
	dh_makeshlibs -plibblas3 -V "libblas3 | libblas.so.3" -n
	dh_makeshlibs -a -Nliblapack3 -Nlibblas3

override_dh_shlibdeps:
	dh_shlibdeps -a -l $$(pwd)

override_dh_strip:
	dh_strip --ddeb-migration='liblapack-dbg (<< 3.6.0-1~)'
