add proc-macro crate type handling

This commit is contained in:
JasperDeSutter 2019-11-24 11:33:12 +01:00
parent ea3124c12a
commit 6a8b4f873a
3 changed files with 30 additions and 1 deletions

View file

@ -54,11 +54,13 @@ struct TargetData {
name: String,
root: PathBuf,
kind: TargetKind,
is_proc_macro: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TargetKind {
Bin,
/// Any kind of Cargo lib crate-type (dylib, rlib, proc-macro, ...).
Lib,
Example,
Test,
@ -74,6 +76,7 @@ impl TargetKind {
"test" => TargetKind::Test,
"bench" => TargetKind::Bench,
"example" => TargetKind::Example,
"proc-macro" => TargetKind::Lib,
_ if kind.contains("lib") => TargetKind::Lib,
_ => continue,
};
@ -123,6 +126,9 @@ impl Target {
pub fn kind(self, ws: &CargoWorkspace) -> TargetKind {
ws.targets[self].kind
}
pub fn is_proc_macro(self, ws: &CargoWorkspace) -> bool {
ws.targets[self].is_proc_macro
}
}
impl CargoWorkspace {
@ -155,11 +161,13 @@ impl CargoWorkspace {
let pkg_data = &mut packages[pkg];
pkg_by_id.insert(id, pkg);
for meta_tgt in meta_pkg.targets {
let is_proc_macro = meta_tgt.kind.as_slice() == &["proc-macro"];
let tgt = targets.alloc(TargetData {
pkg,
name: meta_tgt.name,
root: meta_tgt.src_path.clone(),
kind: TargetKind::new(meta_tgt.kind.as_slice()),
is_proc_macro,
});
pkg_data.targets.push(tgt);
}

View file

@ -210,6 +210,8 @@ impl ProjectWorkspace {
let libcore = sysroot.core().and_then(|it| sysroot_crates.get(&it).copied());
let libstd = sysroot.std().and_then(|it| sysroot_crates.get(&it).copied());
let libproc_macro =
sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied());
let mut pkg_to_lib_crate = FxHashMap::default();
let mut pkg_crates = FxHashMap::default();
@ -236,6 +238,21 @@ impl ProjectWorkspace {
lib_tgt = Some(crate_id);
pkg_to_lib_crate.insert(pkg, crate_id);
}
if tgt.is_proc_macro(&cargo) {
if let Some(proc_macro) = libproc_macro {
if let Err(_) = crate_graph.add_dep(
crate_id,
"proc_macro".into(),
proc_macro,
) {
log::error!(
"cyclic dependency on proc_macro for {}",
pkg.name(&cargo)
)
}
}
}
pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
}
}

View file

@ -35,6 +35,10 @@ impl Sysroot {
self.by_name("std")
}
pub fn proc_macro(&self) -> Option<SysrootCrate> {
self.by_name("proc_macro")
}
pub fn crates<'a>(&'a self) -> impl Iterator<Item = SysrootCrate> + ExactSizeIterator + 'a {
self.crates.iter().map(|(id, _data)| id)
}
@ -70,7 +74,7 @@ impl Sysroot {
}
}
if let Some(alloc) = sysroot.by_name("alloc") {
if let Some(core) = sysroot.by_name("core") {
if let Some(core) = sysroot.core() {
sysroot.crates[alloc].deps.push(core);
}
}