Update build.py to use pathlib

This commit is contained in:
topjohnwu 2024-05-08 21:50:59 -07:00
parent 0e56991369
commit c1cadf4bdc

163
build.py
View File

@ -4,7 +4,6 @@ import glob
import lzma
import multiprocessing
import os
import os.path as op
import platform
import shutil
import stat
@ -13,6 +12,7 @@ import sys
import tarfile
import textwrap
import urllib.request
from pathlib import Path
from zipfile import ZipFile
@ -79,18 +79,16 @@ 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"]
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)
native_gen_path = op.realpath(op.join("native", "out", "generated"))
sdk_path = Path(os.environ["ANDROID_SDK_ROOT"])
ndk_root = sdk_path / "ndk"
ndk_path = ndk_root / "magisk"
ndk_build = ndk_path / "ndk-build"
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 ""))
adb_path = sdk_path / "platform-tools" / f"adb{EXE_EXT}"
native_gen_path = Path("native", "out", "generated").resolve()
# Global vars
config = {}
@ -98,7 +96,7 @@ STDOUT = None
build_tools = None
def mv(source, target):
def mv(source: Path, target: Path):
try:
shutil.move(source, target)
vprint(f"mv {source} -> {target}")
@ -106,7 +104,7 @@ def mv(source, target):
pass
def cp(source, target):
def cp(source: Path, target: Path):
try:
shutil.copyfile(source, target)
vprint(f"cp {source} -> {target}")
@ -114,7 +112,7 @@ def cp(source, target):
pass
def rm(file):
def rm(file: Path):
try:
os.remove(file)
vprint(f"rm {file}")
@ -132,22 +130,11 @@ def rm_on_error(func, path, _):
pass
def rm_rf(path):
def rm_rf(path: Path):
vprint(f"rm -rf {path}")
shutil.rmtree(path, ignore_errors=False, onerror=rm_on_error)
def mkdir(path, mode=0o755):
try:
os.mkdir(path, mode)
except:
pass
def mkdir_p(path, mode=0o755):
os.makedirs(path, mode, exist_ok=True)
def execv(cmd, env=None):
return subprocess.run(cmd, stdout=STDOUT, env=env)
@ -192,11 +179,13 @@ def load_config(args):
config["versionCode"] = 1000000
config["outdir"] = "out"
args.config = Path(args.config)
# Load prop files
if op.exists(args.config):
if args.config.exists():
config.update(parse_props(args.config))
if op.exists("gradle.properties"):
if Path("gradle.properties").exists():
for key, value in parse_props("gradle.properties").items():
if key.startswith("magisk."):
config[key[7:]] = value
@ -206,17 +195,19 @@ def load_config(args):
except ValueError:
error('Config error: "versionCode" is required to be an integer')
mkdir_p(config["outdir"])
config["outdir"] = Path(config["outdir"])
config["outdir"].mkdir(mode=0o755, parents=True, exist_ok=True)
global STDOUT
STDOUT = None if args.verbose else subprocess.DEVNULL
def clean_elf():
if is_windows:
elf_cleaner = op.join("tools", "elf-cleaner.exe")
elf_cleaner = Path("tools", "elf-cleaner.exe")
else:
elf_cleaner = op.join("native", "out", "elf-cleaner")
if not op.exists(elf_cleaner):
elf_cleaner = Path("native", "out", "elf-cleaner")
if not elf_cleaner.exists():
execv(
[
"gcc",
@ -231,7 +222,7 @@ def clean_elf():
)
args = [elf_cleaner, "--api-level", "23"]
args.extend(
op.join("native", "out", arch, bin)
Path("native", "out", arch, bin)
for arch in archs
for bin in ["magisk", "magiskpolicy"]
)
@ -247,22 +238,22 @@ def run_ndk_build(flags):
os.chdir("..")
for arch in archs:
for tgt in support_targets + ["libinit-ld.so"]:
source = op.join("native", "libs", arch, tgt)
target = op.join("native", "out", arch, tgt)
source = Path("native", "libs", arch, tgt)
target = Path("native", "out", arch, tgt)
mv(source, target)
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"] = op.join(rust_bin, "rustc" + EXE_EXT)
env["CARGO_BUILD_RUSTC"] = str(rust_bin / f"rustc{EXE_EXT}")
env["RUSTFLAGS"] = f"-Clinker-plugin-lto -Zthreads={min(8, cpu_count)}"
return execv([cargo, *cmds], env)
def run_cargo_build(args):
native_out = op.join("..", "out")
mkdir(native_out)
native_out = Path("..", "out")
native_out.mkdir(mode=0o755, exist_ok=True)
targets = set(args.target) & set(rust_targets)
if "resetprop" in args.target:
@ -295,11 +286,11 @@ def run_cargo_build(args):
if proc.returncode != 0:
error("Build binary failed!")
arch_out = op.join(native_out, arch)
mkdir(arch_out)
arch_out = native_out / arch
arch_out.mkdir(mode=0o755, exist_ok=True)
for tgt in targets:
source = op.join("target", rust_triple, rust_out, f"lib{tgt}.a")
target = op.join(arch_out, f"lib{tgt}-rs.a")
source = Path("target", rust_triple, rust_out, f"lib{tgt}.a")
target = arch_out / f"lib{tgt}-rs.a"
mv(source, target)
@ -308,14 +299,14 @@ def run_cargo_cmd(args):
STDOUT = None
if len(args.commands) >= 1 and args.commands[0] == "--":
args.commands = args.commands[1:]
os.chdir(op.join("native", "src"))
os.chdir(Path("native", "src"))
run_cargo(args.commands)
os.chdir(op.join("..", ".."))
os.chdir(Path("..", ".."))
def write_if_diff(file_name, text):
def write_if_diff(file_name: Path, text: str):
do_write = True
if op.exists(file_name):
if file_name.exists():
with open(file_name, "r") as f:
orig = f.read()
do_write = orig != text
@ -335,12 +326,12 @@ def binary_dump(src, var_name, compressor=xz):
def dump_bin_header(args):
mkdir_p(native_gen_path)
native_gen_path.mkdir(mode=0o755, parents=True, exist_ok=True)
for arch in archs:
preload = op.join("native", "out", arch, "libinit-ld.so")
preload = Path("native", "out", arch, "libinit-ld.so")
with open(preload, "rb") as src:
text = binary_dump(src, "init_ld_xz")
write_if_diff(op.join(native_gen_path, f"{arch}_binaries.h"), text)
write_if_diff(Path(native_gen_path, f"{arch}_binaries.h"), text)
def dump_flag_header():
@ -357,14 +348,14 @@ def dump_flag_header():
flag_txt += f'#define MAGISK_VER_CODE {config["versionCode"]}\n'
flag_txt += f"#define MAGISK_DEBUG {0 if args.release else 1}\n"
mkdir_p(native_gen_path)
write_if_diff(op.join(native_gen_path, "flags.h"), flag_txt)
native_gen_path.mkdir(mode=0o755, parents=True, exist_ok=True)
write_if_diff(Path(native_gen_path, "flags.h"), flag_txt)
def build_binary(args):
# Verify NDK install
try:
with open(op.join(ndk_path, "ONDK_VERSION"), "r") as ondk_ver:
with open(Path(ndk_path, "ONDK_VERSION"), "r") as ondk_ver:
assert ondk_ver.read().strip(" \t\r\n") == config["ondkVersion"]
except:
error('Unmatched NDK. Please install/upgrade NDK with "build.py ndk"')
@ -381,9 +372,9 @@ def build_binary(args):
header("* Building binaries: " + " ".join(args.target))
os.chdir(op.join("native", "src"))
os.chdir(Path("native", "src"))
run_cargo_build(args)
os.chdir(op.join("..", ".."))
os.chdir(Path("..", ".."))
dump_flag_header()
@ -424,7 +415,7 @@ def build_binary(args):
if clean:
clean_elf()
# BusyBox is built with different libc
# BusyBox is built with different API level
if "busybox" in args.target:
run_ndk_build("B_BB=1")
@ -434,10 +425,10 @@ def find_jdk():
env = os.environ.copy()
if "ANDROID_STUDIO" in env:
studio = env["ANDROID_STUDIO"]
jbr = op.join(studio, "jbr", "bin")
if not op.exists(jbr):
jbr = op.join(studio, "Contents", "jbr", "Contents", "Home", "bin")
if op.exists(jbr):
jbr = Path(studio, "jbr", "bin")
if not jbr.exists():
jbr = Path(studio, "Contents", "jbr", "Contents", "Home", "bin")
if jbr.exists():
env["PATH"] = f'{jbr}{os.pathsep}{env["PATH"]}'
no_jdk = False
@ -470,7 +461,7 @@ def build_apk(args, module):
[
gradlew,
f"{module}:assemble{build_type}",
"-PconfigPath=" + op.abspath(args.config),
f"-PconfigPath={args.config.resolve()}",
],
env=env,
)
@ -480,10 +471,10 @@ def build_apk(args, module):
build_type = build_type.lower()
apk = f"{module}-{build_type}.apk"
source = op.join(module, "build", "outputs", "apk", build_type, apk)
target = op.join(config["outdir"], apk)
source = Path(module, "build", "outputs", "apk", build_type, apk)
target = config["outdir"] / apk
mv(source, target)
header("Output: " + target)
header(f"Output: {target}")
def build_app(args):
@ -494,8 +485,8 @@ def build_app(args):
# build process. Copy the stub APK into output directory.
build_type = "release" if args.release else "debug"
apk = f"stub-{build_type}.apk"
source = op.join("app", "src", build_type, "assets", "stub.apk")
target = op.join(config["outdir"], apk)
source = Path("app", "src", build_type, "assets", "stub.apk")
target = config["outdir"] / apk
cp(source, target)
@ -516,30 +507,30 @@ def cleanup(args):
if "cpp" in args.target:
header("* Cleaning C++")
rm_rf(op.join("native", "libs"))
rm_rf(op.join("native", "obj"))
rm_rf(op.join("native", "out"))
rm_rf(Path("native", "libs"))
rm_rf(Path("native", "obj"))
rm_rf(Path("native", "out"))
if "rust" in args.target:
header("* Cleaning Rust")
rm_rf(op.join("native", "src", "target"))
rm(op.join("native", "src", "boot", "proto", "mod.rs"))
rm(op.join("native", "src", "boot", "proto", "update_metadata.rs"))
rm_rf(Path("native", "src", "target"))
rm(Path("native", "src", "boot", "proto", "mod.rs"))
rm(Path("native", "src", "boot", "proto", "update_metadata.rs"))
for rs_gen in glob.glob("native/**/*-rs.*pp", recursive=True):
rm(rs_gen)
if "java" in args.target:
header("* Cleaning java")
execv([gradlew, "app:clean", "app:shared:clean", "stub:clean"], env=find_jdk())
rm_rf(op.join("app", "src", "debug"))
rm_rf(op.join("app", "src", "release"))
rm_rf(Path("app", "src", "debug"))
rm_rf(Path("app", "src", "release"))
def setup_ndk(args):
ndk_ver = config["ondkVersion"]
url = f"https://github.com/topjohnwu/ondk/releases/download/{ndk_ver}/ondk-{ndk_ver}-{os_name}.tar.xz"
ndk_archive = url.split("/")[-1]
ondk_path = op.join(ndk_root, f"ondk-{ndk_ver}")
ondk_path = Path(ndk_root, f"ondk-{ndk_ver}")
header(f"* Downloading and extracting {ndk_archive}")
rm_rf(ondk_path)
@ -553,10 +544,12 @@ def setup_ndk(args):
def push_files(args, script):
abi = cmd_out([adb_path, "shell", "getprop", "ro.product.cpu.abi"])
apk = config["outdir"] + ("/app-release.apk" if args.release else "/app-debug.apk")
apk = Path(
config["outdir"], ("app-release.apk" if args.release else "app-debug.apk")
)
# Extract busybox from APK
busybox = f'{config["outdir"]}/busybox'
busybox = Path(config["outdir"], "busybox")
with ZipFile(apk) as zf:
with zf.open(f"lib/{abi}/libbusybox.so") as libbb:
with open(busybox, "wb") as bb:
@ -580,7 +573,7 @@ def setup_avd(args):
header("* Setting up emulator")
push_files(args, "scripts/avd_magisk.sh")
push_files(args, Path("scripts", "avd_magisk.sh"))
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_magisk.sh"])
if proc.returncode != 0:
@ -592,26 +585,28 @@ def patch_avd_ramdisk(args):
args.release = False
build_all(args)
args.ramdisk = Path(args.ramdisk)
header("* Patching emulator ramdisk.img")
# Create a backup to prevent accidental overwrites
backup = args.ramdisk + ".bak"
if not op.exists(backup):
backup = args.ramdisk.parent / f"{args.ramdisk.name}.bak"
if not backup.exists():
cp(args.ramdisk, backup)
ini = op.join(op.dirname(args.ramdisk), "advancedFeatures.ini")
ini = args.ramdisk.parent / "advancedFeatures.ini"
with open(ini, "r") as f:
adv_ft = f.read()
# Need to turn off system as root
if "SystemAsRoot = on" in adv_ft:
# Create a backup
cp(ini, ini + ".bak")
cp(ini, ini.parent / f"{ini.name}.bak")
adv_ft = adv_ft.replace("SystemAsRoot = on", "SystemAsRoot = off")
with open(ini, "w") as f:
f.write(adv_ft)
push_files(args, "scripts/avd_patch.sh")
push_files(args, Path("scripts", "avd_patch.sh"))
proc = execv([adb_path, "push", backup, "/data/local/tmp/ramdisk.cpio.tmp"])
if proc.returncode != 0: