Enable Chromium Rust toolchain universally

Bug: 1245714
Change-Id: Ia872b05359382e7f61c4e84603871515160f61dc

Cq-Include-Trybots: luci.chromium.try:android-rust-arm32-rel,android-rust-arm64-dbg,android-rust-arm64-rel,linux-rust-x64-dbg,linux-rust-x64-rel
Change-Id: Ia872b05359382e7f61c4e84603871515160f61dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4379017
Commit-Queue: Collin Baker <[email protected]>
Auto-Submit: Collin Baker <[email protected]>
Reviewed-by: Adrian Taylor <[email protected]>
Reviewed-by: Hans Wennborg <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1124366}
diff --git a/BUILD.gn b/BUILD.gn
index 6617382..7510795 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -858,11 +858,11 @@
         "//mojo/public/rust:mojo_rust_tests",
       ]
     }
-    if (enable_rust_gnrt) {
+
+    # gnrt is a developer tool run on developer machines. It is not needed when
+    # cross-compiling.
+    if (enable_rust_gnrt && host_os == target_os && host_cpu == target_cpu) {
       deps += [
-        # TODO(crbug.com/1291994): Determine whether it makes sense to build/run
-        # this even on Android. If not, remove this and figure out how to run it
-        # only on desktop testers.
         "//tools/crates/gnrt:gnrt_unittests",
         "//tools/crates/gnrt($host_toolchain)",
       ]
@@ -1028,11 +1028,11 @@
     if (use_v4l2_codec || use_vaapi) {
       data_deps += [
         "//components/chromeos_camera:jpeg_encode_accelerator_unittest",
+        "//media/gpu/chromeos:image_processor_test",
         "//media/gpu/test/:video_decode_accelerator_perf_tests",
         "//media/gpu/test/:video_decode_accelerator_tests",
         "//media/gpu/test/:video_encode_accelerator_perf_tests",
         "//media/gpu/test/:video_encode_accelerator_tests",
-        "//media/gpu/chromeos:image_processor_test",
       ]
       if (use_vaapi) {
         data_deps += [
diff --git a/build/config/rust.gni b/build/config/rust.gni
index e50ed8e9..0887148 100644
--- a/build/config/rust.gni
+++ b/build/config/rust.gni
@@ -33,13 +33,8 @@
   # all Rust features enabled.
   enable_all_rust_features = false
 
-  # Use the Rust toolchain built in-tree. See //tools/rust. The toolchain comes
-  # with a prebuilt stdlib for the host platform.
-  #
-  # For now, we don't support cross-compilation at all, but it's in progress. To
-  # allow Rust on Android we have a prebuilt toolchain from the Android team,
-  # which is used when this variable is `false`.
-  use_chromium_rust_toolchain = target_os == host_os
+  # Use the Rust toolchain built in-tree. See //tools/rust.
+  use_chromium_rust_toolchain = true
 
   # Build libstd locally with GN and use that instead of the prebuilts, where
   # applicable. If this is false the prebuilt libstd will always be used. If
@@ -114,7 +109,7 @@
   # Support for Rust mojo bindings. Needs bindgen.
   enable_rust_mojo = enable_all_rust_features
 
-  # Support for the 'gnrt' Rust tool
+  # Support for the 'gnrt' Rust tool.
   enable_rust_gnrt = enable_all_rust_features
 
   # Rust gtest interop
@@ -170,12 +165,6 @@
     rust_sysroot = get_path_info(rust_sysroot_absolute, "abspath")
     use_unverified_rust_toolchain = true
   } else if (use_chromium_rust_toolchain) {
-    if (host_os != target_os) {
-      assert(
-          false,
-          "Attempt to use Chromium Rust toolchain on an unsupported platform")
-    }
-
     rust_sysroot = "//third_party/rust-toolchain"
     use_unverified_rust_toolchain = false
   } else {
diff --git a/build/rust/std/gnrt_config.toml b/build/rust/std/gnrt_config.toml
index 2074c5cb..cb6cca53 100644
--- a/build/rust/std/gnrt_config.toml
+++ b/build/rust/std/gnrt_config.toml
@@ -16,7 +16,7 @@
 output_dir = '$root_out_dir/local_rustc_sysroot/lib/rustlib/$rust_abi_target/lib/'
 
 # This target provides setup needed for building std.
-extra_deps = ['//build/rust/std:std_build_deps']
+extra_gn_deps = ['//build/rust/std:std_build_deps']
 
 [crate.libc]
 # Requires:
@@ -45,3 +45,9 @@
 # Requires:
 # * CFG_DISABLE_UNSTABLE_FEATURES=0 to match how it's built by x.py.
 env = ['CFG_DISABLE_UNSTABLE_FEATURES=0']
+
+# test only depends on proc_macro as an internal detail of the Rust build, so
+# it's implicitly included with std/test. However, we list the std crates and
+# construct the sysroot explicitly. We don't need this, and we don't even want
+# it during cross-compiles (since we will only build host proc_macro crates).
+exclude_deps_in_gn = ['proc_macro']
diff --git a/build/rust/std/rules/BUILD.gn b/build/rust/std/rules/BUILD.gn
index e24daa5a..e7f4f97 100644
--- a/build/rust/std/rules/BUILD.gn
+++ b/build/rust/std/rules/BUILD.gn
@@ -791,7 +791,6 @@
     ":getopts",
     ":panic_abort",
     ":panic_unwind",
-    ":proc_macro",
     ":std",
     "//build/rust/std:std_build_deps",
   ]
diff --git a/build/rust/tests/test_simple_rust_exe/BUILD.gn b/build/rust/tests/test_simple_rust_exe/BUILD.gn
index 3248f29..a800720 100644
--- a/build/rust/tests/test_simple_rust_exe/BUILD.gn
+++ b/build/rust/tests/test_simple_rust_exe/BUILD.gn
@@ -8,5 +8,5 @@
 # //build/rust/rust_executable.gni.
 executable("test_simple_rust_exe") {
   crate_root = "main.rs"
-  deps = [ "//build/rust/std:prebuilt_std_for_rustc" ]
+  deps = [ "//build/rust/std:local_std_for_rustc" ]
 }
diff --git a/mojo/public/rust/BUILD.gn b/mojo/public/rust/BUILD.gn
index a74ddc9..ca460997 100644
--- a/mojo/public/rust/BUILD.gn
+++ b/mojo/public/rust/BUILD.gn
@@ -13,16 +13,18 @@
   deps = [ ":mojo_rust" ]
 }
 
-# Meta target to build test binaries, if supported.
-rust_unit_tests_group("mojo_rust_tests") {
-  deps = []
-  if (can_build_rust_unit_tests) {
-    deps += [
+# Build combined test runner, if Rust-native tests are supported.
+if (can_build_rust_unit_tests) {
+  rust_unit_tests_group("mojo_rust_tests") {
+    deps = [
       "//mojo/public/rust/tests:mojo_rust_encoding_tests",
       "//mojo/public/rust/tests:mojo_rust_integration_tests",
       "//mojo/public/rust/tests:mojo_rust_system_tests",
     ]
   }
+} else {
+  group("mojo_rust_tests") {
+  }
 }
 
 source_set("mojo_c_system_wrapper") {
diff --git a/testing/buildbot/chromium.rust.json b/testing/buildbot/chromium.rust.json
index e10dc77..f69837d 100644
--- a/testing/buildbot/chromium.rust.json
+++ b/testing/buildbot/chromium.rust.json
@@ -44,26 +44,6 @@
           ],
           "service_account": "[email protected]"
         },
-        "test": "gnrt_unittests",
-        "test_id_prefix": "ninja://tools/crates/gnrt:gnrt_unittests/"
-      },
-      {
-        "merge": {
-          "args": [],
-          "script": "//testing/merge_scripts/standard_gtest_merge.py"
-        },
-        "swarming": {
-          "can_use_on_swarming_builders": true,
-          "dimension_sets": [
-            {
-              "device_os": "PQ3A.190801.002",
-              "device_os_flavor": "google",
-              "device_type": "walleye",
-              "os": "Android"
-            }
-          ],
-          "service_account": "[email protected]"
-        },
         "test": "rust_gtest_interop_unittests",
         "test_id_prefix": "ninja://testing/rust_gtest_interop:rust_gtest_interop_unittests/"
       },
@@ -154,26 +134,6 @@
           ],
           "service_account": "[email protected]"
         },
-        "test": "gnrt_unittests",
-        "test_id_prefix": "ninja://tools/crates/gnrt:gnrt_unittests/"
-      },
-      {
-        "merge": {
-          "args": [],
-          "script": "//testing/merge_scripts/standard_gtest_merge.py"
-        },
-        "swarming": {
-          "can_use_on_swarming_builders": true,
-          "dimension_sets": [
-            {
-              "device_os": "PQ3A.190801.002",
-              "device_os_flavor": "google",
-              "device_type": "walleye",
-              "os": "Android"
-            }
-          ],
-          "service_account": "[email protected]"
-        },
         "test": "rust_gtest_interop_unittests",
         "test_id_prefix": "ninja://testing/rust_gtest_interop:rust_gtest_interop_unittests/"
       },
@@ -264,26 +224,6 @@
           ],
           "service_account": "[email protected]"
         },
