Skip to content

Commit

Permalink
[DRAFT] Cargo vendor with graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Grech committed Nov 22, 2022
1 parent 6553589 commit 5afc231
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ subtree of the package given to -p.\n\
/// Parses `--edges` option.
///
/// Returns a tuple of `EdgeKind` map and `no_proc_marco` flag.
fn parse_edge_kinds(config: &Config, args: &ArgMatches) -> CargoResult<(HashSet<EdgeKind>, bool)> {
//TODO: Move it out of tree or figure out defauilt value for vendor command
pub fn parse_edge_kinds(config: &Config, args: &ArgMatches) -> CargoResult<(HashSet<EdgeKind>, bool)> {
let (kinds, no_proc_macro) = {
let mut no_proc_macro = false;
let mut kinds = args.get_many::<String>("edges").map_or_else(
Expand Down
18 changes: 18 additions & 0 deletions src/bin/cargo/commands/vendor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::command_prelude::*;
use cargo::ops;
use std::path::PathBuf;
use crate::commands::tree::parse_edge_kinds;

pub fn cli() -> Command {
subcommand("vendor")
Expand Down Expand Up @@ -34,6 +35,18 @@ pub fn cli() -> Command {
"versioned-dirs",
"Always include version in subdir name",
))
// TODO add more flags supported by cargo-tree
.arg_features()
.arg(
multi_opt(
"edges",
"KINDS",
"The kinds of dependencies to display \
(features, normal, build, dev, all, \
no-normal, no-build, no-dev, no-proc-macro)",
)
.short('e'),
)
.arg(flag("no-merge-sources", "Not supported").hide(true))
.arg(flag("relative-path", "Not supported").hide(true))
.arg(flag("only-git-deps", "Not supported").hide(true))
Expand Down Expand Up @@ -83,12 +96,17 @@ /~https://github.com/rust-lang/cargo/issues/new
.get_one::<PathBuf>("path")
.cloned()
.unwrap_or_else(|| PathBuf::from("vendor"));

let (edge_kinds, _no_proc_macro) = parse_edge_kinds(config, args)?;

ops::vendor(
&ws,
&ops::VendorOptions {
cli_features: args.cli_features()?,
no_delete: args.flag("no-delete"),
destination: &path,
versioned_dirs: args.flag("versioned-dirs"),
edge_kinds,
extra: args
.get_many::<PathBuf>("tomls")
.unwrap_or_default()
Expand Down
15 changes: 12 additions & 3 deletions src/cargo/ops/tree/graph.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Code for building the graph used by `cargo tree`.
use super::TreeOptions;
use crate::core::compiler::{CompileKind, RustcTargetData};
use crate::core::dependency::DepKind;
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
Expand All @@ -9,6 +8,13 @@ use crate::core::{FeatureMap, FeatureValue, Package, PackageId, PackageIdSpec, W
use crate::util::interning::InternedString;
use crate::util::CargoResult;
use std::collections::{HashMap, HashSet};
use crate::ops::tree::Target;

pub struct GraphOptions {
pub target: Target,
pub graph_features: bool,
pub edge_kinds: HashSet<EdgeKind>,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum Node {
Expand Down Expand Up @@ -276,13 +282,14 @@ pub fn build<'a>(
target_data: &RustcTargetData<'_>,
requested_kinds: &[CompileKind],
package_map: HashMap<PackageId, &'a Package>,
opts: &TreeOptions,
opts: &GraphOptions,
) -> CargoResult<Graph<'a>> {
let mut graph = Graph::new(package_map);
let mut members_with_features = ws.members_with_features(specs, cli_features)?;
members_with_features.sort_unstable_by_key(|e| e.0.package_id());
for (member, cli_features) in members_with_features {
let member_id = member.package_id();
println!("Adding package {} to the graph", member_id);
let features_for = FeaturesFor::from_for_host(member.proc_macro());
for kind in requested_kinds {
let member_index = add_pkg(
Expand All @@ -304,6 +311,7 @@ pub fn build<'a>(
if opts.graph_features {
add_internal_features(&mut graph, resolve);
}
println!("Built graph of {} nodes", graph.nodes.len());
Ok(graph)
}

Expand All @@ -320,7 +328,7 @@ fn add_pkg(
features_for: FeaturesFor,
target_data: &RustcTargetData<'_>,
requested_kind: CompileKind,
opts: &TreeOptions,
opts: &GraphOptions,
) -> usize {
let node_features = resolved_features.activated_features(package_id, features_for);
let node_kind = match features_for {
Expand Down Expand Up @@ -356,6 +364,7 @@ fn add_pkg(
};
// Filter out inactivated targets.
if !show_all_targets && !target_data.dep_platform_activated(dep, kind) {
println!("Disabling {}", dep.package_name());
return false;
}
// Filter out dev-dependencies if requested.
Expand Down
18 changes: 15 additions & 3 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ use std::collections::{HashMap, HashSet};
use std::str::FromStr;

mod format;
mod graph;
// TODO: Move it one level up instead of making it public and have deps from vendor to tree
// What is the bast package structure?
pub mod graph;

pub use {graph::EdgeKind, graph::Node};
use crate::ops::tree::graph::GraphOptions;

pub struct TreeOptions {
pub cli_features: CliFeatures,
Expand Down Expand Up @@ -51,7 +54,7 @@ pub struct TreeOptions {
pub no_proc_macro: bool,
}

#[derive(PartialEq)]
#[derive(PartialEq, Clone)]
pub enum Target {
Host,
Specific(Vec<String>),
Expand Down Expand Up @@ -166,6 +169,12 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
.map(|pkg| (pkg.package_id(), pkg))
.collect();

let graph_options = GraphOptions {
target: opts.target.clone(),
graph_features: opts.graph_features,
edge_kinds: opts.edge_kinds.clone(),
};

let mut graph = graph::build(
ws,
&ws_resolve.targeted_resolve,
Expand All @@ -175,7 +184,7 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
&target_data,
&requested_kinds,
package_map,
opts,
&graph_options,
)?;

let root_specs = if opts.invert.is_empty() {
Expand Down Expand Up @@ -213,6 +222,9 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
})
.collect::<CargoResult<Vec<PackageIdSpec>>>()?;

println!("pkgs_to_prune count = {}",
pkgs_to_prune.len());

if root_indexes.len() == 0 {
ws.config().shell().warn(
"nothing to print.\n\n\
Expand Down
Loading

0 comments on commit 5afc231

Please sign in to comment.