Build debug with thin lto

This commit is contained in:
topjohnwu 2024-07-31 16:57:30 -07:00
parent 46a34e19bc
commit 606d076251
5 changed files with 68 additions and 41 deletions

View File

@ -140,17 +140,13 @@ def rm_rf(path: Path):
shutil.rmtree(path, ignore_errors=False, onerror=rm_on_error) shutil.rmtree(path, ignore_errors=False, onerror=rm_on_error)
def execv(cmd, env=None): def execv(cmds: list, env=None):
return subprocess.run(cmd, stdout=STDOUT, env=env) return subprocess.run(cmds, stdout=STDOUT, env=env)
def system(cmd): def cmd_out(cmds: list, env=None):
return subprocess.run(cmd, shell=True, stdout=STDOUT)
def cmd_out(cmd, env=None):
return ( return (
subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=env) subprocess.run(cmds, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=env)
.stdout.strip() .stdout.strip()
.decode("utf-8") .decode("utf-8")
) )
@ -225,21 +221,22 @@ def clean_elf():
elf_cleaner, elf_cleaner,
] ]
) )
args = [elf_cleaner, "--api-level", "23"] cmds = [elf_cleaner, "--api-level", "23"]
args.extend( cmds.extend(glob.glob("native/out/*/magisk"))
Path("native", "out", arch, bin) cmds.extend(glob.glob("native/out/*/magiskpolicy"))
for arch in archs execv(cmds)
for bin in ["magisk", "magiskpolicy"]
)
execv(args)
def run_ndk_build(args, flags): def run_ndk_build(args, cmds: list):
os.chdir("native") os.chdir("native")
flags = "NDK_PROJECT_PATH=. NDK_APPLICATION_MK=src/Application.mk " + flags cmds.append("NDK_PROJECT_PATH=.")
cmds.append("NDK_APPLICATION_MK=src/Application.mk")
cmds.append(f"-j{cpu_count}")
if args.verbose > 1: if args.verbose > 1:
flags = "V=1 " + flags cmds.append("V=1")
proc = system(f"{ndk_build} {flags} -j{cpu_count}") if not args.release:
cmds.append("MAGISK_DEBUG=1")
proc = execv([ndk_build, *cmds])
if proc.returncode != 0: if proc.returncode != 0:
error("Build binary failed!") error("Build binary failed!")
os.chdir("..") os.chdir("..")
@ -255,37 +252,37 @@ def run_ndk_build(args, flags):
def build_cpp_src(args, targets: set): def build_cpp_src(args, targets: set):
dump_flag_header() dump_flag_header()
flags = "" cmds = []
clean = False clean = False
if "magisk" in targets: if "magisk" in targets:
flags += " B_MAGISK=1" cmds.append("B_MAGISK=1")
clean = True clean = True
if "magiskpolicy" in targets: if "magiskpolicy" in targets:
flags += " B_POLICY=1" cmds.append("B_POLICY=1")
clean = True clean = True
if "magiskinit" in targets: if "magiskinit" in targets:
flags += " B_PRELOAD=1" cmds.append("B_PRELOAD=1")
if "resetprop" in targets: if "resetprop" in targets:
flags += " B_PROP=1" cmds.append("B_PROP=1")
if flags: if cmds:
run_ndk_build(args, flags) run_ndk_build(args, cmds)
flags = "" cmds.clear()
if "magiskinit" in targets: if "magiskinit" in targets:
flags += " B_INIT=1" cmds.append("B_INIT=1")
if "magiskboot" in targets: if "magiskboot" in targets:
flags += " B_BOOT=1" cmds.append("B_BOOT=1")
if flags: if cmds:
flags += " B_CRT0=1" cmds.append("B_CRT0=1")
run_ndk_build(args, flags) run_ndk_build(args, cmds)
if clean: if clean:
clean_elf() clean_elf()
@ -295,7 +292,9 @@ def run_cargo(cmds):
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"] = str(rust_bin / f"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"-Z dwarf-version=4 -Clinker-plugin-lto -Z threads={min(8, cpu_count)}"
)
return execv([cargo, *cmds], env) return execv([cargo, *cmds], env)
@ -503,7 +502,6 @@ def cleanup(args):
header("* Cleaning C++") header("* Cleaning C++")
rm_rf(Path("native", "libs")) rm_rf(Path("native", "libs"))
rm_rf(Path("native", "obj")) rm_rf(Path("native", "obj"))
rm_rf(Path("native", "out"))
if "rust" in targets: if "rust" in targets:
header("* Cleaning Rust") header("* Cleaning Rust")
@ -513,6 +511,9 @@ def cleanup(args):
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 "native" in targets:
rm_rf(Path("native", "out"))
if "java" in targets: if "java" in targets:
header("* Cleaning java") header("* Cleaning java")
execv([gradlew, ":app:clean"], env=find_jdk()) execv([gradlew, ":app:clean"], env=find_jdk())