-        "test": "gnrt_unittests",
-        "test_id_prefix": "ninja://tools/crates/gnrt:gnrt_unittests/"
-      },
-      {
-        "merge": {
-          "args": [],
-          "script": "//testing/merge_scripts/standard_gtest_merge.py"
-        },
-        "swarming": {
-          "can_use_on_swarming_builders": true,
-          "dimension_sets": [
-            {
-              "device_os": "PQ3A.190801.002",
-              "device_os_flavor": "google",
-              "device_type": "walleye",
-              "os": "Android"
-            }
-          ],
-          "service_account": "[email protected]"
-        },
         "test": "rust_gtest_interop_unittests",
         "test_id_prefix": "ninja://testing/rust_gtest_interop:rust_gtest_interop_unittests/"
       },
diff --git a/testing/buildbot/test_suites.pyl b/testing/buildbot/test_suites.pyl
index 58f0442..0cca34b 100644
--- a/testing/buildbot/test_suites.pyl
+++ b/testing/buildbot/test_suites.pyl
@@ -4627,9 +4627,9 @@
       },
     },
 
-    'rust_gtests': {
+    # Rust tests run on all targets.
+    'rust_common_gtests': {
       'base_unittests': {},
-      'gnrt_unittests': {},
       'rust_gtest_interop_unittests': {},
       'test_cpp_including_rust_unittests': {
         'swarming': {},
@@ -4652,6 +4652,12 @@
       },
     },
 
+    # Some Rust-related tooling is itself written in Rust. These tools are only
+    # run on build hosts.
+    'rust_tooling_gtests': {
+      'gnrt_unittests': {},
+    },
+
     'site_isolation_android_fyi_gtests': {
       'site_per_process_android_browsertests': {
         'args': [
@@ -6458,6 +6464,12 @@
       'telemetry_perf_unittests_isolated_scripts_android',
     ],
 
+    # Rust tests run on non-cross builds.
+    'rust_host_gtests': [
+      'rust_common_gtests',
+      'rust_tooling_gtests',
+    ],
+
     'sandboxed_chromium_memory_linux_gtests': [
       'chromium_browser_tests',
     ],
diff --git a/testing/buildbot/waterfalls.pyl b/testing/buildbot/waterfalls.pyl
index e5285332..873629a 100644
--- a/testing/buildbot/waterfalls.pyl
+++ b/testing/buildbot/waterfalls.pyl
@@ -5628,7 +5628,7 @@
           'walleye',
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_common_gtests',
           # Currently `can_build_rust_unit_tests` is false on Android (because
           # we need to construct an APK instead of compile an exe).
           # TODO(https://crbug.com/1260120): Cover `rust_native_tests` here.
@@ -5644,7 +5644,7 @@
           'walleye',
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_common_gtests',
           # Currently `can_build_rust_unit_tests` is false on Android (because
           # we need to construct an APK instead of compile an exe).
           # TODO(https://crbug.com/1260120): Cover `rust_native_tests` here.
@@ -5660,7 +5660,7 @@
           'walleye',
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_common_gtests',
           # Currently `can_build_rust_unit_tests` is false on Android (because
           # we need to construct an APK instead of compile an exe).
           # TODO(https://crbug.com/1260120): Cover `rust_native_tests` here.
@@ -5677,7 +5677,7 @@
           'linux-bionic'
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_host_gtests',
           'isolated_scripts': 'rust_native_tests',
         },
       },
@@ -5692,7 +5692,7 @@
           'linux-bionic'
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_host_gtests',
           'isolated_scripts': 'rust_native_tests',
         },
       },
