SUMMARY="Automatically Tuned Linear Algebra Software"
DESCRIPTION="A software library for linear algebra. It provides a mature open \
source implementation of BLAS APIs for C and Fortran77."
HOMEPAGE="http://math-atlas.sourceforge.net/"
COPYRIGHT="2011-2016 The ATLAS Project"
LICENSE="BSD (3-clause)"
REVISION="1"
SOURCE_URI="https://downloads.sf.net/math-atlas/atlas$portVersion.tar.bz2"
CHECKSUM_SHA256="2688eb733a6c5f78a18ef32144039adcd62fabce66f2eb51dd59dde806a6d2b7"
SOURCE_DIR="ATLAS"
archlinuxCommit="286cdb8eafb3756cdb3d63f0f6c6084e55a31169"
archlinuxRepoBaseUri="https://aur.archlinux.org/cgit/aur.git/plain/"
SOURCE_URI_2="$archlinuxRepoBaseUri/$SOURCE_FILENAME_2?h=atlas-lapack&id=$archlinuxCommit#noarchive"
CHECKSUM_SHA256_2="7627446a0d598b55bc8f2795f8ff2028f134928b9ca597b76bf8dd5622dbff69"
SOURCE_FILENAME_2="makefile.shared.mt"
SOURCE_URI_3="$archlinuxRepoBaseUri/$SOURCE_FILENAME_3?h=atlas-lapack&id=$archlinuxCommit#noarchive"
CHECKSUM_SHA256_3="18cfd59a104451c03c3b91020786260cf2b2581642a9c8f8598ba1d26b9cb3bf"
SOURCE_FILENAME_3="makefile.shared.st"
PATCHES="atlas-$portVersion.patchset"

ARCHITECTURES="?all !x86_gcc2"
SECONDARY_ARCHITECTURES="!x86_gcc2 ?x86"

PROVIDES="
	atlas$secondaryArchSuffix = $portVersion
	lib:libatlas$secondaryArchSuffix = $portVersion compat >=3
	lib:libcblas$secondaryArchSuffix = $portVersion compat >=3
	lib:libf77blas$secondaryArchSuffix = $portVersion compat >=3
	lib:liblapack$secondaryArchSuffix = $portVersion compat >=3
	lib:libptcblas$secondaryArchSuffix = $portVersion compat >=3
	lib:libptf77blas$secondaryArchSuffix = $portVersion compat >=3
	"
REQUIRES="
	haiku$secondaryArchSuffix
	lib:libgfortran$secondaryArchSuffix
	lib:libgomp$secondaryArchSuffix
	"
CONFLICTS="
	blas$secondaryArchSuffix
	cblas$secondaryArchSuffix
	lapack$secondaryArchSuffix
	"

PROVIDES_devel="
	atlas${secondaryArchSuffix}_devel = $portVersion
	devel:libatlas$secondaryArchSuffix = $portVersion compat >=3
	devel:libcblas$secondaryArchSuffix = $portVersion compat >=3
	devel:libf77blas$secondaryArchSuffix = $portVersion compat >=3
	devel:liblapack$secondaryArchSuffix = $portVersion compat >=3
	devel:libptcblas$secondaryArchSuffix = $portVersion compat >=3
	devel:libptf77blas$secondaryArchSuffix = $portVersion compat >=3
	"
REQUIRES_devel="
	atlas$secondaryArchSuffix == $portVersion base
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	"
BUILD_PREREQUIRES="
	binutils$secondaryArchSuffix
	cmd:awk
	cmd:bzip2
	cmd:fgrep
	cmd:find
	cmd:g++$secondaryArchSuffix
	cmd:gfortran$secondaryArchSuffix
	cmd:make
	cmd:sed
	"

BUILD()
{
	# Needs to be deleted!
	rm -rf haiku_build || true
	mkdir -p haiku_build
	cd haiku_build

	CORE=`sysinfo -cpu | grep -o "\w*MHz\w*" | grep -o '[0-9]*'` # CPU freq
	NCPU=`sysinfo -cpu | grep "MHz" | awk '{print $1}'` # CPU count

	if [ "$targetArchitecture" = "x86_64" ]; then
		ARCHITECTURE_BUILD_OPTS="-b 64 -s 2 -A x86SSE2 -V 768" # for x86_64
	else
		ARCHITECTURE_BUILD_OPTS="-b 32 -s 1 -A x86x87 -V 512" # for i686
	fi

	echo 'Configuring ATLAS'
	# Not an autotools package
	../configure \
		--prefix=$prefix \
		--incdir=$includeDir \
		--libdir=$libDir \
		$ARCHITECTURE_BUILD_OPTS \
		-Fa alg \
		-fPIC \
		--shared \
		-D c -DPentiumCPS=$CORE \
		-O 13 \
		-t $NCPU \
		-m $CORE

	echo 'Building ATLAS'
	make build

	echo 'Building shared libraries'
	cd lib
	if [ 1 -lt $NCPU ]; then
		cp "$sourceDir2/$SOURCE_FILENAME_2" makefile # shared.mt
	else
		cp "$sourceDir3/$SOURCE_FILENAME_3" makefile # shared.st
	fi
	make -f makefile $jobArgs
}

INSTALL()
{
	echo 'Installing'
	cd haiku_build
	make install

	cp -d lib/*.so* "$libDir"
	[ -e lib/libptlapack.a ] && cp lib/libptlapack.a "$libDir"

	ln -s $libDir/libblas.so $libDir/libblas.so.3
	ln -s $libDir/liblapack.so.3 $libDir/liblapack.so

	prepareInstalledDevelLibs \
		libatlas \
		libcblas \
		libf77blas \
		liblapack \
		libptcblas \
		libptf77blas

	packageEntries devel \
		$developDir

	echo "This package provides a generic, not optimized version of ATLAS."
	echo "In order to improve performance, we recommend compiling a version of ATLAS"
	echo "optimized for your processor, using the atlas-lapack package in HaikuPorts"
}

TEST()
{
	echo 'Testing'
	cd haiku_build
	make check
	make ptcheck
	make time
}
