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 3deeba1
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 44 deletions.
14 changes: 10 additions & 4 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def run_ndk_build(flags):
def run_cargo(cmds, triple="aarch64-linux-android"):
env = os.environ.copy()
env["PATH"] = f'{rust_bin}{os.pathsep}{env["PATH"]}'
env["CARGO_BUILD_RUSTC"] = str(rust_bin / f"rustc{EXE_EXT}")
env["CARGO_BUILD_RUSTC"] = str(cargo)
env["RUSTFLAGS"] = f"-Clinker-plugin-lto -Zthreads={min(8, cpu_count)}"
return execv([cargo, *cmds], env)

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

# 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 / "rustup"
wrapper.unlink()
cp(Path("scripts", "rustup_wrapper.py"), wrapper)
wrapper.unlink(missing_ok=True)
cp(wrapper_src / "target" / "release" / "rustup_wrapper", 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 3deeba1

Please sign in to comment.