Switch rustup_wrapper to Rust implementation

For better Windows portability
This commit is contained in:
topjohnwu 2024-05-09 02:19:07 -07:00
parent 3f2264f2c7
commit f61827cbec
6 changed files with 155 additions and 44 deletions

View File

@ -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)

View File

@ -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)

1
tools/rustup_wrapper/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

92
tools/rustup_wrapper/Cargo.lock generated Normal file
View File

@ -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"

View File

@ -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"

View File

@ -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(|_| ())
}