# Version of bundled lapack
%global lapackver 3.8.0
%global lapacke 1

# Build 64-bit interface binaries?
%ifarch %{ix86}
%global build64 0
%global libblas    libblas.so.3 libcblas.so.3
%global liblapack  liblapack.so.3 liblapacke.so.3
%endif
%ifarch x86_64
%global build64 1
%global libblas    libblas.so.3()(64bit) libcblas.so.3()(64bit)
%global liblapack  liblapack.so.3()(64bit) liblapacke.so.3()(64bit)
%endif

Summary: An optimized BLAS library based on GotoBLAS2
Name:    openblas
Version: 0.3.19
Release: 1%{?_dist_release}

Group:   system
License: BSD
URL:     https://github.com/xianyi/OpenBLAS/

# Source0: https://github.com/xianyi/OpenBLAS/archive/v%{version}.tar.gz
Source0: https://github.com/xianyi/OpenBLAS/archive/v%{version}.tar.gz#/OpenBLAS-%{version}.tar.gz

# pkg-config file (modoified)
Source11: blas.pc.in
Source12: cblas.pc.in
Source13: lapack.pc.in
Source14: lapacke.pc.in

# Drop extra p from threaded library name
Patch1: openblas-0.2.5-libname.patch
# # Supply the proper flags to the test makefile
# Patch2: openblas-0.2.19-tests.patch
# # Disable CPU affinity, from upstream
# Patch3: https://patch-diff.githubusercontent.com/raw/xianyi/OpenBLAS/pull/1495.patch

BuildRoot: %{_tmppath}/%{name}-%{version}-root
BuildRequires: gcc-gfortran

Provides: blas lapack = %{lapackver} %{libblas} %{liblapack}

ExclusiveArch: x86_64 %{ix86}

%global base_description \
OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD \
version. The project is supported by the Lab of Parallel Software and \
Computational Science, ISCAS. http://www.rdcps.ac.cn

%description
%{base_description}


%package Rblas
Summary: A version of OpenBLAS for R to use as libRblas
Group:   system

%description Rblas
%{base_description}


%package openmp
Summary: An optimized BLAS library based on GotoBLAS2, OpenMP version
Group:   system
Provides: blas lapack = %{lapackver} %{libblas} %{liblapack}

%description openmp
%{base_description}

This package contains the library compiled with OpenMP support.


%package threads
Summary: An optimized BLAS library based on GotoBLAS2, pthreads version
Group:   system
Provides: blas lapack = %{lapackver} %{libblas} %{liblapack}

%description threads
%{base_description}

This package contains the library compiled with threading support.


%if %build64
%package serial64
Summary: An optimized BLAS library based on GotoBLAS2, serial version
Group:   system
Provides: blas64 lapack64 = %{lapackver}

%description serial64
%{base_description}

This package contains the sequential library compiled with a 64-bit
interface.


%package serial64_
Summary: An optimized BLAS library based on GotoBLAS2, serial version
Group:   system
Provides: blas64 lapack64 = %{lapackver}

%description serial64_
%{base_description}

This package contains the sequential library compiled with a 64-bit
interface and a symbol name suffix.


%package openmp64
Summary: An optimized BLAS library based on GotoBLAS2, OpenMP version
Group:   system
Provides: blas64 lapack64 = %{lapackver}

%description openmp64
%{base_description}

This package contains the library compiled with OpenMP support and
64-bit interface.


%package openmp64_
Summary: An optimized BLAS library based on GotoBLAS2, OpenMP version
Group:   system
Provides: blas64 lapack64 = %{lapackver}

%description openmp64_
%{base_description}

This package contains the library compiled with OpenMP support and
64-bit interface and a symbol name suffix.


%package threads64
Summary: An optimized BLAS library based on GotoBLAS2, pthreads version
Group:   system
Provides: blas64 lapack64 = %{lapackver}

%description threads64
%{base_description}

This package contains the library compiled with threading support and
64-bit interface.


%package threads64_
Summary: An optimized BLAS library based on GotoBLAS2, pthreads version
Group:   system
Provides: blas64 lapack64 = %{lapackver}

%description threads64_
%{base_description}

This package contains the library compiled with threading support and
64-bit interface and a symbol name suffix.
%endif


