@@ -180,26 +180,46 @@ fn has_feature(feature: &str) -> bool {
180180}
181181
182182/// 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 {
184184 use std:: process:: Stdio ;
185+
185186 let out_dir = var ( "OUT_DIR" ) . unwrap ( ) ;
186187 let rustc = var ( "RUSTC" ) . unwrap ( ) ;
187188 let target = var ( "TARGET" ) . unwrap ( ) ;
188189
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`.
191200 . arg ( "--emit=metadata" ) // Do as little as possible but still parse.
192201 . arg ( "--target" )
193202 . arg ( target)
194203 . 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
196216 . arg ( "-" ) // Read from stdin.
197217 . 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.
199219 . spawn ( )
200220 . unwrap ( ) ;
201221
202- writeln ! ( child. stdin. take( ) . unwrap( ) , "{}" , code ) . unwrap ( ) ;
222+ writeln ! ( child. stdin. take( ) . unwrap( ) , "{}" , test . as_ref ( ) ) . unwrap ( ) ;
203223
204224 child. wait ( ) . unwrap ( ) . success ( )
205225}
0 commit comments