Properly configure Rust builds

This commit is contained in:
topjohnwu 2024-08-03 01:28:53 -07:00
parent d4a9ef7b7f
commit 4c14ae33f5
3 changed files with 33 additions and 37 deletions

View File

@ -148,13 +148,12 @@ def execv(cmds: list, env=None):
return subprocess.run(cmds, stdout=out, env=env, shell=is_windows)
def cmd_out(cmds: list, env=None):
def cmd_out(cmds: list):
return (
subprocess.run(
cmds,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
env=env,
shell=is_windows,
)
.stdout.strip()
@ -210,13 +209,12 @@ def run_ndk_build(cmds: list):
error("Build binary failed!")
os.chdir("..")
for arch in support_abis.keys():
for arch in build_abis.keys():
arch_dir = Path("native", "libs", arch)
if arch_dir.exists():
out_dir = Path("native", "out", arch)
for source in arch_dir.iterdir():
target = out_dir / source.name
mv(source, target)
out_dir = Path("native", "out", arch)
for source in arch_dir.iterdir():
target = out_dir / source.name
mv(source, target)
def build_cpp_src(targets: set):
@ -262,9 +260,7 @@ def run_cargo(cmds):
env = os.environ.copy()
env["PATH"] = f'{rust_bin}{os.pathsep}{env["PATH"]}'
env["CARGO_BUILD_RUSTC"] = str(rust_bin / f"rustc{EXE_EXT}")
env["RUSTFLAGS"] = (
f"-Z dwarf-version=4 -Clinker-plugin-lto -Z threads={min(8, cpu_count)}"
)
env["CARGO_BUILD_RUSTFLAGS"] = f"-Z threads={min(8, cpu_count)}"
return execv([cargo, *cmds], env)
@ -278,41 +274,40 @@ def build_rust_src(targets: set):
os.chdir(Path("native", "src"))
native_out = Path("..", "out")
native_out.mkdir(mode=0o755, exist_ok=True)
# Start building the actual build commands
# Start building the build commands
cmds = ["build", "-p", ""]
rust_out = "debug"
if args.release:
cmds.append("-r")
rust_out = "release"
profile = "release"
else:
profile = "debug"
if args.verbose == 0:
cmds.append("-q")
elif args.verbose > 1:
cmds.append("--verbose")
cmds.append("--target")
cmds.append("")
for triple in build_abis.values():
cmds.append("--target")
cmds.append(triple)
for tgt in targets:
cmds[2] = tgt
proc = run_cargo(cmds)
if proc.returncode != 0:
error("Build binary failed!")
os.chdir(Path("..", ".."))
native_out = Path("native", "out")
rust_out = native_out / "rust"
for arch, triple in build_abis.items():
cmds[-1] = triple
for tgt in targets:
cmds[2] = tgt
proc = run_cargo(cmds)
if proc.returncode != 0:
error("Build binary failed!")
arch_out = native_out / arch
arch_out.mkdir(mode=0o755, exist_ok=True)
for tgt in targets:
source = Path("target", triple, rust_out, f"lib{tgt}.a")
source = rust_out / triple / profile / f"lib{tgt}.a"
target = arch_out / f"lib{tgt}-rs.a"
mv(source, target)
os.chdir(Path("..", ".."))
def write_if_diff(file_name: Path, text: str):
do_write = True
@ -620,10 +615,10 @@ def setup_rustup():
# Build rustup_wrapper
wrapper_src = Path("tools", "rustup_wrapper")
cargo_toml = wrapper_src / "Cargo.toml"
execv(
[cargo, "build", "--release", f"--manifest-path={cargo_toml}"]
+ (["--verbose"] if args.verbose > 1 else [])
)
cmds = ["build", "--release", f"--manifest-path={cargo_toml}"]
if args.verbose > 1:
cmds.append("--verbose")
run_cargo(cmds)
# Replace rustup with wrapper
wrapper = wrapper_dir / (f"rustup{EXE_EXT}")

View File

@ -1,7 +1,10 @@
[build]
# Choose arm64 as the default target to make the IDE happy.
# Set arm64 as the default target
# The actual compilation will have the target overriden by command-line.
target = "aarch64-linux-android"
# Enable cross language LTO, and explicitly set dwarf-version for ThinLTO
rustflags = ["-Z", "dwarf-version=4", "-C", "linker-plugin-lto"]
target-dir = "../out/rust"
[unstable]
build-std = ["std", "panic_abort"]

View File

@ -1,2 +0,0 @@
test.cpp
target/