%package devel
Summary:  Development headers and libraries for OpenBLAS
Group:    programming
Requires: %{name} = %{version}-%{release}
Requires: %{name}-openmp = %{version}-%{release}
Requires: %{name}-threads = %{version}-%{release}

%description devel
%{base_description}

This package contains the development headers and libraries.


%package static
Summary:  Static version of OpenBLAS
Group:    programming
Requires: %{name}-devel = %{version}-%{release}

%description static
%{base_description}

This package contains the static libraries.


%if %build64
%package -n %{name}64-devel
Summary:  Development headers and libraries for OpenBLAS (64bit)
Group:    programming
Requires: %{name}-openmp64 = %{version}-%{release}
Requires: %{name}-threads64 = %{version}-%{release}
Requires: %{name}-serial64 = %{version}-%{release}
Requires: %{name}-openmp64_ = %{version}-%{release}
Requires: %{name}-threads64_ = %{version}-%{release}
Requires: %{name}-serial64_ = %{version}-%{release}
Requires: %{name}-devel = %{version}-%{release}

%description  -n %{name}64-devel
%{base_description}

This package contains the development headers and libraries
 compiled with a 64-bit interface.


%package -n %{name}64-static
Summary:  Static version of OpenBLAS (64bit)
Group:    programming
Requires: %{name}64-devel = %{version}-%{release}

%description -n %{name}64-static
%{base_description}

This package contains the static libraries
 compiled with a 64-bit interface.
%endif


%debug_package


%prep
%setup -q -c -T

# Untar source
tar zxf %{SOURCE0}
cd OpenBLAS-%{version}
%patch1 -p1 -b .libname
# %patch2 -p1 -b .tests
# %patch3 -p1 -b .affinity

# Fix source permissions
find -name \*.f -exec chmod 644 {} \;

# Make serial, threaded and OpenMP versions; as well as 64-bit versions
# Also make an libRblas.so
cd ..
cp -ar OpenBLAS-%{version} openmp
cp -ar OpenBLAS-%{version} threaded

%if %build64
for d in {serial,threaded,openmp}64{,_}; do
    cp -ar OpenBLAS-%{version} $d
done
%endif
cp -ar OpenBLAS-%{version} Rblas
mv OpenBLAS-%{version} serial

# Hackup Rblas Makefiles
sed -i 's|.so.$(MAJOR_VERSION)|.so|g' Rblas/Makefile
sed -i 's|.so.$(MAJOR_VERSION)|.so|g' Rblas/exports/Makefile
sed -i 's|@ln -fs $(LIBSONAME) $(LIBPREFIX).so|#@ln -fs $(LIBSONAME) $(LIBPREFIX).so|g' Rblas/Makefile

%build
# Maximum possible amount of processors
NMAX="NUM_THREADS=128"
TARGET="TARGET=CORE2 DYNAMIC_ARCH=1"
export AVX="NO_AVX2=1"

FCOMMON="%{optflags} -fPIC -frecursive"

make -C Rblas      $TARGET USE_THREAD=0 USEOPENMP=0 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libRblas" LIBSONAME="libRblas.so" $AVX $LAPACKE INTERFACE64=0

# Declare some necessary build flags
COMMON="%{optflags} -fPIC"
FCOMMON="$COMMON -frecursive"
make -C serial     $TARGET USE_THREAD=0 USE_OPENMP=0 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblas"      $AVX $LAPACKE INTERFACE64=0
make -C threaded   $TARGET USE_THREAD=1 USE_OPENMP=0 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblasp"     $AVX $LAPACKE INTERFACE64=0

# USE_THREAD determines use of SMP, not of pthreads
COMMON="%{optflags} -fPIC -fopenmp -pthread"
FCOMMON="$COMMON -frecursive"
make -C openmp     $TARGET USE_THREAD=1 USE_OPENMP=1 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblaso"     $AVX $LAPACKE INTERFACE64=0

%if %build64
COMMON="%{optflags} -fPIC"
FCOMMON="$COMMON -frecursive -fdefault-integer-8"
make -C serial64   $TARGET USE_THREAD=0 USE_OPENMP=0 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblas64"    $AVX $LAPACKE INTERFACE64=1
make -C threaded64 $TARGET USE_THREAD=1 USE_OPENMP=0 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblasp64"   $AVX $LAPACKE INTERFACE64=1

