# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=libxsmm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.17
pkgrel=1
pkgdesc="A library for small dense and small sparse matrix-matrix multiplications. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64')
url='https://github.com/libxsmm/libxsmm'
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-omp"
             python)
source=("https://github.com/libxsmm/libxsmm/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-on-mingw.patch")
sha256sums=('8b642127880e92e8a75400125307724635ecdf4020ca4481e5efe7640451bb92'
            '86702131633edd3e9bfa2d7ce3112071ea39e9ed47b6705a0ba1cf5ebd981469')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.gz..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -r build-${MSYSTEM}
  cp -r ${_realname}-${pkgver} "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  make \
    PREFIX=${MINGW_PREFIX} \
    STATIC=0 \
    OMP=1 \
    CTARGET="-march=x86-64-v3" \
    MKL=0 \
    PYTHON=/usr/bin/python \
    CFLAGS="-O2 -std=gnu17 -DLIBXSMM_BLAS_CONST=const"
}

package() {
  cd "build-${MSYSTEM}"

  make \
    PREFIX="${pkgdir}"${MINGW_PREFIX} \
    STATIC=0 \
    OMP=1 \
    CTARGET="-march=x86-64-v3" \
    MKL=0 \
    PYTHON=/usr/bin/python \
    CFLAGS="-O2 -std=gnu17 -DLIBXSMM_BLAS_CONST=const" \
    PPKGDIR=lib/pkgconfig \
    install

  # Fix pkgconfig files
  # for _pc_file in "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/*.pc; do
  #   sed -i "s|${srcdir}/build-${MSYSTEM}|${MINGW_PREFIX}|g" ${_pc_file}
  # done

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.md \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
