# Maintainer of AUR version: seth <getchoo at tuta dot io>
# Contributor: xantares

# This version has static libs enabled and the correct architecture (in contrast to the AUR version).

pkgname=mingw-w64-clang-aarch64-zstd
_pkgname=zstd
pkgver=1.5.7
pkgrel=1
pkgdesc="Zstandard - Fast real-time compression algorithm (mingw-w64-clang-aarch64)"
url="https://facebook.github.io/zstd/"
arch=('any')
license=('BSD-3-Clause OR GPL-2.0-or-later')
depends=('mingw-w64-clang-aarch64-crt')
makedepends=('mingw-w64-clang-aarch64-cmake' 'ninja')
options=('staticlibs' '!buildflags' '!strip')
source=(https://github.com/facebook/zstd/releases/download/v${pkgver}/zstd-${pkgver}.tar.zst{,.sig})
sha256sums=('5b331d961d6989dc21bb03397fc7a2a4d86bc65a14adc5ffbbce050354e30fd2'
            'SKIP')
b2sums=('20df0493d9d960b8fc98f7cac7630f84dae6226bd906364ab9c9ce7dfb2be44a303fd077e4264979f3ee92efd59072d63c4bb4ae7b65c52f6ce0153513088837'
        'SKIP')
validpgpkeys=(4EF4AC63455FC9F4545D9B7DEF8FE99528B52FFD)

_architectures="aarch64-w64-mingw32"

build() {
  cd "${srcdir}/${_pkgname}-${pkgver}"

  export CFLAGS+=' -ffat-lto-objects'
  export CXXFLAGS+=' -ffat-lto-objects'
  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    mkdir -p "build-${_arch}" && pushd "build-${_arch}"
    ${_arch}-cmake -G Ninja -S ../build/cmake \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_PREFIX=/usr/${_arch} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DZSTD_BUILD_CONTRIB=OFF \
      -DZSTD_BUILD_STATIC=ON \
      -DZSTD_BUILD_SHARED=ON \
      -DZSTD_BUILD_TESTS=OFF \
      -DZSTD_PROGRAMS_LINK_SHARED=OFF \
      -DZSTD_BUILD_PROGRAMS=OFF
    cmake --build ./
    popd
  done
}

package() {
  cd "${srcdir}/${_pkgname}-${pkgver}"

  for _arch in ${_architectures}; do
    source mingw-clang-env $_arch
    pushd "build-${_arch}"
    DESTDIR="${pkgdir}" cmake --install ./
    $STRIP --strip-unneeded "$pkgdir"/usr/${_arch}/bin/*.dll
    $STRIP -g "$pkgdir"/usr/${_arch}/lib/*.a
    popd
  done
}
