Skip to content

Commit

Permalink
rustc: Add knowledge of separate lookup paths
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Dec 23, 2014
1 parent 62fb41c commit d085d9d
Show file tree
Hide file tree
Showing 20 changed files with 251 additions and 60 deletions.
21 changes: 13 additions & 8 deletions src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use back::svh::Svh;
use session::{config, Session};
use session::search_paths::PathKind;
use metadata::cstore;
use metadata::cstore::{CStore, CrateSource};
use metadata::decoder;
Expand Down Expand Up @@ -134,7 +135,8 @@ fn visit_view_item(e: &mut Env, i: &ast::ViewItem) {
info.ident[],
info.name[],
None,
i.span);
i.span,
PathKind::Crate);
e.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
}
None => ()
Expand Down Expand Up @@ -388,12 +390,13 @@ fn register_crate<'a>(e: &mut Env,
(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) {
Expand All @@ -404,7 +407,7 @@ fn resolve_crate<'a>(e: &mut Env,
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!(),
Expand Down Expand Up @@ -434,7 +437,8 @@ fn resolve_crate_deps(e: &mut Env,
dep.name[],
dep.name[],
Some(&dep.hash),
span);
span,
PathKind::Dependency);
(dep.cnum, local_cnum)
}).collect()
}
Expand All @@ -453,7 +457,8 @@ impl<'a> PluginMetadataReader<'a> {
}
}

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();
Expand All @@ -464,7 +469,7 @@ impl<'a> PluginMetadataReader<'a> {
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!(),
Expand All @@ -477,7 +482,7 @@ impl<'a> PluginMetadataReader<'a> {
// 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 \
Expand Down
15 changes: 8 additions & 7 deletions src/librustc/metadata/filesearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

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 {
Expand All @@ -36,8 +36,9 @@ pub type pick<'a> = |path: &Path|: 'a -> 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> {
Expand All @@ -47,9 +48,7 @@ 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 => ()
Expand Down Expand Up @@ -133,12 +132,14 @@ impl<'a> FileSearch<'a> {

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,
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub use self::OutputType::*;
pub use self::DebugInfoLevel::*;

use session::{early_error, Session};
use session::search_paths::SearchPaths;

use rustc_back::target::Target;
use lint;
Expand All @@ -35,7 +36,6 @@ use syntax::parse::token::InternedString;
use std::collections::HashMap;
use std::collections::hash_map::Entry::{Occupied, Vacant};
use getopts;
use std::cell::{RefCell};
use std::fmt;

use llvm;
Expand Down Expand Up @@ -86,7 +86,7 @@ pub struct Options {
// 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,
Expand Down Expand Up @@ -198,7 +198,7 @@ pub fn basic_options() -> Options {
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(),
Expand Down Expand Up @@ -1007,9 +1007,10 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
}
};

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, ':');
Expand Down Expand Up @@ -1109,7 +1110,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
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,
Expand Down
14 changes: 9 additions & 5 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
// 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;
Expand All @@ -28,6 +29,7 @@ use std::os;
use std::cell::{Cell, RefCell};

pub mod config;
pub mod search_paths;

// Represents the data associated with a compilation
// session for a single crate.
Expand Down Expand Up @@ -209,16 +211,18 @@ impl Session {
.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)
}
}

Expand Down
69 changes: 69 additions & 0 deletions src/librustc/session/search_paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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,
}
}
}
}
5 changes: 3 additions & 2 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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};
Expand Down Expand Up @@ -256,7 +257,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
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());
}
Expand Down Expand Up @@ -516,7 +517,7 @@ pub fn phase_6_link_output(sess: &Session,
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());

Expand Down
23 changes: 11 additions & 12 deletions src/librustc_trans/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ use super::svh::Svh;
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;
Expand Down Expand Up @@ -504,10 +506,11 @@ fn link_binary_output(sess: &Session,
}

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;
}

Expand Down Expand Up @@ -832,7 +835,7 @@ fn link_args(cmd: &mut Command,

// 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;
Expand Down Expand Up @@ -1040,14 +1043,10 @@ fn link_args(cmd: &mut Command,
// 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
Expand Down
Loading

12 comments on commit d085d9d

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@d085d9d

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/issue-19767 = d085d9d into auto

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status: {"merge_sha": "bf6fda5a3f280b89a3b5e8a0215f8e358bd8fcf3"}

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 29, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/issue-19767 = d085d9d merged ok, testing candidate = bf6fda5a

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@d085d9d

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/issue-19767 = d085d9d into auto

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status: {"merge_sha": "023dfb0c898d851dee6ace2f8339b73b5287136b"}

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/issue-19767 = d085d9d merged ok, testing candidate = 023dfb0

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 023dfb0

@bors
Copy link
Contributor

@bors bors commented on d085d9d Dec 30, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 023dfb0

Please sign in to comment.