#Packager: George Vlahavas <vlahavas~at~gmail~dot~com>
#
# adapted from the SlackBuild by Kyle Guinn:
# https://slackbuilds.org/slackbuilds/14.1/libraries/blas/blas.SlackBuild

pkgname=blas
pkgver=3.6.0
pkgrel=1gv
source=("http://www.netlib.org/lapack/lapack-$pkgver.tgz" "cmake-piecewise.diff" "generate-pkgconfig.diff" "link-dependencies.diff" "target-cleanup.diff")
docs=("readme" "install" "copying" "changelog" "authors" "news" "todo" "license")
url=http://www.netlib.org/blas

slackdesc=\
(
#|-----handy-ruler------------------------------------------------------|
"blas (Basic Linear Algebra Subprograms)"
"The BLAS (Basic Linear Algebra Subprograms) are routines that provide"
"standard building blocks for performing basic vector and matrix"
"operations. The Level 1 BLAS perform scalar, vector, and vector-vector"
"operations, the Level 2 BLAS perform matrix-vector operations, and the"
"Level 3 BLAS perform matrix-matrix operations. Because the BLAS are"
"efficient, portable, and widely available, they are commonly used in"
"the development of high quality linear algebra software."
"Note: This package contains the reference *unoptimized* BLAS library."
)


build() {
	cd $startdir/src/lapack-$pkgver

	# Fix lots of bugs with the cmake build system and .pc files.
	# More importantly, allow building only the BLAS component.
	patch -p1 < $startdir/src/generate-pkgconfig.diff
	patch -p1 < $startdir/src/link-dependencies.diff
	patch -p1 < $startdir/src/target-cleanup.diff
	patch -p1 < $startdir/src/cmake-piecewise.diff

	# Avoid adding an RPATH entry to the shared lib.  It's unnecessary (except for
	# running the test suite), and it's broken on 64-bit (needs LIBDIRSUFFIX).
	mkdir -p shared
	cd shared
		cmake \
			-DCMAKE_Fortran_FLAGS:STRING="$SLKCFLAGS" \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DCMAKE_BUILD_TYPE=None \
			-DCMAKE_RULE_MESSAGES=OFF \
			-DCMAKE_VERBOSE_MAKEFILE=TRUE \
			-DBUILD_BLAS=ON \
			-DBUILD_TESTING=OFF \
			-DBUILD_SHARED_LIBS=ON \
			-DCMAKE_SKIP_RPATH=YES \
			..
		make -j $numjobs
		make install/strip DESTDIR=$startdir/pkg
	cd ..

	# cmake doesn't appear to let us build both shared and static libs
	# at the same time, so build it twice.  This may build a non-PIC library
	# on some architectures, which should be faster.
	mkdir -p static
	cd static
		cmake \
			-DCMAKE_Fortran_FLAGS:STRING="$SLKCFLAGS" \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DCMAKE_BUILD_TYPE=None \
			-DCMAKE_RULE_MESSAGES=OFF \
			-DCMAKE_VERBOSE_MAKEFILE=TRUE \
			-DBUILD_BLAS=ON \
			-DBUILD_TESTING=OFF \
			..
		make -j $numjobs
		make install/strip DESTDIR=$startdir/pkg
	cd ..

	# Generate man pages.  Requires some fix-ups:
	# 0.  Join all escaped newlines so the entire value is replaced.
	# 1.  Replace "LAPACK" with "BLAS" in headers/footers.
	# 2.  Only generate on the BLAS sources.
	# 3.  Turn off recursion.  Only operate on directories in INPUT.
	# 4.  Turn off some repetitive comments.
	# 5.  Turn off HAVE_DOT.  Graphs are unnecessary for this purpose.
	# 6.  Turn off MAN_LINKS.  See below.
	sed -i \
		-e ':a;/\\$/N;s/\\\n//;ta' \
		-e 's/^\(PROJECT_NAME *=\).*/\1 BLAS/' \
		-e 's/^\(INPUT *=\).*/\1 BLAS\/SRC/' \
		-e 's/^\(RECURSIVE *=\).*/\1 NO/' \
		-e 's/^\(REPEAT_BRIEF *=\).*/\1 NO/' \
		-e 's/^\(HAVE_DOT *=\).*/\1 NO/' \
		-e 's/^\(MAN_LINKS *=\).*/\1 NO/' \
		DOCS/Doxyfile_man
	doxygen DOCS/Doxyfile_man
	# Doxygen generates manpages on a file-by-file basis (note the .f extensions).
	# We want a page for each function, not each file.  MAN_LINKS creates a page
	# for each function that just sources the page for the corresponding file.
	# This adds a lot of bloat.  Luckily, functions map 1:1 with files, so we can
	# rename .f.3 -> .3 to have the page named after the function.
	mkdir -p $startdir/pkg/usr/man/man3
	for i in DOCS/man/man3/*.f.3; do
		gzip -9c $i > $startdir/pkg/usr/man/man3/$(basename $i .f.3).3.gz
	done
}