1
1
use std:: collections:: HashMap ;
2
- use std:: env;
3
2
use std:: path:: { Path , PathBuf } ;
4
3
use std:: str;
5
4
@@ -9,9 +8,9 @@ use crate::core::profiles::Profiles;
9
8
use crate :: core:: { Dependency , Workspace } ;
10
9
use crate :: core:: { PackageId , PackageSet , Resolve } ;
11
10
use crate :: util:: errors:: CargoResult ;
12
- use crate :: util:: { profile, Cfg , CfgExpr , Config , Rustc } ;
13
-
14
- use super :: { BuildConfig , BuildOutput , Kind , Unit } ;
11
+ use crate :: util:: { profile, Cfg , Config , Rustc } ;
12
+ use crate :: core :: compiler :: { Unit , Kind , BuildConfig , BuildOutput } ;
13
+ use crate :: core :: compiler :: unit :: UnitInterner ;
15
14
16
15
mod target_info;
17
16
pub use self :: target_info:: { FileFlavor , TargetInfo } ;
@@ -38,6 +37,7 @@ pub struct BuildContext<'a, 'cfg: 'a> {
38
37
pub target_config : TargetConfig ,
39
38
pub target_info : TargetInfo ,
40
39
pub host_info : TargetInfo ,
40
+ pub units : & ' a UnitInterner < ' a > ,
41
41
}
42
42
43
43
impl < ' a , ' cfg > BuildContext < ' a , ' cfg > {
@@ -48,6 +48,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
48
48
config : & ' cfg Config ,
49
49
build_config : & ' a BuildConfig ,
50
50
profiles : & ' a Profiles ,
51
+ units : & ' a UnitInterner < ' a > ,
51
52
extra_compiler_args : HashMap < Unit < ' a > , Vec < String > > ,
52
53
) -> CargoResult < BuildContext < ' a , ' cfg > > {
53
54
let mut rustc = config. load_global_rustc ( Some ( ws) ) ?;
@@ -83,6 +84,7 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
83
84
build_config,
84
85
profiles,
85
86
extra_compiler_args,
87
+ units,
86
88
} )
87
89
}
88
90
@@ -157,26 +159,12 @@ impl<'a, 'cfg> BuildContext<'a, 'cfg> {
157
159
self . build_config . jobs
158
160
}
159
161
160
- pub fn rustflags_args ( & self , unit : & Unit < ' _ > ) -> CargoResult < Vec < String > > {
161
- env_args (
162
- self . config ,
163
- & self . build_config . requested_target ,
164
- self . host_triple ( ) ,
165
- self . info ( unit. kind ) . cfg ( ) ,
166
- unit. kind ,
167
- "RUSTFLAGS" ,
168
- )
162
+ pub fn rustflags_args ( & self , unit : & Unit < ' _ > ) -> & [ String ] {
163
+ & self . info ( unit. kind ) . rustflags
169
164
}
170
165
171
- pub fn rustdocflags_args ( & self , unit : & Unit < ' _ > ) -> CargoResult < Vec < String > > {
172
- env_args (
173
- self . config ,
174
- & self . build_config . requested_target ,
175
- self . host_triple ( ) ,
176
- self . info ( unit. kind ) . cfg ( ) ,
177
- unit. kind ,
178
- "RUSTDOCFLAGS" ,
179
- )
166
+ pub fn rustdocflags_args ( & self , unit : & Unit < ' _ > ) -> & [ String ] {
167
+ & self . info ( unit. kind ) . rustdocflags
180
168
}
181
169
182
170
pub fn show_warnings ( & self , pkg : PackageId ) -> bool {
@@ -292,124 +280,3 @@ impl TargetConfig {
292
280
Ok ( ret)
293
281
}
294
282
}
295
-
296
- /// Acquire extra flags to pass to the compiler from various locations.
297
- ///
298
- /// The locations are:
299
- ///
300
- /// - the `RUSTFLAGS` environment variable
301
- ///
302
- /// then if this was not found
303
- ///
304
- /// - `target.*.rustflags` from the manifest (Cargo.toml)
305
- /// - `target.cfg(..).rustflags` from the manifest
306
- ///
307
- /// then if neither of these were found
308
- ///
309
- /// - `build.rustflags` from the manifest
310
- ///
311
- /// Note that if a `target` is specified, no args will be passed to host code (plugins, build
312
- /// scripts, ...), even if it is the same as the target.
313
- fn env_args (
314
- config : & Config ,
315
- requested_target : & Option < String > ,
316
- host_triple : & str ,
317
- target_cfg : Option < & [ Cfg ] > ,
318
- kind : Kind ,
319
- name : & str ,
320
- ) -> CargoResult < Vec < String > > {
321
- // We *want* to apply RUSTFLAGS only to builds for the
322
- // requested target architecture, and not to things like build
323
- // scripts and plugins, which may be for an entirely different
324
- // architecture. Cargo's present architecture makes it quite
325
- // hard to only apply flags to things that are not build
326
- // scripts and plugins though, so we do something more hacky
327
- // instead to avoid applying the same RUSTFLAGS to multiple targets
328
- // arches:
329
- //
330
- // 1) If --target is not specified we just apply RUSTFLAGS to
331
- // all builds; they are all going to have the same target.
332
- //
333
- // 2) If --target *is* specified then we only apply RUSTFLAGS
334
- // to compilation units with the Target kind, which indicates
335
- // it was chosen by the --target flag.
336
- //
337
- // This means that, e.g., even if the specified --target is the
338
- // same as the host, build scripts in plugins won't get
339
- // RUSTFLAGS.
340
- let compiling_with_target = requested_target. is_some ( ) ;
341
- let is_target_kind = kind == Kind :: Target ;
342
-
343
- if compiling_with_target && !is_target_kind {
344
- // This is probably a build script or plugin and we're
345
- // compiling with --target. In this scenario there are
346
- // no rustflags we can apply.
347
- return Ok ( Vec :: new ( ) ) ;
348
- }
349
-
350
- // First try RUSTFLAGS from the environment
351
- if let Ok ( a) = env:: var ( name) {
352
- let args = a
353
- . split ( ' ' )
354
- . map ( str:: trim)
355
- . filter ( |s| !s. is_empty ( ) )
356
- . map ( str:: to_string) ;
357
- return Ok ( args. collect ( ) ) ;
358
- }
359
-
360
- let mut rustflags = Vec :: new ( ) ;
361
-
362
- let name = name
363
- . chars ( )
364
- . flat_map ( |c| c. to_lowercase ( ) )
365
- . collect :: < String > ( ) ;
366
- // Then the target.*.rustflags value...
367
- let target = requested_target
368
- . as_ref ( )
369
- . map ( |s| s. as_str ( ) )
370
- . unwrap_or ( host_triple) ;
371
- let key = format ! ( "target.{}.{}" , target, name) ;
372
- if let Some ( args) = config. get_list_or_split_string ( & key) ? {
373
- let args = args. val . into_iter ( ) ;
374
- rustflags. extend ( args) ;
375
- }
376
- // ...including target.'cfg(...)'.rustflags
377
- if let Some ( target_cfg) = target_cfg {
378
- if let Some ( table) = config. get_table ( "target" ) ? {
379
- let cfgs = table
380
- . val
381
- . keys ( )
382
- . filter ( |key| CfgExpr :: matches_key ( key, target_cfg) ) ;
383
-
384
- // Note that we may have multiple matching `[target]` sections and
385
- // because we're passing flags to the compiler this can affect
386
- // cargo's caching and whether it rebuilds. Ensure a deterministic
387
- // ordering through sorting for now. We may perhaps one day wish to
388
- // ensure a deterministic ordering via the order keys were defined
389
- // in files perhaps.
390
- let mut cfgs = cfgs. collect :: < Vec < _ > > ( ) ;
391
- cfgs. sort ( ) ;
392
-
393
- for n in cfgs {
394
- let key = format ! ( "target.{}.{}" , n, name) ;
395
- if let Some ( args) = config. get_list_or_split_string ( & key) ? {
396
- let args = args. val . into_iter ( ) ;
397
- rustflags. extend ( args) ;
398
- }
399
- }
400
- }
401
- }
402
-
403
- if !rustflags. is_empty ( ) {
404
- return Ok ( rustflags) ;
405
- }
406
-
407
- // Then the `build.rustflags` value.
408
- let key = format ! ( "build.{}" , name) ;
409
- if let Some ( args) = config. get_list_or_split_string ( & key) ? {
410
- let args = args. val . into_iter ( ) ;
411
- return Ok ( args. collect ( ) ) ;
412
- }
413
-
414
- Ok ( Vec :: new ( ) )
415
- }
0 commit comments