This commit adds support for the compiler to distinguish between different forms
of lookup paths in the compiler itself. Issue #19767 has some background on this
topic, as well as some sample bugs which can occur if these lookup paths are not
separated.
This commits extends the existing command line flag `-L` with the same trailing
syntax as the `-l` flag. Each argument to `-L` can now have a trailing `:all`,
`:native`, `:crate`, or `:dependency`. This suffix indicates what form of lookup
path the compiler should add the argument to. The `dependency` lookup path is
used when looking up crate dependencies, the `crate` lookup path is used when
looking for immediate dependencies (`extern crate` statements), and the `native`
lookup path is used for probing for native libraries to insert into rlibs. Paths
with `all` are used for all of these purposes (the default).
The default compiler lookup path (the rustlib libdir) is by default added to all
of these paths. Additionally, the `RUST_PATH` lookup path is added to all of
these paths.
Closes #19767
use back::svh::Svh;
use session::{config, Session};
+use session::search_paths::PathKind;
use metadata::cstore;
use metadata::cstore::{CStore, CrateSource};
use metadata::decoder;
info.ident[],
info.name[],
None,
- i.span);
+ i.span,
+ PathKind::Crate);
e.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
}
None => ()
(cnum, cmeta, source)
}
-fn resolve_crate<'a>(e: &mut Env,
+fn resolve_crate(e: &mut Env,
root: &Option<CratePaths>,
ident: &str,
name: &str,
hash: Option<&Svh>,
- span: Span)
+ span: Span,
+ kind: PathKind)
-> (ast::CrateNum, Rc<cstore::crate_metadata>,
cstore::CrateSource) {
match existing_match(e, name, hash) {
ident: ident,
crate_name: name,
hash: hash.map(|a| &*a),
- filesearch: e.sess.target_filesearch(),
+ filesearch: e.sess.target_filesearch(kind),
triple: e.sess.opts.target_triple[],
root: root,
rejected_via_hash: vec!(),
dep.name[],
dep.name[],
Some(&dep.hash),
- span);
+ span,
+ PathKind::Dependency);
(dep.cnum, local_cnum)
}).collect()
}
}
}
- pub fn read_plugin_metadata(&mut self, krate: &ast::ViewItem) -> PluginMetadata {
+ pub fn read_plugin_metadata(&mut self,
+ krate: &ast::ViewItem) -> PluginMetadata {
let info = extract_crate_info(&self.env, krate).unwrap();
let target_triple = self.env.sess.opts.target_triple[];
let is_cross = target_triple != config::host_triple();
ident: info.ident[],
crate_name: info.name[],
hash: None,
- filesearch: self.env.sess.host_filesearch(),
+ filesearch: self.env.sess.host_filesearch(PathKind::Crate),
triple: config::host_triple(),
root: &None,
rejected_via_hash: vec!(),
// try loading from target crates (only valid if there are
// no syntax extensions)
load_ctxt.triple = target_triple;
- load_ctxt.filesearch = self.env.sess.target_filesearch();
+ load_ctxt.filesearch = self.env.sess.target_filesearch(PathKind::Crate);
let lib = load_ctxt.load_library_crate();
if decoder::get_plugin_registrar_fn(lib.metadata.as_slice()).is_some() {
let message = format!("crate `{}` contains a plugin_registrar fn but \
pub use self::FileMatch::*;
-use std::cell::RefCell;
use std::collections::HashSet;
use std::io::fs::PathExtensions;
use std::io::fs;
use std::os;
use util::fs as myfs;
+use session::search_paths::{SearchPaths, PathKind};
#[deriving(Copy)]
pub enum FileMatch {
pub struct FileSearch<'a> {
pub sysroot: &'a Path,
- pub addl_lib_search_paths: &'a RefCell<Vec<Path>>,
+ pub search_paths: &'a SearchPaths,
pub triple: &'a str,
+ pub kind: PathKind,
}
impl<'a> FileSearch<'a> {
let mut visited_dirs = HashSet::new();
let mut found = false;
- debug!("filesearch: searching additional lib search paths [{}]",
- self.addl_lib_search_paths.borrow().len());
- for path in self.addl_lib_search_paths.borrow().iter() {
+ for path in self.search_paths.iter(self.kind) {
match f(path) {
FileMatches => found = true,
FileDoesntMatch => ()
pub fn new(sysroot: &'a Path,
triple: &'a str,
- addl_lib_search_paths: &'a RefCell<Vec<Path>>) -> FileSearch<'a> {
+ search_paths: &'a SearchPaths,
+ kind: PathKind) -> FileSearch<'a> {
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
FileSearch {
sysroot: sysroot,
- addl_lib_search_paths: addl_lib_search_paths,
+ search_paths: search_paths,
triple: triple,
+ kind: kind,
}
}
pub use self::DebugInfoLevel::*;
use session::{early_error, Session};
+use session::search_paths::SearchPaths;
use rustc_back::target::Target;
use lint;
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use getopts;
-use std::cell::{RefCell};
use std::fmt;
use llvm;
// This was mutable for rustpkg, which updates search paths based on the
// parsed code. It remains mutable in case its replacements wants to use
// this.
- pub addl_lib_search_paths: RefCell<Vec<Path>>,
+ pub search_paths: SearchPaths,
pub libs: Vec<(String, cstore::NativeLibraryKind)>,
pub maybe_sysroot: Option<Path>,
pub target_triple: String,
lint_opts: Vec::new(),
describe_lints: false,
output_types: Vec::new(),
- addl_lib_search_paths: RefCell::new(Vec::new()),
+ search_paths: SearchPaths::new(),
maybe_sysroot: None,
target_triple: host_triple().to_string(),
cfg: Vec::new(),
}
};
- let addl_lib_search_paths = matches.opt_strs("L").iter().map(|s| {
- Path::new(s[])
- }).collect();
+ let mut search_paths = SearchPaths::new();
+ for s in matches.opt_strs("L").iter() {
+ search_paths.add_path(s[]);
+ }
let libs = matches.opt_strs("l").into_iter().map(|s| {
let mut parts = s.rsplitn(1, ':');
lint_opts: lint_opts,
describe_lints: describe_lints,
output_types: output_types,
- addl_lib_search_paths: RefCell::new(addl_lib_search_paths),
+ search_paths: search_paths,
maybe_sysroot: sysroot_opt,
target_triple: target,
cfg: cfg,
// except according to those terms.
+use lint;
use metadata::cstore::CStore;
use metadata::filesearch;
-use lint;
+use session::search_paths::PathKind;
use util::nodemap::NodeMap;
use syntax::ast::NodeId;
use std::cell::{Cell, RefCell};
pub mod config;
+pub mod search_paths;
// Represents the data associated with a compilation
// session for a single crate.
.expect("missing sysroot and default_sysroot in Session")
}
}
- pub fn target_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> {
+ pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
filesearch::FileSearch::new(self.sysroot(),
self.opts.target_triple[],
- &self.opts.addl_lib_search_paths)
+ &self.opts.search_paths,
+ kind)
}
- pub fn host_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> {
+ pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
filesearch::FileSearch::new(
self.sysroot(),
config::host_triple(),
- &self.opts.addl_lib_search_paths)
+ &self.opts.search_paths,
+ kind)
}
}
--- /dev/null
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use std::slice;
+
+#[deriving(Clone)]
+pub struct SearchPaths {
+ paths: Vec<(PathKind, Path)>,
+}
+
+pub struct Iter<'a> {
+ kind: PathKind,
+ iter: slice::Iter<'a, (PathKind, Path)>,
+}
+
+#[deriving(Eq, PartialEq, Clone, Copy)]
+pub enum PathKind {
+ Native,
+ Crate,
+ Dependency,
+ All,
+}
+
+impl SearchPaths {
+ pub fn new() -> SearchPaths {
+ SearchPaths { paths: Vec::new() }
+ }
+
+ pub fn add_path(&mut self, path: &str) {
+ let (kind, path) = if path.ends_with(":native") {
+ (PathKind::Native, path.slice_to(path.len() - ":native".len()))
+ } else if path.ends_with(":crate") {
+ (PathKind::Crate, path.slice_to(path.len() - ":crate".len()))
+ } else if path.ends_with(":dependency") {
+ (PathKind::Dependency,
+ path.slice_to(path.len() - ":dependency".len()))
+ } else if path.ends_with(":all") {
+ (PathKind::All, path.slice_to(path.len() - ":all".len()))
+ } else {
+ (PathKind::All, path)
+ };
+ self.paths.push((kind, Path::new(path)));
+ }
+
+ pub fn iter(&self, kind: PathKind) -> Iter {
+ Iter { kind: kind, iter: self.paths.iter() }
+ }
+}
+
+impl<'a> Iterator<&'a Path> for Iter<'a> {
+ fn next(&mut self) -> Option<&'a Path> {
+ loop {
+ match self.iter.next() {
+ Some(&(kind, ref p)) if self.kind == PathKind::All ||
+ kind == PathKind::All ||
+ kind == self.kind => return Some(p),
+ Some(..) => {}
+ None => return None,
+ }
+ }
+ }
+}
use rustc::session::Session;
use rustc::session::config::{mod, Input, OutputFilenames};
+use rustc::session::search_paths::PathKind;
use rustc::lint;
use rustc::metadata::creader;
use rustc::middle::{stability, ty, reachable};
let mut _old_path = String::new();
if cfg!(windows) {
_old_path = os::getenv("PATH").unwrap_or(_old_path);
- let mut new_path = sess.host_filesearch().get_dylib_search_paths();
+ let mut new_path = sess.host_filesearch(PathKind::All).get_dylib_search_paths();
new_path.extend(os::split_paths(_old_path[]).into_iter());
os::setenv("PATH", os::join_paths(new_path[]).unwrap());
}
trans: &trans::CrateTranslation,
outputs: &OutputFilenames) {
let old_path = os::getenv("PATH").unwrap_or_else(||String::new());
- let mut new_path = sess.host_filesearch().get_tools_search_paths();
+ let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths();
new_path.extend(os::split_paths(old_path[]).into_iter());
os::setenv("PATH", os::join_paths(new_path[]).unwrap());
use session::config;
use session::config::NoDebugInfo;
use session::config::{OutputFilenames, Input, OutputTypeBitcode, OutputTypeExe, OutputTypeObject};
+use session::search_paths::PathKind;
use session::Session;
use metadata::common::LinkMeta;
use metadata::{encoder, cstore, filesearch, csearch, creader};
+use metadata::filesearch::FileDoesntMatch;
use trans::{CrateContext, CrateTranslation, gensym_name};
use middle::ty::{mod, Ty};
use util::common::time;
}
fn archive_search_paths(sess: &Session) -> Vec<Path> {
- let mut rustpath = filesearch::rust_path();
- rustpath.push(sess.target_filesearch().get_lib_path());
- let mut search: Vec<Path> = sess.opts.addl_lib_search_paths.borrow().clone();
- search.push_all(rustpath[]);
+ let mut search = Vec::new();
+ sess.target_filesearch(PathKind::Native).for_each_lib_search_path(|path| {
+ search.push(path.clone());
+ FileDoesntMatch
+ });
return search;
}
// The default library location, we need this to find the runtime.
// The location of crates will be determined as needed.
- let lib_path = sess.target_filesearch().get_lib_path();
+ let lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
// target descriptor
let t = &sess.target.target;
// in the current crate. Upstream crates with native library dependencies
// may have their native library pulled in above.
fn add_local_native_libraries(cmd: &mut Command, sess: &Session) {
- for path in sess.opts.addl_lib_search_paths.borrow().iter() {
- cmd.arg("-L").arg(path);
- }
-
- let rustpath = filesearch::rust_path();
- for path in rustpath.iter() {
+ sess.target_filesearch(PathKind::All).for_each_lib_search_path(|path| {
cmd.arg("-L").arg(path);
- }
+ FileDoesntMatch
+ });
// Some platforms take hints about whether a library is static or dynamic.
// For those that support this, we ensure we pass the option if the library
use rustc_driver::driver;
use rustc::session::{mod, config};
+use rustc::session::search_paths::SearchPaths;
use rustc::middle::{privacy, ty};
use rustc::lint;
use rustc_trans::back::link;
pub type Externs = HashMap<String, Vec<String>>;
-pub fn run_core(libs: Vec<Path>, cfgs: Vec<String>, externs: Externs,
+pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
cpath: &Path, triple: Option<String>)
-> (clean::Crate, CrateAnalysis) {
let sessopts = config::Options {
maybe_sysroot: None,
- addl_lib_search_paths: RefCell::new(libs),
+ search_paths: search_paths,
crate_types: vec!(config::CrateTypeRlib),
lint_opts: vec!((warning_lint, lint::Allow)),
externs: externs,
use externalfiles::ExternalHtml;
use serialize::{Decodable, Encodable};
use serialize::json::{mod, Json};
+use rustc::session::search_paths::SearchPaths;
// reexported from `clean` so it can be easily updated with the mod itself
pub use clean::SCHEMA_VERSION;
}
let input = matches.free[0].as_slice();
- let libs = matches.opt_strs("L").iter().map(|s| Path::new(s.as_slice())).collect();
+ let mut libs = SearchPaths::new();
+ for s in matches.opt_strs("L").iter() {
+ libs.add_path(s.as_slice());
+ }
let externs = match parse_externs(&matches) {
Ok(ex) => ex,
Err(err) => {
let mut plugins = matches.opt_strs("plugins");
// First, parse the crate and extract all relevant information.
- let libs: Vec<Path> = matches.opt_strs("L")
- .iter()
- .map(|s| Path::new(s.as_slice()))
- .collect();
+ let mut paths = SearchPaths::new();
+ for s in matches.opt_strs("L").iter() {
+ paths.add_path(s.as_slice());
+ }
let cfgs = matches.opt_strs("cfg");
let triple = matches.opt_str("target");
let (mut krate, analysis) = std::thread::Thread::spawn(move |:| {
let cr = cr;
- core::run_core(libs, cfgs, externs, &cr, triple)
+ core::run_core(paths, cfgs, externs, &cr, triple)
}).join().map_err(|_| "rustc failed").unwrap();
info!("finished with rustc");
let mut analysis = Some(analysis);
// except according to those terms.
use std::io;
-use std::string::String;
use core;
use getopts;
use testing;
+use rustc::session::search_paths::SearchPaths;
use externalfiles::ExternalHtml;
}
/// Run any tests/code examples in the markdown file `input`.
-pub fn test(input: &str, libs: Vec<Path>, externs: core::Externs,
+pub fn test(input: &str, libs: SearchPaths, externs: core::Externs,
mut test_args: Vec<String>) -> int {
let input_str = load_or_return!(input, 1, 2);
use std::collections::{HashSet, HashMap};
use testing;
use rustc::session::{mod, config};
+use rustc::session::search_paths::{SearchPaths, PathKind};
use rustc_driver::driver;
use syntax::ast;
use syntax::codemap::{CodeMap, dummy_spanned};
pub fn run(input: &str,
cfgs: Vec<String>,
- libs: Vec<Path>,
+ libs: SearchPaths,
externs: core::Externs,
mut test_args: Vec<String>,
crate_name: Option<String>)
let sessopts = config::Options {
maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
- addl_lib_search_paths: RefCell::new(libs.clone()),
+ search_paths: libs.clone(),
crate_types: vec!(config::CrateTypeDylib),
externs: externs.clone(),
..config::basic_options().clone()
0
}
-fn runtest(test: &str, cratename: &str, libs: Vec<Path>, externs: core::Externs,
+fn runtest(test: &str, cratename: &str, libs: SearchPaths,
+ externs: core::Externs,
should_fail: bool, no_run: bool, as_test_harness: bool) {
// the test harness wants its own `main` & top level functions, so
// never wrap the test in `fn main() { ... }`
let sessopts = config::Options {
maybe_sysroot: Some(os::self_exe_path().unwrap().dir_path()),
- addl_lib_search_paths: RefCell::new(libs),
+ search_paths: libs,
crate_types: vec!(config::CrateTypeExecutable),
output_types: vec!(config::OutputTypeExe),
no_trans: no_run,
let outdir = TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir");
let out = Some(outdir.path().clone());
let cfg = config::build_configuration(&sess);
- let libdir = sess.target_filesearch().get_lib_path();
+ let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
driver::compile_input(sess, cfg, &input, &out, &None, None);
if no_run { return }
pub struct Collector {
pub tests: Vec<testing::TestDescAndFn>,
names: Vec<String>,
- libs: Vec<Path>,
+ libs: SearchPaths,
externs: core::Externs,
cnt: uint,
use_headers: bool,
}
impl Collector {
- pub fn new(cratename: String, libs: Vec<Path>, externs: core::Externs,
+ pub fn new(cratename: String, libs: SearchPaths, externs: core::Externs,
use_headers: bool) -> Collector {
Collector {
tests: Vec::new(),
--- /dev/null
+-include ../tools.mk
+
+all: $(TMPDIR)/libnative.a
+ mkdir -p $(TMPDIR)/crate
+ mkdir -p $(TMPDIR)/native
+ mv $(TMPDIR)/libnative.a $(TMPDIR)/native
+ $(RUSTC) a.rs
+ mv $(TMPDIR)/liba.rlib $(TMPDIR)/crate
+ $(RUSTC) b.rs -L $(TMPDIR)/crate:native && exit 1 || exit 0
+ $(RUSTC) b.rs -L $(TMPDIR)/crate:dependency && exit 1 || exit 0
+ $(RUSTC) b.rs -L $(TMPDIR)/crate:crate
+ $(RUSTC) b.rs -L $(TMPDIR)/crate
+ $(RUSTC) c.rs -L $(TMPDIR)/crate:native && exit 1 || exit 0
+ $(RUSTC) c.rs -L $(TMPDIR)/crate:crate && exit 1 || exit 0
+ $(RUSTC) c.rs -L $(TMPDIR)/crate:dependency
+ $(RUSTC) c.rs -L $(TMPDIR)/crate
+ $(RUSTC) d.rs -L $(TMPDIR)/native:dependency && exit 1 || exit 0
+ $(RUSTC) d.rs -L $(TMPDIR)/native:crate && exit 1 || exit 0
+ $(RUSTC) d.rs -L $(TMPDIR)/native:native
+ $(RUSTC) d.rs -L $(TMPDIR)/native
+ mkdir -p $(TMPDIR)/e1
+ mkdir -p $(TMPDIR)/e2
+ $(RUSTC) e.rs -o $(TMPDIR)/e1/libe.rlib
+ $(RUSTC) e.rs -o $(TMPDIR)/e2/libe.rlib
+ $(RUSTC) f.rs -L $(TMPDIR)/e1 -L $(TMPDIR)/e2 && exit 1 || exit 0
+ $(RUSTC) f.rs -L $(TMPDIR)/e1:crate -L $(TMPDIR)/e2 && exit 1 || exit 0
+ $(RUSTC) f.rs -L $(TMPDIR)/e1:crate -L $(TMPDIR)/e2:crate && exit 1 || exit 0
+ $(RUSTC) f.rs -L $(TMPDIR)/e1:native -L $(TMPDIR)/e2
+ $(RUSTC) f.rs -L $(TMPDIR)/e1:dependency -L $(TMPDIR)/e2
+ $(RUSTC) f.rs -L $(TMPDIR)/e1:dependency -L $(TMPDIR)/e2:crate
--- /dev/null
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_type = "lib"]
--- /dev/null
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_type = "lib"]
+extern crate a;
--- /dev/null
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_type = "lib"]
+extern crate b;
+
--- /dev/null
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_type = "rlib"]
+
+#[link(name = "native", kind = "static")]
+extern {}
--- /dev/null
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_name = "e"]
+#![crate_type = "rlib"]
--- /dev/null
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_type = "rlib"]
+extern crate e;
-include ../tools.mk
all:
- ar crus libfoo.a foo.rs
- ar d libfoo.a foo.rs
+ ar crus $(TMPDIR)/libfoo.a foo.rs
+ ar d $(TMPDIR)/libfoo.a foo.rs
$(RUSTC) foo.rs