@@ -335,45 +335,8 @@ impl ProjectWorkspace {
335
335
let mut cfg_options = CfgOptions :: default ( ) ;
336
336
cfg_options. extend ( get_rustc_cfg_options ( target) ) ;
337
337
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) ;
377
340
378
341
let mut pkg_to_lib_crate = FxHashMap :: default ( ) ;
379
342
let mut pkg_crates = FxHashMap :: default ( ) ;
@@ -424,14 +387,11 @@ impl ProjectWorkspace {
424
387
}
425
388
if cargo[ tgt] . is_proc_macro {
426
389
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
+ ) {
435
395
log:: error!(
436
396
"cyclic dependency on proc_macro for {}" ,
437
397
& cargo[ pkg] . name
@@ -447,65 +407,33 @@ impl ProjectWorkspace {
447
407
// Set deps to the core, std and to the lib target of the current package
448
408
for & from in pkg_crates. get ( & pkg) . into_iter ( ) . flatten ( ) {
449
409
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
+ )
468
419
}
469
420
}
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) {
477
423
log:: error!( "cyclic dependency on core for {}" , & cargo[ pkg] . name)
478
424
}
479
425
}
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
- }
496
426
}
497
427
}
498
428
499
429
// Now add a dep edge from all targets of upstream to the lib
500
430
// target of downstream.
501
431
for pkg in cargo. packages ( ) {
502
432
for dep in cargo[ pkg] . dependencies . iter ( ) {
433
+ let name = CrateName :: new ( & dep. name ) . unwrap ( ) ;
503
434
if let Some ( & to) = pkg_to_lib_crate. get ( & dep. pkg ) {
504
435
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) {
509
437
log:: error!(
510
438
"cyclic dependency {} -> {}" ,
511
439
& cargo[ pkg] . name,
@@ -563,3 +491,49 @@ fn utf8_stdout(mut cmd: Command) -> Result<String> {
563
491
let stdout = String :: from_utf8 ( output. stdout ) ?;
564
492
Ok ( stdout. trim ( ) . to_string ( ) )
565
493
}
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
+ }
0 commit comments