@@ -180,26 +180,46 @@ fn has_feature(feature: &str) -> bool {
180
180
}
181
181
182
182
/// Test whether the rustc at `var("RUSTC")` can compile the given code.
183
- fn can_compile ( code : & str ) -> bool {
183
+ fn can_compile < T : AsRef < str > > ( test : T ) -> bool {
184
184
use std:: process:: Stdio ;
185
+
185
186
let out_dir = var ( "OUT_DIR" ) . unwrap ( ) ;
186
187
let rustc = var ( "RUSTC" ) . unwrap ( ) ;
187
188
let target = var ( "TARGET" ) . unwrap ( ) ;
188
189
189
- let mut child = std:: process:: Command :: new ( rustc)
190
- . arg ( "--crate-type=rlib" ) // Don't require `main`.
190
+ let mut cmd = if let Ok ( wrapper) = var ( "CARGO_RUSTC_WRAPPER" ) {
191
+ let mut cmd = std:: process:: Command :: new ( wrapper) ;
192
+ // The wrapper's first argument is supposed to be the path to rustc.
193
+ cmd. arg ( rustc) ;
194
+ cmd
195
+ } else {
196
+ std:: process:: Command :: new ( rustc)
197
+ } ;
198
+
199
+ cmd. arg ( "--crate-type=rlib" ) // Don't require `main`.
191
200
. arg ( "--emit=metadata" ) // Do as little as possible but still parse.
192
201
. arg ( "--target" )
193
202
. arg ( target)
194
203
. arg ( "--out-dir" )
195
- . arg ( out_dir) // Put the output somewhere inconsequential.
204
+ . arg ( out_dir) ; // Put the output somewhere inconsequential.
205
+
206
+ // If Cargo wants to set RUSTFLAGS, use that.
207
+ if let Ok ( rustflags) = var ( "CARGO_ENCODED_RUSTFLAGS" ) {
208
+ if !rustflags. is_empty ( ) {
209
+ for arg in rustflags. split ( '\x1f' ) {
210
+ cmd. arg ( arg) ;
211
+ }
212
+ }
213
+ }
214
+
215
+ let mut child = cmd
196
216
. arg ( "-" ) // Read from stdin.
197
217
. stdin ( Stdio :: piped ( ) ) // Stdin is a pipe.
198
- . stderr ( Stdio :: null ( ) )
218
+ . stderr ( Stdio :: null ( ) ) // Errors from feature detection aren't interesting and can be confusing.
199
219
. spawn ( )
200
220
. unwrap ( ) ;
201
221
202
- writeln ! ( child. stdin. take( ) . unwrap( ) , "{}" , code ) . unwrap ( ) ;
222
+ writeln ! ( child. stdin. take( ) . unwrap( ) , "{}" , test . as_ref ( ) ) . unwrap ( ) ;
203
223
204
224
child. wait ( ) . unwrap ( ) . success ( )
205
225
}
0 commit comments