# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Ronald van Haren <ronald.archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>
# Contributor: damir <damir@archlinux.org>
# Contributor: Jason Taylor <jftaylor21@gmail.com>

pkgbase=lapack
pkgname=(blas
         cblas
         lapack
         lapacke
         blas64
         cblas64
         lapack64
         lapacke64
         lapack-doc)
pkgver=3.12.0
pkgrel=5.1
url='https://www.netlib.org/lapack'
pkgdesc='Linear Algebra PACKage'
makedepends=(cmake
             doxygen
             gcc-fortran
             python)
arch=(x86_64 powerpc64le powerpc64 powerpc riscv64)
license=(custom)
source=(https://github.com/Reference-LAPACK/lapack/archive/v$pkgver/$pkgbase-$pkgver.tar.gz)
sha256sums=('eac9570f8e0ad6f30ce4b963f4f033f0f643e7c3912fc9ee6cd99120675ad48b')

build() {
  cmake -B build -S $pkgbase-$pkgver \
    -DCMAKE_SKIP_RPATH=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_Fortran_COMPILER=gfortran \
    -DLAPACKE_WITH_TMG=ON \
    -DCBLAS=ON \
    -DBUILD_DEPRECATED=ON \
    -DBUILD_MAN_DOCUMENTATION=ON
  cmake --build build
  cmake --build build --target man

  cmake -B build64 -S $pkgbase-$pkgver \
    -DCMAKE_SKIP_RPATH=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_Fortran_COMPILER=gfortran \
    -DLAPACKE_WITH_TMG=ON \
    -DCBLAS=ON \
    -DBUILD_DEPRECATED=ON \
    -DBUILD_INDEX64=ON
  cmake --build build64
}

package_lapack() {
  depends=(blas
           gcc-libs
           glibc)
  
  DESTDIR="$pkgdir" cmake --install build
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/

  rm -r "$pkgdir"/usr/lib/{libblas.*,libcblas.*,liblapacke.*}
  rm -r "$pkgdir"/usr/lib/pkgconfig/{blas.*,cblas.*,lapacke.*}
  rm -r "$pkgdir"/usr/lib/cmake/{cblas*,lapacke*}
  rm -r "$pkgdir"/usr/include
}

package_blas() {
  pkgdesc='Basic Linear Algebra Subprograms'
  depends=(gcc-libs
           glibc)

  DESTDIR="$pkgdir" cmake --install build/BLAS
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
}

package_cblas() {
  pkgdesc='C interface to BLAS'
  depends=(blas
           glibc)

  DESTDIR="$pkgdir" cmake --install build/CBLAS
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
}

package_lapacke() {
  pkgdesc='C interface to LAPACK'
  depends=(glibc
           lapack)

  DESTDIR="$pkgdir" cmake --install build/LAPACKE
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
}

package_lapack64() {
  pkgdesc='Linear Algebra PACKage (64-bit integers)'
  depends=(blas64
           gcc-libs
           glibc)

  DESTDIR="$pkgdir" cmake --install build64
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/

  rm -r "$pkgdir"/usr/lib/{libblas64.*,libcblas64.*,liblapacke64.*}
  rm -r "$pkgdir"/usr/lib/pkgconfig/{blas64.*,cblas64.*,lapacke64.*}
  rm -r "$pkgdir"/usr/lib/cmake/{cblas*,lapacke*}
  rm -r "$pkgdir"/usr/include
}

package_blas64() {
  pkgdesc='Basic Linear Algebra Subprograms (64-bit integers)'
  depends=(gcc-libs
           glibc)

  DESTDIR="$pkgdir" cmake --install build64/BLAS
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
}

package_cblas64() {
  pkgdesc='C interface to BLAS (64-bit integers)'
  depends=(blas64
           glibc)
  optdepends=('cblas: development headers')

  DESTDIR="$pkgdir" cmake --install build64/CBLAS
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -r "$pkgdir"/usr/include # Provided by cblas
}

package_lapacke64() {
  pkgdesc='C interface to LAPACK (64-bit integers)'
  depends=(glibc
           lapack64)
  optdepends=('lapacke: development headers')
 
  DESTDIR="$pkgdir" cmake --install build64/LAPACKE
  install -Dm644 $pkgbase-$pkgver/LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -r "$pkgdir"/usr/include # Provided by lapacke
}

package_lapack-doc() {
  pkgdesc='Man pages for BLAS/LAPACK'

  mkdir -p "$pkgdir"/usr/share
  cp -r build/DOCS/man "$pkgdir"/usr/share
  rm "$pkgdir"/usr/share/man/man3/isnan.3 # conflicts with man-pages
}
