From e9d46f8cb10eec1f8ee19ed2aab385d17ec85757 Mon Sep 17 00:00:00 2001 From: Cody P Schafer Date: Tue, 18 Nov 2014 01:40:21 -0500 Subject: [PATCH 02/10] Target: add default target.json path: $libdir/rust/targets --- src/librustc/session/config.rs | 6 +++--- src/librustc/session/mod.rs | 8 ++++++-- src/librustc_back/target/mod.rs | 14 +++++++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index c5db7cd..2b9069f 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -38,7 +38,7 @@ use getopts; use std::collections::HashMap; use std::env; use std::fmt; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use llvm; @@ -655,8 +655,8 @@ pub fn build_configuration(sess: &Session) -> ast::CrateConfig { v } -pub fn build_target_config(opts: &Options, sp: &SpanHandler) -> Config { - let target = match Target::search(&opts.target_triple) { +pub fn build_target_config(sysroot: &Path, opts: &Options, sp: &SpanHandler) -> Config { + let target = match Target::search(sysroot, &opts.target_triple[..]) { Ok(t) => t, Err(e) => { sp.handler().fatal(&format!("Error loading target specification: {}", e)); diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 99a58f0..d25e476 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -385,14 +385,18 @@ pub fn build_session_(sopts: config::Options, local_crate_source_file: Option, span_diagnostic: diagnostic::SpanHandler) -> Session { - let host = match Target::search(config::host_triple()) { + let sysroot = match sopts.maybe_sysroot { + Some(ref x) => PathBuf::from(x), + None => filesearch::get_or_default_sysroot() + }; + let host = match Target::search(&sysroot, config::host_triple()) { Ok(t) => t, Err(e) => { span_diagnostic.handler() .fatal(&format!("Error loading host specification: {}", e)); } }; - let target_cfg = config::build_target_config(&sopts, &span_diagnostic); + let target_cfg = config::build_target_config(&sysroot, &sopts, &span_diagnostic); let p_s = parse::ParseSess::with_span_handler(span_diagnostic); let default_sysroot = match sopts.maybe_sysroot { Some(_) => None, diff --git a/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs index ce05a88..1d1ff70 100644 --- a/src/librustc_back/target/mod.rs +++ b/src/librustc_back/target/mod.rs @@ -49,6 +49,8 @@ use serialize::json::Json; use std::default::Default; use std::io::prelude::*; use syntax::{diagnostic, abi}; +use std::borrow::ToOwned; +use std::path::Path; mod android_base; mod apple_base; @@ -320,12 +322,13 @@ impl Target { /// /// The error string could come from any of the APIs called, including /// filesystem access and JSON decoding. - pub fn search(target: &str) -> Result { + pub fn search(sysroot: &Path, target: &str) -> Result { use std::env; use std::ffi::OsString; use std::fs::File; use std::path::{Path, PathBuf}; use serialize::json; + use std::iter::IntoIterator; fn load_file(path: &Path) -> Result { let mut f = try!(File::open(path).map_err(|e| e.to_string())); @@ -417,9 +420,14 @@ impl Target { let target_path = env::var_os("RUST_TARGET_PATH") .unwrap_or(OsString::new()); - // FIXME 16351: add a sane default search path? + let mut default_path = sysroot.to_owned(); + default_path.push(env!("CFG_LIBDIR_RELATIVE")); + default_path.push("rustlib"); - for dir in env::split_paths(&target_path) { + let paths = env::split_paths(&target_path) + .chain(Some(default_path).into_iter()); + + for dir in paths { let p = dir.join(&path); if p.is_file() { return load_file(&p); -- 2.5.1