

_realname=yosys
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.56
pkgrel=1
pkgdesc="A framework for RTL synthesis tools (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://yosyshq.net/yosys"
msys2_repository_url="https://github.com/YosysHQ/yosys"
msys2_references=(
  'archlinux: yosys'
)
license=('spdx:ISC')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-cc-libs"
  "${MINGW_PACKAGE_PREFIX}-ghdl"
  "${MINGW_PACKAGE_PREFIX}-libwinpthread"
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-readline"
  "${MINGW_PACKAGE_PREFIX}-tcl"
  "${MINGW_PACKAGE_PREFIX}-zlib"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
)
checkdepends=("${MINGW_PACKAGE_PREFIX}-iverilog")
_ghdl_plugin_commit="1b97dc71377cea7e861be6625be4353c377a5fb5"
source=(
  "${_realname}-${pkgver}.tar.gz::https://github.com/YosysHQ/yosys/releases/download/v${pkgver}/yosys.tar.gz"
  "https://github.com/ghdl/ghdl-yosys-plugin/archive/${_ghdl_plugin_commit}/ghdl-yosys-plugin-${_ghdl_plugin_commit}.tar.gz"
  "001-fix-build-on-mingw.patch"
)
sha256sums=('b5270419812a38ab3dade6003130fc2eebc9757a4ed9e48b0ceb311428743d04'
            '84cc34bff7029f76d085e7c741e1a609592ef58f8fdc9d668beaad7691753b0e'
            '3c9c9da87788f3124824d634e89a6702f317793a47cab819e5461b804f280aad')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  mkdir -p ${_realname}-${pkgver}
  tar -xzf ${_realname}-${pkgver}.tar.gz -C ${_realname}-${pkgver}

  cd ${_realname}-${pkgver}/abc
  # https://github.com/berkeley-abc/abc/pull/435
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch

  cp -r "${srcdir}"/ghdl-yosys-plugin-${_ghdl_plugin_commit}/src \
    "${srcdir}"/${_realname}-${pkgver}/frontends/ghdl
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  cp -r ${_realname}-${pkgver} build-${MSYSTEM} && cd build-${MSYSTEM}

  make config-msys2-64

  make \
    PRETTY=0 \
    ENABLE_GHDL=1 \
    GHDL_PREFIX=${MINGW_PREFIX}
}

check() {
  cd build-${MSYSTEM}
  make test
}

package() {
  cd build-${MSYSTEM}

  make DESTDIR="${pkgdir}" install

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