Add kind/platform info to cargo metadata

This commit is contained in:
Eric Huss 2019-07-14 14:02:55 -07:00
parent 5da4b4d479
commit a7faecc34f
6 changed files with 299 additions and 22 deletions

View File

@ -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;
@ -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.
@ -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();

View File

@ -201,6 +201,10 @@ The output has the following format:
],
/* The resolved dependency graph, with the concrete versions and features
selected. The set depends on the enabled features.
All platform-specific dependencies are listed regardless of the current
target.
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
@ -230,7 +234,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.38. */
"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. */

View File

@ -208,6 +208,10 @@ for a Rust API for reading the metadata.</p>
],
/* The resolved dependency graph, with the concrete versions and features
selected. The set depends on the enabled features.
All platform-specific dependencies are listed regardless of the current
target.
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
@ -237,7 +241,20 @@ for a Rust API for reading the metadata.</p>
*/
"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.38. */
"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. */

View File

@ -222,6 +222,10 @@ The output has the following format:
],
/* The resolved dependency graph, with the concrete versions and features
selected. The set depends on the enabled features.
All platform\-specific dependencies are listed regardless of the current
target.
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
@ -251,7 +255,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.38. */
"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. */

View File

@ -486,10 +486,22 @@ fn cargo_metadata_with_deps_and_version() {
],
"deps": [
{
"dep_kinds": [
{
"kind": null,
"target": null
}
],
"name": "bar",
"pkg": "bar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
},
{
"dep_kinds": [
{
"kind": "dev",
"target": null
}
],
"name": "foobar",
"pkg": "foobar 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
}
@ -503,6 +515,12 @@ fn cargo_metadata_with_deps_and_version() {
],
"deps": [
{
"dep_kinds": [
{
"kind": null,
"target": null
}
],
"name": "baz",
"pkg": "baz 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
}
@ -1660,10 +1678,22 @@ fn rename_dependency() {
],
"deps": [
{
"dep_kinds": [
{
"kind": null,
"target": null
}
],
"name": "bar",
"pkg": "bar 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)"
},
{
"dep_kinds": [
{
"kind": null,
"target": null
}
],
"name": "baz",
"pkg": "bar 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
}
@ -2102,7 +2132,7 @@ fn filter_platform() {
"optional": false,
"uses_default_features": true,
"features": [],
"target": "$ALT",
"target": "$ALT_TRIPLE",
"registry": null
},
{
@ -2114,7 +2144,7 @@ fn filter_platform() {
"optional": false,
"uses_default_features": true,
"features": [],
"target": "$HOST",
"target": "$HOST_TRIPLE",
"registry": null
}
],
@ -2145,8 +2175,8 @@ fn filter_platform() {
"links": null
}
"#
.replace("$ALT", &alternate())
.replace("$HOST", &rustc_host());
.replace("$ALT_TRIPLE", &alternate())
.replace("$HOST_TRIPLE", &rustc_host());
// Normal metadata, no filtering, returns *everything*.
p.cargo("metadata")
@ -2188,19 +2218,43 @@ fn filter_platform() {
"deps": [
{
"name": "alt_dep",
"pkg": "alt-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "alt-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": "$ALT_TRIPLE"
}
]
},
{
"name": "cfg_dep",
"pkg": "cfg-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "cfg-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": "cfg(foobar)"
}
]
},
{
"name": "host_dep",
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": "$HOST_TRIPLE"
}
]
},
{
"name": "normal_dep",
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": null
}
]
}
],
"features": []
@ -2225,6 +2279,8 @@ fn filter_platform() {
"workspace_root": "[..]/foo"
}
"#
.replace("$ALT_TRIPLE", &alternate())
.replace("$HOST_TRIPLE", &rustc_host())
.replace("$ALT_DEP", alt_dep)
.replace("$CFG_DEP", cfg_dep)
.replace("$HOST_DEP", host_dep)
@ -2262,11 +2318,23 @@ fn filter_platform() {
"deps": [
{
"name": "alt_dep",
"pkg": "alt-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "alt-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": "$ALT_TRIPLE"
}
]
},
{
"name": "normal_dep",
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": null
}
]
}
],
"features": []
@ -2285,6 +2353,7 @@ fn filter_platform() {
"workspace_root": "[..]foo"
}
"#
.replace("$ALT_TRIPLE", &alternate())
.replace("$ALT_DEP", alt_dep)
.replace("$NORMAL_DEP", normal_dep)
.replace("$FOO", &foo),
@ -2314,11 +2383,23 @@ fn filter_platform() {
"deps": [
{
"name": "host_dep",
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": "$HOST_TRIPLE"
}
]
},
{
"name": "normal_dep",
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": null
}
]
}
],
"features": []
@ -2343,6 +2424,7 @@ fn filter_platform() {
"workspace_root": "[..]foo"
}
"#
.replace("$HOST_TRIPLE", &rustc_host())
.replace("$HOST_DEP", host_dep)
.replace("$NORMAL_DEP", normal_dep)
.replace("$FOO", &foo),
@ -2381,15 +2463,33 @@ fn filter_platform() {
"deps": [
{
"name": "cfg_dep",
"pkg": "cfg-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "cfg-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": "cfg(foobar)"
}
]
},
{
"name": "host_dep",
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "host-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": "$HOST_TRIPLE"
}
]
},
{
"name": "normal_dep",
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)"
"pkg": "normal-dep 0.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dep_kinds": [
{
"kind": null,
"target": null
}
]
}
],
"features": []
@ -2414,6 +2514,7 @@ fn filter_platform() {
"workspace_root": "[..]/foo"
}
"#
.replace("$HOST_TRIPLE", &rustc_host())
.replace("$CFG_DEP", cfg_dep)
.replace("$HOST_DEP", host_dep)
.replace("$NORMAL_DEP", normal_dep)
@ -2421,3 +2522,102 @@ fn filter_platform() {
)
.run();
}
#[cargo_test]
fn dep_kinds() {
Package::new("bar", "0.1.0").publish();
Package::new("winapi", "0.1.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
bar = "0.1"
[dev-dependencies]
bar = "0.1"
[build-dependencies]
bar = "0.1"
[target.'cfg(windows)'.dependencies]
winapi = "0.1"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("metadata")
.with_json(
r#"
{
"packages": "{...}",
"workspace_members": "{...}",
"target_directory": "{...}",
"version": 1,
"workspace_root": "{...}",
"resolve": {
"nodes": [
{
"id": "bar 0.1.0 [..]",
"dependencies": [],
"deps": [],
"features": []
},
{
"id": "foo 0.1.0 [..]",
"dependencies": [
"bar 0.1.0 [..]",
"winapi 0.1.0 [..]"
],
"deps": [
{
"name": "bar",
"pkg": "bar 0.1.0 [..]",
"dep_kinds": [
{
"kind": null,
"target": null
},
{
"kind": "dev",
"target": null
},
{
"kind": "build",
"target": null
}
]
},
{
"name": "winapi",
"pkg": "winapi 0.1.0 [..]",
"dep_kinds": [
{
"kind": null,
"target": "cfg(windows)"
}
]
}
],
"features": []
},
{
"id": "winapi 0.1.0 [..]",
"dependencies": [],
"deps": [],
"features": []
}
],
"root": "foo 0.1.0 [..]"
}
}
"#,
)
.run();
}

View File

@ -525,6 +525,12 @@ fn update_precise_first_run() {
],
"deps": [
{
"dep_kinds": [
{
"kind": null,
"target": null
}
],
"name": "serde",
"pkg": "serde 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)"
}