# Copyright (C) 2020  Dave love, University of Manchester
# Licence: MIT

%global commit ecbe6c0a79876f367a3b01ef9cfa325b788d79df
%global shortcommit  %(c=%{commit}; echo ${c:0:7})

%global sover .0

%{?el7:%global dts devtoolset-9}

Name:		relapack
Version:	1.0
Release:	3.git%shortcommit%{?dist}
Summary:	LAPACK with recursive kernel algorithms
License:	MIT
URL:		https://github.com/HPAC/ReLAPACK
Source0:	https://github.com/HPAC/ReLAPACK/archive/%commit/%name-%shortcommit.tar.gz
BuildRequires:	%{?dts:%dts-}gcc-gfortran openblas-devel openblas-static

%description
ReLAPACK offers a collection of recursive algorithms for many of
LAPACK's compute kernels. Since it preserves LAPACK's established
interfaces, ReLAPACK integrates effortlessly into existing application
codes. ReLAPACK's routines not only outperform the reference LAPACK
but also improve upon the performance of tuned implementations, such
as OpenBLAS and MKL.

%package devel
Summary:	Development files for %name
Requires:	%name = %version-%release

%description devel
Development files for %name.


%prep
%setup -q -n ReLAPACK-%commit
cat <<EOF >README.Fedora
Fedora ReLAPACK packaging
-------------------------

The package contains librelapack, to be linked explicitly, and also
a liblapack.so.3 in %_libdir/%name which may be dynamically linked in
place of the reference liblapack (via LD_LIBRARY_PATH or ld.so.conf).
The latter is built from reference lapack with the relevant routines
replaced by the ReLAPACK ones, to be linked with a libblas (which might
be as supplied similarly by the blis package, for instance).
EOF


%build
%{?el7:source /opt/rh/%dts/enable}
%make_build CFLAGS="%build_cflags -O3 -ffast-math -fPIC"
mkdir obj; cd obj
ar x ../librelapack.a
gcc -shared -Wl,-soname=librelapack.so%sover -o librelapack.so%sover *.o %build_ldflags -lopenblas
ln -s librelapack.so%sover librelapack.so
ar x %_libdir/libopenblas.a
ar x ../librelapack.a
gcc -shared -Wl,-soname=liblapack.so.3 -o liblapack.so.3 *.o %build_ldflags -lopenblas


%install
%{?el7:source /opt/rh/%dts/enable}
mkdir -p %buildroot%_libdir/%name %buildroot%_includedir
cp -a obj/librelapack.so* %buildroot%_libdir
cp -a obj/liblapack.so* %buildroot%_libdir/%name
cp -a inc/relapack.h %buildroot%_includedir


%check
%{?el7:source /opt/rh/%dts/enable}
%make_build test CFLAGS="%build_cflags -O3 -ffast-math" LDFLAGS="%build_ldflags" LINK_TEST='-lopenblas -lgfortran -lm'
# Ensure we can actually link Fortran
cd examples
gfortran linsys.c chegv.f ../librelapack.a -lopenblas
./a.out


%ldconfig_scriptlets

%files
%license LICENSE
%doc README.md coverage.md README.Fedora
%_libdir/librelapack.so%sover
%_libdir/relapack/liblapack.so.3

%files devel
%_includedir/relapack.h
%_libdir/librelapack.so


%changelog
* Fri Sep  4 2020 Dave Love <loveshack@fedoraproject.org> - 1.0-3.git%(c=%{commit}; echo ${c:0:7})%{?dist}
- Build against openblas

* Wed Jul  8 2020 Dave Love <loveshack@fedoraproject.org> - 1.0-2.git%shortcommit
- Update to new snapshot