@@ -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.
@@ -27,9 +27,15 @@ pub fn bare_rustc() -> Rustc {
27
27
#[ must_use]
28
28
pub struct Rustc {
29
29
cmd : Command ,
30
+ target : Option < String > ,
30
31
}
31
32
32
- crate :: macros:: impl_common_helpers!( Rustc ) ;
33
+ // Only fill in the target just before execution, so that it can be overridden.
34
+ crate :: macros:: impl_common_helpers!( Rustc , |rustc: & mut Rustc | {
35
+ if let Some ( target) = & rustc. target {
36
+ rustc. cmd. arg( & format!( "--target={target}" ) ) ;
37
+ }
38
+ } ) ;
33
39
34
40
pub fn rustc_path ( ) -> String {
35
41
env_var ( "RUSTC" )
@@ -46,19 +52,22 @@ impl Rustc {
46
52
// `rustc` invocation constructor methods
47
53
48
54
/// Construct a new `rustc` invocation. This will automatically set the library
49
- /// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
55
+ /// search path as `-L cwd()` and also the compilation target.
56
+ /// Use [`bare_rustc`] to avoid this.
50
57
#[ track_caller]
51
58
pub fn new ( ) -> Self {
52
59
let mut cmd = setup_common ( ) ;
53
60
cmd. arg ( "-L" ) . arg ( cwd ( ) ) ;
54
- Self { cmd }
61
+
62
+ // Automatically default to cross-compilation
63
+ Self { cmd, target : Some ( target ( ) ) }
55
64
}
56
65
57
66
/// Construct a bare `rustc` invocation with no flags set.
58
67
#[ track_caller]
59
68
pub fn bare ( ) -> Self {
60
69
let cmd = setup_common ( ) ;
61
- Self { cmd }
70
+ Self { cmd, target : None }
62
71
}
63
72
64
73
// Argument provider methods
@@ -234,8 +243,9 @@ impl Rustc {
234
243
235
244
/// Specify the target triple, or a path to a custom target json spec file.
236
245
pub fn target < S : AsRef < str > > ( & mut self , target : S ) -> & mut Self {
237
- let target = target. as_ref ( ) ;
238
- self . cmd . arg ( format ! ( "--target={target}" ) ) ;
246
+ // We store the target as a separate field, so that it can be specified multiple times.
247
+ // This is in particular useful to override the default target set in Rustc::new().
248
+ self . target = Some ( target. as_ref ( ) . to_string ( ) ) ;
239
249
self
240
250
}
241
251
0 commit comments