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-
81use 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 } ;
104use crate :: core:: profiles:: Profiles ;
11- use crate :: core:: { Dependency , Workspace } ;
5+ use crate :: core:: { Dependency , InternedString , Workspace } ;
126use crate :: core:: { PackageId , PackageSet } ;
137use 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;
1513
1614mod target_info;
1715pub use self :: target_info:: { FileFlavor , TargetInfo } ;
@@ -33,15 +31,24 @@ pub struct BuildContext<'a, 'cfg> {
3331 pub extra_compiler_args : HashMap < Unit < ' a > , Vec < String > > ,
3432 pub packages : & ' a PackageSet < ' cfg > ,
3533
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.
4435 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 > ,
4552}
4653
4754impl < ' a , ' cfg > BuildContext < ' a , ' cfg > {
@@ -57,19 +64,26 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
5764 let rustc = config. load_global_rustc ( Some ( ws) ) ?;
5865
5966 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+ }
7387
7488 Ok ( BuildContext {
7589 ws,
@@ -88,38 +102,31 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
88102 }
89103
90104 /// 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 {
93107 // If this dependency is only available for certain platforms,
94108 // make sure we're only enabling it for that platform.
95109 let platform = match dep. platform ( ) {
96110 Some ( p) => p,
97111 None => return true ,
98112 } ;
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) )
104115 }
105116
106117 /// 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 > {
108119 self . target_config ( kind) . linker . as_ref ( ) . map ( |s| s. as_ref ( ) )
109120 }
110121
111122 /// 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 > {
113124 self . target_config ( kind) . ar . as_ref ( ) . map ( |s| s. as_ref ( ) )
114125 }
115126
116127 /// 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 ( )
123130 }
124131
125132 /// Gets the host architecture triple.
@@ -128,23 +135,15 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
128135 /// - machine: x86_64,
129136 /// - hardware-platform: unknown,
130137 /// - 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
141140 }
142141
143142 /// 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 {
145144 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 ] ,
148147 }
149148 }
150149
@@ -165,10 +164,10 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
165164 pkg. source_id ( ) . is_path ( ) || self . config . extra_verbose ( )
166165 }
167166
168- fn info ( & self , kind : Kind ) -> & TargetInfo {
167+ pub fn info ( & self , kind : CompileKind ) -> & TargetInfo {
169168 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 ] ,
172171 }
173172 }
174173
@@ -180,11 +179,8 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
180179 ///
181180 /// `lib_name` is the `links` library name and `kind` is whether it is for
182181 /// 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)
188184 }
189185}
190186
0 commit comments