# Maintainer: PktSurf <smlinux@pktsurf.in>
app=rust
version=1.69.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() {
  arch="$HOSTTYPE"
  export arch
  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.?z
    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/0001-Fix-LLVM-build.patch
  applypatch $srcdir/0002-Remove-nostdlib-and-musl_root-from-musl-targets.patch
  applypatch $srcdir/0003-move-debugger-scripts-to-usr-share-rust.patch
  applypatch $srcdir/0005-do-not-install-libunwind-source-removed.patch
  applypatch $srcdir/0007-link-stage-2-tools-dynamically-to-libstd.patch
  applypatch $srcdir/0009-do-not-export-LD_LIBRARY_PATH.patch
  applypatch $srcdir/0010-Fix-dynamic-linkage-of-musl-libc-for-the-libc-crate.patch
  applypatch $srcdir/0013-allow-specifying-native-sysroot-to-use-for-linkage.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="
c5ee5bba97b48e1a5d313d88eed8df8f492b4a8b962b91516cb883af16d359150db6727f01dcc933d19e8bba0cb75a2015a09cb26d4d03afd54c13a52d454003  rust-1.69.0-aarch64-unknown-linux-musl.tar.xz
8879f9031a98169880433c18f71ec08ea1770e59e27279b0d96dcf8aafa67594b2c50f322a5d9b5eb766d6b4da00c969111142e9d4ddd000daf4b3b2ac0b848a  rust-1.69.0-x86_64-unknown-linux-musl.tar.xz
724398fc208ec18adbd8ba81a445e23d1001b746990f36b869126be8a45f1cdfa75f5b9cbdd0abbab506f91a56d3736ab247677699ebd69525245558cfc01a60  rustc-1.69.0-src.tar.xz
e4683f00c6c273dfd915bd4a3aa3e00ceb37633f8869f29faccee93b2e40def1b9733b67e1e57a632cc6aaf069fe36297109ab708122bffc432cfd34560854bb  0001-Fix-LLVM-build.patch
7658be28ad7543863a2e6599cf1553854efc5796edfd3f630ea491376e141f27f0d2bc005ca1c23534b8d12d40dcfa0d2de3a3b729f6bcefd90b1dd54618730c  0002-Remove-nostdlib-and-musl_root-from-musl-targets.patch
efe1db25508e469379be6b6eef9328e3f7becc48f42451df9e54ea915c1f5dbf5e03524d0acc7aa2720f9f073325bd43b143c441e8900b4dd5b027c030346139  0003-move-debugger-scripts-to-usr-share-rust.patch
47dd902d06e10f39648e73098b01c5db7ff3f842c449e1b5bff5a403f75d6295144875e4b27af38746441e093906494e990da0242a6a8d245e7d83973bba62cd  0005-do-not-install-libunwind-source-removed.patch
0b07980a5172178ae9249eb1ff3afe3ca62065049763eef14a8404d3c0c02a539f4b0f3c38eca07f631e9bafc4252da8241e0d0223e54213ac27573bb2ddc1c9  0007-link-stage-2-tools-dynamically-to-libstd.patch
bae78b62536a13d7ab3396bd69d162013a863ff86dee0e6be8d0b4d18dc5812812e15ac208b6ce5ceb5d71482e70befe13589cb92361daac49f718226bc3480f  0009-do-not-export-LD_LIBRARY_PATH.patch
a38344aa316e4d459e4887b1f0bdfd7378930b3cc1209b36fdf8f4196352f5e7499c1ff03223d23567089e0485968cbc688ec82640ce34d4a3ff9240b4948074  0010-Fix-dynamic-linkage-of-musl-libc-for-the-libc-crate.patch
3c7e7cb9a3a1b483455096e31c071a746207e638dd9e7d6445c0a128d003a35fdfe974c5978e0fcefe3e7a2a13c8cc351b2c809d8b026ad7116a69a68285e02f  0013-allow-specifying-native-sysroot-to-use-for-linkage.patch
"
