_realname=wabt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.0.37
pkgrel=1
pkgdesc="The WebAssembly Binary Toolkit is a suite of tools for WebAssembly (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/WebAssembly/wabt"
msys2_references=(
  'archlinux: wabt'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git'
             "${MINGW_PACKAGE_PREFIX}-python-ply"
             "${MINGW_PACKAGE_PREFIX}-gtest"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("git+$url#tag=$pkgver"
        "0001-replace-arm64-with-aarch64.patch"
        "0002-win32-tests.patch"
        "0003-libuv-aarch64.patch::https://github.com/libuv/libuv/pull/3236.patch" 
        "git+https://github.com/WebAssembly/testsuite"
        "git+https://github.com/dabeaz/ply")
validpgpkeys=('5DE3E0509C47EA3CF04A42D34AEE18F83AFDEB23')
sha512sums=('987874aa97df313d0662f4b2c84efb14e86255e157b11bc44a45b1fa79d2fc41b0f0d965ae6fae47e61b95c555021be4b956251cf5780e92a30084102f0979ea'
            'd9f829924ef172dbe0b090e4c9596c947439f6b1726fd596c687bfd4ad67543382ae2c1fda942bd7c45ac29fb2f463462879b0dcde5b25fdc5af58528c50f23c'
            'ee7bce1b344b26ad5d456a862931b4cc7e9ae64894a44be57d5d05142d41e7b87bfc144a3ae8fbcc035c405a09d99010f647096fe274757109ed56d62fda7031'
            '9f39896b20ebcca3b2209cecc193e5dc3ad59464e821cefd129537ecd5821f038c757171618df34831930d7b79721e5716272657c6989d913d4a9890232c7cf1'
            'SKIP'
            'SKIP')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}/${_realname}"

  apply_patch_with_msg \
    "0001-replace-arm64-with-aarch64.patch" \
    "0002-win32-tests.patch"

  git submodule init
  git config submodule."external/testsuite".url "${srcdir}/testsuite"
  git config submodule."external/ply".url "${srcdir}/ply"
  git submodule update --recursive
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DWITH_WASI=ON \
      -DUSE_SYSTEM_GTEST=ON \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      -S "${_realname}" \
      -B "build-${MSYSTEM}"

  pushd "build-${MSYSTEM}"/_deps/libuv-src
  apply_patch_with_msg \
    "0003-libuv-aarch64.patch"
  popd

  cmake --build "build-${MSYSTEM}"
}

check() {
  cmake --build "build-${MSYSTEM}" --target check
}

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

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