#!/usr/bin/make -f

# 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) -fno-whole-file

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

ARCH:=$(shell dpkg --print-architecture)
CPU:=$(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)

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

# Necessary to avoid test failures
ifneq (,$(filter $(CPU),i386 m68k))
  FFLAGS_EXTRA := -ffloat-store
endif


SO=3
VERS=$(SO).0

VN:=$(shell pwd)
VO:=$(VN).orig
UD:=$(shell dirname $$(pwd))

PDF:=cinterface
PDD:=debian/patched-docs
PDP:=$(addprefix $(PDD)/,$(PDF))
PDS:=$(addsuffix .tex,$(PDP))
PDO:=$(addsuffix .pdf,$(PDP))

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


# Rules for recreating the source tarball
# TODO: fix the rules and convert them to a get-orig-source rule

$(UD)/blas.tgz:
	mkdir -p $(@D)
	cd $(@D) && wget -N ftp://ftp.netlib.org/blas/$(@F)

$(UD)/cblas.tgz:
	mkdir -p $(@D)
	cd $(@D) && wget -N ftp://ftp.netlib.org/blas/blast-forum/$(@F)

$(UD)/manpages.tgz:
	mkdir -p $(@D)
	cd $(@D) && wget -N ftp://ftp.netlib.org/lapack/$(@F)

$(VO)/src: $(UD)/blas.tgz
	mkdir -p $@
	cd $@ && tar zxf $<

TF:=s d c z
TF:=$(addsuffix blat1,$(TF)) $(addsuffix blat2,$(TF)) $(addsuffix blat3,$(TF)) \
	$(addsuffix blat2d,$(TF)) $(addsuffix blat3d,$(TF))

$(addprefix $(VO)/test/,$(TF)):
	mkdir -p $(@D)
	cd $(@D) && wget -N ftp://ftp.netlib.org/blas/$(@F)

$(VO)/test: $(addprefix $(VO)/test/,$(TF))

$(VO)/man: $(UD)/manpages.tgz
	mkdir -p $(@D)
	cd $(VO) && tar zxf $< blas
	rm -rf $@
	mv $(VO)/blas/man $@
	rmdir $(VO)/blas

$(VO)/cblas: $(UD)/cblas.tgz
	cd $(VO) && tar zxf $<
	rm -rf $@
	mv $(VO)/CBLAS $@

DF:=faq.html 
$(addprefix $(VO)/doc/,$(DF)):
	mkdir -p $(@D)
	cd $(@D) && wget -N ftp://ftp.netlib.org/blas/$(@F)

$(VO)/doc/cinterface.pdf:
	mkdir -p $(@D)
	cd $(@D) && wget -N ftp://ftp.netlib.org/blas/blast-forum/$(@F)

$(VO)/doc/blue.gif:
	mkdir -p $(@D)
	cd $(@D) && wget -N ftp://ftp.netlib.org/scalapack/html/gif/$(@F)

%.png: %.gif
	gif2png $<

$(VO)/doc: $(addprefix $(VO)/doc/,$(DF)) $(VO)/doc/blue.png

ori: $(addprefix $(VO)/,src test man cblas doc)


# Compilation rules

