Auto merge of #83800 - xobs:impl-16351-nightly, r=nagisa

Add default search path to `Target::search()`

The function `Target::search()` accepts a target triple and returns a `Target` struct defining the requested target.

There is a `// FIXME 16351: add a sane default search path?` comment that indicates it is desirable to include some sort of default. This was raised in https://github.com/rust-lang/rust/issues/16351 which was closed without any resolution.

https://github.com/rust-lang/rust/pull/31117 was proposed, however that has platform-specific logic that is unsuitable for systems without `/etc/`.

This patch implements the suggestion raised in https://github.com/rust-lang/rust/issues/16351#issuecomment-180878193 where a `target.json` file may be placed in `$(rustc --print sysroot)/lib/rustlib/<target-triple>/target.json`. This allows shipping a toolchain distribution as a single file that gets extracted to the sysroot.
This commit is contained in:
bors 2021-05-09 22:01:26 +00:00
commit c55c26cb36
3 changed files with 27 additions and 12 deletions

View file

@ -1898,13 +1898,15 @@ impl Target {
}
/// Search RUST_TARGET_PATH for a JSON file specifying the given target
/// triple. Note that it could also just be a bare filename already, so also
/// 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.
///
/// The error string could come from any of the APIs called, including
/// filesystem access and JSON decoding.
pub fn search(target_triple: &TargetTriple) -> Result<Target, String> {
pub fn search(target_triple: &TargetTriple, sysroot: &PathBuf) -> Result<Target, String> {
use rustc_serialize::json;
use std::env;
use std::fs;
@ -1931,14 +1933,21 @@ impl Target {
let target_path = env::var_os("RUST_TARGET_PATH").unwrap_or_default();
// FIXME 16351: add a sane default search path?
for dir in env::split_paths(&target_path) {
let p = dir.join(&path);
if p.is_file() {
return load_file(&p);
}
}
// 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");
if p.is_file() {
return load_file(&p);
}
Err(format!("Could not find specification for target {:?}", target_triple))
}
TargetTriple::TargetPath(ref target_path) => {