# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_bootstrapping=no

rust_dist_server=https://static.rust-lang.org/dist
#rust_dist_server=https://dev-static.rust-lang.org/dist/2025-09-14

pkgname=rust
pkgver=1.90.0
pkgrel=2
pkgdesc="Systems programming language focused on safety, speed and concurrency"
arch=('x86_64')
url="https://www.rust-lang.org/"
msys2_repository_url="https://github.com/rust-lang/rust"
msys2_references=(
  'anitya: 7635'
  'archlinux: rust'
  "cpe: cpe:/a:rust-lang:rust"
  'gentoo: dev-lang/rust'
)
license=('spdx:Apache-2.0 AND MIT')
makedepends=('gcc'
             'llvm'
             'python'
             'libgit2-devel'
             'pcre2-devel'
             'libssh2-devel'
             'openssl-devel'
             'pkgconf'
             'libsqlite-devel'
             'liblzma-devel'
             'libzstd-devel'
             'zlib-devel'
             'libcurl-devel'
             $([[ "$_bootstrapping" == "no" ]] && echo 'rust'))
source=("${rust_dist_server}/${pkgname}c-${pkgver}-src.tar.gz"{,.asc}
        "bootstrap.toml"
        "https://github.com/rust-lang/rust/commit/cadb56d92e596d4922dda253e3ca374b0479627c.patch"
        "https://github.com/rust-lang/rust/commit/26b157572280d058c8a800e4888f4b37062eb533.patch"
        "https://github.com/rust-lang/rust/commit/af0d6f129096dabeee42978c84596a3acb43c1b0.patch")
noextract=(${pkgname}c-${pkgver}-src.tar.gz)
sha256sums=('799a9f9cba4ed5351e071048bcf6b5560755d9009648def33a407dd4961f9b7e'
            'SKIP'
            '13ef39f92a0904542d00bff8672e041ce9774709987b6ee35c072e82b45609cd'
            '36dc685fb7b10ac7dd6f97095db530c6120b7f3a7d62aea49a7a9eb9d0461b24'
            'f461756029a2b7f35cc458e67eaada44e57eb3e0d8ae98db85f90b52dad2ccfc'
            'b72da28a20a43f924dbeca8798b7c86915c80513ef81d29e0742a699070827bc')
validpgpkeys=('108F66205EAEB0AAA8DD5E1C85AB96E6FA1BE5FE'  # Rust Language (Tag and Release Signing Key) <rust-key@rust-lang.org>
              '474E22316ABF4785A88C6E8EA2C794A986419D8A'  # Tom Stellard <tstellar@redhat.com>
              'B6C8F98282B944E3B0D5C2530FC3042E345AD05D') # Hans Wennborg <hans@chromium.org>

prepare() {
  # workaround strange symlinking error during a build
  export MSYS=winsymlinks:nativestrict
  plain "Extracting ${pkgname}c-${pkgver}-src.tar.gz"
  tar -xzf ${pkgname}c-${pkgver}-src.tar.gz || true

  cd ${pkgname}c-${pkgver}-src
  patch -Np1 -i ../cadb56d92e596d4922dda253e3ca374b0479627c.patch
  patch -Np1 -i ../26b157572280d058c8a800e4888f4b37062eb533.patch
  patch -Np1 -i ../af0d6f129096dabeee42978c84596a3acb43c1b0.patch
}

build() {
  export RUST_BACKTRACE=1
  # force some system libraries
  export LIBSQLITE3_SYS_USE_PKG_CONFIG=1
  export LIBSSH2_SYS_USE_PKG_CONFIG=1

  cd "${pkgname}c-${pkgver}-src"
  # substitute the values in bootstrap.toml
  export PKGREL="${pkgrel}"
  envsubst < ../bootstrap.toml > bootstrap.toml

  if [ "${_bootstrapping}" = "no" ]; then
    sed -i '/^\[build\]/,/^$/ s|^#||g' bootstrap.toml
  fi
  if check_option "debug" "y"; then
    sed -i 's/^#debug/debug/g' bootstrap.toml
  fi

  DESTDIR="$PWD/dest-rust" python x.py install --stage 2
}

check() {
  cd "${pkgname}c-${pkgver}-src"
  python x.py test --stage 2 --exclude src/test/debuginfo
}

package_rust() {
  depends=('gcc'
           'zlib'
           'libzstd'
           'libsqlite'
           'libssh2'
           'libgit2'
           'libopenssl'
           'libcurl'
           'llvm-libs')
  optdepends=("gdb: for rust-gdb script"
              "lldb: for rust-lldb script")

  cd "${pkgname}c-${pkgver}-src"

  cp -a dest-rust/* "${pkgdir}"

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

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