BSRC:=$(shell echo src/*.f)
BOBJ:=$(BSRC:.f=.o)
BOBJ1:=$(filter-out src/izamax.o,$(filter-out src/icamax.o,$(BOBJ)))
BOBJ2:=src/icamax.o src/izamax.o

debian/shared_dir debian/static_dir debian/test_dir:
	mkdir -p $(subst _dir,,$(@))
	touch $@

$(subst src/,debian/static/,$(BOBJ1)): \
	debian/static/%.o : src/%.f debian/static_dir

	$(F77) $(FFLAGS) -c $< -o $@	

$(subst src/,debian/shared/,$(BOBJ1)): \
	debian/shared/%.o : src/%.f debian/shared_dir

	$(F77) $(FFLAGS) -fPIC -c $< -o $@	

$(subst src/,debian/static/,$(BOBJ2)): \
	debian/static/%.o : src/%.f debian/static_dir

	$(F77) $(FFLAGS) $(FFLAGS_EXTRA) -c $< -o $@	

$(subst src/,debian/shared/,$(BOBJ2)): \
	debian/shared/%.o : src/%.f debian/shared_dir

	$(F77) $(FFLAGS) $(FFLAGS_EXTRA) -fPIC -c $< -o $@	

libblas.a: cblas/libcblas.a $(subst src/,debian/static/,$(BOBJ)) debian/static_dir
	cd debian/static && ar x ../../$<
	ar r $@ debian/static/*.o

libblas.so.$(VERS): cblas/libcblas_pic.a $(subst src/,debian/shared/,$(BOBJ)) debian/shared_dir
	cd debian/shared && ar x ../../$<
	$(F77) $(LDFLAGS) -shared -Wl,-soname=libblas.so.$(SO) -o $@ debian/shared/*.o

libblas.so.$(SO): libblas.so.$(VERS)
	ln -snf $< $@

libblas.so: libblas.so.$(SO)
	ln -snf $< $@

cblas/Makefile.in: cblas/Makefile.LINUX
	cat $< | sed -e "s,^BLLIB = .*,BLLIB = -L $$(pwd) -lblas,1" \
		     -e "s,^CBDIR = .*,CBDIR = $$(pwd)/cblas,1" \
		     -e "s,^CC = .*,CC = $(CC) ,1" \
		     -e "s,^FC = .*,FC = $(F77) ,1" \
		     -e "s,^CFLAGS = .*,CFLAGS = $(CPPFLAGS) $(CFLAGS) \$$(PICOPTS) -DADD_,1" \
		     -e "s,^FFLAGS = .*,FFLAGS = $(FFLAGS) \$$(PICOPTS) ,1" \
			>$@
	echo "LOADFLAGS = $(LDFLAGS)" >>$@

cblas/libcblas.a: cblas/Makefile.in
	cd $(@D) && $(MAKE) cleanobj 
	cd $(@D) && $(MAKE) rmlib
	mkdir -p cblas/lib/LINUX
	cd $(@D) && $(MAKE) alllib
	cp cblas/lib/LINUX/cblas_LINUX.a $@

cblas/libcblas_pic.a: cblas/Makefile.in
	cd $(@D) && $(MAKE) cleanobj 
	cd $(@D) && $(MAKE) rmlib
	mkdir -p cblas/lib/LINUX
	cd $(@D) && $(MAKE) PICOPTS=-fPIC alllib
	cp cblas/lib/LINUX/cblas_LINUX.a $@

BIN:=s d c z
BIN:=$(addsuffix cblat1,$(BIN)) $(addsuffix cblat2,$(BIN)) $(addsuffix cblat3,$(BIN))
BIN:=$(addprefix cblas/testing/x,$(BIN))

$(BIN): libblas.so
	cd cblas && $(MAKE) alltst CBLIB=

FBN:=s d c z
FBN:=$(addsuffix blat1,$(FBN)) $(addsuffix blat2,$(FBN)) $(addsuffix blat3,$(FBN))
FBN:=$(addprefix test/x,$(FBN))

test/%.f: test/%
	cp $< $@

test/x%: test/%.f libblas.so 
	$(F77) $(FFLAGS) $(LDFLAGS) -o $@ $< -L $$(pwd) -lblas

OUT:=$(subst cblas/testing/,debian/test/,$(BIN))
OUT1:=$(filter %1,$(OUT))
OUT23:=$(filter-out %1,$(OUT))

$(OUT1): \
	debian/test/%: cblas/testing/% debian/test_dir libblas.so
	LD_LIBRARY_PATH=$$(pwd):$$LD_LIBRARY_PATH $< > $@
	awk '/fail/ || /FAIL/ {exit 1}' $@ || (cat $@ && false)

$(OUT23): \
	debian/test/%: cblas/testing/% debian/test_dir libblas.so
	LD_LIBRARY_PATH=$$(pwd):$$LD_LIBRARY_PATH $< \
		< cblas/testing/$(subst x,,$(subst cblat,in,$*)) > $@
	awk '/fail/ || /FAIL/ {exit 1}' $@ || (cat $@ && false)

FOT:=$(subst test/,debian/test/,$(FBN))
FOT1:=$(filter %1,$(FOT))
FOT23:=$(filter-out %1,$(FOT))

$(FOT1): \
	debian/test/%: test/% debian/test_dir libblas.so
	LD_LIBRARY_PATH=$$(pwd):$$LD_LIBRARY_PATH $< > $@
	awk '/fail/ || /FAIL/ {exit 1}' $@ || (cat $@ && false)

$(FOT23): \
	debian/test/%: test/% debian/test_dir libblas.so
	rm -f *.SUMM
	LD_LIBRARY_PATH=$$(pwd):$$LD_LIBRARY_PATH $< \
		< test/$(subst x,,$*)d
	cat *.SUMM >$@
	awk '/fail/ || /FAIL/ {exit 1}' $@ || (cat $@ && false)

debian/test_results: $(OUT) $(FOT)
	cat $^ > $@
	cat $@ | awk '/fail/ || /FAIL/ {exit 1}'

MFL:=$(shell echo man/manl/*)
%.3: %.l
	cat $< | sed "s,^.TH  *\([^ ]*\)  *l  *\(.*\),.TH \1 3 \2,1" >$@
	echo Converted $< to $@, diff: 
	diff -u $< $@ || true
MF:=$(subst .l,.3,$(MFL))

$(PDO): %.pdf: %.tex
	cd $(@D) && pdflatex $(<F) && pdflatex $(<F)


# Packaging rules

%:
	dh $@ --parallel

override_dh_auto_build-arch: libblas.so libblas.a $(BIN) $(FBN)

override_dh_auto_build-indep: $(MF) $(PDO)

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test-arch: debian/test_results
else
override_dh_auto_test-arch:
	touch debian/test_results
endif

override_dh_auto_clean: cblas/Makefile.in
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f debian/static/* debian/shared/* test/x* test/*.f libblas* cblas/libcblas* man/manl/*.3
	rm -f *SUMM
	rm -rf debian/test* debian/shared* debian/static*
	cd $(PDD) && rm -rf *.pdf *.log *.aux *.ps *.dvi

	-rm -rf static shared
	cd cblas && $(MAKE) clean && $(MAKE) rmlib
	rm -f $<

override_dh_installman-indep:
	dh_installman -p libblas-doc man/manl/*.3

override_dh_installman-arch:
	dh_installman -p libblas-test debian/*.1

override_dh_makeshlibs:
	dh_makeshlibs -a -V "libblas3 | libblas.so.3" -n

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


.PHONY: $(addprefix $(UD)/,blas.tgz cblas.tgz manpages.tgz)
.SUFFIXES:
.INTERMEDIATE: $(VO)/doc/blue.gif

# This Makefile must be run serially (because non-PIC and PIC versions must be
# built sequentially), but the sub-makes can still be parallelized
.NOTPARALLEL:
