Skip to content

Commit 6f1ac8d

Browse files
committed
rustc: target: add sysroot to rust_target_path
This enables placing a `target.json` file into the rust sysroot under the target-specific directory. Signed-off-by: Sean Cross <[email protected]>
1 parent a0d66b5 commit 6f1ac8d

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

compiler/rustc_session/src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ pub fn build_configuration(sess: &Session, mut user_cfg: CrateConfig) -> CrateCo
927927
user_cfg
928928
}
929929

930-
pub fn build_target_config(opts: &Options, target_override: Option<Target>) -> Target {
931-
let target_result = target_override.map_or_else(|| Target::search(&opts.target_triple), Ok);
930+
pub fn build_target_config(opts: &Options, target_override: Option<Target>, sysroot: &PathBuf) -> Target {
931+
let target_result = target_override.map_or_else(|| Target::search(&opts.target_triple, sysroot), Ok);
932932
let target = target_result.unwrap_or_else(|e| {
933933
early_error(
934934
opts.error_format,

compiler/rustc_session/src/session.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1301,9 +1301,14 @@ pub fn build_session(
13011301
DiagnosticOutput::Raw(write) => Some(write),
13021302
};
13031303

1304-
let target_cfg = config::build_target_config(&sopts, target_override);
1304+
let sysroot = match &sopts.maybe_sysroot {
1305+
Some(sysroot) => sysroot.clone(),
1306+
None => filesearch::get_or_default_sysroot(),
1307+
};
1308+
1309+
let target_cfg = config::build_target_config(&sopts, target_override, &sysroot);
13051310
let host_triple = TargetTriple::from_triple(config::host_triple());
1306-
let host = Target::search(&host_triple).unwrap_or_else(|e| {
1311+
let host = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| {
13071312
early_error(sopts.error_format, &format!("Error loading host specification: {}", e))
13081313
});
13091314

@@ -1350,10 +1355,6 @@ pub fn build_session(
13501355

13511356
let mut parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
13521357
parse_sess.assume_incomplete_release = sopts.debugging_opts.assume_incomplete_release;
1353-
let sysroot = match &sopts.maybe_sysroot {
1354-
Some(sysroot) => sysroot.clone(),
1355-
None => filesearch::get_or_default_sysroot(),
1356-
};
13571358

13581359
let host_triple = config::host_triple();
13591360
let target_triple = sopts.target_triple.triple();

compiler/rustc_target/src/spec/mod.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1747,13 +1747,15 @@ impl Target {
17471747
}
17481748

17491749
/// Search RUST_TARGET_PATH for a JSON file specifying the given target
1750-
/// triple. Note that it could also just be a bare filename already, so also
1750+
/// triple. If none is found, look for a file called `target.json` inside
1751+
/// the sysroot under the target-triple's `rustlib` directory.
1752+
/// Note that it could also just be a bare filename already, so also
17511753
/// check for that. If one of the hardcoded targets we know about, just
17521754
/// return it directly.
17531755
///
17541756
/// The error string could come from any of the APIs called, including
17551757
/// filesystem access and JSON decoding.
1756-
pub fn search(target_triple: &TargetTriple) -> Result<Target, String> {
1758+
pub fn search(target_triple: &TargetTriple, sysroot: &PathBuf) -> Result<Target, String> {
17571759
use rustc_serialize::json;
17581760
use std::env;
17591761
use std::fs;
@@ -1780,14 +1782,24 @@ impl Target {
17801782

17811783
let target_path = env::var_os("RUST_TARGET_PATH").unwrap_or_default();
17821784

1783-
// FIXME 16351: add a sane default search path?
1784-
17851785
for dir in env::split_paths(&target_path) {
17861786
let p = dir.join(&path);
17871787
if p.is_file() {
17881788
return load_file(&p);
17891789
}
17901790
}
1791+
1792+
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
1793+
// as a fallback.
1794+
let p = sysroot
1795+
.join("lib")
1796+
.join("rustlib")
1797+
.join(&target_triple)
1798+
.join("target.json");
1799+
if p.is_file() {
1800+
return load_file(&p);
1801+
}
1802+
17911803
Err(format!("Could not find specification for target {:?}", target_triple))
17921804
}
17931805
TargetTriple::TargetPath(ref target_path) => {

0 commit comments

Comments
 (0)