# Maintainer: Mateusz Mikula <mati865@gmail.com>

_realname=crt
_mingw_suff=mingw-w64-cross-clang
pkgname=("${_mingw_suff}-${_realname}")
conflicts=("${_mingw_suff%-*}-${_realname}")
pkgver=9.0.0.6158.1c773877
pkgrel=2
pkgdesc='MinGW-w64 CRT for cross-compiler'
arch=('i686' 'x86_64')
url='https://mingw-w64.sourceforge.io/'
license=('custom')
groups=("${_mingw_suff}-toolchain")
depends=("${_mingw_suff}-headers")
makedepends=("git" "clang" "lld")
options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
_commit='1c773877f4a13c8bd7bfb8da80e1e8761a889f51'
source=("mingw-w64"::"git+https://git.code.sf.net/p/mingw-w64/mingw-w64#commit=$_commit")
sha256sums=('SKIP')

_targets="x86_64-w64-mingw32 i686-w64-mingw32 armv7-w64-mingw32 aarch64-w64-mingw32"

pkgver() {
  cd "${srcdir}/mingw-w64"
  local _major=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MAJOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MINOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _rev=0
  printf "%s.%s.%s.%s.%s" ${_major} ${_minor} ${_rev} "$(git rev-list --count $_commit)" "$(git rev-parse --short $_commit)"
}

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

build() {
  for _target in ${_targets}; do
    msg "Building ${_target} CRT"
  case "${_target}" in
    i686*)
      _crt_configure_args="--disable-lib64 --enable-lib32"
    ;;
    x86_64*)
      _crt_configure_args="--disable-lib32 --enable-lib64"
    ;;
    armv7*)
      _crt_configure_args="--disable-lib32 --disable-lib64 --enable-libarm32"
    ;;
    aarch64*)
      _crt_configure_args="--disable-lib32 --disable-lib64 --disable-libarm32 --enable-libarm64"
    ;;
  esac
    mkdir -p ${srcdir}/crt-${_target} && cd ${srcdir}/crt-${_target}

    export CC="clang" CXX="clang++" AS="clang" AR="llvm-ar" RANLIB="llvm-ranlib" DLLTOOL="llvm-dlltool" LD="clang"
    CFLAGS+=" -target ${_target} --sysroot=/opt/${_target}"
    CPPFLAGS+=" -target ${_target} --sysroot=/opt/${_target}"
    CXXFLAGS+=" -target ${_target} --sysroot=/opt/${_target}"
    LDFLAGS+=" -target ${_target} --sysroot=/opt/${_target} -fuse-ld=lld"

    ${srcdir}/mingw-w64/mingw-w64-crt/configure \
    --build=${CHOST} \
    --prefix=/opt/${_target} \
    --host=${_target} \
    --enable-wildcard \
    --with-default-msvcrt=ucrt \
    ${_crt_configure_args}

    make
  done
}

package() {
  for _target in ${_targets}; do
    msg "Installing ${_target} crt"
    cd ${srcdir}/crt-${_target}
    make DESTDIR=${pkgdir} install
  done
}
