Skip to content

Commit

Permalink
Auto merge of #7132 - ehuss:metadata-kind, r=alexcrichton
Browse files Browse the repository at this point in the history
Add kind/platform info to `cargo metadata`

This adds an array `"dep_kinds"` to the resolve nodes of the `cargo metadata` output. It looks something like this:

```javascript
"resolve": {
  "nodes": [
    "id": "cargo 0.39.0 (path+file:///Users/eric/Proj/rust/cargo2)",
    "deps": [
      {
        "name": "bufstream",
        "pkg": "bufstream 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
        "dep_kinds": [
          {
            "kind": "dev",
            "target": null
          }
        ]
      },
      {
        "name": "winapi",
        "pkg": "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
        "dep_kinds": [
          {
            "kind": null,
            "target": "cfg(windows)"
          }
        ]
      }
    ]
  ]
}
```

This allows one to filter the graph based on the dependency kind and platform.

I'm not completely confident that this is the right course, but I can't think of a better design. In particular, it seems a little strange to include all platforms, but features get filtered. This is probably not a problem in practice (one can use `--all-features` to ensure all features are shown for the top-level packages). Filtering out based on platform is very difficult, because you cannot determine from the resolve alone which nodes will be host vs target. That requires the entire Unit graph. We may expose the Unit graph in the future, but this seems like a useful and simple step.

This is a draft because I wanted to discuss this before moving forward. I'd like to add some more tests.

cc #4632. This doesn't filter based on target, but does expose the target names. As mentioned above, I don't think filtering is possible.
cc #5583. This adds more information.
Closes #3984.
Closes #4631 (I think).

cc @sfackler who filed some of these issues.
  • Loading branch information
bors committed Nov 14, 2019
2 parents 962b8de + b65ebc6 commit d0a41be
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 43 deletions.
28 changes: 24 additions & 4 deletions src/cargo/ops/cargo_output_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::core::compiler::{CompileKind, CompileTarget, TargetInfo};
use crate::core::resolver::{Resolve, ResolveOpts};
use crate::core::{Package, PackageId, Workspace};
use crate::core::{dependency, Dependency, Package, PackageId, Workspace};
use crate::ops::{self, Packages};
use crate::util::CargoResult;

use cargo_platform::Platform;
use serde::Serialize;
use std::collections::HashMap;
use std::path::PathBuf;
Expand Down Expand Up @@ -85,6 +85,22 @@ struct MetadataResolveNode {
struct Dep {
name: String,
pkg: PackageId,
dep_kinds: Vec<DepKindInfo>,
}

#[derive(Serialize)]
struct DepKindInfo {
kind: dependency::Kind,
target: Option<Platform>,
}

impl From<&Dependency> for DepKindInfo {
fn from(dep: &Dependency) -> DepKindInfo {
DepKindInfo {
kind: dep.kind(),
target: dep.platform().cloned(),
}
}
}

/// Builds the resolve graph as it will be displayed to the user.
Expand Down Expand Up @@ -167,12 +183,16 @@ fn build_resolve_graph_r(
}),
None => true,
})
.filter_map(|(dep_id, _deps)| {
.filter_map(|(dep_id, deps)| {
package_map
.get(&dep_id)
.and_then(|pkg| pkg.targets().iter().find(|t| t.is_lib()))
.and_then(|lib_target| resolve.extern_crate_name(pkg_id, dep_id, lib_target).ok())
.map(|name| Dep { name, pkg: dep_id })
.map(|name| Dep {
name,
pkg: dep_id,
dep_kinds: deps.iter().map(DepKindInfo::from).collect(),
})
})
.collect();
let dumb_deps: Vec<PackageId> = deps.iter().map(|dep| dep.pkg).collect();
Expand Down
30 changes: 22 additions & 8 deletions src/doc/man/cargo-metadata.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,14 @@ The output has the following format:
"workspace_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
],
/* The resolved dependency graph, with the concrete versions and features
selected. The set depends on the enabled features.
This is null if --no-deps is specified.
By default, this includes all dependencies for all target platforms.
The `--filter-platform` flag may be used to narrow to a specific
target triple.
*/
// The resolved dependency graph, with the concrete versions and features
// selected. The set depends on the enabled features.
//
// This is null if --no-deps is specified.
//
// By default, this includes all dependencies for all target platforms.
// The `--filter-platform` flag may be used to narrow to a specific
// target triple.
"resolve": {
/* Array of nodes within the dependency graph.
Each node is a package.
Expand All @@ -230,7 +231,20 @@ The output has the following format:
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
/* The dependency kind.
"dev", "build", or null for a normal dependency.
*/
"kind": null,
/* The target platform for the dependency.
null if not a target dependency.
*/
"target": "cfg(windows)"
}
]
}
],
/* Array of features enabled on this package. */
Expand Down
30 changes: 22 additions & 8 deletions src/doc/man/generated/cargo-metadata.html
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,14 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
"workspace_members": [
"my-package 0.1.0 (path+file:///path/to/my-package)",
],
/* The resolved dependency graph, with the concrete versions and features
selected. The set depends on the enabled features.
This is null if --no-deps is specified.
By default, this includes all dependencies for all target platforms.
The `--filter-platform` flag may be used to narrow to a specific
target triple.
*/
// The resolved dependency graph, with the concrete versions and features
// selected. The set depends on the enabled features.
//
// This is null if --no-deps is specified.
//
// By default, this includes all dependencies for all target platforms.
// The `--filter-platform` flag may be used to narrow to a specific
// target triple.
"resolve": {
/* Array of nodes within the dependency graph.
Each node is a package.
Expand All @@ -237,7 +238,20 @@ <h2 id="cargo_metadata_output_format">OUTPUT FORMAT</h2>
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
/* The dependency kind.
"dev", "build", or null for a normal dependency.
*/
"kind": null,
/* The target platform for the dependency.
null if not a target dependency.
*/
"target": "cfg(windows)"
}
]
}
],
/* Array of features enabled on this package. */
Expand Down
30 changes: 22 additions & 8 deletions src/etc/man/cargo-metadata.1
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,14 @@ The output has the following format:
"workspace_members": [
"my\-package 0.1.0 (path+file:///path/to/my\-package)",
],
/* The resolved dependency graph, with the concrete versions and features
selected. The set depends on the enabled features.
This is null if \-\-no\-deps is specified.
By default, this includes all dependencies for all target platforms.
The `\-\-filter\-platform` flag may be used to narrow to a specific
target triple.
*/
// The resolved dependency graph, with the concrete versions and features
// selected. The set depends on the enabled features.
//
// This is null if \-\-no\-deps is specified.
//
// By default, this includes all dependencies for all target platforms.
// The `\-\-filter\-platform` flag may be used to narrow to a specific
// target triple.
"resolve": {
/* Array of nodes within the dependency graph.
Each node is a package.
Expand All @@ -251,7 +252,20 @@ The output has the following format:
*/
"name": "bitflags",
/* The Package ID of the dependency. */
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)"
"pkg": "bitflags 1.0.4 (registry+https://github.com/rust\-lang/crates.io\-index)",
/* Array of dependency kinds. Added in Cargo 1.40. */
"dep_kinds": [
{
/* The dependency kind.
"dev", "build", or null for a normal dependency.
*/
"kind": null,
/* The target platform for the dependency.
null if not a target dependency.
*/
"target": "cfg(windows)"
}
]
}
],
/* Array of features enabled on this package. */
Expand Down
Loading

0 comments on commit d0a41be

Please sign in to comment.