Skip to content

Commit c55c26c

Browse files
committed
Auto merge of rust-lang#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 rust-lang#16351 which was closed without any resolution. rust-lang#31117 was proposed, however that has platform-specific logic that is unsuitable for systems without `/etc/`. This patch implements the suggestion raised in rust-lang#16351 (comment) 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.
2 parents ca82264 + f9d390d commit c55c26c

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

compiler/rustc_session/src/config.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,13 @@ pub fn build_configuration(sess: &Session, mut user_cfg: CrateConfig) -> CrateCo
893893
user_cfg
894894
}
895895

896-
pub(super) fn build_target_config(opts: &Options, target_override: Option<Target>) -> Target {
897-
let target_result = target_override.map_or_else(|| Target::search(&opts.target_triple), Ok);
896+
pub(super) fn build_target_config(
897+
opts: &Options,
898+
target_override: Option<Target>,
899+
sysroot: &PathBuf,
900+
) -> Target {
901+
let target_result =
902+
target_override.map_or_else(|| Target::search(&opts.target_triple, sysroot), Ok);
898903
let target = target_result.unwrap_or_else(|e| {
899904
early_error(
900905
opts.error_format,

compiler/rustc_session/src/session.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1276,9 +1276,14 @@ pub fn build_session(
12761276
DiagnosticOutput::Raw(write) => Some(write),
12771277
};
12781278

1279-
let target_cfg = config::build_target_config(&sopts, target_override);
1279+
let sysroot = match &sopts.maybe_sysroot {
1280+
Some(sysroot) => sysroot.clone(),
1281+
None => filesearch::get_or_default_sysroot(),
1282+
};
1283+
1284+
let target_cfg = config::build_target_config(&sopts, target_override, &sysroot);
12801285
let host_triple = TargetTriple::from_triple(config::host_triple());
1281-
let host = Target::search(&host_triple).unwrap_or_else(|e| {
1286+
let host = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
12821287
early_error(sopts.error_format, &format!("Error loading host specification: {}", e))
12831288
});
12841289

@@ -1325,10 +1330,6 @@ pub fn build_session(
13251330

13261331
let mut parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
13271332
parse_sess.assume_incomplete_release = sopts.debugging_opts.assume_incomplete_release;
1328-
let sysroot = match &sopts.maybe_sysroot {
1329-
Some(sysroot) => sysroot.clone(),
1330-
None => filesearch::get_or_default_sysroot(),
1331-
};
13321333

13331334
let host_triple = config::host_triple();
13341335
let target_triple = sopts.target_triple.triple();

compiler/rustc_target/src/spec/mod.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1898,13 +1898,15 @@ impl Target {
18981898
}
18991899

19001900
/// Search RUST_TARGET_PATH for a JSON file specifying the given target
1901-
/// triple. Note that it could also just be a bare filename already, so also
1901+
/// triple. If none is found, look for a file called `target.json` inside
1902+
/// the sysroot under the target-triple's `rustlib` directory.
1903+
/// Note that it could also just be a bare filename already, so also
19021904
/// check for that. If one of the hardcoded targets we know about, just
19031905
/// return it directly.
19041906
///
19051907
/// The error string could come from any of the APIs called, including
19061908
/// filesystem access and JSON decoding.
1907-
pub fn search(target_triple: &TargetTriple) -> Result<Target, String> {
1909+
pub fn search(target_triple: &TargetTriple, sysroot: &PathBuf) -> Result<Target, String> {
19081910
use rustc_serialize::json;
19091911
use std::env;
19101912
use std::fs;
@@ -1931,14 +1933,21 @@ impl Target {
19311933

19321934
let target_path = env::var_os("RUST_TARGET_PATH").unwrap_or_default();
19331935

1934-
// FIXME 16351: add a sane default search path?
1935-
19361936
for dir in env::split_paths(&target_path) {
19371937
let p = dir.join(&path);
19381938
if p.is_file() {
19391939
return load_file(&p);
19401940
}
19411941
}
1942+
1943+
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
1944+
// as a fallback.
1945+
let p =
1946+
sysroot.join("lib").join("rustlib").join(&target_triple).join("target.json");
1947+
if p.is_file() {
1948+
return load_file(&p);
1949+
}
1950+
19421951
Err(format!("Could not find specification for target {:?}", target_triple))
19431952
}
19441953
TargetTriple::TargetPath(ref target_path) => {

0 commit comments

Comments
 (0)