1
- use std:: collections:: HashMap ;
2
- use std:: path:: { Path , PathBuf } ;
3
- use std:: str;
4
-
5
- use cargo_platform:: Cfg ;
6
- use log:: debug;
7
-
8
1
use crate :: core:: compiler:: unit:: UnitInterner ;
9
- use crate :: core:: compiler:: { BuildConfig , BuildOutput , Kind , Unit } ;
2
+ use crate :: core:: compiler:: CompileTarget ;
3
+ use crate :: core:: compiler:: { BuildConfig , BuildOutput , CompileKind , Unit } ;
10
4
use crate :: core:: profiles:: Profiles ;
11
- use crate :: core:: { Dependency , Workspace } ;
5
+ use crate :: core:: { Dependency , InternedString , Workspace } ;
12
6
use crate :: core:: { PackageId , PackageSet } ;
13
7
use crate :: util:: errors:: CargoResult ;
14
- use crate :: util:: { profile, Config , Rustc } ;
8
+ use crate :: util:: { Config , Rustc } ;
9
+ use cargo_platform:: Cfg ;
10
+ use std:: collections:: HashMap ;
11
+ use std:: path:: { Path , PathBuf } ;
12
+ use std:: str;
15
13
16
14
mod target_info;
17
15
pub use self :: target_info:: { FileFlavor , TargetInfo } ;
@@ -33,15 +31,24 @@ pub struct BuildContext<'a, 'cfg> {
33
31
pub extra_compiler_args : HashMap < Unit < ' a > , Vec < String > > ,
34
32
pub packages : & ' a PackageSet < ' cfg > ,
35
33
36
- /// Information about the compiler.
37
- pub rustc : Rustc ,
38
- /// Build information for the host arch.
39
- pub host_config : TargetConfig ,
40
- /// Build information for the target.
41
- pub target_config : TargetConfig ,
42
- pub target_info : TargetInfo ,
43
- pub host_info : TargetInfo ,
34
+ /// Source of interning new units as they're created.
44
35
pub units : & ' a UnitInterner < ' a > ,
36
+
37
+ /// Information about the compiler that we've detected on the local system.
38
+ pub rustc : Rustc ,
39
+
40
+ /// Build information for the "host", which is information about when
41
+ /// `rustc` is invoked without a `--target` flag. This is used for
42
+ /// procedural macros, build scripts, etc.
43
+ host_config : TargetConfig ,
44
+ host_info : TargetInfo ,
45
+
46
+ /// Build information for targets that we're building for. This will be
47
+ /// empty if the `--target` flag is not passed, and currently also only ever
48
+ /// has at most one entry, but eventually we'd like to support multi-target
49
+ /// builds with Cargo.
50
+ target_config : HashMap < CompileTarget , TargetConfig > ,
51
+ target_info : HashMap < CompileTarget , TargetInfo > ,
45
52
}
46
53
47
54
impl < ' a , ' cfg > BuildContext < ' a , ' cfg > {
@@ -57,19 +64,26 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
57
64
let rustc = config. load_global_rustc ( Some ( ws) ) ?;
58
65
59
66
let host_config = TargetConfig :: new ( config, & rustc. host ) ?;
60
- let target_config = match build_config. requested_target . as_ref ( ) {
61
- Some ( triple) => TargetConfig :: new ( config, triple) ?,
62
- None => host_config. clone ( ) ,
63
- } ;
64
- let ( host_info, target_info) = {
65
- let _p = profile:: start ( "BuildContext::probe_target_info" ) ;
66
- debug ! ( "probe_target_info" ) ;
67
- let host_info =
68
- TargetInfo :: new ( config, & build_config. requested_target , & rustc, Kind :: Host ) ?;
69
- let target_info =
70
- TargetInfo :: new ( config, & build_config. requested_target , & rustc, Kind :: Target ) ?;
71
- ( host_info, target_info)
72
- } ;
67
+ let host_info = TargetInfo :: new (
68
+ config,
69
+ build_config. requested_kind ,
70
+ & rustc,
71
+ CompileKind :: Host ,
72
+ ) ?;
73
+ let mut target_config = HashMap :: new ( ) ;
74
+ let mut target_info = HashMap :: new ( ) ;
75
+ if let CompileKind :: Target ( target) = build_config. requested_kind {
76
+ target_config. insert ( target, TargetConfig :: new ( config, target. short_name ( ) ) ?) ;
77
+ target_info. insert (
78
+ target,
79
+ TargetInfo :: new (
80
+ config,
81
+ build_config. requested_kind ,
82
+ & rustc,
83
+ CompileKind :: Target ( target) ,
84
+ ) ?,
85
+ ) ;
86
+ }
73
87
74
88
Ok ( BuildContext {
75
89
ws,
@@ -88,38 +102,31 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
88
102
}
89
103
90
104
/// Whether a dependency should be compiled for the host or target platform,
91
- /// specified by `Kind `.
92
- pub fn dep_platform_activated ( & self , dep : & Dependency , kind : Kind ) -> bool {
105
+ /// specified by `CompileKind `.
106
+ pub fn dep_platform_activated ( & self , dep : & Dependency , kind : CompileKind ) -> bool {
93
107
// If this dependency is only available for certain platforms,
94
108
// make sure we're only enabling it for that platform.
95
109
let platform = match dep. platform ( ) {
96
110
Some ( p) => p,
97
111
None => return true ,
98
112
} ;
99
- let ( name, info) = match kind {
100
- Kind :: Host => ( self . host_triple ( ) , & self . host_info ) ,
101
- Kind :: Target => ( self . target_triple ( ) , & self . target_info ) ,
102
- } ;
103
- platform. matches ( name, info. cfg ( ) )
113
+ let name = kind. short_name ( self ) ;
114
+ platform. matches ( & name, self . cfg ( kind) )
104
115
}
105
116
106
117
/// Gets the user-specified linker for a particular host or target.
107
- pub fn linker ( & self , kind : Kind ) -> Option < & Path > {
118
+ pub fn linker ( & self , kind : CompileKind ) -> Option < & Path > {
108
119
self . target_config ( kind) . linker . as_ref ( ) . map ( |s| s. as_ref ( ) )
109
120
}
110
121
111
122
/// Gets the user-specified `ar` program for a particular host or target.
112
- pub fn ar ( & self , kind : Kind ) -> Option < & Path > {
123
+ pub fn ar ( & self , kind : CompileKind ) -> Option < & Path > {
113
124
self . target_config ( kind) . ar . as_ref ( ) . map ( |s| s. as_ref ( ) )
114
125
}
115
126
116
127
/// Gets the list of `cfg`s printed out from the compiler for the specified kind.
117
- pub fn cfg ( & self , kind : Kind ) -> & [ Cfg ] {
118
- let info = match kind {
119
- Kind :: Host => & self . host_info ,
120
- Kind :: Target => & self . target_info ,
121
- } ;
122
- info. cfg ( )
128
+ pub fn cfg ( & self , kind : CompileKind ) -> & [ Cfg ] {
129
+ self . info ( kind) . cfg ( )
123
130
}
124
131
125
132
/// Gets the host architecture triple.
@@ -128,23 +135,15 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
128
135
/// - machine: x86_64,
129
136
/// - hardware-platform: unknown,
130
137
/// - operating system: linux-gnu.
131
- pub fn host_triple ( & self ) -> & str {
132
- & self . rustc . host
133
- }
134
-
135
- pub fn target_triple ( & self ) -> & str {
136
- self . build_config
137
- . requested_target
138
- . as_ref ( )
139
- . map ( |s| s. as_str ( ) )
140
- . unwrap_or_else ( || self . host_triple ( ) )
138
+ pub fn host_triple ( & self ) -> InternedString {
139
+ self . rustc . host
141
140
}
142
141
143
142
/// Gets the target configuration for a particular host or target.
144
- fn target_config ( & self , kind : Kind ) -> & TargetConfig {
143
+ pub fn target_config ( & self , kind : CompileKind ) -> & TargetConfig {
145
144
match kind {
146
- Kind :: Host => & self . host_config ,
147
- Kind :: Target => & self . target_config ,
145
+ CompileKind :: Host => & self . host_config ,
146
+ CompileKind :: Target ( s ) => & self . target_config [ & s ] ,
148
147
}
149
148
}
150
149
@@ -165,10 +164,10 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
165
164
pkg. source_id ( ) . is_path ( ) || self . config . extra_verbose ( )
166
165
}
167
166
168
- fn info ( & self , kind : Kind ) -> & TargetInfo {
167
+ pub fn info ( & self , kind : CompileKind ) -> & TargetInfo {
169
168
match kind {
170
- Kind :: Host => & self . host_info ,
171
- Kind :: Target => & self . target_info ,
169
+ CompileKind :: Host => & self . host_info ,
170
+ CompileKind :: Target ( s ) => & self . target_info [ & s ] ,
172
171
}
173
172
}
174
173
@@ -180,11 +179,8 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
180
179
///
181
180
/// `lib_name` is the `links` library name and `kind` is whether it is for
182
181
/// Host or Target.
183
- pub fn script_override ( & self , lib_name : & str , kind : Kind ) -> Option < & BuildOutput > {
184
- match kind {
185
- Kind :: Host => self . host_config . overrides . get ( lib_name) ,
186
- Kind :: Target => self . target_config . overrides . get ( lib_name) ,
187
- }
182
+ pub fn script_override ( & self , lib_name : & str , kind : CompileKind ) -> Option < & BuildOutput > {
183
+ self . target_config ( kind) . overrides . get ( lib_name)
188
184
}
189
185
}
190
186
0 commit comments