# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>

_realname=OpenBLAS
pkgbase=mingw-w64-openblas
pkgname="${MINGW_PACKAGE_PREFIX}-openblas"
pkgver=0.3.13
pkgrel=2
pkgdesc="An optimized BLAS library based on GotoBLAS2 1.13 BSD, providing optimized blas, lapack, and cblas (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.openblas.net/"
license=('BSD')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-openblas-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("perl"
             #"${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
options=('!buildflags')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/xianyi/OpenBLAS/archive/v${pkgver}.tar.gz
        001-defaultlib-not-for-gcc.patch
        002-lgfortran-requires-lquadmath.patch
        004-fix-generate-cmake-config.patch)
install=${_realname}.install
sha256sums=('79197543b17cc314b7e43f7a33148c308b0807cd6381ee77f77e15acf3e6459e'
            'd9192818d0b0a9e7cbed7cc2572dfd380aa311be5ce4d012c0cb55211013db37'
            'ab1c10a66b4d0332f2339f1169c0e3ebd48d9b8bf7afaec3f6a0d62099b52941'
            '3021941b502010169fb77a6cd45158c95fa207795d4b9955f0f75974b16eb500')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-defaultlib-not-for-gcc.patch
  patch -p1 -i ${srcdir}/002-lgfortran-requires-lquadmath.patch
  patch -p1 -i ${srcdir}/004-fix-generate-cmake-config.patch
}

build() {
  cp -rf "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${CARCH}
  cd "${srcdir}"/build-${CARCH}

  unset CFLAGS
  unset CXXFLAGS

  make LIBNAME=libopenblasp.a NO_LAPACK=0 DYNAMIC_ARCH=1   \
       CC=${MINGW_PREFIX}/bin/gcc                                      \
       FC=${MINGW_PREFIX}/bin/gfortran                                 \
       OPENBLAS_INCLUDE_DIR=${MINGW_PREFIX}/include/${_realname}       \
       USE_THREAD=1 NUM_THREADS=64 TARGET=CORE2

  # [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  # mkdir -p build-${CARCH} && cd build-${CARCH}
  
  # MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  # ${MINGW_PREFIX}/bin/cmake \
    # -G"MSYS Makefiles" \
    # -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    # -DDYNAMIC_ARCH=ON \
    # -DCMAKE_BUILD_TYPE=Release \
    # ../${_realname}-${pkgver}
  # make
}

package() {
  cd "${srcdir}"/build-${CARCH}
  #make DESTDIR=${pkgdir} install

  if [ -f libopenblas.dll ]; then
    if [ -f exports/libopenblas.def ]; then
      dlltool -D libopenblas.dll -d exports/libopenblas.def -l libopenblas.dll.a
    else
      echo "Cannot build export library" >&2
      exit 1
    fi
  else
    echo "DLL not found" >&2
    exit 1
  fi
  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib"

  make LIBNAME=libopenblasp.a \
       DESTDIR="${pkgdir}" PREFIX="${MINGW_PREFIX}"  \
       OPENBLAS_INCLUDE_DIR=${MINGW_PREFIX}/include/${_realname} \
       install

  if [ -f libopenblas.dll ]; then
    cp -f libopenblas.dll "${pkgdir}${MINGW_PREFIX}/bin/"
  else
    echo "DLL not found" >&2
    exit 1
  fi

  if [ -f libopenblas.dll.a ]; then
    cp -f libopenblas.dll.a "${pkgdir}${MINGW_PREFIX}/lib/"
  else
    echo "Export library not found" >&2
    exit 1
  fi

  if [ -f libopenblasp.a ]; then
    rm -f "${pkgdir}${MINGW_PREFIX}/lib/libopenblasp.a"
    rm -f "${pkgdir}${MINGW_PREFIX}/lib/libopenblas.a"
    cp -f libopenblasp.a "${pkgdir}${MINGW_PREFIX}/lib/libopenblas.a"
  else
    echo "Static library not found" >&2
    exit 1
  fi

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/openblas/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  # Install License
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm0644 ${srcdir}/${_realname}-${pkgver}/lapack-netlib/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-lapack
}
