@@ -376,9 +376,10 @@ fn cargo_to_crate_graph(
376
376
cfg_options. insert_atom ( "debug_assertions" . into ( ) ) ;
377
377
378
378
let mut pkg_crates = FxHashMap :: default ( ) ;
379
-
379
+ let mut has_private = false ;
380
380
// Next, create crates for each package, target pair
381
381
for pkg in cargo. packages ( ) {
382
+ has_private |= cargo[ pkg] . metadata . rustc_private ;
382
383
let mut lib_tgt = None ;
383
384
for & tgt in cargo[ pkg] . targets . iter ( ) {
384
385
if let Some ( file_id) = load ( & cargo[ tgt] . root ) {
@@ -441,89 +442,88 @@ fn cargo_to_crate_graph(
441
442
442
443
let mut rustc_pkg_crates = FxHashMap :: default ( ) ;
443
444
444
- // If the user provided a path to rustc sources, we add all the rustc_private crates
445
- // and create dependencies on them for the crates which opt-in to that
446
- if let Some ( rustc_workspace) = rustc {
447
- // rustc-dev crates start from 'rustc_driver'
448
- // Therefore, we collect all crates which are transitive dependencies of rustc_driver
449
- if let Some ( root_pkg) = rustc_workspace
450
- . packages ( )
451
- . find ( |package| rustc_workspace[ * package] . name == "rustc_driver" )
452
- {
453
- let mut queue = VecDeque :: new ( ) ;
454
- queue. push_back ( root_pkg) ;
455
- while let Some ( pkg) = queue. pop_front ( ) {
456
- if rustc_pkg_crates. contains_key ( & pkg) {
457
- continue ;
458
- }
459
- for dep in & rustc_workspace[ pkg] . dependencies {
460
- queue. push_back ( dep. pkg ) ;
461
- }
462
- for & tgt in rustc_workspace[ pkg] . targets . iter ( ) {
463
- if rustc_workspace[ tgt] . kind != TargetKind :: Lib {
445
+ if has_private {
446
+ // If the user provided a path to rustc sources, we add all the rustc_private crates
447
+ // and create dependencies on them for the crates which opt-in to that
448
+ if let Some ( rustc_workspace) = rustc {
449
+ // rustc-dev crates start from 'rustc_driver'
450
+ // We want to collect all crates which are transitive dependencies of rustc_driver
451
+ if let Some ( root_pkg) = rustc_workspace
452
+ . packages ( )
453
+ . find ( |package| rustc_workspace[ * package] . name == "rustc_driver" )
454
+ {
455
+ let mut queue = VecDeque :: new ( ) ;
456
+ queue. push_back ( root_pkg) ;
457
+ while let Some ( pkg) = queue. pop_front ( ) {
458
+ // Don't duplicate packages
459
+ if rustc_pkg_crates. contains_key ( & pkg) {
464
460
continue ;
465
461
}
466
- if let Some ( file_id) = load ( & rustc_workspace[ tgt] . root ) {
467
- let crate_id = add_target_crate_root (
468
- & mut crate_graph,
469
- & rustc_workspace[ pkg] ,
470
- rustc_build_data_map. and_then ( |it| it. get ( & rustc_workspace[ pkg] . id ) ) ,
471
- & cfg_options,
472
- proc_macro_loader,
473
- file_id,
474
- ) ;
475
- pkg_to_lib_crate. insert ( pkg, crate_id) ;
476
- // Add dependencies on the core / std / alloc for rustc
477
- for ( name, krate) in public_deps. iter ( ) {
478
- add_dep ( & mut crate_graph, crate_id, name. clone ( ) , * krate) ;
462
+ for dep in & rustc_workspace[ pkg] . dependencies {
463
+ queue. push_back ( dep. pkg ) ;
464
+ }
465
+ for & tgt in rustc_workspace[ pkg] . targets . iter ( ) {
466
+ if rustc_workspace[ tgt] . kind != TargetKind :: Lib {
467
+ continue ;
468
+ }
469
+ if let Some ( file_id) = load ( & rustc_workspace[ tgt] . root ) {
470
+ let crate_id = add_target_crate_root (
471
+ & mut crate_graph,
472
+ & rustc_workspace[ pkg] ,
473
+ rustc_build_data_map
474
+ . and_then ( |it| it. get ( & rustc_workspace[ pkg] . id ) ) ,
475
+ & cfg_options,
476
+ proc_macro_loader,
477
+ file_id,
478
+ ) ;
479
+ pkg_to_lib_crate. insert ( pkg, crate_id) ;
480
+ // Add dependencies on the core / std / alloc for rustc
481
+ for ( name, krate) in public_deps. iter ( ) {
482
+ add_dep ( & mut crate_graph, crate_id, name. clone ( ) , * krate) ;
483
+ }
484
+ rustc_pkg_crates. entry ( pkg) . or_insert_with ( Vec :: new) . push ( crate_id) ;
479
485
}
480
- rustc_pkg_crates. entry ( pkg) . or_insert_with ( Vec :: new) . push ( crate_id) ;
481
486
}
482
487
}
483
488
}
484
- }
485
- // Now add a dep edge from all targets of upstream to the lib
486
- // target of downstream.
487
- for pkg in rustc_pkg_crates . keys ( ) . copied ( ) {
488
- for dep in rustc_workspace [ pkg ] . dependencies . iter ( ) {
489
- let name = CrateName :: new ( & dep. name ) . unwrap ( ) ;
490
- if let Some ( & to ) = pkg_to_lib_crate . get ( & dep . pkg ) {
491
- for & from in rustc_pkg_crates . get ( & pkg ) . into_iter ( ) . flatten ( ) {
492
- add_dep ( & mut crate_graph , from , name . clone ( ) , to ) ;
489
+ // Now add a dep edge from all targets of upstream to the lib
490
+ // target of downstream.
491
+ for pkg in rustc_pkg_crates . keys ( ) . copied ( ) {
492
+ for dep in rustc_workspace [ pkg ] . dependencies . iter ( ) {
493
+ let name = CrateName :: new ( & dep . name ) . unwrap ( ) ;
494
+ if let Some ( & to ) = pkg_to_lib_crate . get ( & dep. pkg ) {
495
+ for & from in rustc_pkg_crates . get ( & pkg) . into_iter ( ) . flatten ( ) {
496
+ add_dep ( & mut crate_graph , from , name . clone ( ) , to ) ;
497
+ }
493
498
}
494
499
}
495
500
}
496
- }
497
501
498
- // Add dependencies for all crates which opt in to rustc_private libraries
499
- for dep in rustc_workspace. packages ( ) {
500
- let name = CrateName :: normalize_dashes ( & rustc_workspace[ dep] . name ) ;
501
-
502
- if let Some ( & to) = pkg_to_lib_crate. get ( & dep) {
503
- for pkg in cargo. packages ( ) {
504
- let package = & cargo[ pkg] ;
505
- if matches ! (
506
- ( package. is_member, package. metadata. rustc_private) ,
507
- ( true , Some ( false ) ) | ( false , Some ( false ) ) | ( false , None )
508
- ) {
509
- continue ;
510
- }
511
- for & from in pkg_crates. get ( & pkg) . into_iter ( ) . flatten ( ) {
512
- // Avoid creating duplicate dependencies
513
- if !crate_graph[ from] . dependencies . iter ( ) . any ( |d| d. name == name) {
514
- add_dep ( & mut crate_graph, from, name. clone ( ) , to) ;
515
- } else {
516
- eprintln ! (
517
- "Skipped {} for {:?}" ,
518
- & name, & crate_graph[ from] . display_name
519
- ) ;
502
+ // Add dependencies for all crates which opt in to rustc_private libraries
503
+ for dep in rustc_workspace. packages ( ) {
504
+ let name = CrateName :: normalize_dashes ( & rustc_workspace[ dep] . name ) ;
505
+
506
+ if let Some ( & to) = pkg_to_lib_crate. get ( & dep) {
507
+ for pkg in cargo. packages ( ) {
508
+ let package = & cargo[ pkg] ;
509
+ if !package. metadata . rustc_private {
510
+ continue ;
511
+ }
512
+ for & from in pkg_crates. get ( & pkg) . into_iter ( ) . flatten ( ) {
513
+ // Avoid creating duplicate dependencies
514
+ if !crate_graph[ from] . dependencies . iter ( ) . any ( |d| d. name == name) {
515
+ add_dep ( & mut crate_graph, from, name. clone ( ) , to) ;
516
+ } else {
517
+ eprintln ! (
518
+ "Skipped {} for {:?}" ,
519
+ & name, & crate_graph[ from] . display_name
520
+ ) ;
521
+ }
520
522
}
521
523
}
522
524
}
523
525
}
524
526
}
525
- } else {
526
- eprintln ! ( "No cargo workspace" ) ;
527
527
}
528
528
crate_graph
529
529
}
0 commit comments