@@ -6,7 +6,7 @@ use crate::command::Command;
6
6
use crate :: env:: env_var;
7
7
use crate :: path_helpers:: cwd;
8
8
use crate :: util:: set_host_compiler_dylib_path;
9
- use crate :: { is_aix, is_darwin, is_msvc, is_windows, uname} ;
9
+ use crate :: { is_aix, is_darwin, is_msvc, is_windows, target , uname} ;
10
10
11
11
/// Construct a new `rustc` invocation. This will automatically set the library
12
12
/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
@@ -33,9 +33,15 @@ pub fn aux_build() -> Rustc {
33
33
#[ must_use]
34
34
pub struct Rustc {
35
35
cmd : Command ,
36
+ target : Option < String > ,
36
37
}
37
38
38
- crate :: macros:: impl_common_helpers!( Rustc ) ;
39
+ /// Only fill in the target just before execution, so that it can be overridden.
40
+ crate :: macros:: impl_common_helpers!( Rustc , |rustc: & mut Rustc | {
41
+ if let Some ( target) = & rustc. target {
42
+ rustc. cmd. arg( & format!( "--target={target}" ) ) ;
43
+ }
44
+ } ) ;
39
45
40
46
pub fn rustc_path ( ) -> String {
41
47
env_var ( "RUSTC" )
@@ -52,27 +58,30 @@ impl Rustc {
52
58
// `rustc` invocation constructor methods
53
59
54
60
/// Construct a new `rustc` invocation. This will automatically set the library
55
- /// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
61
+ /// search path as `-L cwd()` and also the compilation target.
62
+ /// Use [`bare_rustc`] to avoid this.
56
63
#[ track_caller]
57
64
pub fn new ( ) -> Self {
58
65
let mut cmd = setup_common ( ) ;
59
66
cmd. arg ( "-L" ) . arg ( cwd ( ) ) ;
60
- Self { cmd }
67
+
68
+ // Automatically default to cross-compilation
69
+ Self { cmd, target : Some ( target ( ) ) }
61
70
}
62
71
63
72
/// Construct a bare `rustc` invocation with no flags set.
64
73
#[ track_caller]
65
74
pub fn bare ( ) -> Self {
66
75
let cmd = setup_common ( ) ;
67
- Self { cmd }
76
+ Self { cmd, target : None }
68
77
}
69
78
70
79
/// Construct a new `rustc` invocation with `aux_build` preset (setting `--crate-type=lib`).
71
80
#[ track_caller]
72
81
pub fn new_aux_build ( ) -> Self {
73
82
let mut cmd = setup_common ( ) ;
74
83
cmd. arg ( "--crate-type=lib" ) ;
75
- Self { cmd }
84
+ Self { cmd, target : None }
76
85
}
77
86
78
87
// Argument provider methods
@@ -248,8 +257,9 @@ impl Rustc {
248
257
249
258
/// Specify the target triple, or a path to a custom target json spec file.
250
259
pub fn target < S : AsRef < str > > ( & mut self , target : S ) -> & mut Self {
251
- let target = target. as_ref ( ) ;
252
- self . cmd . arg ( format ! ( "--target={target}" ) ) ;
260
+ // We store the target as a separate field, so that it can be specified multiple times.
261
+ // This is in particular useful to override the default target set in Rustc::new().
262
+ self . target = Some ( target. as_ref ( ) . to_string ( ) ) ;
253
263
self
254
264
}
255
265
0 commit comments