From 6f1ac8d756a7a7c22641020926458058a51d5dd3 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sat, 3 Apr 2021 13:45:02 +0800 Subject: [PATCH 1/2] 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 --- compiler/rustc_session/src/config.rs | 4 ++-- compiler/rustc_session/src/session.rs | 13 +++++++------ compiler/rustc_target/src/spec/mod.rs | 20 ++++++++++++++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 77a9a2b227c..f3da9a1bda1 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -927,8 +927,8 @@ pub fn build_configuration(sess: &Session, mut user_cfg: CrateConfig) -> CrateCo user_cfg } -pub fn build_target_config(opts: &Options, target_override: Option) -> Target { - let target_result = target_override.map_or_else(|| Target::search(&opts.target_triple), Ok); +pub fn build_target_config(opts: &Options, target_override: Option, sysroot: &PathBuf) -> Target { + let target_result = target_override.map_or_else(|| Target::search(&opts.target_triple, sysroot), Ok); let target = target_result.unwrap_or_else(|e| { early_error( opts.error_format, diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 493bbb3a762..08a7447008a 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -1301,9 +1301,14 @@ pub fn build_session( DiagnosticOutput::Raw(write) => Some(write), }; - let target_cfg = config::build_target_config(&sopts, target_override); + let sysroot = match &sopts.maybe_sysroot { + Some(sysroot) => sysroot.clone(), + None => filesearch::get_or_default_sysroot(), + }; + + let target_cfg = config::build_target_config(&sopts, target_override, &sysroot); let host_triple = TargetTriple::from_triple(config::host_triple()); - let host = Target::search(&host_triple).unwrap_or_else(|e| { + let host = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| { early_error(sopts.error_format, &format!("Error loading host specification: {}", e)) }); @@ -1350,10 +1355,6 @@ pub fn build_session( let mut parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map); parse_sess.assume_incomplete_release = sopts.debugging_opts.assume_incomplete_release; - let sysroot = match &sopts.maybe_sysroot { - Some(sysroot) => sysroot.clone(), - None => filesearch::get_or_default_sysroot(), - }; let host_triple = config::host_triple(); let target_triple = sopts.target_triple.triple(); diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 039e9a8b274..27653d3c331 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1747,13 +1747,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 { + pub fn search(target_triple: &TargetTriple, sysroot: &PathBuf) -> Result { use rustc_serialize::json; use std::env; use std::fs; @@ -1780,14 +1782,24 @@ 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//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) => { From 8f73fe91f5db7de6e42ad7824a00b9729d2925b2 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Sat, 3 Apr 2021 15:00:10 +0800 Subject: [PATCH 2/2] compiler: run `python3 ./x.py fmt` This fixes a build issue with formatting as part of #83800. Signed-off-by: Sean Cross --- compiler/rustc_session/src/config.rs | 9 +++++++-- compiler/rustc_target/src/spec/mod.rs | 7 ++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index f3da9a1bda1..65be14e3510 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -927,8 +927,13 @@ pub fn build_configuration(sess: &Session, mut user_cfg: CrateConfig) -> CrateCo user_cfg } -pub fn build_target_config(opts: &Options, target_override: Option, sysroot: &PathBuf) -> Target { - let target_result = target_override.map_or_else(|| Target::search(&opts.target_triple, sysroot), Ok); +pub fn build_target_config( + opts: &Options, + target_override: Option, + sysroot: &PathBuf, +) -> Target { + let target_result = + target_override.map_or_else(|| Target::search(&opts.target_triple, sysroot), Ok); let target = target_result.unwrap_or_else(|e| { early_error( opts.error_format, diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 27653d3c331..72b2d089ebc 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1791,11 +1791,8 @@ impl Target { // Additionally look in the sysroot under `lib/rustlib//target.json` // as a fallback. - let p = sysroot - .join("lib") - .join("rustlib") - .join(&target_triple) - .join("target.json"); + let p = + sysroot.join("lib").join("rustlib").join(&target_triple).join("target.json"); if p.is_file() { return load_file(&p); }