View File

@ -1,7 +1,6 @@
APP_BUILD_SCRIPT := src/Android.mk APP_BUILD_SCRIPT := src/Android.mk
APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 riscv64 APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 riscv64
APP_CFLAGS := -Wall -Oz -fomit-frame-pointer -flto APP_CFLAGS := -Wall -Oz -fomit-frame-pointer
APP_LDFLAGS := -flto -Wl,--icf=all
APP_CPPFLAGS := -std=c++23 APP_CPPFLAGS := -std=c++23
APP_STL := none APP_STL := none
APP_PLATFORM := android-23 APP_PLATFORM := android-23
@ -9,11 +8,25 @@ APP_THIN_ARCHIVE := true
APP_STRIP_MODE := none APP_STRIP_MODE := none
APP_SUPPORT_FLEXIBLE_PAGE_SIZES := true APP_SUPPORT_FLEXIBLE_PAGE_SIZES := true
ifdef MAGISK_DEBUG
NDK_APP_OUT := ./obj/debug
APP_CFLAGS += -flto=thin -gdwarf-4
APP_LDFLAGS += -flto=thin
else
NDK_APP_OUT := ./obj/release
APP_CFLAGS += -flto
APP_LDFLAGS += -flto -Wl,--icf=all
endif
ifdef B_CRT0 ifdef B_CRT0
# Disable all security and debugging features # Disable all security and debugging features
APP_CFLAGS += -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-stack-protector -U_FORTIFY_SOURCE APP_CFLAGS += -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-stack-protector -U_FORTIFY_SOURCE
# Override output folder to make sure all dependencies are rebuilt with new CFLAGS # Override output folder to make sure all dependencies are rebuilt with new CFLAGS
NDK_APP_OUT := ./obj/nolibc NDK_APP_OUT := $(NDK_APP_OUT)-nolibc
endif endif

View File

@ -58,7 +58,7 @@ rev = "809df65b20d61e88afb7f514b5cfdd3d1958a40f"
[profile.dev] [profile.dev]
opt-level = "z" opt-level = "z"
lto = true lto = "thin"
panic = "abort" panic = "abort"
[profile.release] [profile.release]

View File

@ -7,8 +7,16 @@
using namespace std; using namespace std;
#ifdef USE_CRT0 #ifdef USE_CRT0
__asm__(".global vfprintf \n vfprintf = musl_vfprintf"); __BEGIN_DECLS
__asm__(".global vsscanf \n vsscanf = tfp_vsscanf"); int tfp_vsscanf(const char *s, const char *fmt, va_list args);
int vsscanf(const char *s, const char *fmt, va_list args) {
return tfp_vsscanf(s, fmt, args);
}
int musl_vfprintf(FILE *stream, const char *format, va_list arg);
int vfprintf(FILE *stream, const char *format, va_list arg) {
return musl_vfprintf(stream, format, arg);
}
__END_DECLS
#endif #endif
static void print_formats() { static void print_formats() {

View File

@ -12,7 +12,12 @@
using namespace std; using namespace std;
#ifdef USE_CRT0 #ifdef USE_CRT0
__asm__(".global vfprintf \n vfprintf = tfp_vfprintf"); __BEGIN_DECLS
int tfp_vfprintf(FILE *stream, const char *format, va_list arg);
int vfprintf(FILE *stream, const char *format, va_list arg) {
return tfp_vfprintf(stream, format, arg);
}
__END_DECLS
#endif #endif
bool unxz(out_stream &strm, rust::Slice<const uint8_t> bytes) { bool unxz(out_stream &strm, rust::Slice<const uint8_t> bytes) {