Provide easy access to the cargo command

This commit is contained in:
topjohnwu 2023-05-30 18:41:37 -07:00
parent c79bc83275
commit 665c6bdc4b

View File

@ -60,6 +60,8 @@ if "ANDROID_SDK_ROOT" not in os.environ:
error("Please set Android SDK path to environment variable ANDROID_SDK_ROOT!")
cpu_count = multiprocessing.cpu_count()
os_name = platform.system().lower()
archs = ["armeabi-v7a", "x86", "arm64-v8a", "x86_64"]
triples = [
"armv7a-linux-androideabi",
@ -71,11 +73,14 @@ default_targets = ["magisk", "magiskinit", "magiskboot", "magiskpolicy", "busybo
support_targets = default_targets + ["resetprop"]
rust_targets = ["magisk", "magiskinit", "magiskboot", "magiskpolicy"]
sdk_path = os.environ["ANDROID_SDK_ROOT"]
sdk_path = op.realpath(os.environ["ANDROID_SDK_ROOT"])
ndk_root = op.join(sdk_path, "ndk")
ndk_path = op.join(ndk_root, "magisk")
ndk_build = op.join(ndk_path, "ndk-build")
rust_bin = op.join(ndk_path, "toolchains", "rust", "bin")
llvm_bin = op.join(
ndk_path, "toolchains", "llvm", "prebuilt", f"{os_name}-x86_64", "bin"
)
cargo = op.join(rust_bin, "cargo" + EXE_EXT)
gradlew = op.join(".", "gradlew" + (".bat" if is_windows else ""))
adb_path = op.join(sdk_path, "platform-tools", "adb" + EXE_EXT)
@ -237,6 +242,16 @@ def run_ndk_build(flags):
mv(source, target)
def run_cargo(cmds, triple="aarch64-linux-android"):
env = os.environ.copy()
env["PATH"] = f'{rust_bin}:{env["PATH"]}'
env["CARGO_BUILD_RUSTC"] = op.join(rust_bin, "rustc" + EXE_EXT)
env["RUSTFLAGS"] = "-Clinker-plugin-lto"
env["TARGET_CC"] = op.join(llvm_bin, "clang" + EXE_EXT)
env["TARGET_CFLAGS"] = f"--target={triple}23"
return execv([cargo, *cmds], env)
def run_cargo_build(args):
os.chdir(op.join("native", "src"))
native_out = op.join("..", "out")
@ -246,11 +261,8 @@ def run_cargo_build(args):
if "resetprop" in args.target:
targets.add("magisk")
env = os.environ.copy()
env["CARGO_BUILD_RUSTC"] = op.join(rust_bin, "rustc" + EXE_EXT)
# Start building the actual build commands
cmds = [cargo, "build"]
cmds = ["build"]
for target in targets:
cmds.append("-p")
cmds.append(target)
@ -261,18 +273,15 @@ def run_cargo_build(args):
if not args.verbose:
cmds.append("-q")
os_name = platform.system().lower()
llvm_bin = op.join(
ndk_path, "toolchains", "llvm", "prebuilt", f"{os_name}-x86_64", "bin"
)
env["TARGET_CC"] = op.join(llvm_bin, "clang" + EXE_EXT)
env["RUSTFLAGS"] = "-Clinker-plugin-lto"
cmds.append("--target")
cmds.append("")
for arch, triple in zip(archs, triples):
env["TARGET_CFLAGS"] = f"--target={triple}23"
rust_triple = (
"thumbv7neon-linux-androideabi" if triple.startswith("armv7") else triple
)
proc = execv([*cmds, "--target", rust_triple], env)
cmds[-1] = rust_triple
proc = run_cargo(cmds, triple)
if proc.returncode != 0:
error("Build binary failed!")
@ -286,6 +295,16 @@ def run_cargo_build(args):
os.chdir(op.join("..", ".."))
def run_cargo_cmd(args):
global STDOUT
STDOUT = None
if len(args.commands) >= 1 and args.commands[0] == "--":
args.commands = args.commands[1:]
os.chdir(op.join("native", "src"))
run_cargo(args.commands)
os.chdir(op.join("..", ".."))
def write_if_diff(file_name, text):
do_write = True
if op.exists(file_name):
@ -503,7 +522,6 @@ def cleanup(args):
def setup_ndk(args):
os_name = platform.system().lower()
ndk_ver = config["ondkVersion"]
url = f"https://github.com/topjohnwu/ondk/releases/download/{ndk_ver}/ondk-{ndk_ver}-{os_name}.tar.gz"
ndk_archive = url.split("/")[-1]
@ -644,6 +662,10 @@ binary_parser.add_argument(
)
binary_parser.set_defaults(func=build_binary)
cargo_parser = subparsers.add_parser("cargo", help="run cargo with proper environment")
cargo_parser.add_argument("commands", nargs=argparse.REMAINDER)
cargo_parser.set_defaults(func=run_cargo_cmd)
app_parser = subparsers.add_parser("app", help="build the Magisk app")
app_parser.set_defaults(func=build_app)