build: improve build.py and env.py scripts

Modernize build scripts and improve reliability across environments:
- Update required Java toolchain to JDK 25 across scripts and CI
- Propagate subprocess exit codes and report filesystem errors
- Fix argument parsing edge cases and dynamic linker path handling
- Anchor working directory and improve terminal output formatting

Assisted-by: Gemini 3.8 Flash
This commit is contained in:
topjohnwu
2026-09-05 08:17:11 +00:00
parent 96221b69fa
commit 173153fcc7
3 changed files with 117 additions and 66 deletions
+47 -15
View File
@@ -10,24 +10,42 @@ from pathlib import Path
from typing import NoReturn
__all__ = [
"ondk_version",
"color_print",
"error",
"header",
"Paths",
"paths",
"run_once",
"ensure_toolchain",
"ensure_cargo",
"ensure_jdk",
"os_name",
"is_windows",
"EXE_EXT",
"no_color",
"cpu_count",
]
ondk_version = "r30.1"
def color_print(code, str):
def color_print(code, msg):
if no_color:
print(str)
print(msg)
else:
str = str.replace("\n", f"\033[0m\n{code}")
print(f"{code}{str}\033[0m")
msg = msg.replace("\n", f"\033[0m\n{code}")
print(f"{code}{msg}\033[0m")
def error(str) -> NoReturn:
color_print("\033[41;39m", f"\n! {str}\n")
def error(msg) -> NoReturn:
color_print("\033[41;39m", f"\n! {msg}\n")
sys.exit(1)
def header(str):
color_print("\033[44;39m", f"\n{str}\n")
def header(msg):
color_print("\033[44;39m", f"\n{msg}\n")
class Paths:
@@ -56,6 +74,7 @@ def paths() -> Paths:
def run_once(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
if not wrapper.has_run:
wrapper.has_run = True
@@ -74,10 +93,12 @@ def ensure_toolchain():
except:
error('Unmatched NDK. Please install/upgrade NDK with "build.py ndk"')
# Use sccache for both Rust and C/C++ by default
if sccache := shutil.which("sccache"):
os.environ["RUSTC_WRAPPER"] = sccache
os.environ["NDK_CCACHE"] = sccache
os.environ["CARGO_INCREMENTAL"] = "0"
# Prefer ccache over sccache for NDK builds when both are installed
if ccache := shutil.which("ccache"):
os.environ["NDK_CCACHE"] = ccache
@@ -97,10 +118,17 @@ def ensure_cargo():
# Cargo calls executables in $RUSTROOT/lib/rustlib/$TRIPLE/bin, we need
# to make sure the runtime linker also search $RUSTROOT/lib for libraries.
# This is only required on Unix, as Windows search dlls from PATH.
rust_lib = str(paths().rust_sysroot / "lib")
if os_name == "darwin":
os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = str(paths().rust_sysroot / "lib")
cur = os.environ.get("DYLD_FALLBACK_LIBRARY_PATH")
os.environ["DYLD_FALLBACK_LIBRARY_PATH"] = (
f"{rust_lib}{os.pathsep}{cur}" if cur else rust_lib
)
elif os_name == "linux":
os.environ["LD_LIBRARY_PATH"] = str(paths().rust_sysroot / "lib")
cur = os.environ.get("LD_LIBRARY_PATH")
os.environ["LD_LIBRARY_PATH"] = (
f"{rust_lib}{os.pathsep}{cur}" if cur else rust_lib
)
@run_once
@@ -124,14 +152,14 @@ def ensure_jdk():
shell=True,
)
output = proc.stdout.strip().decode("utf-8")
no_jdk = proc.returncode != 0 and output.startswith("javac 21")
no_jdk = proc.returncode != 0 or not output.startswith("javac 25")
except FileNotFoundError:
no_jdk = True
if no_jdk:
error(
"Please set Android Studio's path to environment variable ANDROID_STUDIO,\n"
+ "or install JDK 21 and make sure 'javac' is available in PATH"
+ "or install JDK 25 and make sure 'javac' is available in PATH"
)
@@ -144,8 +172,8 @@ if os_name != "linux" and os_name != "darwin":
os_name = "windows"
EXE_EXT = ".exe" if is_windows else ""
no_color = False
if is_windows:
no_color = "NO_COLOR" in os.environ or not sys.stdout.isatty()
if is_windows and not no_color:
try:
import colorama
@@ -162,6 +190,10 @@ cpu_count = multiprocessing.cpu_count()
# When directly invoked, make it a command wrapper
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: env.py <command> [args...]")
sys.exit(1)
ensure_cargo()
ensure_jdk()
subprocess.run(sys.argv[1:])
proc = subprocess.run(sys.argv[1:])
sys.exit(proc.returncode)