COMMON="%{optflags} -fPIC -fopenmp -pthread"
FCOMMON="$COMMON -frecursive -fdefault-integer-8"
make -C openmp64   $TARGET USE_THREAD=1 USE_OPENMP=1 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblaso64"   $AVX $LAPACKE INTERFACE64=1

COMMON="%{optflags} -fPIC"
FCOMMON="$COMMON -frecursive  -fdefault-integer-8"
make -C serial64_   $TARGET USE_THREAD=0 USE_OPENMP=0 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblas64_"  $AVX $LAPACKE INTERFACE64=1 SYMBOLSUFFIX=64_
make -C threaded64_ $TARGET USE_THREAD=1 USE_OPENMP=0 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblasp64_" $AVX $LAPACKE INTERFACE64=1 SYMBOLSUFFIX=64_

COMMON="%{optflags} -fPIC -fopenmp -pthread"
FCOMMON="$COMMON -frecursive -fdefault-integer-8"
make -C openmp64_   $TARGET USE_THREAD=1 USE_OPENMP=1 FC=gfortran CC=gcc COMMON_OPT="$COMMON" FCOMMON_OPT="$FCOMMON" $NMAX LIBPREFIX="libopenblaso64_" $AVX $LAPACKE INTERFACE64=1 SYMBOLSUFFIX=64_
%endif

%install
rm -rf %{buildroot}
# Install serial library and headers
make -C serial USE_THREAD=0 PREFIX=%{buildroot} OPENBLAS_LIBRARY_DIR=%{buildroot}%{_libdir}/ OPENBLAS_INCLUDE_DIR=%{buildroot}%{_includedir}/%{name} OPENBLAS_BINARY_DIR=%{buildroot}%{_bindir} OPENBLAS_CMAKE_DIR=%{buildroot}%{_libdir}/cmake install

slibname=`basename %{buildroot}%{_libdir}/libopenblas${suffix}-*.so .so`
mv %{buildroot}%{_libdir}/${slibname}.a \
   %{buildroot}%{_libdir}/lib%{name}.a
if [[ "$suffix" != "" ]]; then
   sname=$(echo $slibname | sed "s|$suffix||g")
   mv %{buildroot}%{_libdir}/${slibname}.so \
      %{buildroot}%{_libdir}/${sname}.so
else
   sname=${slibname}
fi

# Install the Rblas library
mkdir -p %{buildroot}%{_libdir}/R/lib/
install -p -m 755 Rblas/libRblas.so %{buildroot}%{_libdir}/R/lib/

# Install the OpenMP library
olibname=`echo ${slibname} | sed "s|lib%{name}|lib%{name}o|g"`
install -D -p -m 644 openmp/${olibname}.a \
        %{buildroot}%{_libdir}/lib%{name}o.a
if [[ "$suffix" != "" ]]; then
   oname=$(echo $olibname | sed "s|$suffix||g")
else
   oname=${olibname}
fi
install -D -p -m 755 openmp/${olibname}.so \
        %{buildroot}%{_libdir}/${oname}.so

# Install the threaded library
plibname=`echo ${slibname} | sed "s|lib%{name}|lib%{name}p|g"`
install -D -p -m 644 threaded/${plibname}.a \
        %{buildroot}%{_libdir}/lib%{name}p.a
if [[ "$suffix" != "" ]]; then
   pname=$(echo $plibname | sed "s|$suffix||g")
else
   pname=${plibname}
fi
install -D -p -m 755 threaded/${plibname}.so \
        %{buildroot}%{_libdir}/${pname}.so

# Install the 64-bit interface libraries
%if %build64
slibname64=`echo ${slibname} | sed "s|lib%{name}|lib%{name}64|g"`
install -D -p -m 644 serial64/${slibname64}.a \
        %{buildroot}%{_libdir}/lib%{name}64.a
slibname64_=`echo ${slibname} | sed "s|lib%{name}|lib%{name}64_|g"`
install -D -p -m 644 serial64_/${slibname64_}.a \
        %{buildroot}%{_libdir}/lib%{name}64_.a

