2021-05-22 13:53:47 +00:00
|
|
|
//! See [`CargoWorkspace`].
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2021-07-19 18:20:10 +00:00
|
|
|
use std::convert::TryInto;
|
2021-06-14 04:41:46 +00:00
|
|
|
use std::iter;
|
2021-03-02 12:27:29 +00:00
|
|
|
use std::path::PathBuf;
|
2021-07-19 18:20:10 +00:00
|
|
|
use std::{ops, process::Command};
|
2019-01-10 19:21:14 +00:00
|
|
|
|
2020-02-13 10:10:50 +00:00
|
|
|
use anyhow::{Context, Result};
|
2020-08-13 14:25:38 +00:00
|
|
|
use base_db::Edition;
|
2021-01-22 11:11:01 +00:00
|
|
|
use cargo_metadata::{CargoOpt, MetadataCommand};
|
2021-01-14 15:47:42 +00:00
|
|
|
use la_arena::{Arena, Idx};
|
2020-06-24 11:34:24 +00:00
|
|
|
use paths::{AbsPath, AbsPathBuf};
|
2019-07-04 20:05:17 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2021-03-06 12:17:22 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde_json::from_value;
|
2019-01-10 19:21:14 +00:00
|
|
|
|
2021-06-14 04:41:46 +00:00
|
|
|
use crate::CfgOverrides;
|
2021-07-19 18:20:10 +00:00
|
|
|
use crate::{utf8_stdout, ManifestPath};
|
2020-07-23 16:57:27 +00:00
|
|
|
|
2021-05-22 13:53:47 +00:00
|
|
|
/// [`CargoWorkspace`] represents the logical structure of, well, a Cargo
|
2019-01-10 19:21:14 +00:00
|
|
|
/// workspace. It pretty closely mirrors `cargo metadata` output.
|
|
|
|
///
|
2019-02-05 22:10:49 +00:00
|
|
|
/// Note that internally, rust analyzer uses a different structure:
|
2019-01-10 19:21:14 +00:00
|
|
|
/// `CrateGraph`. `CrateGraph` is lower-level: it knows only about the crates,
|
2019-02-11 16:18:27 +00:00
|
|
|
/// while this knows about `Packages` & `Targets`: purely cargo-related
|
2019-01-10 19:21:14 +00:00
|
|
|
/// concepts.
|
2020-06-24 11:34:24 +00:00
|
|
|
///
|
|
|
|
/// We use absolute paths here, `cargo metadata` guarantees to always produce
|
|
|
|
/// abs paths.
|
2020-07-10 13:27:34 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2019-01-10 19:21:14 +00:00
|
|
|
pub struct CargoWorkspace {
|
2020-03-19 15:00:11 +00:00
|
|
|
packages: Arena<PackageData>,
|
|
|
|
targets: Arena<TargetData>,
|
2020-06-24 11:34:24 +00:00
|
|
|
workspace_root: AbsPathBuf,
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 16:53:31 +00:00
|
|
|
impl ops::Index<Package> for CargoWorkspace {
|
|
|
|
type Output = PackageData;
|
|
|
|
fn index(&self, index: Package) -> &PackageData {
|
|
|
|
&self.packages[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Index<Target> for CargoWorkspace {
|
|
|
|
type Output = TargetData;
|
|
|
|
fn index(&self, index: Target) -> &TargetData {
|
|
|
|
&self.targets[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 16:34:56 +00:00
|
|
|
/// Describes how to set the rustc source directory.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum RustcSource {
|
|
|
|
/// Explicit path for the rustc source directory.
|
|
|
|
Path(AbsPathBuf),
|
|
|
|
/// Try to automatically detect where the rustc source directory is.
|
|
|
|
Discover,
|
|
|
|
}
|
|
|
|
|
2021-08-23 17:18:11 +00:00
|
|
|
/// Crates to disable `#[cfg(test)]` on.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum UnsetTestCrates {
|
|
|
|
None,
|
|
|
|
Only(Vec<String>),
|
|
|
|
All,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for UnsetTestCrates {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 14:06:00 +00:00
|
|
|
#[derive(Default, Clone, Debug, PartialEq, Eq)]
|
2020-04-01 16:51:16 +00:00
|
|
|
pub struct CargoConfig {
|
2019-12-13 10:16:34 +00:00
|
|
|
/// Do not activate the `default` feature.
|
|
|
|
pub no_default_features: bool,
|
|
|
|
|
|
|
|
/// Activate all available features
|
|
|
|
pub all_features: bool,
|
|
|
|
|
|
|
|
/// List of features to activate.
|
|
|
|
/// This will be ignored if `cargo_all_features` is true.
|
|
|
|
pub features: Vec<String>,
|
2020-03-16 12:43:29 +00:00
|
|
|
|
2020-04-27 22:15:54 +00:00
|
|
|
/// rustc target
|
2020-05-05 16:01:54 +00:00
|
|
|
pub target: Option<String>,
|
2020-11-10 20:50:05 +00:00
|
|
|
|
2020-11-13 16:38:26 +00:00
|
|
|
/// Don't load sysroot crates (`std`, `core` & friends). Might be useful
|
|
|
|
/// when debugging isolated issues.
|
|
|
|
pub no_sysroot: bool,
|
|
|
|
|
2020-11-10 20:50:05 +00:00
|
|
|
/// rustc private crate source
|
2021-02-11 16:34:56 +00:00
|
|
|
pub rustc_source: Option<RustcSource>,
|
2021-06-14 04:41:46 +00:00
|
|
|
|
|
|
|
/// crates to disable `#[cfg(test)]` on
|
2021-08-23 17:18:11 +00:00
|
|
|
pub unset_test_crates: UnsetTestCrates,
|
2021-07-18 08:29:22 +00:00
|
|
|
|
|
|
|
pub wrap_rustc_in_build_scripts: bool,
|
2021-06-14 04:41:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CargoConfig {
|
|
|
|
pub fn cfg_overrides(&self) -> CfgOverrides {
|
2021-08-23 17:18:11 +00:00
|
|
|
match &self.unset_test_crates {
|
|
|
|
UnsetTestCrates::None => CfgOverrides::Selective(iter::empty().collect()),
|
|
|
|
UnsetTestCrates::Only(unset_test_crates) => CfgOverrides::Selective(
|
|
|
|
unset_test_crates
|
|
|
|
.iter()
|
2021-09-19 17:00:06 +00:00
|
|
|
.cloned()
|
2021-08-23 17:18:11 +00:00
|
|
|
.zip(iter::repeat_with(|| {
|
|
|
|
cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())])
|
|
|
|
.unwrap()
|
|
|
|
}))
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
UnsetTestCrates::All => CfgOverrides::Wildcard(
|
|
|
|
cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())]).unwrap(),
|
|
|
|
),
|
|
|
|
}
|
2021-06-14 04:41:46 +00:00
|
|
|
}
|
2019-12-13 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
pub type Package = Idx<PackageData>;
|
2019-01-10 19:21:14 +00:00
|
|
|
|
2020-03-19 15:00:11 +00:00
|
|
|
pub type Target = Idx<TargetData>;
|
2019-01-10 19:21:14 +00:00
|
|
|
|
2021-01-06 15:39:19 +00:00
|
|
|
/// Information associated with a cargo crate
|
2020-07-10 13:27:34 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-19 16:53:31 +00:00
|
|
|
pub struct PackageData {
|
2021-01-06 15:39:19 +00:00
|
|
|
/// Version given in the `Cargo.toml`
|
2021-07-18 08:29:22 +00:00
|
|
|
pub version: semver::Version,
|
2021-01-06 15:39:19 +00:00
|
|
|
/// Name as given in the `Cargo.toml`
|
2020-03-19 16:53:31 +00:00
|
|
|
pub name: String,
|
2021-01-06 15:39:19 +00:00
|
|
|
/// Path containing the `Cargo.toml`
|
2021-07-19 18:20:10 +00:00
|
|
|
pub manifest: ManifestPath,
|
2021-01-06 15:39:19 +00:00
|
|
|
/// Targets provided by the crate (lib, bin, example, test, ...)
|
2020-03-19 16:53:31 +00:00
|
|
|
pub targets: Vec<Target>,
|
2021-09-07 15:29:58 +00:00
|
|
|
/// Does this package come from the local filesystem (and is editable)?
|
|
|
|
pub is_local: bool,
|
2021-01-06 15:39:19 +00:00
|
|
|
/// List of packages this package depends on
|
2020-03-19 16:53:31 +00:00
|
|
|
pub dependencies: Vec<PackageDependency>,
|
2021-01-06 15:39:19 +00:00
|
|
|
/// Rust edition for this package
|
2020-03-19 16:53:31 +00:00
|
|
|
pub edition: Edition,
|
2021-01-21 11:12:19 +00:00
|
|
|
/// Features provided by the crate, mapped to the features required by that feature.
|
|
|
|
pub features: FxHashMap<String, Vec<String>>,
|
|
|
|
/// List of features enabled on this package
|
|
|
|
pub active_features: Vec<String>,
|
2021-01-28 15:33:02 +00:00
|
|
|
// String representation of package id
|
|
|
|
pub id: String,
|
2021-03-06 12:17:22 +00:00
|
|
|
// The contents of [package.metadata.rust-analyzer]
|
|
|
|
pub metadata: RustAnalyzerPackageMetaData,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Default, Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct RustAnalyzerPackageMetaData {
|
2021-03-07 12:24:20 +00:00
|
|
|
pub rustc_private: bool,
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 13:27:34 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2019-01-10 19:21:14 +00:00
|
|
|
pub struct PackageDependency {
|
|
|
|
pub pkg: Package,
|
2019-02-09 09:51:06 +00:00
|
|
|
pub name: String,
|
2021-05-12 12:16:51 +00:00
|
|
|
pub kind: DepKind,
|
|
|
|
}
|
|
|
|
|
2021-05-24 16:31:43 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
|
2021-05-12 12:16:51 +00:00
|
|
|
pub enum DepKind {
|
|
|
|
/// Available to the library, binary, and dev targets in the package (but not the build script).
|
|
|
|
Normal,
|
|
|
|
/// Available only to test and bench targets (and the library target, when built with `cfg(test)`).
|
|
|
|
Dev,
|
|
|
|
/// Available only to the build script target.
|
|
|
|
Build,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DepKind {
|
2021-05-24 16:31:43 +00:00
|
|
|
fn iter(list: &[cargo_metadata::DepKindInfo]) -> impl Iterator<Item = Self> + '_ {
|
|
|
|
let mut dep_kinds = Vec::new();
|
|
|
|
if list.is_empty() {
|
|
|
|
dep_kinds.push(Self::Normal);
|
|
|
|
}
|
2021-05-12 12:16:51 +00:00
|
|
|
for info in list {
|
2021-05-24 16:31:43 +00:00
|
|
|
let kind = match info.kind {
|
|
|
|
cargo_metadata::DependencyKind::Normal => Self::Normal,
|
|
|
|
cargo_metadata::DependencyKind::Development => Self::Dev,
|
|
|
|
cargo_metadata::DependencyKind::Build => Self::Build,
|
2021-05-12 12:16:51 +00:00
|
|
|
cargo_metadata::DependencyKind::Unknown => continue,
|
2021-05-24 16:31:43 +00:00
|
|
|
};
|
|
|
|
dep_kinds.push(kind);
|
2021-05-12 12:16:51 +00:00
|
|
|
}
|
2021-05-24 16:31:43 +00:00
|
|
|
dep_kinds.sort_unstable();
|
|
|
|
dep_kinds.dedup();
|
|
|
|
dep_kinds.into_iter()
|
2021-05-12 12:16:51 +00:00
|
|
|
}
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:39:44 +00:00
|
|
|
/// Information associated with a package's target
|
2020-07-10 13:27:34 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-19 16:53:31 +00:00
|
|
|
pub struct TargetData {
|
2021-01-06 15:39:44 +00:00
|
|
|
/// Package that provided this target
|
2020-03-19 16:53:31 +00:00
|
|
|
pub package: Package,
|
2021-01-06 15:39:44 +00:00
|
|
|
/// Name as given in the `Cargo.toml` or generated from the file name
|
2020-03-19 16:53:31 +00:00
|
|
|
pub name: String,
|
2021-01-06 15:39:44 +00:00
|
|
|
/// Path to the main source file of the target
|
2020-06-24 11:34:24 +00:00
|
|
|
pub root: AbsPathBuf,
|
2021-01-06 15:39:44 +00:00
|
|
|
/// Kind of target
|
2020-03-19 16:53:31 +00:00
|
|
|
pub kind: TargetKind,
|
2021-01-06 15:39:44 +00:00
|
|
|
/// Is this target a proc-macro
|
2020-03-19 16:53:31 +00:00
|
|
|
pub is_proc_macro: bool,
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum TargetKind {
|
|
|
|
Bin,
|
2019-11-24 10:33:12 +00:00
|
|
|
/// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
|
2019-01-10 19:21:14 +00:00
|
|
|
Lib,
|
|
|
|
Example,
|
|
|
|
Test,
|
|
|
|
Bench,
|
2021-05-12 12:16:51 +00:00
|
|
|
BuildScript,
|
2019-01-10 19:21:14 +00:00
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TargetKind {
|
|
|
|
fn new(kinds: &[String]) -> TargetKind {
|
|
|
|
for kind in kinds {
|
|
|
|
return match kind.as_str() {
|
|
|
|
"bin" => TargetKind::Bin,
|
|
|
|
"test" => TargetKind::Test,
|
|
|
|
"bench" => TargetKind::Bench,
|
|
|
|
"example" => TargetKind::Example,
|
2021-05-12 12:16:51 +00:00
|
|
|
"custom-build" => TargetKind::BuildScript,
|
2019-11-24 10:33:12 +00:00
|
|
|
"proc-macro" => TargetKind::Lib,
|
2019-01-10 19:21:14 +00:00
|
|
|
_ if kind.contains("lib") => TargetKind::Lib,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
TargetKind::Other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 12:17:22 +00:00
|
|
|
#[derive(Deserialize, Default)]
|
|
|
|
// Deserialise helper for the cargo metadata
|
|
|
|
struct PackageMetadata {
|
|
|
|
#[serde(rename = "rust-analyzer")]
|
|
|
|
rust_analyzer: Option<RustAnalyzerPackageMetaData>,
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:21:14 +00:00
|
|
|
impl CargoWorkspace {
|
2021-07-17 20:43:54 +00:00
|
|
|
pub fn fetch_metadata(
|
2021-07-19 18:20:10 +00:00
|
|
|
cargo_toml: &ManifestPath,
|
2020-11-13 16:38:26 +00:00
|
|
|
config: &CargoConfig,
|
2021-01-07 17:08:46 +00:00
|
|
|
progress: &dyn Fn(String),
|
2021-07-17 20:43:54 +00:00
|
|
|
) -> Result<cargo_metadata::Metadata> {
|
2021-07-19 18:21:41 +00:00
|
|
|
let target = config
|
|
|
|
.target
|
|
|
|
.clone()
|
|
|
|
.or_else(|| cargo_config_build_target(cargo_toml))
|
|
|
|
.or_else(|| rustc_discover_host_triple(cargo_toml));
|
|
|
|
|
2019-01-26 20:16:15 +00:00
|
|
|
let mut meta = MetadataCommand::new();
|
2020-08-12 14:52:28 +00:00
|
|
|
meta.cargo_path(toolchain::cargo());
|
2020-07-05 09:15:35 +00:00
|
|
|
meta.manifest_path(cargo_toml.to_path_buf());
|
2020-11-13 16:38:26 +00:00
|
|
|
if config.all_features {
|
2019-12-13 10:16:34 +00:00
|
|
|
meta.features(CargoOpt::AllFeatures);
|
2020-07-30 14:23:07 +00:00
|
|
|
} else {
|
2020-11-13 16:38:26 +00:00
|
|
|
if config.no_default_features {
|
2020-07-30 14:23:07 +00:00
|
|
|
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
|
|
|
|
// https://github.com/oli-obk/cargo_metadata/issues/79
|
|
|
|
meta.features(CargoOpt::NoDefaultFeatures);
|
|
|
|
}
|
2020-11-13 16:38:26 +00:00
|
|
|
if !config.features.is_empty() {
|
|
|
|
meta.features(CargoOpt::SomeFeatures(config.features.clone()));
|
2020-07-30 14:23:07 +00:00
|
|
|
}
|
2019-12-13 10:16:34 +00:00
|
|
|
}
|
2021-07-19 18:20:10 +00:00
|
|
|
meta.current_dir(cargo_toml.parent().as_os_str());
|
2021-07-19 18:21:41 +00:00
|
|
|
|
2020-12-17 01:38:21 +00:00
|
|
|
if let Some(target) = target {
|
|
|
|
meta.other_options(vec![String::from("--filter-platform"), target]);
|
2020-04-26 22:11:04 +00:00
|
|
|
}
|
2020-12-31 12:42:59 +00:00
|
|
|
|
2021-07-17 20:43:54 +00:00
|
|
|
// FIXME: Fetching metadata is a slow process, as it might require
|
|
|
|
// calling crates.io. We should be reporting progress here, but it's
|
|
|
|
// unclear whether cargo itself supports it.
|
2021-01-07 17:08:46 +00:00
|
|
|
progress("metadata".to_string());
|
2021-07-17 20:43:54 +00:00
|
|
|
|
|
|
|
let meta = meta.exec().with_context(|| {
|
2021-07-19 18:20:10 +00:00
|
|
|
format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display(),)
|
2020-02-13 10:10:50 +00:00
|
|
|
})?;
|
2020-03-16 12:43:29 +00:00
|
|
|
|
2021-07-17 20:43:54 +00:00
|
|
|
Ok(meta)
|
|
|
|
}
|
|
|
|
|
2021-07-18 08:29:22 +00:00
|
|
|
pub fn new(mut meta: cargo_metadata::Metadata) -> CargoWorkspace {
|
2019-01-10 19:21:14 +00:00
|
|
|
let mut pkg_by_id = FxHashMap::default();
|
|
|
|
let mut packages = Arena::default();
|
|
|
|
let mut targets = Arena::default();
|
|
|
|
|
2020-07-10 20:28:41 +00:00
|
|
|
meta.packages.sort_by(|a, b| a.id.cmp(&b.id));
|
2021-01-28 15:33:02 +00:00
|
|
|
for meta_pkg in &meta.packages {
|
2021-03-06 12:17:22 +00:00
|
|
|
let cargo_metadata::Package {
|
|
|
|
id, edition, name, manifest_path, version, metadata, ..
|
|
|
|
} = meta_pkg;
|
|
|
|
let meta = from_value::<PackageMetadata>(metadata.clone()).unwrap_or_default();
|
2021-07-17 20:43:54 +00:00
|
|
|
let edition = edition.parse::<Edition>().unwrap_or_else(|err| {
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::error!("Failed to parse edition {}", err);
|
2021-07-17 20:43:54 +00:00
|
|
|
Edition::CURRENT
|
|
|
|
});
|
2021-09-07 15:26:21 +00:00
|
|
|
// We treat packages without source as "local" packages. That includes all members of
|
|
|
|
// the current workspace, as well as any path dependency outside the workspace.
|
2021-09-07 15:29:58 +00:00
|
|
|
let is_local = meta_pkg.source.is_none();
|
2021-03-02 12:27:29 +00:00
|
|
|
|
2019-01-10 19:21:14 +00:00
|
|
|
let pkg = packages.alloc(PackageData {
|
2021-01-28 15:33:02 +00:00
|
|
|
id: id.repr.clone(),
|
|
|
|
name: name.clone(),
|
2021-07-18 08:29:22 +00:00
|
|
|
version: version.clone(),
|
2021-07-19 18:20:10 +00:00
|
|
|
manifest: AbsPathBuf::assert(PathBuf::from(&manifest_path)).try_into().unwrap(),
|
2019-01-10 19:21:14 +00:00
|
|
|
targets: Vec::new(),
|
2021-09-07 15:29:58 +00:00
|
|
|
is_local,
|
2019-11-12 10:53:31 +00:00
|
|
|
edition,
|
2019-01-10 19:21:14 +00:00
|
|
|
dependencies: Vec::new(),
|
2021-01-28 15:33:02 +00:00
|
|
|
features: meta_pkg.features.clone().into_iter().collect(),
|
2021-01-21 11:12:19 +00:00
|
|
|
active_features: Vec::new(),
|
2021-03-06 12:17:22 +00:00
|
|
|
metadata: meta.rust_analyzer.unwrap_or_default(),
|
2019-01-10 19:21:14 +00:00
|
|
|
});
|
|
|
|
let pkg_data = &mut packages[pkg];
|
2019-11-12 10:53:31 +00:00
|
|
|
pkg_by_id.insert(id, pkg);
|
2021-01-28 15:33:02 +00:00
|
|
|
for meta_tgt in &meta_pkg.targets {
|
2020-02-18 13:32:19 +00:00
|
|
|
let is_proc_macro = meta_tgt.kind.as_slice() == ["proc-macro"];
|
2019-01-10 19:21:14 +00:00
|
|
|
let tgt = targets.alloc(TargetData {
|
2020-03-19 16:53:31 +00:00
|
|
|
package: pkg,
|
2021-01-28 15:33:02 +00:00
|
|
|
name: meta_tgt.name.clone(),
|
2021-03-02 12:27:29 +00:00
|
|
|
root: AbsPathBuf::assert(PathBuf::from(&meta_tgt.src_path)),
|
2019-01-10 19:21:14 +00:00
|
|
|
kind: TargetKind::new(meta_tgt.kind.as_slice()),
|
2019-11-24 10:33:12 +00:00
|
|
|
is_proc_macro,
|
2019-01-10 19:21:14 +00:00
|
|
|
});
|
|
|
|
pkg_data.targets.push(tgt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let resolve = meta.resolve.expect("metadata executed with deps");
|
2020-07-10 20:28:41 +00:00
|
|
|
for mut node in resolve.nodes {
|
2020-01-28 09:08:17 +00:00
|
|
|
let source = match pkg_by_id.get(&node.id) {
|
|
|
|
Some(&src) => src,
|
2020-01-30 17:01:38 +00:00
|
|
|
// FIXME: replace this and a similar branch below with `.unwrap`, once
|
|
|
|
// https://github.com/rust-lang/cargo/issues/7841
|
|
|
|
// is fixed and hits stable (around 1.43-is probably?).
|
2020-01-28 09:08:17 +00:00
|
|
|
None => {
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::error!("Node id do not match in cargo metadata, ignoring {}", node.id);
|
2020-01-28 09:08:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2020-07-10 20:28:41 +00:00
|
|
|
node.deps.sort_by(|a, b| a.pkg.cmp(&b.pkg));
|
2021-05-24 16:31:43 +00:00
|
|
|
for (dep_node, kind) in node
|
|
|
|
.deps
|
|
|
|
.iter()
|
|
|
|
.flat_map(|dep| DepKind::iter(&dep.dep_kinds).map(move |kind| (dep, kind)))
|
|
|
|
{
|
2020-01-28 09:08:17 +00:00
|
|
|
let pkg = match pkg_by_id.get(&dep_node.pkg) {
|
|
|
|
Some(&pkg) => pkg,
|
|
|
|
None => {
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::error!(
|
2020-01-28 09:08:17 +00:00
|
|
|
"Dep node id do not match in cargo metadata, ignoring {}",
|
|
|
|
dep_node.pkg
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2021-05-24 16:31:43 +00:00
|
|
|
let dep = PackageDependency { name: dep_node.name.clone(), pkg, kind };
|
2019-01-10 19:21:14 +00:00
|
|
|
packages[source].dependencies.push(dep);
|
|
|
|
}
|
2021-01-21 11:12:19 +00:00
|
|
|
packages[source].active_features.extend(node.features);
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 12:27:29 +00:00
|
|
|
let workspace_root =
|
|
|
|
AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
|
2021-01-28 15:33:02 +00:00
|
|
|
|
2021-07-18 08:29:22 +00:00
|
|
|
CargoWorkspace { packages, targets, workspace_root }
|
2021-07-17 20:43:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 08:54:51 +00:00
|
|
|
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
|
2019-01-10 19:21:14 +00:00
|
|
|
self.packages.iter().map(|(id, _pkg)| id)
|
|
|
|
}
|
2019-02-05 22:10:49 +00:00
|
|
|
|
2020-06-24 11:34:24 +00:00
|
|
|
pub fn target_by_root(&self, root: &AbsPath) -> Option<Target> {
|
2020-03-19 16:53:31 +00:00
|
|
|
self.packages()
|
2020-06-24 11:34:24 +00:00
|
|
|
.filter_map(|pkg| self[pkg].targets.iter().find(|&&it| &self[it].root == root))
|
2020-03-19 16:53:31 +00:00
|
|
|
.next()
|
|
|
|
.copied()
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
2020-01-10 21:41:52 +00:00
|
|
|
|
2020-06-24 13:52:07 +00:00
|
|
|
pub fn workspace_root(&self) -> &AbsPath {
|
2020-01-10 21:41:52 +00:00
|
|
|
&self.workspace_root
|
|
|
|
}
|
2020-03-31 07:39:04 +00:00
|
|
|
|
|
|
|
pub fn package_flag(&self, package: &PackageData) -> String {
|
|
|
|
if self.is_unique(&*package.name) {
|
|
|
|
package.name.clone()
|
|
|
|
} else {
|
2020-04-02 07:00:44 +00:00
|
|
|
format!("{}:{}", package.name, package.version)
|
2020-03-31 07:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_unique(&self, name: &str) -> bool {
|
|
|
|
self.packages.iter().filter(|(_, v)| v.name == name).count() == 1
|
|
|
|
}
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
2021-05-08 16:17:18 +00:00
|
|
|
|
2021-07-19 18:20:10 +00:00
|
|
|
fn rustc_discover_host_triple(cargo_toml: &ManifestPath) -> Option<String> {
|
2021-05-08 16:17:18 +00:00
|
|
|
let mut rustc = Command::new(toolchain::rustc());
|
2021-07-19 18:20:10 +00:00
|
|
|
rustc.current_dir(cargo_toml.parent()).arg("-vV");
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("Discovering host platform by {:?}", rustc);
|
2021-05-08 16:17:18 +00:00
|
|
|
match utf8_stdout(rustc) {
|
|
|
|
Ok(stdout) => {
|
|
|
|
let field = "host: ";
|
|
|
|
let target = stdout.lines().find_map(|l| l.strip_prefix(field));
|
|
|
|
if let Some(target) = target {
|
|
|
|
Some(target.to_string())
|
|
|
|
} else {
|
|
|
|
// If we fail to resolve the host platform, it's not the end of the world.
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::info!("rustc -vV did not report host platform, got:\n{}", stdout);
|
2021-05-08 16:17:18 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::warn!("Failed to discover host platform: {}", e);
|
2021-05-08 16:17:18 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 18:20:10 +00:00
|
|
|
fn cargo_config_build_target(cargo_toml: &ManifestPath) -> Option<String> {
|
2021-05-08 16:17:18 +00:00
|
|
|
let mut cargo_config = Command::new(toolchain::cargo());
|
2021-05-08 22:07:04 +00:00
|
|
|
cargo_config
|
2021-07-19 18:20:10 +00:00
|
|
|
.current_dir(cargo_toml.parent())
|
2021-05-08 22:07:04 +00:00
|
|
|
.args(&["-Z", "unstable-options", "config", "get", "build.target"])
|
|
|
|
.env("RUSTC_BOOTSTRAP", "1");
|
2021-05-08 16:17:18 +00:00
|
|
|
// if successful we receive `build.target = "target-triple"`
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("Discovering cargo config target by {:?}", cargo_config);
|
2021-05-08 16:17:18 +00:00
|
|
|
match utf8_stdout(cargo_config) {
|
|
|
|
Ok(stdout) => stdout
|
|
|
|
.strip_prefix("build.target = \"")
|
|
|
|
.and_then(|stdout| stdout.strip_suffix('"'))
|
|
|
|
.map(ToOwned::to_owned),
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|