Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make more cargo-as-a-library functions pub #10414

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::compiler::{CompileKind, CompileMode, RustcTargetData, Unit};
use crate::core::profiles::{Profiles, UnitFor};
use crate::core::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
use crate::core::resolver::HasDevUnits;
use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspace};
use crate::core::{Dependency, PackageId, PackageIdSpec, PackageSet, Resolve, SourceId, Workspace};
use crate::ops::{self, Packages};
use crate::util::errors::CargoResult;
use std::collections::{HashMap, HashSet};
Expand All @@ -31,13 +31,12 @@ pub fn parse_unstable_flag(value: Option<&str>) -> Vec<String> {
crates.into_iter().map(|s| s.to_string()).collect()
}

/// Resolve the standard library dependencies.
pub fn resolve_std<'cfg>(
/// Make a Workspace representing the standard library.
pub fn make_std_ws<'cfg>(
ws: &Workspace<'cfg>,
target_data: &RustcTargetData<'cfg>,
requested_targets: &[CompileKind],
crates: &[String],
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
) -> CargoResult<(Workspace<'cfg>, CliFeatures, Vec<PackageIdSpec>)> {
let src_path = detect_sysroot_src_path(target_data)?;
let to_patch = [
"rustc-std-workspace-core",
Expand Down Expand Up @@ -111,6 +110,19 @@ pub fn resolve_std<'cfg>(
let cli_features = CliFeatures::from_command_line(
&features, /*all_features*/ false, /*uses_default_features*/ false,
)?;

Ok((std_ws, cli_features, specs))
}

/// Resolve the standard library dependencies.
pub fn resolve_std<'cfg>(
ws: &Workspace<'cfg>,
target_data: &RustcTargetData<'cfg>,
requested_targets: &[CompileKind],
crates: &[String],
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
let (std_ws, cli_features, specs) = make_std_ws(ws, target_data, crates)?;

let resolve = ops::resolve_ws_with_opts(
&std_ws,
target_data,
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,9 @@ impl<'cfg> PackageSet<'cfg> {
Ok(())
}

fn filter_deps<'a>(
/// Filter the dependency closure of the given package to only those dependencies needed in the
/// given configuration.
pub fn filter_deps<'a>(
pkg_id: PackageId,
resolve: &'a Resolve,
has_dev_units: HasDevUnits,
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,8 @@ where
Ok(None)
}

fn make_ws_rustc_target<'cfg>(
/// Make a `Workspace` for the given to-be-installed package.
pub fn make_ws_rustc_target<'cfg>(
config: &'cfg Config,
opts: &ops::CompileOptions,
source_id: &SourceId,
Expand All @@ -725,7 +726,7 @@ fn make_ws_rustc_target<'cfg>(

/// Parses x.y.z as if it were =x.y.z, and gives CLI-specific error messages in the case of invalid
/// values.
fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
pub fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
// If the version begins with character <, >, =, ^, ~ parse it as a
// version range, otherwise parse it as a specific version
let first = v
Expand Down
5 changes: 4 additions & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use self::cargo_fetch::{fetch, FetchOptions};
pub use self::cargo_generate_lockfile::generate_lockfile;
pub use self::cargo_generate_lockfile::update_lockfile;
pub use self::cargo_generate_lockfile::UpdateOptions;
pub use self::cargo_install::{install, install_list};
pub use self::cargo_install::{install, install_list, parse_semver_flag};
pub use self::cargo_new::{init, new, NewOptions, VersionControl};
pub use self::cargo_output_metadata::{output_metadata, ExportInfo, OutputMetadataOptions};
pub use self::cargo_package::{package, package_one, PackageOpts};
Expand All @@ -19,6 +19,9 @@ pub use self::cargo_read_manifest::{read_package, read_packages};
pub use self::cargo_run::run;
pub use self::cargo_test::{run_benches, run_tests, TestOptions};
pub use self::cargo_uninstall::uninstall;
pub use self::common_for_install_and_uninstall::{
exe_names, path_source, resolve_root, select_dep_pkg, select_pkg,
};
pub use self::fix::{fix, fix_maybe_exec_rustc, FixOptions};
pub use self::lockfile::{load_pkg_lockfile, resolve_to_string, write_pkg_lockfile};
pub use self::registry::HttpTimeout;
Expand Down
72 changes: 60 additions & 12 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ use anyhow::Context as _;
use log::{debug, trace};
use std::collections::{HashMap, HashSet};

/// Result for `just_resolve_ws_with_opts`.
pub struct PartialWorkspaceResolve<'cfg> {
pub registry: PackageRegistry<'cfg>,
/// The resolve for the entire workspace.
///
/// This may be `None` for things like `cargo install` and `-Zavoid-dev-deps`.
/// This does not include `paths` overrides.
pub workspace_resolve: Option<Resolve>,
/// The narrowed resolve, with the specific features enabled, and only the
/// given package specs requested.
pub targeted_resolve: Resolve,
}

