Support `--target=<value>` in addition to `--target <value>`

This fixes a downstream problem for crates where rustflags included
`--target=<value>` but the rust-project.json generated by GN excluded
a target field for the crate.

Fixed: 522275056
Change-Id: Iaa762e2c73bce54b4679352bb78714ed2008db6f
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/23100
Reviewed-by: David Turner <digit@google.com>
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: Charles Celerier <chcl@google.com>
diff --git a/src/gn/rust_project_writer.cc b/src/gn/rust_project_writer.cc
index c5b4590..3324e3a 100644
--- a/src/gn/rust_project_writer.cc
+++ b/src/gn/rust_project_writer.cc
@@ -172,7 +172,11 @@
   }
 
   auto compiler_args = ExtractCompilerArgs(target);
-  auto compiler_target = FindArgValue("--target", compiler_args);
+  auto compiler_target =
+      FindArgValueAfterPrefix(std::string("--target="), compiler_args);
+  if (!compiler_target.has_value()) {
+    compiler_target = FindArgValue("--target", compiler_args);
+  }
   auto crate_deps = GetRustDeps(target);
 
   // Add all dependencies of this crate, before this crate.
diff --git a/src/gn/rust_project_writer_unittest.cc b/src/gn/rust_project_writer_unittest.cc
index 8f80824..d48a879 100644
--- a/src/gn/rust_project_writer_unittest.cc
+++ b/src/gn/rust_project_writer_unittest.cc
@@ -435,6 +435,60 @@
   target.source_types_used().Set(SourceFile::SOURCE_RS);
   target.rust_values().set_crate_root(lib);
   target.rust_values().crate_name() = "foo";
+  target.config_values().rustflags().push_back("--target=x86-64_unknown");
+  target.SetToolchain(setup.toolchain());
+  ASSERT_TRUE(target.OnResolved(&err));
+
+  std::ostringstream stream;
+  std::vector<const Target*> targets;
+  targets.push_back(&target);
+  RustProjectWriter::RenderJSON(setup.build_settings(), targets, stream);
+  std::string out = stream.str();
+#if defined(OS_WIN)
+  base::ReplaceSubstringsAfterOffset(&out, 0, "\r\n", "\n");
+#endif
+  const char expected_json[] =
+      "{\n"
+      "  \"crates\": [\n"
+      "    {\n"
+      "      \"crate_id\": 0,\n"
+      "      \"root_module\": \"path/foo/lib.rs\",\n"
+      "      \"label\": \"//foo:bar\",\n"
+      "      \"source\": {\n"
+      "          \"include_dirs\": [\n"
+      "               \"path/foo/\"\n"
+      "          ],\n"
+      "          \"exclude_dirs\": []\n"
+      "      },\n"
+      "      \"target\": \"x86-64_unknown\",\n"
+      "      \"compiler_args\": [\"--target=x86-64_unknown\"],\n"
+      "      \"deps\": [\n"
+      "      ],\n"
+      "      \"edition\": \"2015\",\n"
+      "      \"cfg\": [\n"
+      "        \"test\",\n"
+      "        \"debug_assertions\"\n"
+      "      ]\n"
+      "    }\n"
+      "  ]\n"
+      "}\n";
+
+  ExpectEqOrShowDiff(expected_json, out);
+}
+
+TEST_F(RustProjectJSONWriter, OneRustTargetWithRustcTargetSetAlternate) {
+  Err err;
+  TestWithScope setup;
+  setup.build_settings()->SetRootPath(UTF8ToFilePath("path"));
+
+  Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
+  target.set_output_type(Target::RUST_LIBRARY);
+  target.visibility().SetPublic();
+  SourceFile lib("//foo/lib.rs");
+  target.sources().push_back(lib);
+  target.source_types_used().Set(SourceFile::SOURCE_RS);
+  target.rust_values().set_crate_root(lib);
+  target.rust_values().crate_name() = "foo";
   target.config_values().rustflags().push_back("--target");
   target.config_values().rustflags().push_back("x86-64_unknown");
   target.SetToolchain(setup.toolchain());