rust: Omit non-Rust link deps from rlib order-only deps Compiling an rlib does not invoke a linker, so non-Rust native dependencies (source_set object files, static libraries, shared libraries, and Rust cdylibs) are not used by rustc and are already propagated via inherited libraries to final linking targets. https://gn-review.googlesource.com/c/gn/+/27000 removed non-Rust linkable_deps (static_library, shared_library, and Rust cdylib) from an rlib's command line and implicit deps (`|`), avoiding rlib invalidation on C/C++ relinks and removing edges when no source_set or group sits in the path. However, it was not sufficient to unblock rlibs in the Ninja graph: - source_set targets are classified as non_linkable_deps, so GetOrderOnlyDepsFromNonLinkableDeps still emitted `|| phony/.../<target>.linkdeps` on the rlib. A source_set's `.linkdeps` depends on all of its `.o` files and transitively on the order-only deps of its non_linkable_deps (including upstream `.a` and `.so`/`.TOC` targets). - group targets are also classified as non_linkable_deps, and ResolvedTargetData::GetOrderOnlyDeps(group) expanded all targets in the group, still emitting `||` edges to `.linkdeps`, `.a`, and `.so`/`.TOC` outputs. Filter rlib order-only deps in NinjaRustBinaryTargetWriter to skip non-Rust source_sets, non-Rust native libraries, and Rust cdylibs (including when reached through groups), while preserving hard deps (via input deps) and data_deps (direct and through groups). Measured on Chromium `origin/main` (`e118cca65f028`) in a Linux component build (`is_component_build = true`, `is_debug = false`) comparing Chromium's GN (`ddc6c4ae`, which includes CL 27000) against `ddc6c4ae` + this CL, 469 of 523 `.rlib` targets drop native order-only deps: - `//third_party/crabbyavif:crabbyavif`: `.o`/`.a`/`.so`/`.TOC` inputs drop from 256 to 1 (1 host proc-macro `.so`; 255 -> 0 C/C++ outputs); Ninja build steps drop from 340 to 84. - `//net/base:filename_util`: inputs drop from 3857 to 445; steps drop from 4219 to 709. - `//base/test:scoped_feature_list`: inputs drop from 3429 to 445; steps drop from 3972 to 808. - //components/safe_browsing/content/renderer/phishing_classifier/rust:scorer_bridge inputs drop from 11895 to 482; steps drop from 13184 to 1580. - `//base/logging/rust_logger:rust_logger` (after https://crrev.com/c/8823780): inputs drop from 5 to 2 (removing `libcxx_cppdeps_lib.a`, `cxx.o`, and `libclang_rt.builtins.a` pulled through a group; 3 -> 0 C/C++ outputs); steps drop from 106 to 103. This was measured with: ``` $ third_party/ninja/ninja -C out/rel -t inputs \ third_party/crabbyavif:crabbyavif | \ grep -E '\.(o|a|so|TOC)$' | wc -l 1 $ third_party/ninja/ninja -C out/rel -t commands \ third_party/crabbyavif:crabbyavif | wc -l 84 ``` Bug: gn:563098656 Change-Id: I8c1a87c48e7abeaa486d5630591baf4fcb58288e Reviewed-on: https://gn-review.googlesource.com/c/gn/+/29140 Reviewed-by: David Turner <digit@google.com> Reviewed-by: Takuto Ikuta <tikuta@google.com> Commit-Queue: David Turner <digit@google.com>
diff --git a/src/gn/ninja_rust_binary_target_writer.cc b/src/gn/ninja_rust_binary_target_writer.cc index c263c75..3b1f3e4 100644 --- a/src/gn/ninja_rust_binary_target_writer.cc +++ b/src/gn/ninja_rust_binary_target_writer.cc
@@ -99,6 +99,52 @@ opts, out); } +// Returns true if |dep| is a Rust crate dependency (not a C/C++ target or a +// Rust cdylib, which is treated as a non-Rust dependency for linking). +bool IsRustCrateDep(const Target* dep) { + return dep->source_types_used().RustSourceUsed() && + dep->rust_values().crate_type() != RustValues::CRATE_CDYLIB; +} + +// Appends to |out| the order-only deps that compiling an rlib needs from |dep|. +void AppendRlibOrderOnlyDeps(const ResolvedTargetData& resolved, + const Target* dep, + const TargetSet& data_deps, + TargetSet* visited, + UniqueVector<OutputFile>* out) { + if (!visited->add(dep)) + return; + + // |data_deps| are kept as they are, so that building the rlib still builds + // its runtime dependencies. + if (data_deps.contains(dep)) { + out->Append(resolved.GetOrderOnlyDeps(dep)); + return; + } + + // rustc doesn't use non-Rust source sets (object files) or native libraries + // when building an rlib. Ignore such |dep| to avoid unnecessarily waiting + // for it before starting to build an rlib. Generated files from its hard + // deps are still covered by the rlib's input deps (WriteCompilerInputDeps). + if ((dep->output_type() == Target::SOURCE_SET || dep->IsLinkable()) && + !IsRustCrateDep(dep)) { + return; + } + + // Apply the same filtering as above to GN groups. + if (dep->output_type() == Target::GROUP) { + const ResolvedTargetDeps& group_deps = resolved.GetTargetDeps(dep); + for (const Target* group_dep : group_deps.linked_deps()) + AppendRlibOrderOnlyDeps(resolved, group_dep, data_deps, visited, out); + for (const Target* group_data_dep : group_deps.data_deps()) + out->Append(resolved.GetOrderOnlyDeps(group_data_dep)); + return; + } + + // Wait for any other kind of |dep|. + out->Append(resolved.GetOrderOnlyDeps(dep)); +} + } // namespace NinjaRustBinaryTargetWriter::NinjaRustBinaryTargetWriter( @@ -130,8 +176,9 @@ // dependencies (object files, static and shared libraries) are not used to // build it. Since RUST_LIBRARY is not final, those dependencies are instead // propagated to the final linked target as inherited libraries. Don't add - // them to the rlib's command line nor its implicit deps, so that compiling - // an rlib doesn't have to wait for C/C++ code to be compiled and linked. + // them to the rlib's command line, its implicit deps, nor its order-only + // deps, so that compiling an rlib doesn't have to wait for C/C++ code to be + // compiled and linked. const bool is_rlib = RustValues::InferredCrateType(target_) == RustValues::CRATE_RLIB; @@ -180,13 +227,22 @@ } } - std::vector<OutputFile> group_order_only_deps = - GetOrderOnlyDepsFromNonLinkableDeps(classified_deps.non_linkable_deps); - order_only_deps.Append(std::move(group_order_only_deps)); + if (is_rlib) { + // See the comment on `is_rlib` above and on AppendRlibOrderOnlyDeps(). + TargetSet data_deps; + for (const Target* data_dep : resolved().GetDataDeps(target_)) + data_deps.add(data_dep); + TargetSet visited; + for (const Target* dep : classified_deps.non_linkable_deps) { + AppendRlibOrderOnlyDeps(resolved(), dep, data_deps, &visited, + &order_only_deps); + } + } else { + order_only_deps.Append( + GetOrderOnlyDepsFromNonLinkableDeps(classified_deps.non_linkable_deps)); + } for (const auto* linkable_dep : classified_deps.linkable_deps) { - // Rust cdylibs are treated as non-Rust dependencies for linking purposes. - if (linkable_dep->source_types_used().RustSourceUsed() && - linkable_dep->rust_values().crate_type() != RustValues::CRATE_CDYLIB) { + if (IsRustCrateDep(linkable_dep)) { rustdeps.push_back(linkable_dep->link_output_file()); } else if (is_rlib) { // Not needed to build an rlib. See the comment on `is_rlib` above.
diff --git a/src/gn/ninja_rust_binary_target_writer_unittest.cc b/src/gn/ninja_rust_binary_target_writer_unittest.cc index 4f5282e..569a3cc 100644 --- a/src/gn/ninja_rust_binary_target_writer_unittest.cc +++ b/src/gn/ninja_rust_binary_target_writer_unittest.cc
@@ -711,6 +711,53 @@ EXPECT_EQ(expected, out); } + + // Rust library G (depender on group E). An rlib doesn't wait for non-Rust + // source sets, even if they are reached through a group (see also the + // NonRustDeps test). + Target target_g(setup.settings(), Label(SourceDir("//foo/"), "g")); + target_g.set_output_type(Target::RUST_LIBRARY); + SourceFile lib("//foo/lib.rs"); + target_g.sources().push_back(lib); + target_g.source_types_used().Set(SourceFile::SOURCE_RS); + target_g.rust_values().set_crate_root(lib); + target_g.rust_values().crate_name() = "g_crate"; + target_g.private_deps().push_back(LabelTargetPair(&target_e)); + target_g.SetToolchain(setup.toolchain()); + ASSERT_TRUE(target_g.OnResolved(&err)) << err.message(); + + { + StringOutputBuffer out; + NinjaRustBinaryTargetWriter writer(&target_g, out); + writer.Run(); + + // Before https://gn-review.googlesource.com/c/gn/+/29140 `out` would + // unnecessarily indicate that `libg.rlib` needs to wait for + // `phony/foo/b.linkdeps`: + // `build obj/foo/libg.rlib: ... || phony/foo/b.linkdeps` + const char expected[] = + "crate_name = g_crate\n" + "crate_type = rlib\n" + "output_extension = .rlib\n" + "output_dir = \n" + "rustflags =\n" + "rustenv =\n" + "root_out_dir = .\n" + "target_gen_dir = gen/foo\n" + "target_out_dir = obj/foo\n" + "target_output_name = libg\n" + "\n" + "build obj/foo/libg.rlib: rust_rlib ../../foo/lib.rs | " + "../../foo/lib.rs\n" + " source_file_part = lib.rs\n" + " source_name_part = lib\n" + " externs =\n" + " rustdeps =\n" + " ldflags =\n" + " sources = ../../foo/lib.rs\n"; + + EXPECT_EQ(expected, out); + } } TEST_F(NinjaRustBinaryTargetWriterTest, RenamedDeps) { @@ -992,8 +1039,9 @@ } // An rlib is not linked by rustc, so its non-Rust dependencies are neither - // passed to rustc nor implicit dependencies of the rlib. This avoids - // compiling the rlib having to wait for C/C++ code to be compiled/linked. + // passed to rustc nor dependencies of the rlib (not even order-only + // dependencies on the `.linkdeps` of source sets). This avoids compiling the + // rlib having to wait for C/C++ code to be compiled/linked. Target rlib_with_nonrust(setup.settings(), Label(SourceDir("//qux/"), "qux")); rlib_with_nonrust.set_output_type(Target::RUST_LIBRARY); rlib_with_nonrust.visibility().SetPublic(); @@ -1016,6 +1064,10 @@ NinjaRustBinaryTargetWriter writer(&rlib_with_nonrust, out); writer.Run(); + // Before https://gn-review.googlesource.com/c/gn/+/29140 `out` would + // unnecessarily indicate that `libqux.rlib` needs to wait for + // `phony/baz/sourceset.linkdeps`: + // `build obj/qux/libqux.rlib: ... || phony/baz/sourceset.linkdeps` const char expected[] = "crate_name = qux\n" "crate_type = rlib\n" @@ -1029,8 +1081,7 @@ "target_output_name = libqux\n" "\n" "build obj/qux/libqux.rlib: rust_rlib ../../qux/lib.rs | " - "../../qux/lib.rs obj/bar/libmylib.rlib " - "|| phony/baz/sourceset.linkdeps\n" + "../../qux/lib.rs obj/bar/libmylib.rlib\n" " source_file_part = lib.rs\n" " source_name_part = lib\n" " externs = --extern mylib=obj/bar/libmylib.rlib\n" @@ -1086,6 +1137,235 @@ " sources = ../../qux/main.rs\n"; EXPECT_EQ(expected, out); } + + // Non-Rust native dependencies (and Rust cdylibs) reached through a group + // are also omitted from an rlib's dependencies. + Target cdylib(setup.settings(), Label(SourceDir("//foo/"), "cdylib")); + cdylib.set_output_type(Target::SHARED_LIBRARY); + cdylib.visibility().SetPublic(); + SourceFile cdylib_root("//foo/cdylib.rs"); + cdylib.sources().push_back(cdylib_root); + cdylib.source_types_used().Set(SourceFile::SOURCE_RS); + cdylib.rust_values().set_crate_root(cdylib_root); + cdylib.rust_values().crate_name() = "cdylib"; + cdylib.rust_values().set_crate_type(RustValues::CRATE_CDYLIB); + cdylib.SetToolchain(setup.toolchain()); + ASSERT_TRUE(cdylib.OnResolved(&err)); + + Target nonrust_group(setup.settings(), + Label(SourceDir("//qux/"), "nonrust_group")); + nonrust_group.set_output_type(Target::GROUP); + nonrust_group.visibility().SetPublic(); + nonrust_group.private_deps().push_back(LabelTargetPair(&staticlib)); + nonrust_group.private_deps().push_back(LabelTargetPair(&sharedlib)); + nonrust_group.private_deps().push_back(LabelTargetPair(&csourceset)); + nonrust_group.private_deps().push_back(LabelTargetPair(&sharedlib_with_toc)); + nonrust_group.private_deps().push_back(LabelTargetPair(&cdylib)); + nonrust_group.SetToolchain(setup.toolchain()); + ASSERT_TRUE(nonrust_group.OnResolved(&err)); + + Target rlib_with_nonrust_group(setup.settings(), + Label(SourceDir("//qux/"), "qux_group")); + rlib_with_nonrust_group.set_output_type(Target::RUST_LIBRARY); + rlib_with_nonrust_group.visibility().SetPublic(); + rlib_with_nonrust_group.sources().push_back(quxlib); + rlib_with_nonrust_group.source_types_used().Set(SourceFile::SOURCE_RS); + rlib_with_nonrust_group.rust_values().set_crate_root(quxlib); + rlib_with_nonrust_group.rust_values().crate_name() = "qux_group"; + rlib_with_nonrust_group.private_deps().push_back(LabelTargetPair(&rlib)); + rlib_with_nonrust_group.private_deps().push_back( + LabelTargetPair(&nonrust_group)); + rlib_with_nonrust_group.SetToolchain(setup.toolchain()); + ASSERT_TRUE(rlib_with_nonrust_group.OnResolved(&err)); + + { + StringOutputBuffer out; + NinjaRustBinaryTargetWriter writer(&rlib_with_nonrust_group, out); + writer.Run(); + + // Before https://gn-review.googlesource.com/c/gn/+/29140 `out` would + // unnecessarily indicate that `libqux_group.rlib` needs to wait for the + // non-Rust targets in `nonrust_group`: + // `build obj/qux/libqux_group.rlib: ... || obj/foo/libstatic.a + // ./libshared.so phony/baz/sourceset.linkdeps ./libshared_with_toc.so.TOC + // obj/foo/libcdylib.so` + const char expected[] = + "crate_name = qux_group\n" + "crate_type = rlib\n" + "output_extension = .rlib\n" + "output_dir = \n" + "rustflags =\n" + "rustenv =\n" + "root_out_dir = .\n" + "target_gen_dir = gen/qux\n" + "target_out_dir = obj/qux\n" + "target_output_name = libqux_group\n" + "\n" + "build obj/qux/libqux_group.rlib: rust_rlib ../../qux/lib.rs | " + "../../qux/lib.rs obj/bar/libmylib.rlib\n" + " source_file_part = lib.rs\n" + " source_name_part = lib\n" + " externs = --extern mylib=obj/bar/libmylib.rlib\n" + " rustdeps = -Ldependency=obj/bar\n" + " ldflags =\n" + " sources = ../../qux/lib.rs\n"; + EXPECT_EQ(expected, out); + } +} + +// An rlib doesn't wait for the object files of non-Rust source sets, but it +// still waits for the actions that they depend on (e.g. an action that +// generates a `.rs` file which the rlib `include!`s). +TEST_F(NinjaRustBinaryTargetWriterTest, RlibHardDepsThroughNonRustSourceSet) { + Err err; + TestWithScope setup; + + Target action(setup.settings(), Label(SourceDir("//foo/"), "gen")); + action.set_output_type(Target::ACTION); + action.visibility().SetPublic(); + action.action_values().set_script(SourceFile("//foo/gen.py")); + action.action_values().outputs() = + SubstitutionList::MakeForTest("//out/Debug/gen/foo/generated.rs"); + action.SetToolchain(setup.toolchain()); + ASSERT_TRUE(action.OnResolved(&err)); + + Target csourceset(setup.settings(), Label(SourceDir("//foo/"), "cpp")); + csourceset.set_output_type(Target::SOURCE_SET); + csourceset.visibility().SetPublic(); + csourceset.sources().push_back(SourceFile("//foo/cpp.cc")); + csourceset.source_types_used().Set(SourceFile::SOURCE_CPP); + csourceset.public_deps().push_back(LabelTargetPair(&action)); + csourceset.SetToolchain(setup.toolchain()); + ASSERT_TRUE(csourceset.OnResolved(&err)); + + Target rlib(setup.settings(), Label(SourceDir("//foo/"), "mylib")); + rlib.set_output_type(Target::RUST_LIBRARY); + rlib.visibility().SetPublic(); + SourceFile lib("//foo/lib.rs"); + rlib.sources().push_back(lib); + rlib.source_types_used().Set(SourceFile::SOURCE_RS); + rlib.rust_values().set_crate_root(lib); + rlib.rust_values().crate_name() = "mylib"; + rlib.private_deps().push_back(LabelTargetPair(&csourceset)); + rlib.SetToolchain(setup.toolchain()); + ASSERT_TRUE(rlib.OnResolved(&err)); + + StringOutputBuffer out; + NinjaRustBinaryTargetWriter writer(&rlib, out); + writer.Run(); + + const char expected[] = + "crate_name = mylib\n" + "crate_type = rlib\n" + "output_extension = .rlib\n" + "output_dir = \n" + "rustflags =\n" + "rustenv =\n" + "root_out_dir = .\n" + "target_gen_dir = gen/foo\n" + "target_out_dir = obj/foo\n" + "target_output_name = libmylib\n" + "\n" + "build obj/foo/libmylib.rlib: rust_rlib ../../foo/lib.rs | " + "../../foo/lib.rs || phony/foo/gen\n" + " source_file_part = lib.rs\n" + " source_name_part = lib\n" + " externs =\n" + " rustdeps =\n" + " ldflags =\n" + " sources = ../../foo/lib.rs\n"; + EXPECT_EQ(expected, out); +} + +// Building an rlib still builds its `data_deps` (both direct and reached +// through a group), even if they are non-Rust source sets (unlike the non-Rust +// source sets in `deps`). +TEST_F(NinjaRustBinaryTargetWriterTest, RlibDataDepsOnNonRustSourceSet) { + Err err; + TestWithScope setup; + + Target csourceset(setup.settings(), Label(SourceDir("//foo/"), "cpp")); + csourceset.set_output_type(Target::SOURCE_SET); + csourceset.visibility().SetPublic(); + csourceset.sources().push_back(SourceFile("//foo/cpp.cc")); + csourceset.source_types_used().Set(SourceFile::SOURCE_CPP); + csourceset.SetToolchain(setup.toolchain()); + ASSERT_TRUE(csourceset.OnResolved(&err)); + + Target csourceset_in_group(setup.settings(), + Label(SourceDir("//foo/"), "cpp_in_group")); + csourceset_in_group.set_output_type(Target::SOURCE_SET); + csourceset_in_group.visibility().SetPublic(); + csourceset_in_group.sources().push_back(SourceFile("//foo/cpp2.cc")); + csourceset_in_group.source_types_used().Set(SourceFile::SOURCE_CPP); + csourceset_in_group.SetToolchain(setup.toolchain()); + ASSERT_TRUE(csourceset_in_group.OnResolved(&err)); + + Target data_group(setup.settings(), Label(SourceDir("//foo/"), "data_group")); + data_group.set_output_type(Target::GROUP); + data_group.visibility().SetPublic(); + data_group.data_deps().push_back(LabelTargetPair(&csourceset_in_group)); + data_group.SetToolchain(setup.toolchain()); + ASSERT_TRUE(data_group.OnResolved(&err)); + + Target csourceset_in_data_dep_group( + setup.settings(), Label(SourceDir("//foo/"), "cpp_in_data_dep_group")); + csourceset_in_data_dep_group.set_output_type(Target::SOURCE_SET); + csourceset_in_data_dep_group.visibility().SetPublic(); + csourceset_in_data_dep_group.sources().push_back(SourceFile("//foo/cpp3.cc")); + csourceset_in_data_dep_group.source_types_used().Set(SourceFile::SOURCE_CPP); + csourceset_in_data_dep_group.SetToolchain(setup.toolchain()); + ASSERT_TRUE(csourceset_in_data_dep_group.OnResolved(&err)); + + Target data_dep_group(setup.settings(), + Label(SourceDir("//foo/"), "data_dep_group")); + data_dep_group.set_output_type(Target::GROUP); + data_dep_group.visibility().SetPublic(); + data_dep_group.private_deps().push_back( + LabelTargetPair(&csourceset_in_data_dep_group)); + data_dep_group.SetToolchain(setup.toolchain()); + ASSERT_TRUE(data_dep_group.OnResolved(&err)); + + Target rlib(setup.settings(), Label(SourceDir("//foo/"), "mylib")); + rlib.set_output_type(Target::RUST_LIBRARY); + rlib.visibility().SetPublic(); + SourceFile lib("//foo/lib.rs"); + rlib.sources().push_back(lib); + rlib.source_types_used().Set(SourceFile::SOURCE_RS); + rlib.rust_values().set_crate_root(lib); + rlib.rust_values().crate_name() = "mylib"; + rlib.private_deps().push_back(LabelTargetPair(&data_group)); + rlib.data_deps().push_back(LabelTargetPair(&csourceset)); + rlib.data_deps().push_back(LabelTargetPair(&data_dep_group)); + rlib.SetToolchain(setup.toolchain()); + ASSERT_TRUE(rlib.OnResolved(&err)); + + StringOutputBuffer out; + NinjaRustBinaryTargetWriter writer(&rlib, out); + writer.Run(); + + const char expected[] = + "crate_name = mylib\n" + "crate_type = rlib\n" + "output_extension = .rlib\n" + "output_dir = \n" + "rustflags =\n" + "rustenv =\n" + "root_out_dir = .\n" + "target_gen_dir = gen/foo\n" + "target_out_dir = obj/foo\n" + "target_output_name = libmylib\n" + "\n" + "build obj/foo/libmylib.rlib: rust_rlib ../../foo/lib.rs | " + "../../foo/lib.rs || phony/foo/cpp_in_group.linkdeps " + "phony/foo/cpp.linkdeps phony/foo/cpp_in_data_dep_group.linkdeps\n" + " source_file_part = lib.rs\n" + " source_name_part = lib\n" + " externs =\n" + " rustdeps =\n" + " ldflags =\n" + " sources = ../../foo/lib.rs\n"; + EXPECT_EQ(expected, out); } TEST_F(NinjaRustBinaryTargetWriterTest, RlibInLibrary) {