# Maintainer: Martell Malone <Martell Malone at g mail dot com>

_realname=ninja
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.13.1
pkgrel=1
pkgdesc="Ninja is a small build system with a focus on speed (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://ninja-build.org"
msys2_repository_url="https://github.com/ninja-build/ninja"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "re2c")
source=("https://github.com/ninja-build/ninja/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-manifest-build.patch")
sha256sums=('f0055ad0369bf2e372955ba55128d000cfcc21777057806015b45e4accbebf23'
            '22ed317cf053a59b7334cbca1547b09da9efea1c8021ae6ed842ab21f1f52a31')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 < "${srcdir}/0001-fix-manifest-build.patch"
}

build() {
  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=" \
    cmake \
      "-GMSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_TESTING=OFF \
      "${extra_config[@]}" \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

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