/// Result for `resolve_ws_with_opts`.
pub struct WorkspaceResolve<'cfg> {
/// Packages to be downloaded.
Expand Down Expand Up @@ -79,15 +92,17 @@ pub fn resolve_ws<'a>(ws: &Workspace<'a>) -> CargoResult<(PackageSet<'a>, Resolv
///
/// `specs` may be empty, which indicates it should resolve all workspace
/// members. In this case, `opts.all_features` must be `true`.
pub fn resolve_ws_with_opts<'cfg>(
///
/// This function will only consult the index, and will not download crate artifacts. It will
/// therefore not trim dependency edges that are not needed on the particular platform or given the
/// way features happen to interact, as some of that requires downloading the source of a subset of
/// the crates in the resolve. If that functionality is needed, use `resolve_ws_with_opts`.
pub fn just_resolve_ws_with_opts<'cfg>(
ws: &Workspace<'cfg>,
target_data: &RustcTargetData<'cfg>,
requested_targets: &[CompileKind],
cli_features: &CliFeatures,
specs: &[PackageIdSpec],
has_dev_units: HasDevUnits,
force_all_targets: ForceAllTargets,
) -> CargoResult<WorkspaceResolve<'cfg>> {
) -> CargoResult<PartialWorkspaceResolve<'cfg>> {
let mut registry = PackageRegistry::new(ws.config())?;
let mut add_patches = true;
let resolve = if ws.ignore_lock() {
Expand Down Expand Up @@ -143,15 +158,47 @@ pub fn resolve_ws_with_opts<'cfg>(
add_patches,
)?;

let pkg_set = get_resolved_packages(&resolved_with_overrides, registry)?;
Ok(PartialWorkspaceResolve {
registry,
workspace_resolve: resolve,
targeted_resolve: resolved_with_overrides,
})
}

/// Resolves dependencies for some packages of the workspace,
/// taking into account `paths` overrides and activated features.
///
/// This function will also write the result of resolution as a new lock file
/// (unless `Workspace::require_optional_deps` is false, such as `cargo
/// install` or `-Z avoid-dev-deps`), or it is an ephemeral workspace (`cargo
/// install` or `cargo package`).
///
/// `specs` may be empty, which indicates it should resolve all workspace
/// members. In this case, `opts.all_features` must be `true`.
pub fn resolve_ws_with_opts<'cfg>(
ws: &Workspace<'cfg>,
target_data: &RustcTargetData<'cfg>,
requested_targets: &[CompileKind],
cli_features: &CliFeatures,
specs: &[PackageIdSpec],
has_dev_units: HasDevUnits,
force_all_targets: ForceAllTargets,
) -> CargoResult<WorkspaceResolve<'cfg>> {
let PartialWorkspaceResolve {
registry,
workspace_resolve,
targeted_resolve,
} = just_resolve_ws_with_opts(ws, cli_features, specs, has_dev_units)?;

let pkg_set = get_resolved_packages(&targeted_resolve, registry)?;

let member_ids = ws
.members_with_features(specs, cli_features)?
.into_iter()
.map(|(p, _fts)| p.package_id())
.collect::<Vec<_>>();
pkg_set.download_accessible(
&resolved_with_overrides,
&targeted_resolve,
&member_ids,
has_dev_units,
requested_targets,
Expand All @@ -163,7 +210,7 @@ pub fn resolve_ws_with_opts<'cfg>(
let resolved_features = FeatureResolver::resolve(
ws,
target_data,
&resolved_with_overrides,
&targeted_resolve,
&pkg_set,
cli_features,
specs,
Expand All @@ -173,7 +220,7 @@ pub fn resolve_ws_with_opts<'cfg>(

pkg_set.warn_no_lib_packages_and_artifact_libs_overlapping_deps(
ws,
&resolved_with_overrides,
&targeted_resolve,
&member_ids,
has_dev_units,
requested_targets,
Expand All @@ -183,13 +230,14 @@ pub fn resolve_ws_with_opts<'cfg>(

Ok(WorkspaceResolve {
pkg_set,
workspace_resolve: resolve,
targeted_resolve: resolved_with_overrides,
workspace_resolve,
targeted_resolve,
resolved_features,
})
}

fn resolve_with_registry<'cfg>(
/// Resolve the dependency closure of the given workspace in the context of the given registry.
pub fn resolve_with_registry<'cfg>(
ws: &Workspace<'cfg>,
registry: &mut PackageRegistry<'cfg>,
) -> CargoResult<Resolve> {
Expand Down