# Maintainer: umarcor <unai.martinezcorral@ehu.eus>

_realname=prjtrellis
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4
pkgrel=6
pkgdesc="Documenting the Lattice ECP5 bit-stream format (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/YosysHQ/prjtrellis"
license=('spdx:MIT')
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=(
  "${MINGW_PACKAGE_PREFIX}-boost-libs"
  "${MINGW_PACKAGE_PREFIX}-python"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-boost"
  "git"
)
_commit_db='ce8cdafe7a8c718f0ec43895894b668a479ba33f'
source=("https://github.com/YosysHQ/prjtrellis/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "${_realname}-db"::"git+https://github.com/YosysHQ/prjtrellis-db.git#commit=${_commit_db}"
        "001-fix-build-with-boost-built-with-cmake.patch")
sha256sums=('46fe9d98676953e0cccf1d6332755d217a0861e420f1a12dabfda74d81ccc147'
            '7f4f327c03ba5daa05d3349f32e7465c3cc1e649d684b1a68884205e0e1eb96c'
            '779a9b7822b0836be17a6a47726e7892046a59c29a6c4fb5962ad386d500335c')

prepare () {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-with-boost-built-with-cmake.patch

  cd "${srcdir}/${_realname}-db"
  cp -r * "${srcdir}/${_realname}-${pkgver}/database/"
}

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

  local -a _extra_config
  if check_option "debug" "y"; then
    _extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    _extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_PREFIX_PATH="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DPython3_LIBRARY=${MINGW_PREFIX}/lib/libpython${_pyver}.dll.a \
      "${_extra_config[@]}" \
      ../${_realname}-${pkgver}/libtrellis

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

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

  for item in ecpbram ecpmulti ecppack ecppll ecpunpack; do
    msg2 "Check $item"
    ./"$item" --help
  done
}

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

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

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