Skip to content

Commit 1fc0630

Browse files
committed
Auto merge of #7977 - ehuss:unit-graph, r=alexcrichton
Add unit-graph JSON output. This adds a `--unit-graph` flag that will emit a JSON object of Cargo's internal build graph. See unstable.md for more details. The primary motivator is to provide an accurate picture of which features are set. With the new feature resolver it is not possible to properly represent the features in the `cargo metadata` structure, because features are no longer unified. Also, features selected depend on the command, and exactly which packages are being built. To handle that in `cargo metadata`, it would need to add a "mode" flag, and a superset of flags for all build commands (test, check, build, etc.). To me that seemed like a difficult path to take. This may also be helpful for making visualizations of the true dependencies. `cargo metadata` doesn't show the intra-package dependencies like build scripts or test units, and walking the `cargo metadata` graph correctly isn't always obvious. This initial concept exposes almost all of the fields. That may be a little too much, but I imagine we could always trim it before stabilizing. This structure also has a high risk of being unstable, since it has a good chance of changing form in the future. I figure that can be addressed with documentation emphasizing that it may change and we may not always provide backwards-compatibility (though we will try if it is not too much burden). This could also potentially be extended in the future to include things like artifact paths, or "freshness", if we'd like to.
2 parents 3d39211 + 4107872 commit 1fc0630

File tree

24 files changed

+540
-37
lines changed

24 files changed

+540
-37
lines changed

src/bin/cargo/commands/bench.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub fn cli() -> App {
4444
"no-fail-fast",
4545
"Run all benchmarks regardless of failure",
4646
))
47+
.arg_unit_graph()
4748
.after_help(
4849
"\
4950
The benchmark filtering argument BENCHNAME and all the arguments following the

src/bin/cargo/commands/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub fn cli() -> App {
4141
.arg_manifest_path()
4242
.arg_message_format()
4343
.arg_build_plan()
44+
.arg_unit_graph()
4445
.after_help(
4546
"\
4647
All packages in the workspace are built if the `--workspace` flag is supplied. The

src/bin/cargo/commands/check.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub fn cli() -> App {
3333
.arg_target_dir()
3434
.arg_manifest_path()
3535
.arg_message_format()
36+
.arg_unit_graph()
3637
.after_help(
3738
"\
3839
If the `--package` argument is given, then SPEC is a package ID specification

src/bin/cargo/commands/doc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn cli() -> App {
3030
.arg_target_dir()
3131
.arg_manifest_path()
3232
.arg_message_format()
33+
.arg_unit_graph()
3334
.after_help(
3435
"\
3536
By default the documentation for the local package and all dependencies is

src/bin/cargo/commands/run.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn cli() -> App {
2424
.arg_target_dir()
2525
.arg_manifest_path()
2626
.arg_message_format()
27+
.arg_unit_graph()
2728
.after_help(
2829
"\
2930
If neither `--bin` nor `--example` are given, then if the package only has one

src/bin/cargo/commands/rustc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub fn cli() -> App {
2929
.arg_target_dir()
3030
.arg_manifest_path()
3131
.arg_message_format()
32+
.arg_unit_graph()
3233
.after_help(
3334
"\
3435
The specified target for the current package (or package specified by SPEC if

src/bin/cargo/commands/rustdoc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub fn cli() -> App {
3333
.arg_target_dir()
3434
.arg_manifest_path()
3535
.arg_message_format()
36+
.arg_unit_graph()
3637
.after_help(
3738
"\
3839
The specified target for the current package (or package specified by SPEC if

src/bin/cargo/commands/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub fn cli() -> App {
5454
.arg_target_dir()
5555
.arg_manifest_path()
5656
.arg_message_format()
57+
.arg_unit_graph()
5758
.after_help(
5859
"\
5960
The test filtering argument TESTNAME and all the arguments following the

src/cargo/core/compiler/build_config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct BuildConfig {
2222
pub force_rebuild: bool,
2323
/// Output a build plan to stdout instead of actually compiling.
2424
pub build_plan: bool,
25+
/// Output the unit graph to stdout instead of actually compiling.
26+
pub unit_graph: bool,
2527
/// An optional override of the rustc process for primary units
2628
pub primary_unit_rustc: Option<ProcessBuilder>,
2729
pub rustfix_diagnostic_server: RefCell<Option<RustfixDiagnosticServer>>,
@@ -79,6 +81,7 @@ impl BuildConfig {
7981
message_format: MessageFormat::Human,
8082
force_rebuild: false,
8183
build_plan: false,
84+
unit_graph: false,
8285
primary_unit_rustc: None,
8386
rustfix_diagnostic_server: RefCell::new(None),
8487
})

src/cargo/core/compiler/compile_kind.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::path::Path;
99
/// compilations, where cross compilations happen at the request of `--target`
1010
/// and host compilations happen for things like build scripts and procedural
1111
/// macros.
12-
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord, Serialize)]
12+
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy, PartialOrd, Ord)]
1313
pub enum CompileKind {
1414
/// Attached to a unit that is compiled for the "host" system or otherwise
1515
/// is compiled without a `--target` flag. This is used for procedural
@@ -41,6 +41,18 @@ impl CompileKind {
4141
}
4242
}
4343

44+
impl serde::ser::Serialize for CompileKind {
45+
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
46+
where
47+
S: serde::ser::Serializer,
48+
{
49+
match self {
50+
CompileKind::Host => None::<&str>.serialize(s),
51+
CompileKind::Target(t) => Some(t.name).serialize(s),
52+
}
53+
}
54+
}
55+
4456
/// Abstraction for the representation of a compilation target that Cargo has.
4557
///
4658
/// Compilation targets are one of two things right now:

0 commit comments

Comments
 (0)