if [[ "$suffix" != "" ]]; then
   sname64=$(echo ${slibname64} | sed "s|$suffix||g")
   sname64_=$(echo ${slibname64_} | sed "s|$suffix||g")
else
   sname64=${slibname64}
   sname64_=${slibname64_}
fi
install -D -p -m 755 serial64/${slibname64}.so \
        %{buildroot}%{_libdir}/${sname64}.so
install -D -p -m 755 serial64_/${slibname64_}.so \
        %{buildroot}%{_libdir}/${sname64_}.so

olibname64=`echo ${slibname} | sed "s|lib%{name}|lib%{name}o64|g"`
install -D -p -m 644 openmp64/${olibname64}.a \
        %{buildroot}%{_libdir}/lib%{name}o64.a
olibname64_=`echo ${slibname} | sed "s|lib%{name}|lib%{name}o64_|g"`
install -D -p -m 644 openmp64_/${olibname64_}.a \
        %{buildroot}%{_libdir}/lib%{name}o64_.a

if [[ "$suffix" != "" ]]; then
   oname64=$(echo ${olibname64} | sed "s|$suffix||g")
   oname64_=$(echo ${olibname64_} | sed "s|$suffix||g")
else
   oname64=${olibname64}
   oname64_=${olibname64_}
fi
install -D -p -m 755 openmp64/${olibname64}.so \
        %{buildroot}%{_libdir}/${oname64}.so
install -D -p -m 755 openmp64_/${olibname64_}.so \
        %{buildroot}%{_libdir}/${oname64_}.so

plibname64=`echo ${slibname} | sed "s|lib%{name}|lib%{name}p64|g"`
install -D -p -m 644 threaded64/${plibname64}.a \
        %{buildroot}%{_libdir}/lib%{name}p64.a
plibname64_=`echo ${slibname} | sed "s|lib%{name}|lib%{name}p64_|g"`
install -D -p -m 644 threaded64_/${plibname64_}.a \
        %{buildroot}%{_libdir}/lib%{name}p64_.a

if [[ "$suffix" != "" ]]; then
   pname64=$(echo $plibname64 | sed "s|$suffix||g")
   pname64_=$(echo $plibname64_ | sed "s|$suffix||g")
else
   pname64=${plibname64}
   pname64_=${plibname64_}
fi
install -D -p -m 755 threaded64/${plibname64}.so \
        %{buildroot}%{_libdir}/${pname64}.so
install -D -p -m 755 threaded64_/${plibname64_}.so \
        %{buildroot}%{_libdir}/${pname64_}.so
%endif

# Fix symlinks
pushd %{buildroot}%{_libdir}
# Serial libraries
ln -sf ${sname}.so lib%{name}.so
ln -sf ${sname}.so lib%{name}.so.0
# OpenMP libraries
ln -sf ${oname}.so lib%{name}o.so
ln -sf ${oname}.so lib%{name}o.so.0
# Threaded libraries
ln -sf ${pname}.so lib%{name}p.so
ln -sf ${pname}.so lib%{name}p.so.0

%if %build64
# Serial libraries
ln -sf ${sname64}.so lib%{name}64.so
ln -sf ${sname64}.so lib%{name}64.so.0
ln -sf ${sname64_}.so lib%{name}64_.so
ln -sf ${sname64_}.so lib%{name}64_.so.0
# OpenMP libraries
ln -sf ${oname64}.so lib%{name}o64.so
ln -sf ${oname64}.so lib%{name}o64.so.0
ln -sf ${oname64_}.so lib%{name}o64_.so
ln -sf ${oname64_}.so lib%{name}o64_.so.0
# Threaded libraries
ln -sf ${pname64}.so lib%{name}p64.so
ln -sf ${pname64}.so lib%{name}p64.so.0
ln -sf ${pname64_}.so lib%{name}p64_.so
ln -sf ${pname64_}.so lib%{name}p64_.so.0
%endif
popd

# Get rid of generated CMake config
rm -rf %{buildroot}%{_libdir}/cmake

# pkg-config
# fix libdir/includedir in openblas.pc
pushd %{buildroot}%{_libdir}/pkgconfig
mv openblas.pc tmp
echo "libdir=%{_libdir}" > openblas.pc
echo "includedir=%{_includedir}/%{name}" >> openblas.pc
sed -e "1,2d" tmp >> openblas.pc
rm -f tmp

