Skip to content

Commit 7ad3a2b

Browse files
committed
compiler/rustc_session: fix sysroot detection logic ...
... on systems where /usr/lib contains a multi-arch structure
1 parent 8f55d60 commit 7ad3a2b

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

compiler/rustc_session/src/filesearch.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
164164
fix_windows_verbatim_for_gcc(&path)
165165
}
166166

167+
// Use env::current_exe() to get the path of the executable following
168+
// symlinks/canonicalizing components.
169+
#[cfg(target_os = "linux")]
170+
fn default_from_current_exe() -> Result<PathBuf, io::Error> {
171+
match env::current_exe() {
172+
Ok(exe) => {
173+
let mut p = canonicalize(exe)?;
174+
p.pop();
175+
p.pop();
176+
177+
Ok(p)
178+
}
179+
Err(e) => Err(e),
180+
}
181+
}
182+
167183
fn default_from_rustc_driver_dll() -> Result<PathBuf, String> {
168184
let dll = current_dll_path().map(|s| canonicalize(s))?;
169185

@@ -193,6 +209,18 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
193209
}
194210
}
195211

212+
// Select a default path based on the platform this Rust binary is currently running on
213+
fn default_from_current_platform() -> Result<PathBuf, String> {
214+
#[cfg(target_os = "linux")]
215+
{
216+
if let Ok(p) = default_from_current_exe() {
217+
return Ok(p);
218+
}
219+
}
220+
221+
default_from_rustc_driver_dll()
222+
}
223+
196224
// Use env::args().next() to get the path of the executable without
197225
// following symlinks/canonicalizing any component. This makes the rustc
198226
// binary able to locate Rust libraries in systems using content-addressable
@@ -223,5 +251,5 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
223251
}
224252
}
225253

226-
Ok(from_env_args_next().unwrap_or(default_from_rustc_driver_dll()?))
254+
Ok(from_env_args_next().unwrap_or(default_from_current_platform()?))
227255
}

0 commit comments

Comments
 (0)