Skip to content

Commit 388b3fa

Browse files
committed
Make non-x.py-built compilers default to system sysroot
If the sysroot does not exist, use the one from the toolchain used to build rustc.
1 parent e91592c commit 388b3fa

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/librustc_session/build.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::error::Error;
2+
use std::ffi::OsString;
23
use std::process::Command;
34

45
fn output(cmd: &mut Command) -> Result<String, Box<dyn Error>> {
@@ -141,6 +142,17 @@ fn default_build_triple() -> Result<String, Box<dyn Error>> {
141142
Ok(format!("{}-{}", cputype, ostype))
142143
}
143144

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+
144156
fn main() {
145157
println!("cargo:rerun-if-changed=build.rs");
146158
println!("cargo:rerun-if-env-changed=CFG_RELEASE");
@@ -152,11 +164,25 @@ fn main() {
152164
println!("cargo:rerun-if-env-changed=CFG_VIRTUAL_RUST_SOURCE_BASE_DIR");
153165
println!("cargo:rerun-if-env-changed=CFG_COMPILER_HOST_TRIPLE");
154166
println!("cargo:rerun-if-env-changed=CFG_LIBDIR_RELATIVE");
167+
println!("cargo:rerun-if-env-changed=RUSTC_STAGE");
155168

156169
if std::env::var_os("CFG_COMPILER_HOST_TRIPLE").is_none() {
157170
println!(
158171
"cargo:rustc-env=CFG_COMPILER_HOST_TRIPLE={}",
159172
default_build_triple().expect("Unable to determine build triple not found")
160173
)
161174
};
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+
}
162188
}

src/librustc_session/filesearch.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn get_or_default_sysroot() -> PathBuf {
125125
})
126126
}
127127

128-
match env::current_exe() {
128+
let mut sysroot = match env::current_exe() {
129129
Ok(exe) => match canonicalize(Some(exe)) {
130130
Some(mut p) => {
131131
p.pop();
@@ -135,7 +135,15 @@ pub fn get_or_default_sysroot() -> PathBuf {
135135
None => panic!("can't determine value for sysroot"),
136136
},
137137
Err(ref e) => panic!(format!("failed to get current_exe: {}", e)),
138+
};
139+
140+
if let Some(default_sysroot) = option_env!("CFG_DEFAULT_SYSROOT") {
141+
if !Path::new(&*find_libdir(&sysroot)).exists() {
142+
sysroot = PathBuf::from(default_sysroot);
143+
}
138144
}
145+
146+
sysroot
139147
}
140148

141149
// The name of the directory rustc expects libraries to be located.

0 commit comments

Comments
 (0)