# Maintainer: PktSurf <smlinux@pktsurf.in>
app=rust
version=1.64.0
build=1sml
homepage="https://rust-lang.org"
download="https://static.rust-lang.org/dist/rustc-$version-src.tar.xz"
desc="A safe, concurrent, practical language"
requires="gcc-libs bash llvm clang gdb"
# We don't want .rlib files to be removed
preservestaticlibs=1

prepbuilddir() {
  mkandenterbuilddir
  rm -rf rustc-$version-src

  tar xf $srcdir/rustc-$version-src.tar.?z*
  cd rustc-$version-src
  fixbuilddirpermissions

  # If a rust toolchain does not already exist, extract and install one of the provided ready-to-use toolchains
  if [ ! -x /bin/rustc ] ; then
    # Create a temporary directory that will house a prebuilt architecture-specific rust toolchain provided by rust developers for bootstrapping
    mkdir -p temp-toolchain
    cd temp-toolchain

    # Extract the toolchain into it
    tar xf $srcdir/$app-$version-$arch-unknown-linux-musl.tar.gz
    cd $app-$version-$arch-unknown-linux-musl

    # Now install the toolchain 
    ./install.sh --prefix="/" || true

    # Go back to the temporary build directory
    cd ../../
  fi

  applypatch $srcdir/1.patch
  applypatch $srcdir/2.patch
  applypatch $srcdir/3.patch
  applypatch $srcdir/4.patch
  applypatch $srcdir/5.patch
  applypatch $srcdir/6.patch
  applypatch $srcdir/7.patch
  applypatch $srcdir/8.patch

  clear_vendor_checksums() {
    sed -i 's/\("files":{\)[^}]*/\1/' vendor/$1/.cargo-checksum.json
  }

  sed -i /LD_LIBRARY_PATH/d src/bootstrap/bootstrap.py

  clear_vendor_checksums libc

  # We use our system's LLVM. Discard rust's own bundled LLVM directory
  rm -Rf src/llvm-project/

  cp $srcdir/config.toml .

  if [ "$arch" = "aarch64" ] ; then
    sed -i 's@ARCH@aarch64@g' config.toml
  elif [ "$arch" = "x86_64" ] ; then
    sed -i 's@ARCH@x86_64@g' config.toml
  fi
}

build() {
  export RUST_BACKTRACE=1

  # Go easy on the pi
  unset MAKEFLAGS
  python3 ./x.py dist -v -j4
  DESTDIR="$pkg" python3 ./x.py install

  cp LICENSE* COPYRIGHT $pkgdocs/

  find $pkg/lib -name "*.so" -exec chmod 755 "{}" \+

  if [ -x /bin/rustc ] && [ -d temp-toolchain ]; then
    # Remove the temporary toolchain
    cd temp-toolchain/$app-$version-$arch-unknown-linux-musl
    ./install.sh --uninstall --prefix="/"
  fi

  mkfinalpkg
}

sha512sums="
6276b72762a833c992326cb4f2fc09c55f621af1dccc793d0eb75840bc4b408697e95fa8140f0459514cfa7fdfbaa30e433c93f0475a790ff6170af6e8e696a3  rust-1.64.0-aarch64-unknown-linux-musl.tar.gz
5c1e2ec6060559970862384152d37a52ce5a81296506db7158945fc253d102550dfad49488bd8fc00dccd86c1ab06057046917eed1b11c21fbc3e7aca0bf57f8  rust-1.64.0-x86_64-unknown-linux-musl.tar.gz
6d1cc90bb73468b6f3118bc8f2c1177dd424fd8ebc27dcb9b61d14a14fa7b5fdec3d109a51ab725c5cfc2119f994a668b0ffd4d3bc7491928d3b7ba07f3e610a  rustc-1.64.0-src.tar.lz
e4683f00c6c273dfd915bd4a3aa3e00ceb37633f8869f29faccee93b2e40def1b9733b67e1e57a632cc6aaf069fe36297109ab708122bffc432cfd34560854bb  1.patch
5b77f8b5505e7d1bc8225f8bde5fdfd67680fcb5e9774e77fc1842ca32c1042fe8e59e329fccc556e3805db3c0768c273fa17fc0446750503d4be53e92db8b71  2.patch
7309d2add788061f83e0e11a4cfa1433bcbbab4dc11584ae865c0fce36b9a97f5f553bcf6d4f8933e2734869b3c95edbe62950e5fe622165b922301187c31c50  3.patch
47dd902d06e10f39648e73098b01c5db7ff3f842c449e1b5bff5a403f75d6295144875e4b27af38746441e093906494e990da0242a6a8d245e7d83973bba62cd  4.patch
0b07980a5172178ae9249eb1ff3afe3ca62065049763eef14a8404d3c0c02a539f4b0f3c38eca07f631e9bafc4252da8241e0d0223e54213ac27573bb2ddc1c9  5.patch
bae78b62536a13d7ab3396bd69d162013a863ff86dee0e6be8d0b4d18dc5812812e15ac208b6ce5ceb5d71482e70befe13589cb92361daac49f718226bc3480f  6.patch
a38344aa316e4d459e4887b1f0bdfd7378930b3cc1209b36fdf8f4196352f5e7499c1ff03223d23567089e0485968cbc688ec82640ce34d4a3ff9240b4948074  7.patch
3c7e7cb9a3a1b483455096e31c071a746207e638dd9e7d6445c0a128d003a35fdfe974c5978e0fcefe3e7a2a13c8cc351b2c809d8b026ad7116a69a68285e02f  8.patch
"