sed "s/-lopenblas/-lopenblaso/" openblas.pc >> openblaso.pc
sed "s/-lopenblas/-lopenblasp/" openblas.pc >> openblasp.pc

%if %build64
sed "s/-lopenblas/-lopenblas64/" openblas.pc >> openblas64.pc
sed "s/-lopenblas/-lopenblaso64/" openblas.pc >> openblaso64.pc
sed "s/-lopenblas/-lopenblasp64/" openblas.pc >> openblasp64.pc
sed "s/-lopenblas/-lopenblas64_/" openblas.pc >> openblas64_.pc
sed "s/-lopenblas/-lopenblaso64_/" openblas.pc >> openblaso64_.pc
sed "s/-lopenblas/-lopenblasp64_/" openblas.pc >> openblasp64_.pc
%endif
popd

# blas/lapack
mkdir -p %{buildroot}%{_libdir}/%{name}

pushd %{buildroot}%{_libdir}/%{name}
cp %{SOURCE11} blas.pc
cp %{SOURCE12} cblas.pc
cp %{SOURCE13} lapack.pc
cp %{SOURCE14} lapacke.pc

# blas
sed -i 's|@libdir@|%{_libdir}|g' blas.pc
sed -i 's|@LAPACK_VERSION@|%{version}|g' blas.pc

# cblas
sed -i 's|@libdir@|%{_libdir}|g' cblas.pc
sed -i 's|@includedir@|%{_includedir}/%{name}|g' cblas.pc
sed -i 's|@LAPACK_VERSION@|%{version}|g' cblas.pc

# lapack
sed -i 's|@libdir@|%{_libdir}|g' lapack.pc
sed -i 's|@LAPACK_VERSION@|%{version}|g' lapack.pc

# lapacke
sed -i 's|@libdir@|%{_libdir}|g' lapacke.pc
sed -i 's|@includedir@|%{_includedir}/%{name}|g' lapacke.pc
sed -i 's|@LAPACK_VERSION@|%{version}|g' lapacke.pc

%if %build64
sed 's/-lblas/-blas64_/g' blas.pc > blas64.pc
sed 's/-lcblas/-cblas64_/g' cblas.pc > cblas64.pc
sed 's/-llapack/-llapack64_/g' lapack.pc > lapack64.pc
sed 's/-llapacke/-llapacke64_/g' lapacke.pc > lapacke64.pc
%endif
popd


%post
update-alternatives \
    --install %{_libdir}/libblas.so.3 libblas.so.3 \
              %{_libdir}/libopenblas.so.0 40 \
    --slave   %{_libdir}/libcblas.so.3 libcblas.so.3 \
              %{_libdir}/libopenblas.so.0
update-alternatives \
    --install %{_libdir}/liblapack.so.3 liblapack.so.3 \
              %{_libdir}/libopenblas.so.0 40 \
    --slave   %{_libdir}/liblapacke.so.3 liblapacke.so.3 \
              %{_libdir}/libopenblas.so.0

%preun
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas.so.3 %{_libdir}/libopenblas.so.0
    update-alternatives \
        --remove liblapack.so.3 %{_libdir}/libopenblas.so.0
fi


%post openmp
update-alternatives \
    --install %{_libdir}/libblas.so.3 libblas.so.3 \
              %{_libdir}/libopenblaso.so.0 42 \
    --slave   %{_libdir}/libcblas.so.3 libcblas.so.3 \
              %{_libdir}/libopenblaso.so.0
update-alternatives \
    --install %{_libdir}/liblapack.so.3 liblapack.so.3 \
              %{_libdir}/libopenblaso.so.0 42 \
    --slave   %{_libdir}/liblapacke.so.3 liblapacke.so.3 \
              %{_libdir}/libopenblaso.so.0

%preun openmp
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas.so.3 %{_libdir}/libopenblaso.so.0
    update-alternatives \
        --remove liblapack.so.3 %{_libdir}/libopenblaso.so.0
fi

%post threads
update-alternatives \
    --install %{_libdir}/libblas.so.3 libblas.so.3 \
              %{_libdir}/libopenblasp.so.0 45 \
    --slave   %{_libdir}/libcblas.so.3 libcblas.so.3 \
              %{_libdir}/libopenblasp.so.0
