# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=crt
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=13.0.0.r388.gbb8673e5f
pkgrel=1
_commit='bb8673e5f137300b018d1dcdeab1c28c1d292e97'
pkgdesc='MinGW-w64 CRT for Windows (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.mingw-w64.org/'
msys2_repository_url="https://sourceforge.net/p/mingw-w64/mingw-w64/ci/master/tree/mingw-w64-crt/"
license=('spdx:ZPL-2.1')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-headers")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=${pkgver}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
options=('!strip' '!buildflags' '!emptydirs')
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit")
sha256sums=('8668e7f398b98b493e131fee57500466ab334c3fff0e6cf754e49aee5f223b44')

pkgver() {
  cd "${srcdir}/mingw-w64"
  git describe --long ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${srcdir}/mingw-w64"
}

build() {
  cd "${srcdir}/mingw-w64"
  declare -a _extra_config
  case "$CARCH" in
    i686)
      _extra_config=("--disable-lib64" "--enable-lib32")
    ;;
    x86_64)
      _extra_config=("--disable-lib32" "--enable-lib64")
    ;;
    armv7)
      _extra_config=("--disable-lib32" "--disable-lib64" "--enable-libarm32")
    ;;
    aarch64)
      _extra_config=("--disable-lib32" "--disable-lib64" "--disable-libarm32" "--enable-libarm64")
    ;;
  esac

  # only clang+lld support this at the moment
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    _extra_config+=("--enable-cfguard")
  fi

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]] || [[ $MINGW_PACKAGE_PREFIX == *-ucrt-* ]]; then
    _extra_config+=("--with-default-msvcrt=ucrt")
  else
    _extra_config+=("--with-default-msvcrt=msvcrt")
  fi

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

  "${srcdir}"/mingw-w64/mingw-w64-crt/configure \
    --prefix=${MINGW_PREFIX} \
    --disable-dependency-tracking \
    "${_extra_config[@]}"

  make
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install-strip

  # Create empty dummy archives, to avoid failing when the compiler driver
  # adds -lssp -lssh_nonshared when linking.
  ar rcs "${pkgdir}"${MINGW_PREFIX}/lib/libssp.a
  ar rcs "${pkgdir}"${MINGW_PREFIX}/lib/libssp_nonshared.a

  install -Dm644 "${srcdir}"/mingw-w64/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64/COPYING.MinGW-w64.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64.txt
  install -Dm644 "${srcdir}"/mingw-w64/COPYING.MinGW-w64-runtime/COPYING.MinGW-w64-runtime.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MinGW-w64-runtime.txt
}