@@ -5712,7 +5712,7 @@
           'xctest',
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_host_gtests',
           'isolated_scripts': 'rust_native_tests',
         },
       },
@@ -5728,7 +5728,7 @@
           'x86-64',
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_host_gtests',
           'isolated_scripts': 'rust_native_tests',
         },
       },
@@ -5744,7 +5744,7 @@
           'x86-64',
         ],
         'test_suites': {
-          'gtest_tests': 'rust_gtests',
+          'gtest_tests': 'rust_host_gtests',
           'isolated_scripts': 'rust_native_tests',
         },
       },
diff --git a/tools/crates/gnrt/config.rs b/tools/crates/gnrt/config.rs
index 39d47e7..d76c53a 100644
--- a/tools/crates/gnrt/config.rs
+++ b/tools/crates/gnrt/config.rs
@@ -34,8 +34,15 @@
     /// Extra rustc flags.
     #[serde(default)]
     pub rustflags: Vec<String>,
+    /// Sets GN output_dir variable.
     #[serde(default)]
     pub output_dir: Option<String>,
+    /// GN deps to add to the generated target.
     #[serde(default)]
-    pub extra_deps: Vec<String>,
+    pub extra_gn_deps: Vec<String>,
+    /// Deps on generated targets to exclude from this target's `deps` list.
+    /// These do not affect dependency resolution, so it will not change any
+    /// other generated targets.
+    #[serde(default)]
+    pub exclude_deps_in_gn: Vec<String>,
 }
