Skip to content

Commit 3f72168

Browse files
bors[bot]matklad
andauthored
Merge #5878
5878: Prepare to share sysroot lowering code between Cargo & ProjectJSON r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 32be2d6 + 7fcda5a commit 3f72168

File tree

2 files changed

+73
-105
lines changed

2 files changed

+73
-105
lines changed

crates/project_model/src/lib.rs

Lines changed: 66 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -335,45 +335,8 @@ impl ProjectWorkspace {
335335
let mut cfg_options = CfgOptions::default();
336336
cfg_options.extend(get_rustc_cfg_options(target));
337337

338-
let sysroot_crates: FxHashMap<_, _> = sysroot
339-
.crates()
340-
.filter_map(|krate| {
341-
let file_id = load(&sysroot[krate].root)?;
342-
343-
let env = Env::default();
344-
let proc_macro = vec![];
345-
let name = sysroot[krate].name.clone();
346-
let crate_id = crate_graph.add_crate_root(
347-
file_id,
348-
Edition::Edition2018,
349-
Some(name),
350-
cfg_options.clone(),
351-
env,
352-
proc_macro,
353-
);
354-
Some((krate, crate_id))
355-
})
356-
.collect();
357-
358-
for from in sysroot.crates() {
359-
for &to in sysroot[from].deps.iter() {
360-
let name = &sysroot[to].name;
361-
if let (Some(&from), Some(&to)) =
362-
(sysroot_crates.get(&from), sysroot_crates.get(&to))
363-
{
364-
if crate_graph.add_dep(from, CrateName::new(name).unwrap(), to).is_err()
365-
{
366-
log::error!("cyclic dependency between sysroot crates")
367-
}
368-
}
369-
}
370-
}
371-
372-
let libcore = sysroot.core().and_then(|it| sysroot_crates.get(&it).copied());
373-
let liballoc = sysroot.alloc().and_then(|it| sysroot_crates.get(&it).copied());
374-
let libstd = sysroot.std().and_then(|it| sysroot_crates.get(&it).copied());
375-
let libproc_macro =
376-
sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied());
338+
let (public_deps, libproc_macro) =
339+
sysroot_to_crate_graph(&mut crate_graph, sysroot, &cfg_options, load);
377340

