# Contributor: Raed Rizqie <raed.rizqie@gmail.com>

_realname=gn
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=0.2385.9ece3f52
pkgrel=1
pkgdesc='Meta-build system that generates build files for Ninja (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gn.googlesource.com/gn/"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
_commit=9ece3f5254c273cb46606a6571963f931c3b012d
source=(git+https://gn.googlesource.com/gn#commit=$_commit
        001-build-fixes.patch
        002-missing-header.patch
        003-remove-msvc-flags.patch
        004-add-arm64-arch.patch)
sha256sums=('37a17310ed6250f3f6a4150f6dfb485c4ffa01e2ecce3ed9afa6c613ca98be8c'
            '01045719f76a1127dc54586b8f1dc6111cc6210440d1d72a0afbc876d29d3c36'
            '350c02266816fdb734243d3c8d04a7b7e79abc62057302c5ff02af323d6cc0f2'
            '67f58d677f63bf5e9919f63d68afa007b1a2d14c5fae13cd5487bd1c41cf69de'
            '1cd84e9f2c8e4daf4bdb12ae7accc15b57d3f2abf87ff40ebf2c5e180ddeca3f')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd ${_realname}
  echo 0.$(git rev-list --count initial-commit..).$(git rev-parse --short=8 HEAD)
}

prepare() {
  cd ${_realname}

  apply_patch_with_msg \
    001-build-fixes.patch \
    002-missing-header.patch \
    003-remove-msvc-flags.patch \
    004-add-arm64-arch.patch
}

build() {
  cd ${_realname}
  ./build/gen.py --allow-warnings --no-static-libstdc++

  ninja -C ./out
}

package() {
  cd ${_realname}

  install -Dm755 out/gn.exe "${pkgdir}${MINGW_PREFIX}/bin/gn.exe"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}" docs/*
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" LICENSE

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/vim/vimfiles"
  cp -r misc/vim/{autoload,ftdetect,ftplugin,syntax} \
    "${pkgdir}${MINGW_PREFIX}/share/vim/vimfiles/"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/emacs/site-lisp" misc/emacs/gn-mode.el
}

# vim:set ts=2 sw=2 et:
