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