update-alternatives \
    --install %{_libdir}/liblapack.so.3 liblapack.so.3 \
              %{_libdir}/libopenblasp.so.0 45 \
    --slave   %{_libdir}/liblapacke.so.3 liblapacke.so.3 \
              %{_libdir}/libopenblasp.so.0

%preun threads
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas.so.3 %{_libdir}/libopenblasp.so.0
    update-alternatives \
        --remove liblapack.so.3 %{_libdir}/libopenblasp.so.0
fi

%if %build64
%post serial64_
update-alternatives \
    --install %{_libdir}/libblas64_.so.3 libblas64_.so.3 \
              %{_libdir}/libopenblas64_.so.0 40 \
    --slave   %{_libdir}/libcblas64_.so.3 libcblas64_.so.3 \
              %{_libdir}/libopenblas64_.so.0
update-alternatives \
    --install %{_libdir}/liblapack64_.so.3 liblapack64_.so.3 \
              %{_libdir}/libopenblas64_.so.0 40
update-alternatives \
    --install %{_libdir}/liblapacke64_.so.3 liblapacke64_.so.3 \
              %{_libdir}/libopenblas64_.so.0 40

%preun serial64_
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas64_.so.3 %{_libdir}/libopenblas64_.so.0
    update-alternatives \
        --remove liblapack64_.so.3 %{_libdir}/libopenblas64_.so.0
    update-alternatives \
        --remove liblapacke64_.so.3 %{_libdir}/libopenblas64_.so.0
fi

%post openmp64_
update-alternatives \
    --install %{_libdir}/libblas64_.so.3 libblas64_.so.3 \
              %{_libdir}/libopenblaso64_.so.0 42 \
    --slave   %{_libdir}/libcblas64_.so.3 libcblas64_.so.3 \
              %{_libdir}/libopenblaso64_.so.0
update-alternatives \
    --install %{_libdir}/liblapack64_.so.3 liblapack64_.so.3 \
              %{_libdir}/libopenblaso64_.so.0 42
update-alternatives \
    --install %{_libdir}/liblapacke64_.so.3 liblapacke64_.so.3 \
              %{_libdir}/libopenblaso64_.so.0 42

%preun openmp64_
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas64_.so.3 %{_libdir}/libopenblaso64_.so.0
    update-alternatives \
        --remove liblapack64_.so.3 %{_libdir}/libopenblaso64_.so.0
    update-alternatives \
        --remove liblapacke64_.so.3 %{_libdir}/libopenblaso64_.so.0
fi

%post threads64_
update-alternatives \
    --install %{_libdir}/libblas64_.so.3 libblas64_.so.3 \
              %{_libdir}/libopenblasp64_.so.0 45 \
    --slave   %{_libdir}/libcblas64_.so.3 libcblas64_.so.3 \
              %{_libdir}/libopenblasp64_.so.0
update-alternatives \
    --install %{_libdir}/liblapack64_.so.3 liblapack64_.so.3 \
              %{_libdir}/libopenblasp64_.so.0 45
update-alternatives \
    --install %{_libdir}/liblapacke64_.so.3 liblapacke64_.so.3 \
              %{_libdir}/libopenblasp64_.so.0 45

%preun threads64_
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas64_.so.3 %{_libdir}/libopenblasp64_.so.0
    update-alternatives \
        --remove liblapack64_.so.3 %{_libdir}/libopenblasp64_.so.0
    update-alternatives \
        --remove liblapacke64_.so.3 %{_libdir}/libopenblasp64_.so.0
fi
%endif

%post devel
update-alternatives \
    --install %{_libdir}/libblas.so libblas.so \
              %{_libdir}/libopenblas.so 40 \
    --slave   %{_libdir}/libcblas.so libcblas.so \
              %{_libdir}/libopenblas.so \
    --slave   %{_libdir}/pkgconfig/blas.pc blas.pc \
              %{_libdir}/%{name}/blas.pc \
    --slave   %{_libdir}/pkgconfig/cblas.pc cblas.pc \
              %{_libdir}/%{name}/cblas.pc
