From 3f2264f2c7d37456799f48b5caa022effda02298 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 9 May 2024 00:34:12 -0700 Subject: [PATCH] Support rustup wrapper --- build.py | 23 ++++++++++++++++++++++ scripts/rustup_wrapper.py | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 scripts/rustup_wrapper.py diff --git a/build.py b/build.py index 2729517c0..fcc21182c 100755 --- a/build.py +++ b/build.py @@ -626,6 +626,25 @@ def build_all(args): build_app(args) +def setup_rustup(args): + wrapper_dir = Path(args.wrapper_dir) + rm_rf(wrapper_dir) + wrapper_dir.mkdir(mode=0o755, parents=True, exist_ok=True) + if "CARGO_HOME" in os.environ: + cargo_home = Path(os.environ["CARGO_HOME"]) + else: + cargo_home = Path.home() / ".cargo" + cargo_bin = cargo_home / "bin" + 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) + wrapper.chmod(0o755) + + parser = argparse.ArgumentParser(description="Magisk build script") parser.set_defaults(func=lambda x: None) parser.add_argument( @@ -656,6 +675,10 @@ cargo_parser = subparsers.add_parser("cargo", help="run cargo with proper enviro cargo_parser.add_argument("commands", nargs=argparse.REMAINDER) cargo_parser.set_defaults(func=run_cargo_cmd) +rustup_parser = subparsers.add_parser("rustup", help="setup rustup wrapper") +rustup_parser.add_argument("wrapper_dir", help="path to setup rustup wrapper binaries") +rustup_parser.set_defaults(func=setup_rustup) + app_parser = subparsers.add_parser("app", help="build the Magisk app") app_parser.set_defaults(func=build_app) diff --git a/scripts/rustup_wrapper.py b/scripts/rustup_wrapper.py new file mode 100755 index 000000000..ef9a1303a --- /dev/null +++ b/scripts/rustup_wrapper.py @@ -0,0 +1,40 @@ +#!/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)