1
Fork 0

Adjust target search algorithm for rustlib path

With this the concerns expressed in #83800 should be addressed.
This commit is contained in:
Simonas Kazlauskas 2021-05-10 19:15:19 +03:00
parent 2fb1dee14b
commit b7c5599d22
5 changed files with 83 additions and 66 deletions

View file

@ -15,6 +15,8 @@
#![feature(associated_type_bounds)]
#![feature(exhaustive_patterns)]
use std::path::{Path, PathBuf};
#[macro_use]
extern crate rustc_macros;
@ -29,3 +31,52 @@ pub mod spec;
/// This is a hack to allow using the `HashStable_Generic` derive macro
/// instead of implementing everything in `rustc_middle`.
pub trait HashStableContext {}
/// The name of rustc's own place to organize libraries.
///
/// Used to be `rustc`, now the default is `rustlib`.
const RUST_LIB_DIR: &str = "rustlib";
/// Returns a `rustlib` path for this particular target, relative to the provided sysroot.
///
/// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
/// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
let libdir = find_libdir(sysroot);
std::array::IntoIter::new([
Path::new(libdir.as_ref()),
Path::new(RUST_LIB_DIR),
Path::new(target_triple),
])
.collect::<PathBuf>()
}
/// The name of the directory rustc expects libraries to be located.
fn find_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
// FIXME: This is a quick hack to make the rustc binary able to locate
// Rust libraries in Linux environments where libraries might be installed
// to lib64/lib32. This would be more foolproof by basing the sysroot off
// of the directory where `librustc_driver` is located, rather than
// where the rustc binary is.
// If --libdir is set during configuration to the value other than
// "lib" (i.e., non-default), this value is used (see issue #16552).
#[cfg(target_pointer_width = "64")]
const PRIMARY_LIB_DIR: &str = "lib64";
#[cfg(target_pointer_width = "32")]
const PRIMARY_LIB_DIR: &str = "lib32";
const SECONDARY_LIB_DIR: &str = "lib";
match option_env!("CFG_LIBDIR_RELATIVE") {
None | Some("lib") => {
if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
PRIMARY_LIB_DIR.into()
} else {
SECONDARY_LIB_DIR.into()
}
}
Some(libdir) => libdir.into(),
}
}

View file

@ -1897,15 +1897,15 @@ impl Target {
Ok(base)
}
/// Search RUST_TARGET_PATH for a JSON file specifying the given target
/// triple. If none is found, look for a file called `target.json` inside
/// the sysroot under the target-triple's `rustlib` directory.
/// Note that it could also just be a bare filename already, so also
/// check for that. If one of the hardcoded targets we know about, just
/// return it directly.
/// Search for a JSON file specifying the given target triple.
///
/// The error string could come from any of the APIs called, including
/// filesystem access and JSON decoding.
/// If none is found in `$RUST_TARGET_PATH`, look for a file called `target.json` inside the
/// sysroot under the target-triple's `rustlib` directory. Note that it could also just be a
/// bare filename already, so also check for that. If one of the hardcoded targets we know
/// about, just return it directly.
///
/// The error string could come from any of the APIs called, including filesystem access and
/// JSON decoding.
pub fn search(target_triple: &TargetTriple, sysroot: &PathBuf) -> Result<Target, String> {
use rustc_serialize::json;
use std::env;
@ -1942,8 +1942,13 @@ impl Target {
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
// as a fallback.
let p =
sysroot.join("lib").join("rustlib").join(&target_triple).join("target.json");
let rustlib_path = crate::target_rustlib_path(&sysroot, &target_triple);
let p = std::array::IntoIter::new([
Path::new(sysroot),
Path::new(&rustlib_path),
Path::new("target.json"),
])
.collect::<PathBuf>();
if p.is_file() {
return load_file(&p);
}