2019-09-30 08:58:53 +00:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
2019-02-05 22:10:49 +00:00
|
|
|
mod cargo_workspace;
|
2021-01-05 18:02:13 +00:00
|
|
|
mod cfg_flag;
|
2020-06-24 12:57:37 +00:00
|
|
|
mod project_json;
|
2019-02-05 22:10:49 +00:00
|
|
|
mod sysroot;
|
2020-11-13 15:44:48 +00:00
|
|
|
mod workspace;
|
2019-02-05 22:10:49 +00:00
|
|
|
|
2019-03-05 21:29:23 +00:00
|
|
|
use std::{
|
2020-11-13 15:44:48 +00:00
|
|
|
fs::{read_dir, ReadDir},
|
2020-06-24 13:52:07 +00:00
|
|
|
io,
|
2020-07-23 16:57:27 +00:00
|
|
|
process::Command,
|
2019-03-05 21:29:23 +00:00
|
|
|
};
|
2019-02-05 22:10:49 +00:00
|
|
|
|
2020-02-13 10:10:50 +00:00
|
|
|
use anyhow::{bail, Context, Result};
|
2020-08-13 08:32:19 +00:00
|
|
|
use paths::{AbsPath, AbsPathBuf};
|
2020-11-13 15:44:48 +00:00
|
|
|
use rustc_hash::FxHashSet;
|
2019-03-21 08:43:47 +00:00
|
|
|
|
2019-02-05 22:10:49 +00:00
|
|
|
pub use crate::{
|
2021-01-05 18:02:13 +00:00
|
|
|
cargo_workspace::{
|
|
|
|
CargoConfig, CargoWorkspace, Package, PackageData, PackageDependency, Target, TargetData,
|
|
|
|
TargetKind,
|
|
|
|
},
|
2020-06-24 13:52:07 +00:00
|
|
|
project_json::{ProjectJson, ProjectJsonData},
|
2019-02-05 22:10:49 +00:00
|
|
|
sysroot::Sysroot,
|
2020-11-13 15:44:48 +00:00
|
|
|
workspace::{PackageRoot, ProjectWorkspace},
|
2019-02-05 22:10:49 +00:00
|
|
|
};
|
2020-07-23 16:57:27 +00:00
|
|
|
|
2020-08-13 10:07:28 +00:00
|
|
|
pub use proc_macro_api::ProcMacroClient;
|
2019-02-05 22:10:49 +00:00
|
|
|
|
2020-06-03 09:44:51 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
|
2020-06-03 10:05:50 +00:00
|
|
|
pub enum ProjectManifest {
|
2020-06-24 11:34:24 +00:00
|
|
|
ProjectJson(AbsPathBuf),
|
|
|
|
CargoToml(AbsPathBuf),
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 10:05:50 +00:00
|
|
|
impl ProjectManifest {
|
2020-06-24 11:34:24 +00:00
|
|
|
pub fn from_manifest_file(path: AbsPathBuf) -> Result<ProjectManifest> {
|
2020-04-16 20:02:10 +00:00
|
|
|
if path.ends_with("rust-project.json") {
|
2020-06-03 10:05:50 +00:00
|
|
|
return Ok(ProjectManifest::ProjectJson(path));
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
|
|
|
if path.ends_with("Cargo.toml") {
|
2020-06-03 10:05:50 +00:00
|
|
|
return Ok(ProjectManifest::CargoToml(path));
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
|
|
|
bail!("project root must point to Cargo.toml or rust-project.json: {}", path.display())
|
2019-08-19 12:41:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:34:24 +00:00
|
|
|
pub fn discover_single(path: &AbsPath) -> Result<ProjectManifest> {
|
2020-06-03 10:05:50 +00:00
|
|
|
let mut candidates = ProjectManifest::discover(path)?;
|
2020-04-16 20:19:38 +00:00
|
|
|
let res = match candidates.pop() {
|
|
|
|
None => bail!("no projects"),
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
|
|
|
if !candidates.is_empty() {
|
|
|
|
bail!("more than one project")
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2020-06-24 11:34:24 +00:00
|
|
|
pub fn discover(path: &AbsPath) -> io::Result<Vec<ProjectManifest>> {
|
2020-05-08 23:51:59 +00:00
|
|
|
if let Some(project_json) = find_in_parent_dirs(path, "rust-project.json") {
|
2020-06-03 10:05:50 +00:00
|
|
|
return Ok(vec![ProjectManifest::ProjectJson(project_json)]);
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
2020-04-16 20:19:38 +00:00
|
|
|
return find_cargo_toml(path)
|
2020-06-03 10:05:50 +00:00
|
|
|
.map(|paths| paths.into_iter().map(ProjectManifest::CargoToml).collect());
|
2020-04-16 20:02:10 +00:00
|
|
|
|
2020-06-24 11:34:24 +00:00
|
|
|
fn find_cargo_toml(path: &AbsPath) -> io::Result<Vec<AbsPathBuf>> {
|
2020-05-08 23:51:59 +00:00
|
|
|
match find_in_parent_dirs(path, "Cargo.toml") {
|
|
|
|
Some(it) => Ok(vec![it]),
|
|
|
|
None => Ok(find_cargo_toml_in_child_dir(read_dir(path)?)),
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
2020-05-08 23:51:59 +00:00
|
|
|
}
|
2020-04-16 20:02:10 +00:00
|
|
|
|
2020-06-24 11:34:24 +00:00
|
|
|
fn find_in_parent_dirs(path: &AbsPath, target_file_name: &str) -> Option<AbsPathBuf> {
|
2020-05-08 23:51:59 +00:00
|
|
|
if path.ends_with(target_file_name) {
|
2020-06-24 11:34:24 +00:00
|
|
|
return Some(path.to_path_buf());
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut curr = Some(path);
|
2020-05-08 23:51:59 +00:00
|
|
|
|
2020-04-16 20:02:10 +00:00
|
|
|
while let Some(path) = curr {
|
2020-05-08 23:51:59 +00:00
|
|
|
let candidate = path.join(target_file_name);
|
2020-04-16 20:02:10 +00:00
|
|
|
if candidate.exists() {
|
|
|
|
return Some(candidate);
|
|
|
|
}
|
|
|
|
curr = path.parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2020-06-24 11:34:24 +00:00
|
|
|
fn find_cargo_toml_in_child_dir(entities: ReadDir) -> Vec<AbsPathBuf> {
|
2020-04-16 20:02:10 +00:00
|
|
|
// Only one level down to avoid cycles the easy way and stop a runaway scan with large projects
|
2020-05-08 23:51:59 +00:00
|
|
|
entities
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.map(|it| it.path().join("Cargo.toml"))
|
|
|
|
.filter(|it| it.exists())
|
2020-06-24 11:34:24 +00:00
|
|
|
.map(AbsPathBuf::assert)
|
2020-05-08 23:51:59 +00:00
|
|
|
.collect()
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-03 09:44:51 +00:00
|
|
|
|
2020-06-24 11:34:24 +00:00
|
|
|
pub fn discover_all(paths: &[impl AsRef<AbsPath>]) -> Vec<ProjectManifest> {
|
2020-06-03 09:44:51 +00:00
|
|
|
let mut res = paths
|
|
|
|
.iter()
|
2020-06-03 10:05:50 +00:00
|
|
|
.filter_map(|it| ProjectManifest::discover(it.as_ref()).ok())
|
2020-06-03 09:44:51 +00:00
|
|
|
.flatten()
|
|
|
|
.collect::<FxHashSet<_>>()
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
res.sort();
|
|
|
|
res
|
|
|
|
}
|
2020-04-16 20:02:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 16:57:27 +00:00
|
|
|
fn utf8_stdout(mut cmd: Command) -> Result<String> {
|
2020-05-08 12:54:29 +00:00
|
|
|
let output = cmd.output().with_context(|| format!("{:?} failed", cmd))?;
|
|
|
|
if !output.status.success() {
|
2020-05-08 16:53:53 +00:00
|
|
|
match String::from_utf8(output.stderr) {
|
|
|
|
Ok(stderr) if !stderr.is_empty() => {
|
|
|
|
bail!("{:?} failed, {}\nstderr:\n{}", cmd, output.status, stderr)
|
|
|
|
}
|
|
|
|
_ => bail!("{:?} failed, {}", cmd, output.status),
|
|
|
|
}
|
2020-05-08 12:54:29 +00:00
|
|
|
}
|
2020-07-23 16:57:27 +00:00
|
|
|
let stdout = String::from_utf8(output.stdout)?;
|
2020-08-25 15:00:08 +00:00
|
|
|
Ok(stdout.trim().to_string())
|
2020-05-08 12:54:29 +00:00
|
|
|
}
|