@@ -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.
@@ -22,20 +22,20 @@ pub fn bare_rustc() -> Rustc {
2222 Rustc :: bare ( )
2323}
2424
25- /// Construct a new `rustc` aux-build invocation.
26- #[ track_caller]
27- pub fn aux_build ( ) -> Rustc {
28- Rustc :: new_aux_build ( )
29- }
30-
3125/// A `rustc` invocation builder.
3226#[ derive( Debug ) ]
3327#[ must_use]
3428pub struct Rustc {
3529 cmd : Command ,
30+ target : Option < String > ,
3631}
3732
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+ } ) ;
3939
4040pub fn rustc_path ( ) -> String {
4141 env_var ( "RUSTC" )
@@ -52,27 +52,22 @@ impl Rustc {
5252 // `rustc` invocation constructor methods
5353
5454 /// 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.
5657 #[ track_caller]
5758 pub fn new ( ) -> Self {
5859 let mut cmd = setup_common ( ) ;
5960 cmd. arg ( "-L" ) . arg ( cwd ( ) ) ;
60- Self { cmd }
61+
62+ // Automatically default to cross-compilation
63+ Self { cmd, target : Some ( target ( ) ) }
6164 }
6265
6366 /// Construct a bare `rustc` invocation with no flags set.
6467 #[ track_caller]
6568 pub fn bare ( ) -> Self {
6669 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 }
7671 }
7772
7873 // Argument provider methods
@@ -248,8 +243,9 @@ impl Rustc {
248243
249244 /// Specify the target triple, or a path to a custom target json spec file.
250245 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 ( ) ) ;
253249 self
254250 }
255251
0 commit comments