378341
let mut pkg_to_lib_crate = FxHashMap::default();
379342
let mut pkg_crates = FxHashMap::default();
@@ -424,14 +387,11 @@ impl ProjectWorkspace {
424387
}
425388
if cargo[tgt].is_proc_macro {
426389
if let Some(proc_macro) = libproc_macro {
427-
if crate_graph
428-
.add_dep(
429-
crate_id,
430-
CrateName::new("proc_macro").unwrap(),
431-
proc_macro,
432-
)
433-
.is_err()
434-
{
390+
if let Err(_) = crate_graph.add_dep(
391+
crate_id,
392+
CrateName::new("proc_macro").unwrap(),
393+
proc_macro,
394+
) {
435395
log::error!(
436396
"cyclic dependency on proc_macro for {}",
437397
&cargo[pkg].name
@@ -447,65 +407,33 @@ impl ProjectWorkspace {
447407
// Set deps to the core, std and to the lib target of the current package
448408
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
449409
if let Some((to, name)) = lib_tgt.clone() {
450-
if to != from
451-
&& crate_graph
452-
.add_dep(
453-
from,
454-
// For root projects with dashes in their name,
455-
// cargo metadata does not do any normalization,
456-
// so we do it ourselves currently
457-
CrateName::normalize_dashes(&name),
458-
to,
459-
)
460-
.is_err()
461-
{
462-
{
463-
log::error!(
464-
"cyclic dependency between targets of {}",
465-
&cargo[pkg].name
466-
)
467-
}
410+
// For root projects with dashes in their name,
411+
// cargo metadata does not do any normalization,
412+
// so we do it ourselves currently
413+
let name = CrateName::normalize_dashes(&name);
414+
if to != from && crate_graph.add_dep(from, name, to).is_err() {
415+
log::error!(
416+
"cyclic dependency between targets of {}",
417+
&cargo[pkg].name
418+
)
468419
}
469420
}
470-
// core is added as a dependency before std in order to
471-
// mimic rustcs dependency order
472-
if let Some(core) = libcore {
473-
if crate_graph
474-
.add_dep(from, CrateName::new("core").unwrap(), core)
475-
.is_err()
476-
{
421+
for (name, krate) in public_deps.iter() {
422+
if let Err(_) = crate_graph.add_dep(from, name.clone(), *krate) {
477423
log::error!("cyclic dependency on core for {}", &cargo[pkg].name)
478424
}
479425
}
480-
if let Some(alloc) = liballoc {
481-
if crate_graph
482-
.add_dep(from, CrateName::new("alloc").unwrap(), alloc)
483-
.is_err()
484-
{
485-
log::error!("cyclic dependency on alloc for {}", &cargo[pkg].name)
486-
}
487-
}
488-
if let Some(std) = libstd {
489-
if crate_graph
490-
.add_dep(from, CrateName::new("std").unwrap(), std)
491-
.is_err()
492-
{
493-
log::error!("cyclic dependency on std for {}", &cargo[pkg].name)
494-
}
495-
}
496426
}
497427
}
498428

499429
// Now add a dep edge from all targets of upstream to the lib
500430
// target of downstream.
501431
for pkg in cargo.packages() {
502432
for dep in cargo[pkg].dependencies.iter() {
433+
let name = CrateName::new(&dep.name).unwrap();
503434
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
504435
for &from in pkg_crates.get(&pkg).into_iter().flatten() {
505-
if crate_graph
506-
.add_dep(from, CrateName::new(&dep.name).unwrap(), to)
507-
.is_err()
508-
{
436+
if let Err(_) = crate_graph.add_dep(from, name.clone(), to) {
509437
log::error!(
510438
"cyclic dependency {} -> {}",
511439
&cargo[pkg].name,
@@ -563,3 +491,49 @@ fn utf8_stdout(mut cmd: Command) -> Result<String> {
563491
let stdout = String::from_utf8(output.stdout)?;
564492
Ok(stdout.trim().to_string())
565493
}
494+
495+
fn sysroot_to_crate_graph(
496+
crate_graph: &mut CrateGraph,
497+
sysroot: &Sysroot,
498+
cfg_options: &CfgOptions,
499+
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
500+
) -> (Vec<(CrateName, CrateId)>, Option<CrateId>) {
501+
let sysroot_crates: FxHashMap<_, _> = sysroot
502+
.crates()
503+
.filter_map(|krate| {
504+
let file_id = load(&sysroot[krate].root)?;
505+
506+
let env = Env::default();
507+
let proc_macro = vec![];
508+
let name = sysroot[krate].name.clone();
509+
let crate_id = crate_graph.add_crate_root(
510+
file_id,
511+
Edition::Edition2018,
512+
Some(name),
513+
cfg_options.clone(),
514+
env,
515+
proc_macro,
516+
);
517+
Some((krate, crate_id))
518+
})
519+
.collect();
520+
521+
for from in sysroot.crates() {
522+
for &to in sysroot[from].deps.iter() {
523+
let name = CrateName::new(&sysroot[to].name).unwrap();
524+
if let (Some(&from), Some(&to)) = (sysroot_crates.get(&from), sysroot_crates.get(&to)) {
525+
if let Err(_) = crate_graph.add_dep(from, name, to) {
526+
log::error!("cyclic dependency between sysroot crates")
527+
}
528+
}
529+
}
530+
}
531+
532+
let public_deps = sysroot
533+
.public_deps()
534+
.map(|(name, idx)| (CrateName::new(name).unwrap(), sysroot_crates[&idx]))
535+
.collect::<Vec<_>>();
536+
537+
let libproc_macro = sysroot.proc_macro().and_then(|it| sysroot_crates.get(&it).copied());
538+
(public_deps, libproc_macro)
539+
}

crates/project_model/src/sysroot.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,10 @@ impl ops::Index<SysrootCrate> for Sysroot {
3434
}
3535

3636
impl Sysroot {
37-
pub fn core(&self) -> Option<SysrootCrate> {
38-
self.by_name("core")
39-
}
40-
41-
pub fn alloc(&self) -> Option<SysrootCrate> {
42-
self.by_name("alloc")
43-
}
44-
45-
pub fn std(&self) -> Option<SysrootCrate> {
46-
self.by_name("std")
37+
pub fn public_deps(&self) -> impl Iterator<Item = (&'static str, SysrootCrate)> + '_ {
38+
// core is added as a dependency before std in order to
39+
// mimic rustcs dependency order
40+
vec!["core", "alloc", "std"].into_iter().filter_map(move |it| Some((it, self.by_name(it)?)))
4741
}
4842

4943
pub fn proc_macro(&self) -> Option<SysrootCrate> {
@@ -81,16 +75,16 @@ impl Sysroot {
8175
}
8276
}
8377

84-
if let Some(std) = sysroot.std() {
78+
if let Some(std) = sysroot.by_name("std") {
8579
for dep in STD_DEPS.trim().lines() {
8680
if let Some(dep) = sysroot.by_name(dep) {
8781
sysroot.crates[std].deps.push(dep)
8882
}
8983
}
9084
}
9185

92-
if let Some(alloc) = sysroot.alloc() {
93-
if let Some(core) = sysroot.core() {
86+
if let Some(alloc) = sysroot.by_name("alloc") {
87+
if let Some(core) = sysroot.by_name("core") {
9488
sysroot.crates[alloc].deps.push(core);
9589
}
9690
}

0 commit comments

Comments
 (0)