Skip to content

Commit

Permalink
Switch rustup_wrapper to Rust implementation
Browse files Browse the repository at this point in the history
For better Windows portability
  • Loading branch information
topjohnwu committed May 9, 2024
1 parent 3f2264f commit ec54aed
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 45 deletions.
16 changes: 11 additions & 5 deletions build.py
Expand Up @@ -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()

Expand Down Expand Up @@ -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)


Expand Down
40 changes: 0 additions & 40 deletions scripts/rustup_wrapper.py

This file was deleted.

1 change: 1 addition & 0 deletions tools/rustup_wrapper/.gitignore
@@ -0,0 +1 @@
target/
92 changes: 92 additions & 0 deletions tools/rustup_wrapper/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions 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"
43 changes: 43 additions & 0 deletions 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<String> = 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(|_| ())
}

0 comments on commit ec54aed

Please sign in to comment.