From 3fd7bc8b1c5f7eaab9374674e7fc0a5798673b5b Mon Sep 17 00:00:00 2001
From: Matt Jolly <kangie@gentoo.org>
Date: Sat, 22 Aug 2026 15:54:04 +1000
Subject: [PATCH 1/2] build/rust: Use rust_sysroot for bindings and wrappers
 globally

Hardcoded `//third_party/rust-toolchain` paths were still lingering
in various places, causing build failures (e.g., ninja errors for missing
`rustc` and `rs_bindings_from_cc` rules) when utilizing a custom Rust
sysroot.

This patch updates `rustc_wrapper_inputs`, `cc_bindings_from_rs`,
`rs_bindings_from_cc`, `rustfmt`, and `clippy-driver` to use the
configured `rust_sysroot` GN argument instead of hardcoding the default
tree path. For Python scripts like `run_rs_bindings_from_cc.py`, paths
are now parsed via command line arguments rather than relying on
relative directory resolution.

Bug: 491242305
Signed-off-by: Matt Jolly <kangie@gentoo.org>
--- a/build/config/rust.gni
+++ b/build/config/rust.gni
@@ -431,7 +431,7 @@ rustc_wrapper_inputs = [
 ]
 
 if (host_os == "win") {
-  rustc_wrapper_inputs += [ "//third_party/rust-toolchain/bin/rustc.exe" ]
+  rustc_wrapper_inputs += [ "$rust_sysroot/bin/rustc.exe" ]
 } else {
-  rustc_wrapper_inputs += [ "//third_party/rust-toolchain/bin/rustc" ]
+  rustc_wrapper_inputs += [ "$rust_sysroot/bin/rustc" ]
 }
--- a/build/rust/gni_impl/run_rs_bindings_from_cc.py
+++ b/build/rust/gni_impl/run_rs_bindings_from_cc.py
@@ -15,15 +15,6 @@
 sys.path.append(THIS_DIR)
 from run_bindgen import filter_clang_args
 
-RUST_TOOLCHAIN_DIR = os.path.join(
-    CHROMIUM_SRC_DIR, "third_party", "rust-toolchain"
-)
-RUSTFMT_EXE_PATH = os.path.join(RUST_TOOLCHAIN_DIR, "bin", "rustfmt")
-RUSTFMT_CONFIG_PATH = os.path.join(CHROMIUM_SRC_DIR, ".rustfmt.toml")
-RS_BINDINGS_FROM_CC_EXE_PATH = os.path.join(
-    RUST_TOOLCHAIN_DIR, "bin", "rs_bindings_from_cc"
-)
-
 
 def format_cmdline(args):
 
@@ -38,6 +29,8 @@
 
 def main():
     parser = argparse.ArgumentParser()
+    parser.add_argument("--rs_bindings_from_cc_path", required=True)
+    parser.add_argument("--rustfmt_path", required=True)
     (
         parser.add_argument(
             "--targets_and_args_from_gn",
@@ -111,8 +104,9 @@
     )
 
     # All Crubit invocations in Chromium share the following cmdline args.
-    generator_args.append(f"--rustfmt_exe_path={RUSTFMT_EXE_PATH}")
-    generator_args.append(f"--rustfmt_config_path={RUSTFMT_CONFIG_PATH}")
+    generator_args.append(f"--rustfmt_exe_path={args.rustfmt_path}")
+    rustfmt_config_path = os.path.join(CHROMIUM_SRC_DIR, ".rustfmt.toml")
+    generator_args.append(f"--rustfmt_config_path={rustfmt_config_path}")
     generator_args.append(
         "--crubit_support_path=third_party/crubit/src/rs_bindings_from_cc/support"
     )
@@ -143,7 +137,7 @@
     clang_args += ["-Wno-unused-command-line-argument"]
 
     # Print a copy&pastable final cmdline when asked for debugging help.
-    cmdline = [RS_BINDINGS_FROM_CC_EXE_PATH, f"--flagfile={params_file_path}"]
+    cmdline = [args.rs_bindings_from_cc_path, f"--flagfile={params_file_path}"]
     cmdline.extend(clang_args)
     if "CRUBIT_DEBUG" in os.environ:
         pretty_cmdline = format_cmdline(cmdline)
--- a/build/rust/gni_impl/rust_target.gni
+++ b/build/rust/gni_impl/rust_target.gni
@@ -725,9 +725,9 @@ template("rust_target") {
           # `import`ed for `LoadRustEnvAndFlags`, etc.
           inputs = rustc_wrapper_inputs
           if (host_os == "win") {
-            inputs += [ "//third_party/rust-toolchain/bin/clippy-driver.exe" ]
+            inputs += [ "$rust_sysroot/bin/clippy-driver.exe" ]
           } else {
-            inputs += [ "//third_party/rust-toolchain/bin/clippy-driver" ]
+            inputs += [ "$rust_sysroot/bin/clippy-driver" ]
           }
           if (rust_clippy_conf_file != "") {
             inputs += [ rust_clippy_conf_file ]
@@ -735,7 +735,7 @@ template("rust_target") {
 
           args = [
             "--clippy-driver",
-            rebase_path("//third_party/rust-toolchain/bin/clippy-driver",
+            rebase_path("$rust_sysroot/bin/clippy-driver",
                         root_build_dir),
             "--rustc-env-and-flags",
             rebase_path(_rustc_env_and_flags, root_build_dir),
--- a/build/rust/rs_bindings_from_cc.gni
+++ b/build/rust/rs_bindings_from_cc.gni
@@ -248,7 +248,15 @@ template("rs_bindings_from_cc") {
     }
 
     script = "//build/rust/gni_impl/run_rs_bindings_from_cc.py"
-    inputs = [ "//third_party/rust-toolchain/bin/rs_bindings_from_cc" ]
+
+    if (host_os == "win") {
+      _rs_bindings_from_cc_exe = "${rust_sysroot}/bin/rs_bindings_from_cc.exe"
+      _rustfmt_exe = "${rust_sysroot}/bin/rustfmt.exe"
+    } else {
+      _rs_bindings_from_cc_exe = "${rust_sysroot}/bin/rs_bindings_from_cc"
+      _rustfmt_exe = "${rust_sysroot}/bin/rustfmt"
+    }
+    inputs = [ _rs_bindings_from_cc_exe ]
     sources = invoker.public_headers
     outputs = [
       _rs_out_path,
@@ -268,6 +276,10 @@ template("rs_bindings_from_cc") {
       string_join(",", _rebased_public_headers),
       "--targets_and_args_from_gn",
       rebase_path(_metadata_path),
+      "--rs_bindings_from_cc_path",
+      rebase_path(_rs_bindings_from_cc_exe, root_build_dir),
+      "--rustfmt_path",
+      rebase_path(_rustfmt_exe, root_build_dir),
     ]
 
     # Several important compiler flags come from default_compiler_configs
-- 
2.55.0