update-alternatives \
    --install %{_libdir}/liblapack.so liblapack.so \
              %{_libdir}/libopenblas.so 40 \
    --slave   %{_libdir}/liblapacke.so liblapacke.so \
              %{_libdir}/libopenblas.so \
    --slave   %{_libdir}/pkgconfig/lapack.pc lapack.pc \
              %{_libdir}/%{name}/lapack.pc \
    --slave   %{_libdir}/pkgconfig/lapacke.pc lapacke.pc \
              %{_libdir}/%{name}/lapacke.pc

%preun devel
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas.so %{_libdir}/libopenblas.so
    update-alternatives \
        --remove liblapack.so %{_libdir}/libopenblas.so
fi

%if %build64
%post -n %{name}64-devel
update-alternatives \
    --install %{_libdir}/libblas64_.so libblas64_.so \
              %{_libdir}/libopenblas64_.so 40 \
    --slave   %{_libdir}/libcblas64_.so libcblas64_.so \
              %{_libdir}/libopenblas64_.so \
    --slave   %{_libdir}/pkgconfig/blas64.pc blas64.pc \
              %{_libdir}/%{name}/blas64.pc \
    --slave   %{_libdir}/pkgconfig/cblas64.pc cblas64.pc \
              %{_libdir}/%{name}/cblas64.pc
update-alternatives \
    --install %{_libdir}/liblapack64_.so liblapack64_.so \
              %{_libdir}/libopenblas64_.so 40 \
    --slave   %{_libdir}/pkgconfig/lapack64.pc lapack64.pc \
              %{_libdir}/%{name}/lapack64.pc
update-alternatives \
    --install %{_libdir}/liblapacke64_.so liblapacke64_.so \
              %{_libdir}/libopenblas64_.so 40 \
    --slave   %{_libdir}/pkgconfig/lapacke64.pc lapacke64.pc \
              %{_libdir}/%{name}/lapacke64.pc

%preun -n %{name}64-devel
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas64_.so %{_libdir}/libopenblas64_.so
    update-alternatives \
        --remove liblapack64_.so %{_libdir}/libopenblas64_.so
    update-alternatives \
        --remove liblapacke64_.so %{_libdir}/libopenblas64_.so
fi
%endif

%post static
update-alternatives \
    --install %{_libdir}/libblas.a libblas.a \
              %{_libdir}/libopenblas.a 40 \
    --slave   %{_libdir}/libcblas.a libcblas.a \
              %{_libdir}/libopenblas.a
update-alternatives \
    --install %{_libdir}/liblapack.a liblapack.a \
              %{_libdir}/libopenblas.a 40 \
    --slave   %{_libdir}/liblapacke.a liblapacke.a \
              %{_libdir}/libopenblas.a

%preun static
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas.a %{_libdir}/libopenblas.a
    update-alternatives \
        --remove liblapack.a %{_libdir}/libopenblas.a
fi


%if %build64
%post -n %{name}64-static
update-alternatives \
    --install %{_libdir}/libblas64_.a libblas64_.a \
              %{_libdir}/libopenblas64_.a 40 \
    --slave   %{_libdir}/libcblas64_.a libcblas64_.a \
              %{_libdir}/libopenblas64_.a
update-alternatives \
    --install %{_libdir}/liblapack64_.a liblapack64_.a \
              %{_libdir}/libopenblas64_.a 40
update-alternatives \
    --install %{_libdir}/liblapacke64_.a liblapacke64_.a \
              %{_libdir}/libopenblas64_.a 40

%preun -n %{name}64-static
if [ "$1" = "0" ]
then
    update-alternatives \
        --remove libblas64_.a %{_libdir}/libopenblas64_.a
    update-alternatives \
        --remove liblapack64_.a %{_libdir}/libopenblas64_.a
    update-alternatives \
        --remove liblapacke64_.a %{_libdir}/libopenblas64_.a
fi
%endif


%clean
rm -rf %{buildroot}


%files
%doc serial/Changelog.txt serial/GotoBLAS* serial/LICENSE
%{_libdir}/lib%{name}-*.so
%{_libdir}/lib%{name}.so.*

%files openmp
%doc openmp/Changelog.txt openmp/GotoBLAS* openmp/LICENSE
%{_libdir}/lib%{name}o-*.so
%{_libdir}/lib%{name}o.so.*

