2019-09-30 08:58:53 +00:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2020-03-19 16:53:31 +00:00
|
|
|
use std::{
|
2020-03-26 20:26:34 +00:00
|
|
|
ffi::OsStr,
|
2020-03-19 16:53:31 +00:00
|
|
|
ops,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2019-01-10 19:21:14 +00:00
|
|
|
|
2020-02-13 10:10:50 +00:00
|
|
|
use anyhow::{Context, Result};
|
2020-03-21 21:30:33 +00:00
|
|
|
use cargo_metadata::{BuildScript, CargoOpt, Message, MetadataCommand, PackageId};
|
2020-03-19 15:00:11 +00:00
|
|
|
use ra_arena::{Arena, Idx};
|
2020-03-16 12:43:29 +00:00
|
|
|
use ra_cargo_watch::run_cargo;
|
2019-02-13 19:31:27 +00:00
|
|
|
use ra_db::Edition;
|
2019-07-04 20:05:17 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2019-12-13 10:16:34 +00:00
|
|
|
use serde::Deserialize;
|
2019-01-10 19:21:14 +00:00
|
|
|
|
2019-02-05 22:10:49 +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.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct CargoWorkspace {
|
2020-03-19 15:00:11 +00:00
|
|
|
packages: Arena<PackageData>,
|
|
|
|
targets: Arena<TargetData>,
|
2020-01-10 21:41:52 +00:00
|
|
|
workspace_root: PathBuf,
|
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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 15:45:14 +00:00
|
|
|
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
|
2019-12-13 10:16:34 +00:00
|
|
|
#[serde(rename_all = "camelCase", default)]
|
|
|
|
pub struct CargoFeatures {
|
|
|
|
/// 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
|
|
|
|
|
|
|
/// Runs cargo check on launch to figure out the correct values of OUT_DIR
|
|
|
|
pub load_out_dirs_from_check: bool,
|
2019-12-13 10:16:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-14 15:45:14 +00:00
|
|
|
impl Default for CargoFeatures {
|
|
|
|
fn default() -> Self {
|
2020-03-16 12:43:29 +00:00
|
|
|
CargoFeatures {
|
|
|
|
no_default_features: false,
|
|
|
|
all_features: true,
|
|
|
|
features: Vec::new(),
|
|
|
|
load_out_dirs_from_check: false,
|
|
|
|
}
|
2019-12-14 15:45:14 +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
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-03-19 16:53:31 +00:00
|
|
|
pub struct PackageData {
|
2020-03-31 07:39:04 +00:00
|
|
|
pub id: String,
|
2020-03-19 16:53:31 +00:00
|
|
|
pub name: String,
|
|
|
|
pub manifest: PathBuf,
|
|
|
|
pub targets: Vec<Target>,
|
|
|
|
pub is_member: bool,
|
|
|
|
pub dependencies: Vec<PackageDependency>,
|
|
|
|
pub edition: Edition,
|
|
|
|
pub features: Vec<String>,
|
|
|
|
pub out_dir: Option<PathBuf>,
|
2020-03-18 12:56:46 +00:00
|
|
|
pub proc_macro_dylib_path: Option<PathBuf>,
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct PackageDependency {
|
|
|
|
pub pkg: Package,
|
2019-02-09 09:51:06 +00:00
|
|
|
pub name: String,
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-03-19 16:53:31 +00:00
|
|
|
pub struct TargetData {
|
|
|
|
pub package: Package,
|
|
|
|
pub name: String,
|
|
|
|
pub root: PathBuf,
|
|
|
|
pub kind: TargetKind,
|
|
|
|
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,
|
|
|
|
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,
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 16:53:31 +00:00
|
|
|
impl PackageData {
|
|
|
|
pub fn root(&self) -> &Path {
|
|
|
|
self.manifest.parent().unwrap()
|
2019-11-24 10:33:12 +00:00
|
|
|
}
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CargoWorkspace {
|
2019-12-13 10:16:34 +00:00
|
|
|
pub fn from_cargo_metadata(
|
|
|
|
cargo_toml: &Path,
|
|
|
|
cargo_features: &CargoFeatures,
|
|
|
|
) -> Result<CargoWorkspace> {
|
2019-01-26 20:16:15 +00:00
|
|
|
let mut meta = MetadataCommand::new();
|
2019-12-13 10:16:34 +00:00
|
|
|
meta.manifest_path(cargo_toml);
|
|
|
|
if cargo_features.all_features {
|
|
|
|
meta.features(CargoOpt::AllFeatures);
|
|
|
|
} else if cargo_features.no_default_features {
|
|
|
|
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
|
|
|
|
// https://github.com/oli-obk/cargo_metadata/issues/79
|
|
|
|
meta.features(CargoOpt::NoDefaultFeatures);
|
2020-02-18 12:53:02 +00:00
|
|
|
} else if !cargo_features.features.is_empty() {
|
2019-12-13 10:16:34 +00:00
|
|
|
meta.features(CargoOpt::SomeFeatures(cargo_features.features.clone()));
|
|
|
|
}
|
2019-01-26 20:16:15 +00:00
|
|
|
if let Some(parent) = cargo_toml.parent() {
|
|
|
|
meta.current_dir(parent);
|
|
|
|
}
|
2020-02-13 10:10:50 +00:00
|
|
|
let meta = meta.exec().with_context(|| {
|
|
|
|
format!("Failed to run `cargo metadata --manifest-path {}`", cargo_toml.display())
|
|
|
|
})?;
|
2020-03-16 12:43:29 +00:00
|
|
|
|
|
|
|
let mut out_dir_by_id = FxHashMap::default();
|
2020-03-18 12:56:46 +00:00
|
|
|
let mut proc_macro_dylib_paths = FxHashMap::default();
|
2020-03-16 12:43:29 +00:00
|
|
|
if cargo_features.load_out_dirs_from_check {
|
2020-03-18 12:56:46 +00:00
|
|
|
let resources = load_extern_resources(cargo_toml, cargo_features);
|
|
|
|
out_dir_by_id = resources.out_dirs;
|
|
|
|
proc_macro_dylib_paths = resources.proc_dylib_paths;
|
2020-03-16 12:43:29 +00:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
let ws_members = &meta.workspace_members;
|
|
|
|
|
|
|
|
for meta_pkg in meta.packages {
|
2019-11-12 10:53:31 +00:00
|
|
|
let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg;
|
|
|
|
let is_member = ws_members.contains(&id);
|
2020-02-13 10:10:50 +00:00
|
|
|
let edition = edition
|
|
|
|
.parse::<Edition>()
|
|
|
|
.with_context(|| format!("Failed to parse edition {}", edition))?;
|
2019-01-10 19:21:14 +00:00
|
|
|
let pkg = packages.alloc(PackageData {
|
2019-11-12 10:53:31 +00:00
|
|
|
name,
|
2020-03-31 07:39:04 +00:00
|
|
|
id: id.to_string(),
|
2019-11-12 10:53:31 +00:00
|
|
|
manifest: manifest_path,
|
2019-01-10 19:21:14 +00:00
|
|
|
targets: Vec::new(),
|
|
|
|
is_member,
|
2019-11-12 10:53:31 +00:00
|
|
|
edition,
|
2019-01-10 19:21:14 +00:00
|
|
|
dependencies: Vec::new(),
|
2019-10-02 18:02:53 +00:00
|
|
|
features: Vec::new(),
|
2020-03-16 12:43:29 +00:00
|
|
|
out_dir: out_dir_by_id.get(&id).cloned(),
|
2020-03-18 12:56:46 +00:00
|
|
|
proc_macro_dylib_path: proc_macro_dylib_paths.get(&id).cloned(),
|
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);
|
2019-01-10 19:21:14 +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,
|
2019-06-03 14:21:08 +00:00
|
|
|
name: meta_tgt.name,
|
2019-01-10 19:21:14 +00:00
|
|
|
root: meta_tgt.src_path.clone(),
|
|
|
|
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");
|
|
|
|
for 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 => {
|
|
|
|
log::error!("Node id do not match in cargo metadata, ignoring {}", node.id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2019-01-10 19:21:14 +00:00
|
|
|
for dep_node in node.deps {
|
2020-01-28 09:08:17 +00:00
|
|
|
let pkg = match pkg_by_id.get(&dep_node.pkg) {
|
|
|
|
Some(&pkg) => pkg,
|
|
|
|
None => {
|
|
|
|
log::error!(
|
|
|
|
"Dep node id do not match in cargo metadata, ignoring {}",
|
|
|
|
dep_node.pkg
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let dep = PackageDependency { name: dep_node.name, pkg };
|
2019-01-10 19:21:14 +00:00
|
|
|
packages[source].dependencies.push(dep);
|
|
|
|
}
|
2019-10-02 18:02:53 +00:00
|
|
|
packages[source].features.extend(node.features);
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-13 17:45:21 +00:00
|
|
|
Ok(CargoWorkspace { packages, targets, workspace_root: meta.workspace_root })
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
2019-02-05 22:10:49 +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
|
|
|
|
2019-01-10 19:21:14 +00:00
|
|
|
pub fn target_by_root(&self, root: &Path) -> Option<Target> {
|
2020-03-19 16:53:31 +00:00
|
|
|
self.packages()
|
|
|
|
.filter_map(|pkg| self[pkg].targets.iter().find(|&&it| self[it].root == root))
|
|
|
|
.next()
|
|
|
|
.copied()
|
2019-01-10 19:21:14 +00:00
|
|
|
}
|
2020-01-10 21:41:52 +00:00
|
|
|
|
|
|
|
pub fn workspace_root(&self) -> &Path {
|
|
|
|
&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 {
|
|
|
|
package.id.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-03-16 12:43:29 +00:00
|
|
|
|
2020-03-18 12:56:46 +00:00
|
|
|
#[derive(Debug, Clone, Default)]
|
|
|
|
pub struct ExternResources {
|
|
|
|
out_dirs: FxHashMap<PackageId, PathBuf>,
|
|
|
|
proc_dylib_paths: FxHashMap<PackageId, PathBuf>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_extern_resources(cargo_toml: &Path, cargo_features: &CargoFeatures) -> ExternResources {
|
2020-03-16 12:43:29 +00:00
|
|
|
let mut args: Vec<String> = vec![
|
|
|
|
"check".to_string(),
|
|
|
|
"--message-format=json".to_string(),
|
|
|
|
"--manifest-path".to_string(),
|
2020-03-21 21:30:33 +00:00
|
|
|
cargo_toml.display().to_string(),
|
2020-03-16 12:43:29 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if cargo_features.all_features {
|
|
|
|
args.push("--all-features".to_string());
|
|
|
|
} else if cargo_features.no_default_features {
|
|
|
|
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
|
|
|
|
// https://github.com/oli-obk/cargo_metadata/issues/79
|
|
|
|
args.push("--no-default-features".to_string());
|
2020-03-21 21:30:33 +00:00
|
|
|
} else {
|
|
|
|
args.extend(cargo_features.features.iter().cloned());
|
2020-03-16 12:43:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 12:56:46 +00:00
|
|
|
let mut acc = ExternResources::default();
|
2020-03-21 21:30:33 +00:00
|
|
|
let res = run_cargo(&args, cargo_toml.parent(), &mut |message| {
|
2020-03-16 12:43:29 +00:00
|
|
|
match message {
|
2020-03-21 21:30:33 +00:00
|
|
|
Message::BuildScriptExecuted(BuildScript { package_id, out_dir, .. }) => {
|
2020-03-18 12:56:46 +00:00
|
|
|
acc.out_dirs.insert(package_id, out_dir);
|
2020-03-16 12:43:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 12:56:46 +00:00
|
|
|
Message::CompilerArtifact(message) => {
|
|
|
|
if message.target.kind.contains(&"proc-macro".to_string()) {
|
|
|
|
let package_id = message.package_id;
|
2020-03-26 20:26:34 +00:00
|
|
|
// Skip rmeta file
|
|
|
|
if let Some(filename) =
|
|
|
|
message.filenames.iter().filter(|name| is_dylib(name)).next()
|
|
|
|
{
|
2020-03-18 12:56:46 +00:00
|
|
|
acc.proc_dylib_paths.insert(package_id, filename.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 12:43:29 +00:00
|
|
|
Message::CompilerMessage(_) => (),
|
|
|
|
Message::Unknown => (),
|
|
|
|
}
|
|
|
|
true
|
|
|
|
});
|
|
|
|
|
2020-03-21 21:30:33 +00:00
|
|
|
if let Err(err) = res {
|
|
|
|
log::error!("Failed to load outdirs: {:?}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
acc
|
2020-03-16 12:43:29 +00:00
|
|
|
}
|
2020-03-26 20:26:34 +00:00
|
|
|
|
|
|
|
// FIXME: File a better way to know if it is a dylib
|
|
|
|
fn is_dylib(path: &Path) -> bool {
|
2020-03-27 04:15:38 +00:00
|
|
|
match path.extension().and_then(OsStr::to_str).map(|it| it.to_string().to_lowercase()) {
|
|
|
|
None => false,
|
|
|
|
Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
|
2020-03-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
}
|