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

View File

@ -1,7 +1,10 @@
[build] [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. # The actual compilation will have the target overriden by command-line.
target = "aarch64-linux-android" 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] [unstable]
build-std = ["std", "panic_abort"] build-std = ["std", "panic_abort"]

View File

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