%files threads
%doc threaded/Changelog.txt threaded/GotoBLAS* threaded/LICENSE
%{_libdir}/lib%{name}p-*.so
%{_libdir}/lib%{name}p.so.*

%if %build64
%files serial64
%doc serial64/Changelog.txt serial64/GotoBLAS* serial64/LICENSE
%{_libdir}/lib%{name}64-*.so
%{_libdir}/lib%{name}64.so.*

%files serial64_
%doc serial64_/Changelog.txt serial64_/GotoBLAS* serial64_/LICENSE
%{_libdir}/lib%{name}64_-*.so
%{_libdir}/lib%{name}64_.so.*

%files openmp64
%doc openmp64/Changelog.txt openmp64/GotoBLAS* openmp64/LICENSE
%{_libdir}/lib%{name}o64-*.so
%{_libdir}/lib%{name}o64.so.*

%files openmp64_
%doc openmp64_/Changelog.txt openmp64_/GotoBLAS* openmp64_/LICENSE
%{_libdir}/lib%{name}o64_-*.so
%{_libdir}/lib%{name}o64_.so.*

%files threads64
%doc threaded64/Changelog.txt threaded64/GotoBLAS* threaded64/LICENSE
%{_libdir}/lib%{name}p64-*.so
%{_libdir}/lib%{name}p64.so.*

%files threads64_
%doc threaded64_/Changelog.txt threaded64_/GotoBLAS* threaded64_/LICENSE
%{_libdir}/lib%{name}p64_-*.so
%{_libdir}/lib%{name}p64_.so.*
%endif

%files Rblas
%dir %{_libdir}/R/lib
%{_libdir}/R/lib/libRblas.so

%files devel
%{_includedir}/%{name}/
%{_libdir}/lib%{name}.so
%{_libdir}/lib%{name}o.so
%{_libdir}/lib%{name}p.so
%{_libdir}/pkgconfig/openblas.pc
%{_libdir}/pkgconfig/openblaso.pc
%{_libdir}/pkgconfig/openblasp.pc
%dir %{_libdir}/%{name}
%{_libdir}/%{name}/blas.pc
%{_libdir}/%{name}/cblas.pc
%{_libdir}/%{name}/lapack.pc
%{_libdir}/%{name}/lapacke.pc

%files static
%{_libdir}/lib%{name}.a
%{_libdir}/lib%{name}o.a
%{_libdir}/lib%{name}p.a

%if %build64
%files -n %{name}64-devel
%{_libdir}/lib%{name}64.so
%{_libdir}/lib%{name}o64.so
%{_libdir}/lib%{name}p64.so
%{_libdir}/lib%{name}64_.so
%{_libdir}/lib%{name}o64_.so
%{_libdir}/lib%{name}p64_.so
%{_libdir}/pkgconfig/*64.pc
%{_libdir}/pkgconfig/*64_.pc
%dir %{_libdir}/%{name}
%{_libdir}/%{name}/*64*.pc

%files -n %{name}64-static
%{_libdir}/lib%{name}64.a
%{_libdir}/lib%{name}o64.a
%{_libdir}/lib%{name}p64.a
%{_libdir}/lib%{name}64_.a
%{_libdir}/lib%{name}o64_.a
%{_libdir}/lib%{name}p64_.a
%endif


%changelog
* Tue Jan 25 2022 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.3.19-1
- new upstream release.
- dropped ldconfig scriptlets.

* Sun Feb 28 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.3.13-1
- new upstream release.

* Mon Oct 14 2019 Toshiaki Ara <ara_t@384.jp> 0.3.7-1
- update to 0.3.7

* Fri Jan 25 2019 Toshiaki Ara <ara_t@384.jp> 0.3.3-2
- rebuild with gfortran-8.2.0

* Sat Oct 06 2018 Toshiaki Ara <ara_t@384.jp> 0.3.3-1
- update to 0.3.3
- drop Patch2 and 3

* Mon Jul 02 2018 Toshiaki Ara <ara_t@384.jp> 0.2.20-2
- divide subpackages: openblas64-devel openblas64-static
- fix wrong Provides

* Wed Jun 27 2018 Toshiaki Ara <ara_t@384.jp> 0.2.20-1
- initial build for Vine Linux (only x86_64 and %%{ix86})

