# Maintainer: Marc-André Lureau <marcandre.lureau@redhat.com>

_realname=dtc
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.2
pkgrel=1
pkgdesc="Device Tree Compiler"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://git.kernel.org/pub/scm/utils/dtc/dtc.git/"
msys2_references=(
  "cpe: cpe:2.3:a:dtc_project:dtc"
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-libyaml")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools-scm"
             "${MINGW_PACKAGE_PREFIX}-swig")
source=("https://git.kernel.org/pub/scm/utils/dtc/dtc.git/snapshot/${_realname}-${pkgver}.tar.gz"
        "0001-remove-setup-py-install.patch")
sha256sums=('8f1486962f093f28a2f79f01c1fd82f144ef640ceabe555536d43362212ceb7c'
            '72ada7074ec472f8d06a05a1d09300a490436f8665d7a4a600d6bd56fb0d7d30')

prepare() {
  cd "${_realname}-${pkgver}"

  # conflicts with the wheel build and uses deprecated setuptools install
  patch -Np1 -i "${srcdir}/0001-remove-setup-py-install.patch"
}

build() {
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    -Dtests=false \
    -Dtools=true \
    "build-${MSYSTEM}" \
    "${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson compile -C "build-${MSYSTEM}"

  LDFLAGS+=" -L$(cygpath -wm "${srcdir}/build-${MSYSTEM}/libfdt")" \
  python -m build --wheel --skip-dependency-check --no-isolation \
  "${_realname}-${pkgver}"
}

package() {
  ${MINGW_PREFIX}/bin/meson install -C "build-${MSYSTEM}" --destdir "${pkgdir}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" \
    "${_realname}-${pkgver}"/dist/*.whl

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