# Maintainer: Diego Sogari <diego.sogari@gmail.com>
# Contributor: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=binaryen
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=124
pkgrel=1
pkgdesc="Compiler infrastructure and toolchain library for WebAssembly, in C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/WebAssembly/binaryen"
msys2_references=(
  "archlinux: binaryen"
  "aur: binaryen-git"
  "cpe: cpe:/a:webassembly:binaryen"
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/WebAssembly/binaryen/archive/version_${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('b8d06af81a8c2bb27c34d1f9e3cf7c621f93fc901f896809e0490f3586a63ca4')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  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=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DENABLE_WERROR=OFF \
    -DBUILD_TESTS=OFF \
    ${extra_config} \
    "../${_realname}-version_${pkgver}"

  cmake --build .
}

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

  DESTDIR="${pkgdir}" cmake --install .

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