# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cmake
pkgbase=mingw-w64-${_realname}-bootstrap
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-bootstrap")
pkgver=4.2.1
pkgrel=1
pkgdesc="A cross-platform open-source make system"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.cmake.org/"
msys2_repository_url="https://gitlab.kitware.com/cmake/cmake"
msys2_references=(
  'archlinux: cmake'
  "cpe: cpe:2.3:a:cmake_project:cmake"
)
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-pkgconf")
conflicts=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-cmake-cmcldeps"
  "${MINGW_PACKAGE_PREFIX}-cmake-docs"
)
provides=("${MINGW_PACKAGE_PREFIX}-cmake=${pkgver}")
source=("https://github.com/Kitware/CMake/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0003-fix-find-python-on-mingw-aarch64.patch"
        "0005-Default-to-ninja-generator.patch"
        "0100-Restore-compilation-on-32-bit-MinGW.patch")
sha256sums=('414aacfac54ba0e78e64a018720b64ed6bfca14b587047b8b3489f407a14a070'
            '557b5cbc05d4d50b3a67a7892391fcaa5cd95c492cdb4338d86305d1f4a3b88a'
            '426818278090704d2a12f62ef3dfd94c47b11fa2784bb842989b7f6a09ee7aa2'
            '2306d79614a10d51826c33e0dc5bc533ad06ee221a0d72ca9efb0a105574804d')

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

  patch -Np1 -i "${srcdir}/0003-fix-find-python-on-mingw-aarch64.patch"
  patch -Np1 -i "${srcdir}/0005-Default-to-ninja-generator.patch"
  patch -Np1 -i "${srcdir}/0100-Restore-compilation-on-32-bit-MinGW.patch"

  find . -name "*.orig" -exec rm -f {} \;
}

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

  MSYSTEM=MINGW MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX="\
    "${srcdir}/${_realname}-${pkgver}"/configure \
    --prefix=${MINGW_PREFIX} \
    --datadir=share/cmake \
    --docdir=share/doc/cmake \
    --mandir=share/man \
    --no-system-libs \
    --no-qt-gui \
    --parallel=${NUMBER_OF_PROCESSORS} \
    -- \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}

  make
}

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

  make install DESTDIR="${pkgdir}"
}
