16
16
//! graph of `Unit`s, which capture these properties.
17
17
18
18
use crate :: core:: compiler:: Unit ;
19
- use crate :: core:: compiler:: { BuildContext , CompileMode , Context , Kind } ;
19
+ use crate :: core:: compiler:: { BuildContext , CompileMode , Kind } ;
20
20
use crate :: core:: dependency:: Kind as DepKind ;
21
21
use crate :: core:: package:: Downloads ;
22
22
use crate :: core:: profiles:: UnitFor ;
@@ -25,25 +25,24 @@ use crate::CargoResult;
25
25
use log:: trace;
26
26
use std:: collections:: { HashMap , HashSet } ;
27
27
28
- struct State < ' a , ' cfg , ' tmp > {
29
- cx : & ' tmp mut Context < ' a , ' cfg > ,
28
+ struct State < ' a , ' cfg > {
29
+ bcx : & ' a BuildContext < ' a , ' cfg > ,
30
30
waiting_on_download : HashSet < PackageId > ,
31
31
downloads : Downloads < ' a , ' cfg > ,
32
+ unit_dependencies : HashMap < Unit < ' a > , Vec < Unit < ' a > > > ,
33
+ package_cache : HashMap < PackageId , & ' a Package > ,
32
34
}
33
35
34
36
pub fn build_unit_dependencies < ' a , ' cfg > (
35
- cx : & mut Context < ' a , ' cfg > ,
37
+ bcx : & ' a BuildContext < ' a , ' cfg > ,
36
38
roots : & [ Unit < ' a > ] ,
37
- ) -> CargoResult < ( ) > {
38
- assert ! (
39
- cx. unit_dependencies. is_empty( ) ,
40
- "can only build unit deps once"
41
- ) ;
42
-
39
+ ) -> CargoResult < HashMap < Unit < ' a > , Vec < Unit < ' a > > > > {
43
40
let mut state = State {
44
- downloads : cx . bcx . packages . enable_download ( ) ? ,
45
- cx ,
41
+ bcx,
42
+ downloads : bcx . packages . enable_download ( ) ? ,
46
43
waiting_on_download : HashSet :: new ( ) ,
44
+ unit_dependencies : HashMap :: new ( ) ,
45
+ package_cache : HashMap :: new ( ) ,
47
46
} ;
48
47
49
48
loop {
@@ -56,7 +55,7 @@ pub fn build_unit_dependencies<'a, 'cfg>(
56
55
// cleared, and avoid building the lib thrice (once with `panic`, once
57
56
// without, once for `--test`). In particular, the lib included for
58
57
// Doc tests and examples are `Build` mode here.
59
- let unit_for = if unit. mode . is_any_test ( ) || state. cx . bcx . build_config . test ( ) {
58
+ let unit_for = if unit. mode . is_any_test ( ) || state. bcx . build_config . test ( ) {
60
59
UnitFor :: new_test ( )
61
60
} else if unit. target . is_custom_build ( ) {
62
61
// This normally doesn't happen, except `clean` aggressively
@@ -73,32 +72,30 @@ pub fn build_unit_dependencies<'a, 'cfg>(
73
72
74
73
if !state. waiting_on_download . is_empty ( ) {
75
74
state. finish_some_downloads ( ) ?;
76
- state. cx . unit_dependencies . clear ( ) ;
75
+ state. unit_dependencies . clear ( ) ;
77
76
} else {
78
77
break ;
79
78
}
80
79
}
81
80
82
81
connect_run_custom_build_deps ( & mut state) ;
83
82
84
- trace ! ( "ALL UNIT DEPENDENCIES {:#?}" , state. cx. unit_dependencies) ;
85
-
86
- record_units_requiring_metadata ( state. cx ) ;
83
+ trace ! ( "ALL UNIT DEPENDENCIES {:#?}" , state. unit_dependencies) ;
87
84
88
85
// Dependencies are used in tons of places throughout the backend, many of
89
86
// which affect the determinism of the build itself. As a result be sure
90
87
// that dependency lists are always sorted to ensure we've always got a
91
88
// deterministic output.
92
- for list in state. cx . unit_dependencies . values_mut ( ) {
89
+ for list in state. unit_dependencies . values_mut ( ) {
93
90
list. sort ( ) ;
94
91
}
95
92
96
- Ok ( ( ) )
93
+ Ok ( state . unit_dependencies )
97
94
}
98
95
99
- fn deps_of < ' a , ' cfg , ' tmp > (
96
+ fn deps_of < ' a , ' cfg > (
100
97
unit : & Unit < ' a > ,
101
- state : & mut State < ' a , ' cfg , ' tmp > ,
98
+ state : & mut State < ' a , ' cfg > ,
102
99
unit_for : UnitFor ,
103
100
) -> CargoResult < ( ) > {
104
101
// Currently the `unit_dependencies` map does not include `unit_for`. This should
@@ -107,10 +104,10 @@ fn deps_of<'a, 'cfg, 'tmp>(
107
104
// `TestDependency`. `CustomBuild` should also be fine since if the
108
105
// requested unit's settings are the same as `Any`, `CustomBuild` can't
109
106
// affect anything else in the hierarchy.
110
- if !state. cx . unit_dependencies . contains_key ( unit) {
107
+ if !state. unit_dependencies . contains_key ( unit) {
111
108
let unit_deps = compute_deps ( unit, state, unit_for) ?;
112
109
let to_insert: Vec < _ > = unit_deps. iter ( ) . map ( |& ( unit, _) | unit) . collect ( ) ;
113
- state. cx . unit_dependencies . insert ( * unit, to_insert) ;
110
+ state. unit_dependencies . insert ( * unit, to_insert) ;
114
111
for ( unit, unit_for) in unit_deps {
115
112
deps_of ( & unit, state, unit_for) ?;
116
113
}
@@ -122,19 +119,19 @@ fn deps_of<'a, 'cfg, 'tmp>(
122
119
/// for that package.
123
120
/// This returns a `Vec` of `(Unit, UnitFor)` pairs. The `UnitFor`
124
121
/// is the profile type that should be used for dependencies of the unit.
125
- fn compute_deps < ' a , ' cfg , ' tmp > (
122
+ fn compute_deps < ' a , ' cfg > (
126
123
unit : & Unit < ' a > ,
127
- state : & mut State < ' a , ' cfg , ' tmp > ,
124
+ state : & mut State < ' a , ' cfg > ,
128
125
unit_for : UnitFor ,
129
126
) -> CargoResult < Vec < ( Unit < ' a > , UnitFor ) > > {
130
127
if unit. mode . is_run_custom_build ( ) {
131
- return compute_deps_custom_build ( unit, state. cx . bcx ) ;
128
+ return compute_deps_custom_build ( unit, state. bcx ) ;
132
129
} else if unit. mode . is_doc ( ) {
133
130
// Note: this does not include doc test.
134
131
return compute_deps_doc ( unit, state) ;
135
132
}
136
133
137
- let bcx = state. cx . bcx ;
134
+ let bcx = state. bcx ;
138
135
let id = unit. pkg . package_id ( ) ;
139
136
let deps = bcx. resolve . deps ( id) . filter ( |& ( _id, deps) | {
140
137
assert ! ( !deps. is_empty( ) ) ;
@@ -289,11 +286,11 @@ fn compute_deps_custom_build<'a, 'cfg>(
289
286
}
290
287
291
288
/// Returns the dependencies necessary to document a package.
292
- fn compute_deps_doc < ' a , ' cfg , ' tmp > (
289
+ fn compute_deps_doc < ' a , ' cfg > (
293
290
unit : & Unit < ' a > ,
294
- state : & mut State < ' a , ' cfg , ' tmp > ,
291
+ state : & mut State < ' a , ' cfg > ,
295
292
) -> CargoResult < Vec < ( Unit < ' a > , UnitFor ) > > {
296
- let bcx = state. cx . bcx ;
293
+ let bcx = state. bcx ;
297
294
let deps = bcx
298
295
. resolve
299
296
. deps ( unit. pkg . package_id ( ) )
@@ -438,7 +435,7 @@ fn new_unit<'a>(
438
435
///
439
436
/// Here we take the entire `deps` map and add more dependencies from execution
440
437
/// of one build script to execution of another build script.
441
- fn connect_run_custom_build_deps ( state : & mut State < ' _ , ' _ , ' _ > ) {
438
+ fn connect_run_custom_build_deps ( state : & mut State < ' _ , ' _ > ) {
442
439
let mut new_deps = Vec :: new ( ) ;
443
440
444
441
{
@@ -448,7 +445,7 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_, '_>) {
448
445
// have the build script as the key and the library would be in the
449
446
// value's set.
450
447
let mut reverse_deps = HashMap :: new ( ) ;
451
- for ( unit, deps) in state. cx . unit_dependencies . iter ( ) {
448
+ for ( unit, deps) in state. unit_dependencies . iter ( ) {
452
449
for dep in deps {
453
450
if dep. mode == CompileMode :: RunCustomBuild {
454
451
reverse_deps
@@ -469,7 +466,6 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_, '_>) {
469
466
// `dep_build_script` to manufacture an appropriate build script unit to
470
467
// depend on.
471
468
for unit in state
472
- . cx
473
469
. unit_dependencies
474
470
. keys ( )
475
471
. filter ( |k| k. mode == CompileMode :: RunCustomBuild )
@@ -481,13 +477,13 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_, '_>) {
481
477
482
478
let to_add = reverse_deps
483
479
. iter ( )
484
- . flat_map ( |reverse_dep| state. cx . unit_dependencies [ reverse_dep] . iter ( ) )
480
+ . flat_map ( |reverse_dep| state. unit_dependencies [ reverse_dep] . iter ( ) )
485
481
. filter ( |other| {
486
482
other. pkg != unit. pkg
487
483
&& other. target . linkable ( )
488
484
&& other. pkg . manifest ( ) . links ( ) . is_some ( )
489
485
} )
490
- . filter_map ( |other| dep_build_script ( other, state. cx . bcx ) . map ( |p| p. 0 ) )
486
+ . filter_map ( |other| dep_build_script ( other, state. bcx ) . map ( |p| p. 0 ) )
491
487
. collect :: < HashSet < _ > > ( ) ;
492
488
493
489
if !to_add. is_empty ( ) {
@@ -499,38 +495,23 @@ fn connect_run_custom_build_deps(state: &mut State<'_, '_, '_>) {
499
495
// And finally, add in all the missing dependencies!
500
496
for ( unit, new_deps) in new_deps {
501
497
state
502
- . cx
503
498
. unit_dependencies
504
499
. get_mut ( & unit)
505
500
. unwrap ( )
506
501
. extend ( new_deps) ;
507
502
}
508
503
}
509
504
510
- /// Records the list of units which are required to emit metadata.
511
- ///
512
- /// Units which depend only on the metadata of others requires the others to
513
- /// actually produce metadata, so we'll record that here.
514
- fn record_units_requiring_metadata ( cx : & mut Context < ' _ , ' _ > ) {
515
- for ( key, deps) in cx. unit_dependencies . iter ( ) {
516
- for dep in deps {
517
- if cx. only_requires_rmeta ( key, dep) {
518
- cx. rmeta_required . insert ( * dep) ;
519
- }
520
- }
521
- }
522
- }
523
-
524
- impl < ' a , ' cfg , ' tmp > State < ' a , ' cfg , ' tmp > {
505
+ impl < ' a , ' cfg > State < ' a , ' cfg > {
525
506
fn get ( & mut self , id : PackageId ) -> CargoResult < Option < & ' a Package > > {
526
- if let Some ( pkg) = self . cx . package_cache . get ( & id) {
507
+ if let Some ( pkg) = self . package_cache . get ( & id) {
527
508
return Ok ( Some ( pkg) ) ;
528
509
}
529
510
if !self . waiting_on_download . insert ( id) {
530
511
return Ok ( None ) ;
531
512
}
532
513
if let Some ( pkg) = self . downloads . start ( id) ? {
533
- self . cx . package_cache . insert ( id, pkg) ;
514
+ self . package_cache . insert ( id, pkg) ;
534
515
self . waiting_on_download . remove ( & id) ;
535
516
return Ok ( Some ( pkg) ) ;
536
517
}
@@ -550,7 +531,7 @@ impl<'a, 'cfg, 'tmp> State<'a, 'cfg, 'tmp> {
550
531
loop {
551
532
let pkg = self . downloads . wait ( ) ?;
552
533
self . waiting_on_download . remove ( & pkg. package_id ( ) ) ;
553
- self . cx . package_cache . insert ( pkg. package_id ( ) , pkg) ;
534
+ self . package_cache . insert ( pkg. package_id ( ) , pkg) ;
554
535
555
536
// Arbitrarily choose that 5 or more packages concurrently download
556
537
// is a good enough number to "fill the network pipe". If we have
0 commit comments