From ec54aed2c6e97c652587955dcb4274c6cd51de19 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 9 May 2024 02:16:21 -0700 Subject: [PATCH] Switch rustup_wrapper to Rust implementation For better Windows portability --- build.py | 16 ++++-- scripts/rustup_wrapper.py | 40 -------------- tools/rustup_wrapper/.gitignore | 1 + tools/rustup_wrapper/Cargo.lock | 92 ++++++++++++++++++++++++++++++++ tools/rustup_wrapper/Cargo.toml | 9 ++++ tools/rustup_wrapper/src/main.rs | 43 +++++++++++++++ 6 files changed, 156 insertions(+), 45 deletions(-) delete mode 100755 scripts/rustup_wrapper.py create mode 100644 tools/rustup_wrapper/.gitignore create mode 100644 tools/rustup_wrapper/Cargo.lock create mode 100644 tools/rustup_wrapper/Cargo.toml create mode 100644 tools/rustup_wrapper/src/main.rs diff --git a/build.py b/build.py index fcc21182ca519..55167795d2ac5 100755 --- a/build.py +++ b/build.py @@ -86,7 +86,7 @@ def vprint(str): rust_bin = ndk_path / "toolchains" / "rust" / "bin" llvm_bin = ndk_path / "toolchains" / "llvm" / "prebuilt" / f"{os_name}-x86_64" / "bin" cargo = rust_bin / f"cargo{EXE_EXT}" -gradlew = Path("gradlew" + (".bat" if is_windows else "")) +gradlew = Path("gradlew" + (".bat" if is_windows else "")).resolve() adb_path = sdk_path / "platform-tools" / f"adb{EXE_EXT}" native_gen_path = Path("native", "out", "generated").resolve() @@ -638,10 +638,16 @@ def setup_rustup(args): for src in cargo_bin.iterdir(): tgt = wrapper_dir / src.name tgt.symlink_to(src) - # Replace rustup with python script - wrapper = wrapper_dir / "rustup" - wrapper.unlink() - cp(Path("scripts", "rustup_wrapper.py"), wrapper) + + # Build rustup_wrapper + wrapper_src = Path("tools", "rustup_wrapper") + cargo_toml = wrapper_src / "Cargo.toml" + execv([cargo, "build", "--release", f"--manifest-path={cargo_toml}"]) + + # Replace rustup with wrapper + wrapper = wrapper_dir / (f"rustup{EXE_EXT}") + wrapper.unlink(missing_ok=True) + cp(wrapper_src / "target" / "release" / (f"rustup_wrapper{EXE_EXT}"), wrapper) wrapper.chmod(0o755) diff --git a/scripts/rustup_wrapper.py b/scripts/rustup_wrapper.py deleted file mode 100755 index ef9a1303ae791..0000000000000 --- a/scripts/rustup_wrapper.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python3 -import subprocess -import sys -import os -from pathlib import Path - -################################ -# Why do we need this wrapper? -################################ -# -# The command `rustup component list` does not work with custom toolchains: -# > error: toolchain 'magisk' does not support components -# -# However, this command is used by several IDEs to determine available -# components that can be used, such as clippy, rustfmt etc. -# In this script, we simply redirect the output when using the nightly -# channel if any `component` command failed. - -if "CARGO_HOME" in os.environ: - cargo_home = Path(os.environ["CARGO_HOME"]) -else: - cargo_home = Path.home() / ".cargo" - -rustup = cargo_home / "bin" / "rustup" - -if "component" in sys.argv: - proc = subprocess.run( - [rustup, *sys.argv[1:]], - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - if proc.returncode != 0: - # Remove any channel overrides - if sys.argv[1].startswith("+"): - sys.argv.pop(1) - # Return the nightly channel results - sys.exit(subprocess.run([rustup, "+nightly", *sys.argv[1:]]).returncode) - -# Passthrough -sys.exit(subprocess.run([rustup, *sys.argv[1:]]).returncode) diff --git a/tools/rustup_wrapper/.gitignore b/tools/rustup_wrapper/.gitignore new file mode 100644 index 0000000000000..9f970225adb6a --- /dev/null +++ b/tools/rustup_wrapper/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/tools/rustup_wrapper/Cargo.lock b/tools/rustup_wrapper/Cargo.lock new file mode 100644 index 0000000000000..98169f35539ea --- /dev/null +++ b/tools/rustup_wrapper/Cargo.lock @@ -0,0 +1,92 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "home" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "rustup_wrapper" +version = "0.0.0" +dependencies = [ + "home", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/tools/rustup_wrapper/Cargo.toml b/tools/rustup_wrapper/Cargo.toml new file mode 100644 index 0000000000000..e4e0845ca1be1 --- /dev/null +++ b/tools/rustup_wrapper/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rustup_wrapper" +version = "0.0.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +home = "0.5" \ No newline at end of file diff --git a/tools/rustup_wrapper/src/main.rs b/tools/rustup_wrapper/src/main.rs new file mode 100644 index 0000000000000..6cd9ec49e71d5 --- /dev/null +++ b/tools/rustup_wrapper/src/main.rs @@ -0,0 +1,43 @@ +use std::env; +use std::process::{Command, Stdio}; + +use home::cargo_home; + +/******************************** + * Why do we need this wrapper? + ******************************** + * + * The command `rustup component list` does not work with custom toolchains: + * > error: toolchain 'magisk' does not support components + * + * However, this command is used by several IDEs to determine available + * components that can be used, such as clippy, rustfmt etc. + * In this script, we simply redirect the output when using the nightly + * channel if any `component` command failed. +*/ + +fn main() -> std::io::Result<()> { + let rustup = cargo_home()?.join("bin").join("rustup"); + let argv: Vec = env::args().skip(1).collect(); + + if argv.iter().any(|s| s == "component") { + let status = Command::new(&rustup) + .args(&argv) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status()?; + if !status.success() { + let mut cmd = Command::new(&rustup); + cmd.arg("+nightly"); + if argv[0].starts_with('+') { + cmd.args(argv.iter().skip(1)); + } else { + cmd.args(&argv); + } + return cmd.status().map(|_| ()); + } + } + + // Simply pass through + Command::new(&rustup).args(argv.iter()).status().map(|_| ()) +}