Auto merge of #79253 - rcvalle:fix-rustc-sysroot-cas, r=nagisa
Fix rustc sysroot in systems using CAS Change filesearch::get_or_default_sysroot() to check if sysroot is found using env::args().next() if rustc in argv[0] is a symlink; otherwise, or if it is not found, use env::current_exe() to imply sysroot. This makes the rustc binary able to locate Rust libraries in systems using content-addressable storage (CAS).
This commit is contained in:
commit
16b805713c
1 changed files with 45 additions and 7 deletions
|
@ -113,6 +113,8 @@ pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
|
||||||
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
|
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This function checks if sysroot is found using env::args().next(), and if it
|
||||||
|
// is not found, uses env::current_exe() to imply sysroot.
|
||||||
pub fn get_or_default_sysroot() -> PathBuf {
|
pub fn get_or_default_sysroot() -> PathBuf {
|
||||||
// Follow symlinks. If the resolved path is relative, make it absolute.
|
// Follow symlinks. If the resolved path is relative, make it absolute.
|
||||||
fn canonicalize(path: PathBuf) -> PathBuf {
|
fn canonicalize(path: PathBuf) -> PathBuf {
|
||||||
|
@ -123,6 +125,9 @@ pub fn get_or_default_sysroot() -> PathBuf {
|
||||||
fix_windows_verbatim_for_gcc(&path)
|
fix_windows_verbatim_for_gcc(&path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use env::current_exe() to get the path of the executable following
|
||||||
|
// symlinks/canonicalizing components.
|
||||||
|
fn from_current_exe() -> PathBuf {
|
||||||
match env::current_exe() {
|
match env::current_exe() {
|
||||||
Ok(exe) => {
|
Ok(exe) => {
|
||||||
let mut p = canonicalize(exe);
|
let mut p = canonicalize(exe);
|
||||||
|
@ -134,6 +139,39 @@ pub fn get_or_default_sysroot() -> PathBuf {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use env::args().next() to get the path of the executable without
|
||||||
|
// following symlinks/canonicalizing any component. This makes the rustc
|
||||||
|
// binary able to locate Rust libraries in systems using content-addressable
|
||||||
|
// storage (CAS).
|
||||||
|
fn from_env_args_next() -> Option<PathBuf> {
|
||||||
|
match env::args_os().next() {
|
||||||
|
Some(first_arg) => {
|
||||||
|
let mut p = PathBuf::from(first_arg);
|
||||||
|
|
||||||
|
// Check if sysroot is found using env::args().next() only if the rustc in argv[0]
|
||||||
|
// is a symlink (see #79253). We might want to change/remove it to conform with
|
||||||
|
// https://www.gnu.org/prep/standards/standards.html#Finding-Program-Files in the
|
||||||
|
// future.
|
||||||
|
if fs::read_link(&p).is_err() {
|
||||||
|
// Path is not a symbolic link or does not exist.
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
p.pop();
|
||||||
|
p.pop();
|
||||||
|
let mut libdir = PathBuf::from(&p);
|
||||||
|
libdir.push(find_libdir(&p).as_ref());
|
||||||
|
if libdir.exists() { Some(p) } else { None }
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if sysroot is found using env::args().next(), and if is not found,
|
||||||
|
// use env::current_exe() to imply sysroot.
|
||||||
|
from_env_args_next().unwrap_or(from_current_exe())
|
||||||
|
}
|
||||||
|
|
||||||
// The name of the directory rustc expects libraries to be located.
|
// The name of the directory rustc expects libraries to be located.
|
||||||
fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
|
fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
|
||||||
// FIXME: This is a quick hack to make the rustc binary able to locate
|
// FIXME: This is a quick hack to make the rustc binary able to locate
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue