@@ -6,7 +6,7 @@ use crate::command::Command;
66use crate :: env:: env_var;
77use crate :: path_helpers:: cwd;
88use 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} ;
1010
1111/// Construct a new `rustc` invocation. This will automatically set the library
1212/// search path as `-L cwd()`. Use [`bare_rustc`] to avoid this.
@@ -27,9 +27,15 @@ pub fn bare_rustc() -> Rustc {
2727#[ must_use]
2828pub struct Rustc {
2929 cmd : Command ,
30+ target : Option < String > ,
3031}
3132
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+ } ) ;
3339
3440pub fn rustc_path ( ) -> String {
3541 env_var ( "RUSTC" )
@@ -46,19 +52,22 @@ impl Rustc {
4652 // `rustc` invocation constructor methods
4753
4854 /// 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.
5057 #[ track_caller]
5158 pub fn new ( ) -> Self {
5259 let mut cmd = setup_common ( ) ;
5360 cmd. arg ( "-L" ) . arg ( cwd ( ) ) ;
54- Self { cmd }
61+
62+ // Automatically default to cross-compilation
63+ Self { cmd, target : Some ( target ( ) ) }
5564 }
5665
5766 /// Construct a bare `rustc` invocation with no flags set.
5867 #[ track_caller]
5968 pub fn bare ( ) -> Self {
6069 let cmd = setup_common ( ) ;
61- Self { cmd }
70+ Self { cmd, target : None }
6271 }
6372
6473 // Argument provider methods
@@ -234,8 +243,9 @@ impl Rustc {
234243
235244 /// Specify the target triple, or a path to a custom target json spec file.
236245 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 ( ) ) ;
239249 self
240250 }
241251
0 commit comments