From: Lucas C.S. <lucascs@protonmail.com>
Subject: [PATCH] components/cbor: gate the Crubit CBOR reader on enable_cpp_api_from_rust

M152 moved the CBOR reader to a Rust implementation exposed to C++ through
Crubit, but gated it on `!is_cronet_build` alone.  Crubit's C++ support library
is not in the Chromium tree: `//build/rust/crubit` reaches it through
`crubit_src_dir`, which `//build_overrides/crubit.gni` hardcodes to
`//third_party/rust-toolchain/lib/third_party/crubit` -- a path that exists only
inside Google's bundled Rust toolchain and that the release tarball does not
ship at all (`third_party/crubit/` holds an OWNERS file and a README saying so).
A build against a system Rust sysroot therefore dies in `gn gen`:

  ERROR at //build/rust/crubit/BUILD.gn:11:5: Unable to load
  ".../third_party/rust-toolchain/lib/third_party/crubit/support/BUILD.gn".
      "$crubit_src_dir/support:support_cpp",

`enable_cpp_api_from_rust`, in //build/config/rust.gni, already states exactly
this condition -- it is false whenever `rust_sysroot_absolute` is set -- and
every other Crubit consumer honours it: `components/qr_code_generator`,
`build/rust/std`, and `build/rust/tests/test_cpp_including_rust` all guard their
`//build/rust/crubit` dependency with it.  `components/cbor` is the one that
does not.

Honouring it here selects the C++ reader instead, which is not a degraded path:
it is the same fallback Cronet builds use, kept alive behind
`BUILDFLAG(USE_CBOR_RUST)` in reader.{h,cc} and exercised by reader_unittest.cc.

Upstream keeps the buildflag until Cronet gains Crubit support
(crbug.com/535682335) and has not fixed the gate on main as of 2026-08-28.
Drop this patch once the upstream condition accounts for a non-bundled Rust
toolchain.

M155 added the same unconditional `!is_cronet_build` dependency on
`cbor_rust_bindings` to `:unit_tests`; it gets the same gate.  The tests
themselves already sit behind `BUILDFLAG(USE_CBOR_RUST)`.

M156 split the reader and writer out of `component("cbor")` into a private
`source_set("cbor_rust_impl")`, and the Crubit dependency moved with them;
the gate follows it there.  writer.cc gained a Rust path in the same series and
keeps its C++ fallback behind the same buildflag, so the C++ writer is selected
alongside the C++ reader.
--- a/components/cbor/BUILD.gn
+++ b/components/cbor/BUILD.gn
@@ -4,15 +4,21 @@
 
 import("//build/buildflag_header.gni")
 import("//build/config/cronet/config.gni")
+import("//build/config/rust.gni")
 import("//testing/libfuzzer/fuzzer_test.gni")
 
+# Crubit's C++ support library ships only inside the bundled Rust toolchain, so
+# a build using a system Rust sysroot cannot reach //build/rust/crubit at all.
+# `enable_cpp_api_from_rust` is already false in exactly that configuration.
+_use_cbor_rust = !is_cronet_build && enable_cpp_api_from_rust
+
 buildflag_header("buildflags") {
   header = "cbor_buildflags.h"
 
   # TODO(crbug.com/535682335): Remove `USE_CBOR_RUST` buildflag entirely,
   # unconditionally enable Rust CBOR parser, and drop `is_cronet_build` check
   # once Cronet supports Crubit dependencies.
-  flags = [ "USE_CBOR_RUST=!$is_cronet_build" ]
+  flags = [ "USE_CBOR_RUST=$_use_cbor_rust" ]
 }
 
 source_set("cbor_headers") {
@@ -48,7 +54,7 @@
     "//base",
   ]
 
-  if (!is_cronet_build) {
+  if (_use_cbor_rust) {
     deps += [
       "//build/rust/crubit",
       "//components/cbor/rust:cbor_rust_bindings",
@@ -91,7 +97,7 @@
     "//testing/gtest",
     "//third_party/fuzztest",
   ]
-  if (!is_cronet_build) {
+  if (_use_cbor_rust) {
     deps += [
       "//components/cbor/rust:cbor_rust_bindings",
       "//components/cbor/rust:cbor_rust_unittests",
