# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Zion Nimchuk <zionnimchuk@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com

_realname=rust
_bootstrapping=yes
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-rust-docs")
pkgver=1.53.0
pkgrel=3
pkgdesc="Systems programming language focused on safety, speed and concurrency (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.rust-lang.org/"
license=('MIT' 'Apache')
makedepends=("bison"
             "flex"
             "git"
             "${MINGW_PACKAGE_PREFIX}-cmake>=3.4.3"
             "${MINGW_PACKAGE_PREFIX}-curl"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-libssh2"
             "${MINGW_PACKAGE_PREFIX}-lldb"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-python"
             $([[ "$_bootstrapping" == "no" ]] && echo "${MINGW_PACKAGE_PREFIX}-rust")
             "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs')
#install=rust.install
source=("https://static.rust-lang.org/dist/${_realname}c-${pkgver}-src.tar.gz"
        "force-curl-rust.patch"
        "0001-add-missing-libs.patch"
        "0004-unbundle-gcc.patch"
        "0005-win32-config.patch"
        "0006-Update-cc.patch"
        "0007-clang-subsystem.patch"
        "0008-disable-self-contained.patch")
sha256sums=('5cf7ca39a10f6bf4e0b0bd15e3b9a61ce721f301e12d148262e5ba968ab825b9'
            'bd63b990d2c1626acb87d5ea534c3d82fc4daddb5f784a83053be7b162327445'
            'c706841ace2ef76fd1004b9c0486ea9ef7d99073c5ce2884e4c97fa4a7beab25'
            '6cc3234644768e24824e455d6304585c717ae25aa53ebf9cf1f1dea87bf869b7'
            'c4e5ffeef84296d39c3e3e8f807fc8b33ce786b1e4edb21eef26b053586aca27'
            'dd8f785138a78398c486fe60237833d1c6a84be5128ad6b3210c135dda258497'
            '9e2e2eb9c0684f329b1ce6598fcb66727abf84121087b24670a9cfc44a514888'
            '29f84cb8e05ce304e102e28912a3b4464add406a8ec37a6c6d717b9b7d81b67b')
#validpgpkeys=('108F66205EAEB0AAA8DD5E1C85AB96E6FA1BE5FE') # Rust Language (Tag and Release Signing Key) <rust-key@rust-lang.org>
noextract=(${_realname}c-${pkgver}-src.tar.gz)

prepare() {
  [[ -d ${srcdir}/${_realname}c-${pkgver}-src ]] && rm -rf ${srcdir}/${_realname}c-${pkgver}-src
  tar -xzf ${srcdir}/${_realname}c-${pkgver}-src.tar.gz -C ${srcdir} || true

  cd ${srcdir}/${_realname}c-${pkgver}-src
  patch -p1 -i "${srcdir}/force-curl-rust.patch"
  patch -p1 -i "${srcdir}/0001-add-missing-libs.patch"
  patch -p1 -i "${srcdir}/0004-unbundle-gcc.patch"
  patch -p1 -i "${srcdir}/0005-win32-config.patch"
  patch -p1 -i "${srcdir}/0006-Update-cc.patch"
  patch -p1 -i "${srcdir}/0008-disable-self-contained.patch"

  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    patch -p1 -i "${srcdir}/0007-clang-subsystem.patch"
  fi
}

build() {
  [[ -d "${srcdir}/${CARCH}" ]] && rm -rf "${srcdir}/${CARCH}"
  mkdir -p "${srcdir}/${CARCH}" && cd "${srcdir}/${CARCH}"


  # The ultimate hack to let the bootstrap compiler use libgcc* libs
  if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
    export GCC_LIBS_HACK="$(cygpath -am build/missing-libs-hack)"
    mkdir -p "${GCC_LIBS_HACK}"
    cp "$(cygpath -u $(clang -print-libgcc-file-name))" "${GCC_LIBS_HACK}/libgcc.a"
    cp "${MINGW_PREFIX}/lib/libunwind.a" "${GCC_LIBS_HACK}/libgcc_eh.a"
    cp "${MINGW_PREFIX}/lib/libunwind.dll.a" "${GCC_LIBS_HACK}/libgcc_s.a"
    export RUSTFLAGS_BOOTSTRAP="-C link-arg=-L$(cygpath -am build/missing-libs-hack)"
  fi

  #We have to do the following because rust doesn't count x86_64-w64-mingw32 as a target triple
  OSTYPE="$CARCH-pc-windows-gnu"

  local -a _rust_conf=()
  if [ "${_bootstrapping}" = "no" ]; then
    _rust_conf+=("--local-rust-root=$(cygpath -m ${MINGW_PREFIX})")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix;--sysconfdir;--localstatedir" \
  ../${_realname}c-${pkgver}-src/configure \
    --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var/lib \
    --build=$OSTYPE \
    --host=$OSTYPE \
    --target=$OSTYPE \
    --release-channel=stable \
    --enable-ninja \
    --enable-extended \
    --disable-codegen-tests \
    --llvm-root=${MINGW_PREFIX} \
    --python=${MINGW_PREFIX}/bin/python \
    ${_rust_conf}

  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py build --verbose --stage 2

  #create the install at a temporary directory
  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  DESTDIR="$PWD"/dest-rust ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py install --stage 2 --verbose

    # move docs out of the way for splitting
  mv dest-rust/${MINGW_PREFIX}/share/doc dest-doc

  rm -f dest-rust/${MINGW_PREFIX}/bin/libgcc*.dll
  rm -f dest-rust/${MINGW_PREFIX}/bin/libstd*.dll
  rm -f dest-rust/${MINGW_PREFIX}/bin/libwinpthread*.dll
  rm -f dest-rust/${MINGW_PREFIX}/lib/rustlib/$OSTYPE/lib/self-contained/*
}

check() {
  cd "${srcdir}/${CARCH}"
  # it might fail on Windows
  DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
  DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
  DEP_Z_ROOT=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py test --stage 2 || true
}


package_rust() {
  depends=("${MINGW_PACKAGE_PREFIX}-gcc"
           "${MINGW_PACKAGE_PREFIX}-curl"
           "${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-libssh2")

  cd "${srcdir}/${CARCH}"

  cp -a dest-rust/* "$pkgdir"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-APACHE"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-MIT"

  # delete unnecesary files, e.g. components and manifest files only used for the uninstall script
  cd "$pkgdir"/${MINGW_PREFIX}/lib/rustlib
  rm components install.log manifest-* rust-installer-version uninstall.sh

  install -d "$pkgdir/${MINGW_PREFIX}/share/bash-completion/completions"
  mv "${pkgdir}${MINGW_PREFIX}/etc/bash_completion.d/cargo" \
     "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/cargo"

}

package_rust-docs() {
  pkgdesc='Documentation for the Rust programming language'
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}<1.52.0")

  cd "${srcdir}/${CARCH}"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/share/licenses/$pkgname/LICENSE-APACHE"
  install -Dm644 ${srcdir}/${_realname}c-${pkgver}-src/LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/$pkgname/LICENSE-MIT"

  install -d "$pkgdir"/${MINGW_PREFIX}/share/
  cp -a dest-doc "$pkgdir"/${MINGW_PREFIX}/share/doc
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
