# Maintainer: Frederik Carlier <frederik.carlier@keysight.com>

_realname=gnustep-make
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.9.3
pkgrel=1
pkgdesc="GNUstep build system (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://gnustep.github.org/'
msys2_repository_url='https://github.com/gnustep/tools-make'
license=('spdx:GPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-lld"
             "${MINGW_PACKAGE_PREFIX}-libobjc2")
source=("${msys2_repository_url}/releases/download/make-${pkgver//./_}/${_realname}-${pkgver}.tar.gz")
sha256sums=('93ca320b706279ebca53760da89d4c3f2bbc547f4723967140a34346d9f04c24')

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

  # libobjc2 defers exception handling to the C++ library, so link with a C++ library
  case ${MSYSTEM} in
    MINGW*|UCRT*)
      export LDFLAGS="-fuse-ld=lld -lstdc++ -lgcc_s"
      ;;
    CLANG*)
      export LDFLAGS="-Wl,--no-whole-archive -lc++"
      ;;
  esac

  CFLAGS+=" -DSTRICT_APPLE_COMPATIBILITY=1" \
  CC="$MINGW_PREFIX/bin/clang" \
  CXX="$MINGW_PREFIX/bin/clang++" \
  ../"${_realname}-${pkgver}"/configure \
    --with-library-combo=ng-gnu-gnu \
    --with-runtime-abi=gnustep-2.0 \
    --prefix="${MINGW_PREFIX}" \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}"

  make
}

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

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

  make install DESTDIR="${pkgdir}"

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