cargo/ops/
cargo_output_metadata.rs

1use crate::core::compiler::artifact::match_artifacts_kind_with_targets;
2use crate::core::compiler::{CompileKind, RustcTargetData};
3use crate::core::dependency::DepKind;
4use crate::core::package::SerializedPackage;
5use crate::core::resolver::{features::CliFeatures, HasDevUnits, Resolve};
6use crate::core::{Package, PackageId, PackageIdSpec, Workspace};
7use crate::ops::{self, Packages};
8use crate::util::interning::InternedString;
9use crate::util::CargoResult;
10use cargo_platform::Platform;
11use serde::Serialize;
12use std::collections::BTreeMap;
13use std::path::PathBuf;
14
15const VERSION: u32 = 1;
16
17pub struct OutputMetadataOptions {
18    pub cli_features: CliFeatures,
19    pub no_deps: bool,
20    pub version: u32,
21    pub filter_platforms: Vec<String>,
22}
23
24/// Loads the manifest, resolves the dependencies of the package to the concrete
25/// used versions - considering overrides - and writes all dependencies in a JSON
26/// format to stdout.
27pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> CargoResult<ExportInfo> {
28    if opt.version != VERSION {
29        anyhow::bail!(
30            "metadata version {} not supported, only {} is currently supported",
31            opt.version,
32            VERSION
33        );
34    }
35    let (packages, resolve) = if opt.no_deps {
36        let packages = ws
37            .members()
38            .map(|pkg| pkg.serialized(ws.gctx().cli_unstable(), ws.unstable_features()))
39            .collect();
40        (packages, None)
41    } else {
42        let (packages, resolve) = build_resolve_graph(ws, opt)?;
43        (packages, Some(resolve))
44    };
45
46    Ok(ExportInfo {
47        packages,
48        workspace_members: ws.members().map(|pkg| pkg.package_id().to_spec()).collect(),
49        workspace_default_members: ws
50            .default_members()
51            .map(|pkg| pkg.package_id().to_spec())
52            .collect(),
53        resolve,
54        target_directory: ws.target_dir().into_path_unlocked(),
55        version: VERSION,
56        workspace_root: ws.root().to_path_buf(),
57        metadata: ws.custom_metadata().cloned(),
58    })
59}
60
61/// This is the structure that is serialized and displayed to the user.
62///
63/// See cargo-metadata.adoc for detailed documentation of the format.
64#[derive(Serialize)]
65pub struct ExportInfo {
66    packages: Vec<SerializedPackage>,
67    workspace_members: Vec<PackageIdSpec>,
68    workspace_default_members: Vec<PackageIdSpec>,
69    resolve: Option<MetadataResolve>,
70    target_directory: PathBuf,
71    version: u32,
72    workspace_root: PathBuf,
73    metadata: Option<toml::Value>,
74}
75
76#[derive(Serialize)]
77struct MetadataResolve {
78    nodes: Vec<MetadataResolveNode>,
79    root: Option<PackageIdSpec>,
80}
81
82#[derive(Serialize)]
83struct MetadataResolveNode {
84    id: PackageIdSpec,
85    dependencies: Vec<PackageIdSpec>,
86    deps: Vec<Dep>,
87    features: Vec<InternedString>,
88}
89
90#[derive(Serialize)]
91struct Dep {
92    // TODO(bindeps): after -Zbindeps gets stabilized,
93    // mark this field as deprecated in the help manual of cargo-metadata
94    name: InternedString,
95    pkg: PackageIdSpec,
96    #[serde(skip)]
97    pkg_id: PackageId,
98    dep_kinds: Vec<DepKindInfo>,
99}
100
101#[derive(Serialize, PartialEq, Eq, PartialOrd, Ord)]
102struct DepKindInfo {
103    kind: DepKind,
104    target: Option<Platform>,
105
106    // vvvvv The fields below are introduced for `-Z bindeps`.
107    /// What the manifest calls the crate.
108    ///
109    /// A renamed dependency will show the rename instead of original name.
110    // TODO(bindeps): Remove `Option` after -Zbindeps get stabilized.
111    #[serde(skip_serializing_if = "Option::is_none")]
112    extern_name: Option<InternedString>,
113    /// Artifact's crate type, e.g. staticlib, cdylib, bin...
114    #[serde(skip_serializing_if = "Option::is_none")]
115    artifact: Option<&'static str>,
116    /// Equivalent to `{ target = "…" }` in an artifact dependency requirement.
117    ///
118    /// * If the target points to a custom target JSON file, the path will be absolute.
119    /// * If the target is a build assumed target `{ target = "target" }`, it will show as `<target>`.
120    #[serde(skip_serializing_if = "Option::is_none")]
121    compile_target: Option<InternedString>,
122    /// Executable name for an artifact binary dependency.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    bin_name: Option<String>,
125    // ^^^^^ The fields above are introduced for `-Z bindeps`.
126}
127
128/// Builds the resolve graph as it will be displayed to the user.
129fn build_resolve_graph(
130    ws: &Workspace<'_>,
131    metadata_opts: &OutputMetadataOptions,
132) -> CargoResult<(Vec<SerializedPackage>, MetadataResolve)> {
133    // TODO: Without --filter-platform, features are being resolved for `host` only.
134    // How should this work?
135    let requested_kinds =
136        CompileKind::from_requested_targets(ws.gctx(), &metadata_opts.filter_platforms)?;
137    let mut target_data = RustcTargetData::new(ws, &requested_kinds)?;
138    // Resolve entire workspace.
139    let specs = Packages::All(Vec::new()).to_package_id_specs(ws)?;
140    let force_all = if metadata_opts.filter_platforms.is_empty() {
141        crate::core::resolver::features::ForceAllTargets::Yes
142    } else {
143        crate::core::resolver::features::ForceAllTargets::No
144    };
145
146    // Note that even with --filter-platform we end up downloading host dependencies as well,
147    // as that is the behavior of download_accessible.
148    let dry_run = false;
149    let ws_resolve = ops::resolve_ws_with_opts(
150        ws,
151        &mut target_data,
152        &requested_kinds,
153        &metadata_opts.cli_features,
154        &specs,
155        HasDevUnits::Yes,
156        force_all,
157        dry_run,
158    )?;
159
160    let package_map: BTreeMap<PackageId, Package> = ws_resolve
161        .pkg_set
162        .packages()
163        // This is a little lazy, but serde doesn't handle Rc fields very well.
164        .map(|pkg| (pkg.package_id(), Package::clone(pkg)))
165        .collect();
166
167    // Start from the workspace roots, and recurse through filling out the
168    // map, filtering targets as necessary.
169    let mut node_map = BTreeMap::new();
170    for member_pkg in ws.members() {
171        build_resolve_graph_r(
172            &mut node_map,
173            member_pkg.package_id(),
174            &ws_resolve.targeted_resolve,
175            &package_map,
176            &target_data,
177            &requested_kinds,
178        )?;
179    }
180    // Get a Vec of Packages.
181    let actual_packages = package_map
182        .into_iter()
183        .filter_map(|(pkg_id, pkg)| node_map.get(&pkg_id).map(|_| pkg))
184        .map(|pkg| pkg.serialized(ws.gctx().cli_unstable(), ws.unstable_features()))
185        .collect();
186
187    let mr = MetadataResolve {
188        nodes: node_map.into_iter().map(|(_pkg_id, node)| node).collect(),
189        root: ws.current_opt().map(|pkg| pkg.package_id().to_spec()),
190    };
191    Ok((actual_packages, mr))
192}
193
194fn build_resolve_graph_r(
195    node_map: &mut BTreeMap<PackageId, MetadataResolveNode>,
196    pkg_id: PackageId,
197    resolve: &Resolve,
198    package_map: &BTreeMap<PackageId, Package>,
199    target_data: &RustcTargetData<'_>,
200    requested_kinds: &[CompileKind],
201) -> CargoResult<()> {
202    if node_map.contains_key(&pkg_id) {
203        return Ok(());
204    }
205    // This normalizes the IDs so that they are consistent between the
206    // `packages` array and the `resolve` map. This is a bit of a hack to
207    // compensate for the fact that
208    // SourceKind::Git(GitReference::Branch("master")) is the same as
209    // SourceKind::Git(GitReference::DefaultBranch). We want IDs in the JSON
210    // to be opaque, and compare with basic string equality, so this will
211    // always prefer the style of ID in the Package instead of the resolver.
212    // Cargo generally only exposes PackageIds from the Package struct, and
213    // AFAIK this is the only place where the resolver variant is exposed.
214    //
215    // This diverges because the SourceIds created for Packages are built
216    // based on the Dependency declaration, but the SourceIds in the resolver
217    // are deserialized from Cargo.lock. Cargo.lock may have been generated by
218    // an older (or newer!) version of Cargo which uses a different style.
219    let normalize_id = |id| -> PackageId { *package_map.get_key_value(&id).unwrap().0 };
220    let features = resolve.features(pkg_id).to_vec();
221
222    let deps = {
223        let mut dep_metadatas = Vec::new();
224        let iter = resolve.deps(pkg_id).filter(|(_dep_id, deps)| {
225            if requested_kinds == [CompileKind::Host] {
226                true
227            } else {
228                requested_kinds.iter().any(|kind| {
229                    deps.iter()
230                        .any(|dep| target_data.dep_platform_activated(dep, *kind))
231                })
232            }
233        });
234        for (dep_id, deps) in iter {
235            let mut dep_kinds = Vec::new();
236
237            let targets = package_map[&dep_id].targets();
238
239            // Try to get the extern name for lib, or crate name for bins.
240            let extern_name = |target| {
241                resolve
242                    .extern_crate_name_and_dep_name(pkg_id, dep_id, target)
243                    .map(|(ext_crate_name, _)| ext_crate_name)
244            };
245
246            let lib_target = targets.iter().find(|t| t.is_lib());
247
248            for dep in deps.iter() {
249                if let Some(target) = lib_target {
250                    // When we do have a library target, include them in deps if...
251                    let included = match dep.artifact() {
252                        // it is not an artifact dep at all
253                        None => true,
254                        // it is also an artifact dep with `{ …, lib = true }`
255                        Some(a) if a.is_lib() => true,
256                        _ => false,
257                    };
258                    // TODO(bindeps): Cargo shouldn't have `extern_name` field
259                    // if the user is not using -Zbindeps.
260                    // Remove this condition ` after -Zbindeps gets stabilized.
261                    let extern_name = if dep.artifact().is_some() {
262                        Some(extern_name(target)?)
263                    } else {
264                        None
265                    };
266                    if included {
267                        dep_kinds.push(DepKindInfo {
268                            kind: dep.kind(),
269                            target: dep.platform().cloned(),
270                            extern_name,
271                            artifact: None,
272                            compile_target: None,
273                            bin_name: None,
274                        });
275                    }
276                }
277
278                // No need to proceed if there is no artifact dependency.
279                let Some(artifact_requirements) = dep.artifact() else {
280                    continue;
281                };
282
283                let compile_target = match artifact_requirements.target() {
284                    Some(t) => t
285                        .to_compile_target()
286                        .map(|t| t.rustc_target())
287                        // Given that Cargo doesn't know which target it should resolve to,
288                        // when an artifact dep is specified with { target = "target" },
289                        // keep it with a special "<target>" string,
290                        .or_else(|| Some(InternedString::new("<target>"))),
291                    None => None,
292                };
293
294                let target_set =
295                    match_artifacts_kind_with_targets(dep, targets, pkg_id.name().as_str())?;
296                dep_kinds.reserve(target_set.len());
297                for (kind, target) in target_set.into_iter() {
298                    dep_kinds.push(DepKindInfo {
299                        kind: dep.kind(),
300                        target: dep.platform().cloned(),
301                        extern_name: extern_name(target).ok(),
302                        artifact: Some(kind.crate_type()),
303                        compile_target,
304                        bin_name: target.is_bin().then(|| target.name().to_string()),
305                    })
306                }
307            }
308
309            dep_kinds.sort();
310
311            let pkg_id = normalize_id(dep_id);
312
313            let dep = match (lib_target, dep_kinds.len()) {
314                (Some(target), _) => Dep {
315                    name: extern_name(target)?,
316                    pkg: pkg_id.to_spec(),
317                    pkg_id,
318                    dep_kinds,
319                },
320                // No lib target exists but contains artifact deps.
321                (None, 1..) => Dep {
322                    name: InternedString::new(""),
323                    pkg: pkg_id.to_spec(),
324                    pkg_id,
325                    dep_kinds,
326                },
327                // No lib or artifact dep exists.
328                // Usually this mean parent depending on non-lib bin crate.
329                (None, _) => continue,
330            };
331
332            dep_metadatas.push(dep)
333        }
334        dep_metadatas
335    };
336
337    let to_visit: Vec<PackageId> = deps.iter().map(|dep| dep.pkg_id).collect();
338    let node = MetadataResolveNode {
339        id: normalize_id(pkg_id).to_spec(),
340        dependencies: to_visit.iter().map(|id| id.to_spec()).collect(),
341        deps,
342        features,
343    };
344    node_map.insert(pkg_id, node);
345    for dep_id in to_visit {
346        build_resolve_graph_r(
347            node_map,
348            dep_id,
349            resolve,
350            package_map,
351            target_data,
352            requested_kinds,
353        )?;
354    }
355
356    Ok(())
357}