diff --git a/tools/crates/gnrt/gn.rs b/tools/crates/gnrt/gn.rs
index d919ca6e..535ab3fd 100644
--- a/tools/crates/gnrt/gn.rs
+++ b/tools/crates/gnrt/gn.rs
@@ -194,9 +194,9 @@
         crate_config.env.iter().chain(extra_config.all_config.env.iter()).cloned().collect();
 
     let extra_deps = crate_config
-        .extra_deps
+        .extra_gn_deps
         .iter()
-        .chain(extra_config.all_config.extra_deps.iter())
+        .chain(extra_config.all_config.extra_gn_deps.iter())
         .cloned()
         .map(|dep| RuleDep { cond: Condition::Always, rule: dep })
         .collect();
@@ -234,9 +234,16 @@
         .map(|pki| pki.features.clone())
         .unwrap_or(vec![]);
 
+    let exclude_deps: Vec<String> = crate_config
+        .exclude_deps_in_gn
+        .iter()
+        .chain(extra_config.all_config.exclude_deps_in_gn.iter())
+        .cloned()
+        .collect();
+
     // Add only normal dependencies: we don't run unit tests, and we don't run
     // build scripts (instead manually configuring build flags and env vars).
-    for dep_of_dep in &dep.dependencies {
+    for dep_of_dep in dep.dependencies.iter().filter(|d| !exclude_deps.contains(&d.package_name)) {
         let cond = match &dep_of_dep.platform {
             None => Condition::Always,
             Some(p) => Condition::If(platform_to_condition(p)),