app=rust
version=1.53.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

build() {
  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/0001-Fix-LLVM-build.patch
  applypatch $srcdir/00016-do-not-copy-libunwind.patch
  applypatch $srcdir/0002-Allow-rustdoc-to-work-when-cross-compiling-on-musl.patch
  applypatch $srcdir/0003-Require-static-native-libraries-when-linking-static-.patch
  applypatch $srcdir/0004-Remove-nostdlib-and-musl_root-from-musl-targets.patch
  applypatch $srcdir/0005-Prefer-libgcc_eh-over-libunwind-on-musl.patch
  applypatch $srcdir/0006-test-use-extern-for-plugins-Don-t-assume-multilib.patch
  applypatch $srcdir/0007-test-sysroot-crates-are-unstable-Fix-test-when-rpath.patch
  applypatch $srcdir/0008-Ignore-broken-and-non-applicable-tests.patch
  applypatch $srcdir/0009-Link-stage2-tools-dynamically-to-libstd.patch
  applypatch $srcdir/0010-Move-debugger-scripts-to-usr-share-rust.patch
  applypatch $srcdir/0012-Fix-dynamic-linkage-of-musl-libc-for-the-libc-crate.patch
  applypatch $srcdir/0014-Void-fix-linkage-against-host-target-LLVM-in-cross-s.patch
  applypatch $srcdir/disable_miri.patch

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

  export RUST_BACKTRACE=1
  export PKG_CONFIG_ALLOW_CROSS=1
  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

  # Go easy on the pi
  unset MAKEFLAGS
  python3 ./x.py dist -v -j2
  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="
ddd5fbd62242e7541245c0abd0ba76ac0197e8182ea425610da3e8be8700d6639763c4dadc699bfa29e1441e659de82fdfbe6e235efce29b9e28d0c8e9ad44f3  rust-1.53.0-aarch64-unknown-linux-musl.tar.gz
97d1c3c43f02a378ae2fba0148b6ec37c4c69756bb518c3e1624fa6c5dee9383925312d69ff4bde45a2c3a04a3ef6bb10ed0d2939a932b12cfbb507550cc3c4f  rust-1.53.0-x86_64-unknown-linux-musl.tar.gz
e279491a6252b2a62a70c0352e4dd9603ffbb629231315572ef2eac4581ebad394076ff05da47ff71f488f1b84a783ac95b41665b3c85b524ecc8a4eccd7c4b0  rustc-1.53.0-src.tar.xz
3a2961af1f90f19f06cd079c051566952f7e347bd29dcb5f2b6ffe85d59e00f9de1eda4a5ddad7a5abd0ef85d5f8e7c5b2a143923cb286bff9369291e562856e  0001-Fix-LLVM-build.patch
165316d689dc664101a720af4baf5772159dc87459f3937488ceb1cfd4063b76bcb190046fe3927489b399f9815d24007845f4270d58fb06e4500d05cc39fa50  00016-do-not-copy-libunwind.patch
966a3f898d516b9e327a6caa2dcecb189f1af2ad2b606a4254452b7525b992d160fbb5acc5e6747dd8f572d6f055dded4d07b0e5680184af3f58c86ef434e6c7  0002-Allow-rustdoc-to-work-when-cross-compiling-on-musl.patch
4fc9d7ced03f0effb3bcce385fa1663d37ae9f271b79c8270fa442a6e9a2de3694246f3a95eaa0fb5177558c0f312e8df38ce514b69c889642b6895197e0b15f  0003-Require-static-native-libraries-when-linking-static-.patch
a9fd9de296f3c4a54534901d3b9912b1cafd0787ad2358e872a030393c7f9f6e57adf39f6a16699621389056edd8118dbb4e8071bce97906b1311b39ccec4098  0004-Remove-nostdlib-and-musl_root-from-musl-targets.patch
fa67468667903290b8e6d16d6777f80aac49d987e8e60c43f8302da77fa1e0e35c4d363d216867c79989c8c35d552c0d5dd6bce69734ed0ce75b4b67ca924953  0005-Prefer-libgcc_eh-over-libunwind-on-musl.patch
5798114c8eb1b914b7affe9925c477f5dcd135925ed925580b69811ca3249c40ca73731425b5e15322879817ad5cb17e19dc78748e993a6c56fa5570a44ce228  0006-test-use-extern-for-plugins-Don-t-assume-multilib.patch
7a559ce8f92f015cc5173fdf378e7e3b52052b7a0ea2604e851121bcdab098220c6cdba59980a087560865b339e761afa0570fefb63b0def5e45f512bb0d1a9e  0007-test-sysroot-crates-are-unstable-Fix-test-when-rpath.patch
1bd86522b04d95baa42aff5656cc12642f4772de021aa6b13fc809cca998ec2ac78b075f66c57c0a565804a14ad6f99f34fd9b3470198970c57b13b61e6bbcad  0008-Ignore-broken-and-non-applicable-tests.patch
ab7cea491237894bfa77e2a74ca27a3ff20c38c6428e853fa384e5ba511afe496f7ec3210048f26b393224c47414440c5e7beea0e480a471128b301515750196  0009-Link-stage2-tools-dynamically-to-libstd.patch
5ed3c48782f52e30180b0a012686a5dbea18bf69e053eefd4211fb077b2e5696b1e7031f6b37659cbdf78e9fa902abe36aeb384a3ff88f8ab2ba5ada7b32a3f9  0010-Move-debugger-scripts-to-usr-share-rust.patch
63701e7d8c5606abe94cba6d157a60682b67f6eb585f16dd543061229e5cfff777a675d984541398a4c0bf4d8f7a873428b9849ebcaa9acdc685c269b40c6a57  0012-Fix-dynamic-linkage-of-musl-libc-for-the-libc-crate.patch
9d772f7d109becbae2fa007e350a4357b70afc13a6a2d9b64885cb0970742785cd4e770b28cdaa6a4314a7db0d44a91f0a95334c46b51591c89ad17cdc3e020b  0014-Void-fix-linkage-against-host-target-LLVM-in-cross-s.patch
70b12454888b80487b5819e03800c86ad978099f0b607e60b8e129f2388780068b11e79a0382a58af7a03f7c2e8bf28e1682edc1d55118ad4be8e72ddf011a59  disable_miri.patch
"
