@@ -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.
@@ -22,20 +22,20 @@ pub fn bare_rustc() -> Rustc {
22
22
Rustc :: bare ( )
23
23
}
24
24
25
- /// Construct a new `rustc` aux-build invocation.
26
- #[ track_caller]
27
- pub fn aux_build ( ) -> Rustc {
28
- Rustc :: new_aux_build ( )
29
- }
30
-
31
25
/// A `rustc` invocation builder.
32
26
#[ derive( Debug ) ]
33
27
#[ must_use]
34
28
pub struct Rustc {
35
29
cmd : Command ,
30
+ target : Option < String > ,
36
31
}
37
32
38
- 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
+ } ) ;
39
39
40
40
pub fn rustc_path ( ) -> String {
41
41
env_var ( "RUSTC" )
@@ -52,27 +52,22 @@ impl Rustc {
52
52
// `rustc` invocation constructor methods
53
53
54
54
/// Construct a new `rustc` invocation. This will automatically set the library
55
- /// 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.
56
57
#[ track_caller]
57
58
pub fn new ( ) -> Self {
58
59
let mut cmd = setup_common ( ) ;
59
60
cmd. arg ( "-L" ) . arg ( cwd ( ) ) ;
60
- Self { cmd }
61
+
62
+ // Automatically default to cross-compilation
63
+ Self { cmd, target : Some ( target ( ) ) }
61
64
}
62
65
63
66
/// Construct a bare `rustc` invocation with no flags set.
64
67
#[ track_caller]
65
68
pub fn bare ( ) -> Self {
66
69
let cmd = setup_common ( ) ;
67
- Self { cmd }
68
- }
69
-
70
- /// Construct a new `rustc` invocation with `aux_build` preset (setting `--crate-type=lib`).
71
- #[ track_caller]
72
- pub fn new_aux_build ( ) -> Self {
73
- let mut cmd = setup_common ( ) ;
74
- cmd. arg ( "--crate-type=lib" ) ;
75
- Self { cmd }
70
+ Self { cmd, target : None }
76
71
}
77
72
78
73
// Argument provider methods
@@ -248,8 +243,9 @@ impl Rustc {
248
243
249
244
/// Specify the target triple, or a path to a custom target json spec file.
250
245
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}" ) ) ;
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 ( ) ) ;
253
249
self
254
250
}
255
251
0 commit comments