1
1
use std:: error:: Error ;
2
+ use std:: ffi:: OsString ;
2
3
use std:: process:: Command ;
3
4
4
5
fn output ( cmd : & mut Command ) -> Result < String , Box < dyn Error > > {
@@ -141,6 +142,17 @@ fn default_build_triple() -> Result<String, Box<dyn Error>> {
141
142
Ok ( format ! ( "{}-{}" , cputype, ostype) )
142
143
}
143
144
145
+ fn get_rustc_sysroot ( ) -> Result < String , Box < dyn Error > > {
146
+ Ok ( String :: from_utf8 (
147
+ Command :: new ( std:: env:: var_os ( "RUSTC" ) . unwrap_or_else ( || OsString :: from ( "rustc" ) ) )
148
+ . arg ( "--print=sysroot" )
149
+ . output ( ) ?
150
+ . stdout ,
151
+ ) ?
152
+ . trim ( )
153
+ . into ( ) )
154
+ }
155
+
144
156
fn main ( ) {
145
157
println ! ( "cargo:rerun-if-changed=build.rs" ) ;
146
158
println ! ( "cargo:rerun-if-env-changed=CFG_RELEASE" ) ;
@@ -152,11 +164,25 @@ fn main() {
152
164
println ! ( "cargo:rerun-if-env-changed=CFG_VIRTUAL_RUST_SOURCE_BASE_DIR" ) ;
153
165
println ! ( "cargo:rerun-if-env-changed=CFG_COMPILER_HOST_TRIPLE" ) ;
154
166
println ! ( "cargo:rerun-if-env-changed=CFG_LIBDIR_RELATIVE" ) ;
167
+ println ! ( "cargo:rerun-if-env-changed=RUSTC_STAGE" ) ;
155
168
156
169
if std:: env:: var_os ( "CFG_COMPILER_HOST_TRIPLE" ) . is_none ( ) {
157
170
println ! (
158
171
"cargo:rustc-env=CFG_COMPILER_HOST_TRIPLE={}" ,
159
172
default_build_triple( ) . expect( "Unable to determine build triple not found" )
160
173
)
161
174
} ;
175
+
176
+ if std:: env:: var_os ( "RUSTC_STAGE" ) . is_none ( ) {
177
+ match get_rustc_sysroot ( ) {
178
+ Ok ( sysroot) => println ! ( "cargo:rustc-env=CFG_DEFAULT_SYSROOT={}" , sysroot) ,
179
+ Err ( err) => {
180
+ println ! ( "\"
181
+ cargo:warning=ERROR: unable to get rustc sysroot: {}\n \
182
+ cargo:warning=You will need to pass --sysroot= manually to the compiler that will be built",
183
+ err
184
+ ) ;
185
+ }
186
+ } ;
187
+ }
162
188
}
0 commit comments