# Maintainer: Ranieri Althoff <ranisalt+aur@gmail.com>
# Contributor (MSYS2): Rafał Brzegowy <rafal.brzegfowy@yahoo.com>

_realname=mimalloc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.2.4
pkgrel=1
pkgdesc='General-purpose allocator with excellent performance characteristics (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/microsoft/mimalloc'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/microsoft/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('754a98de5e2912fddbeaf24830f982b4540992f1bab4a0a8796ee118e0752bda')

build() {
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DMI_BUILD_OBJECT=OFF \
      -DMI_INSTALL_TOPLEVEL=ON \
      -DMI_BUILD_TESTS=OFF \
      -DMI_WIN_REDIRECT=OFF \
      -S ${_realname}-${pkgver} \
      -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

check() {
  cmake -DMI_BUILD_TESTS=ON -S ${_realname}-${pkgver} -B build-${MSYSTEM}
  cmake --build build-${MSYSTEM}
  ctest build-${MSYSTEM} || true
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

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