_realname=wasi-libc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0+440+3f7eb4c7
_commit=3f7eb4c7d6ede4dde3c4bffa6ed14e8d656fe93f # tags/wasi-sdk-27
pkgrel=1
epoch=1
pkgdesc="WASI libc implementation for WebAssembly (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/WebAssembly/wasi-libc"
msys2_references=(
  'archlinux: wasi-libc'
)
license=('Apache-2.0 WITH LLVM-exception AND Apache-2.0 AND MIT')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-clang"
  "${MINGW_PACKAGE_PREFIX}-llvm"
  'git'
)
options=('!strip')
source=("git+$url.git#commit=${_commit}")
sha256sums=('856d6bdb49021ae20ebc1bfdd0a333c62c663d53bc68372b6cd7928adc859c49')

pkgver() {
  cd "${_realname}"

  printf "0+%s+%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

build() {
  cd "${_realname}"

  local -A targets=(
    wasm32-wasip1         ''
    wasm32-wasip1-threads 'THREAD_MODEL=posix'
    wasm32-wasip2         'WASI_SNAPSHOT=p2'
  )

  for target in "${!targets[@]}"; do
    make CC=clang AR=llvm-ar NM=llvm-nm TARGET_TRIPLE="$target" ${targets[$target]}
  done
}

package() {
  cd "${_realname}"

  install -dm755 "${pkgdir}${MINGW_PREFIX}"/share

  cp -dr --preserve=mode,timestamp sysroot "${pkgdir}${MINGW_PREFIX}"/share/wasi-sysroot

  # Package licenses
  install -Dm644 LICENSE* -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
}
