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

_realname=cmake
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.20.5
pkgrel=3
pkgdesc="A cross-platform open-source make system (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.cmake.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-bzip2"
             "${MINGW_PACKAGE_PREFIX}-ncurses"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-zstd"
             $([[ ${MSYSTEM} == "CLANG32" ]] || \
               [[ ${MSYSTEM} == "CLANGARM64" ]] || \
               echo "${MINGW_PACKAGE_PREFIX}-qt5")
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || \
               [[ ${MINGW_PACKAGE_PREFIX} == *-ucrt-* ]] || \
               echo "${MINGW_PACKAGE_PREFIX}-emacs"))

depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-pkgconf"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-rhash"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5: CMake Qt GUI"
            "${MINGW_PACKAGE_PREFIX}-emacs: for cmake emacs mode")
options=('staticlibs') # '!strip' 'debug'
source=("https://github.com/Kitware/CMake/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-Disable-response-files-for-MSYS-Generator.patch"
        "0002-Do-not-install-Qt-bundle-in-cmake-gui.patch"
        "0004-Output-line-numbers-in-callstacks.patch"
        "35d3e00e4e9b138dec1d1a646766fc342d04b8f5.patch"
        "f90d15458a9a98180fcc95158f2ab5d2b1ad3152.patch")
sha256sums=('12c8040ef5c6f1bc5b8868cede16bb7926c18980f59779e299ab52cbc6f15bb0'
            '25793edcbac05bb6d17fa9947b52ace4a6b5ccccf7758e22ae9ae022ed089061'
            'f6cf6a6f2729db2b9427679acd09520af2cd79fc26900b19a49cead05a55cd1a'
            '15fdf3fb1a0f1c153c8cfbdd2b5c64035b7a04de618bfe61d7d74ced0d6a5c4b'
            'c4c120046860c014e0da4be959decf330ad8dcdae5a48ebcb8ec59006eb0e625'
            'e0dbe6da13372f9817d6451a7d75c5fd9cdf5843a305e80d8803ad76d04434a4')


# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    0001-Disable-response-files-for-MSYS-Generator.patch \
    0002-Do-not-install-Qt-bundle-in-cmake-gui.patch \
    0004-Output-line-numbers-in-callstacks.patch \
    35d3e00e4e9b138dec1d1a646766fc342d04b8f5.patch \
    f90d15458a9a98180fcc95158f2ab5d2b1ad3152.patch
}

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

  local -a _qtconfig
  if [[ "${MSYSTEM}" != "CLANG32" ]] &&
     [[ "${MSYSTEM}" != "CLANGARM64" ]]; then
     _qtconfig+=("--qt-gui" "--qt-qmake=${MINGW_PREFIX}/bin/qmake.exe")
  fi

  MSYSTEM=MINGW FC=${MINGW_PREFIX}/bin/gfortran.exe \
    "${srcdir}"/${_realname}-${pkgver}/configure  \
    --prefix=${MINGW_PREFIX}                      \
    --system-libs                                 \
    --parallel=${NUMBER_OF_PROCESSORS}            \
    --mandir=share/man                            \
    --docdir=share/doc/cmake                      \
    --sphinx-man --sphinx-html                    \
    --bootstrap-system-jsoncpp                    \
    --bootstrap-system-libuv                      \
    --bootstrap-system-librhash                   \
    ${_qtconfig[@]}

  plain "Start building..."
  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ./bin/ctest.exe -j$(($(nproc)+1))
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  ./bin/cmake.exe \
     -DCMAKE_INSTALL_PREFIX:PATH=${pkgdir}${MINGW_PREFIX} \
     -DCMAKE_INSTALL_LOCAL_ONLY:BOOL=OFF -P cmake_install.cmake
  install -d "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && \
     [[ ${MINGW_PACKAGE_PREFIX} != *-ucrt-* ]]; then
    ${MINGW_PREFIX}/bin/emacs -batch -f batch-byte-compile \
      "${pkgdir}${MINGW_PREFIX}"/share/emacs/site-lisp/cmake-mode.el
  fi
}
