Skip to content

Commit 023dfb0

Browse files
committed
auto merge of #19941 : alexcrichton/rust/issue-19767, r=brson
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
2 parents d2368c3 + d085d9d commit 023dfb0

File tree

20 files changed

+251
-60
lines changed

20 files changed

+251
-60
lines changed

src/librustc/metadata/creader.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
1515
use back::svh::Svh;
1616
use session::{config, Session};
17+
use session::search_paths::PathKind;
1718
use metadata::cstore;
1819
use metadata::cstore::{CStore, CrateSource};
1920
use metadata::decoder;
@@ -134,7 +135,8 @@ fn visit_view_item(e: &mut Env, i: &ast::ViewItem) {
134135
info.ident[],
135136
info.name[],
136137
None,
137-
i.span);
138+
i.span,
139+
PathKind::Crate);
138140
e.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
139141
}
140142
None => ()
@@ -388,12 +390,13 @@ fn register_crate<'a>(e: &mut Env,
388390
(cnum, cmeta, source)
389391
}
390392

391-
fn resolve_crate<'a>(e: &mut Env,
393+
fn resolve_crate(e: &mut Env,
392394
root: &Option<CratePaths>,
393395
ident: &str,
394396
name: &str,
395397
hash: Option<&Svh>,
396-
span: Span)
398+
span: Span,
399+
kind: PathKind)
397400
-> (ast::CrateNum, Rc<cstore::crate_metadata>,
398401
cstore::CrateSource) {
399402
match existing_match(e, name, hash) {
@@ -404,7 +407,7 @@ fn resolve_crate<'a>(e: &mut Env,
404407
ident: ident,
405408
crate_name: name,
406409
hash: hash.map(|a| &*a),
407-
filesearch: e.sess.target_filesearch(),
410+
filesearch: e.sess.target_filesearch(kind),
408411
triple: e.sess.opts.target_triple[],
409412
root: root,
410413
rejected_via_hash: vec!(),
@@ -434,7 +437,8 @@ fn resolve_crate_deps(e: &mut Env,
434437
dep.name[],
435438
dep.name[],
436439
Some(&dep.hash),
437-
span);
440+
span,
441+
PathKind::Dependency);
438442
(dep.cnum, local_cnum)
439443
}).collect()
440444
}
@@ -453,7 +457,8 @@ impl<'a> PluginMetadataReader<'a> {
453457
}
454458
}
455459

456-
pub fn read_plugin_metadata(&mut self, krate: &ast::ViewItem) -> PluginMetadata {
460+
pub fn read_plugin_metadata(&mut self,
461+
krate: &ast::ViewItem) -> PluginMetadata {
457462
let info = extract_crate_info(&self.env, krate).unwrap();
458463
let target_triple = self.env.sess.opts.target_triple[];
459464
let is_cross = target_triple != config::host_triple();
@@ -464,7 +469,7 @@ impl<'a> PluginMetadataReader<'a> {
464469
ident: info.ident[],
465470
crate_name: info.name[],
466471
hash: None,
467-
filesearch: self.env.sess.host_filesearch(),
472+
filesearch: self.env.sess.host_filesearch(PathKind::Crate),
468473
triple: config::host_triple(),
469474
root: &None,
470475
rejected_via_hash: vec!(),
@@ -477,7 +482,7 @@ impl<'a> PluginMetadataReader<'a> {
477482
// try loading from target crates (only valid if there are
478483
// no syntax extensions)
479484
load_ctxt.triple = target_triple;
480-
load_ctxt.filesearch = self.env.sess.target_filesearch();
485+
load_ctxt.filesearch = self.env.sess.target_filesearch(PathKind::Crate);
481486
let lib = load_ctxt.load_library_crate();
482487
if decoder::get_plugin_registrar_fn(lib.metadata.as_slice()).is_some() {
483488
let message = format!("crate `{}` contains a plugin_registrar fn but \

src/librustc/metadata/filesearch.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
pub use self::FileMatch::*;
1414

15-
use std::cell::RefCell;
1615
use std::collections::HashSet;
1716
use std::io::fs::PathExtensions;
1817
use std::io::fs;
1918
use std::os;
2019

2120
use util::fs as myfs;
21+
use session::search_paths::{SearchPaths, PathKind};
2222

2323
#[deriving(Copy)]
2424
pub enum FileMatch {
@@ -36,8 +36,9 @@ pub type pick<'a> = |path: &Path|: 'a -> FileMatch;
3636

3737
pub struct FileSearch<'a> {
3838
pub sysroot: &'a Path,
39-
pub addl_lib_search_paths: &'a RefCell<Vec<Path>>,
39+
pub search_paths: &'a SearchPaths,
4040
pub triple: &'a str,
41+
pub kind: PathKind,
4142
}
4243

4344
impl<'a> FileSearch<'a> {
@@ -47,9 +48,7 @@ impl<'a> FileSearch<'a> {
4748
let mut visited_dirs = HashSet::new();
4849
let mut found = false;
4950

50-
debug!("filesearch: searching additional lib search paths [{}]",
51-
self.addl_lib_search_paths.borrow().len());
52-
for path in self.addl_lib_search_paths.borrow().iter() {
51+
for path in self.search_paths.iter(self.kind) {
5352
match f(path) {
5453
FileMatches => found = true,
5554
FileDoesntMatch => ()
@@ -133,12 +132,14 @@ impl<'a> FileSearch<'a> {
133132

134133
pub fn new(sysroot: &'a Path,
135134
triple: &'a str,
136-
addl_lib_search_paths: &'a RefCell<Vec<Path>>) -> FileSearch<'a> {
135+
search_paths: &'a SearchPaths,
136+
kind: PathKind) -> FileSearch<'a> {
137137
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
138138
FileSearch {
139139
sysroot: sysroot,
140-
addl_lib_search_paths: addl_lib_search_paths,
140+
search_paths: search_paths,
141141
triple: triple,
142+
kind: kind,
142143
}
143144
}
144145

src/librustc/session/config.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub use self::OutputType::*;
1919
pub use self::DebugInfoLevel::*;
2020

2121
use session::{early_error, Session};
22+
use session::search_paths::SearchPaths;
2223

2324
use rustc_back::target::Target;
2425
use lint;
@@ -35,7 +36,6 @@ use syntax::parse::token::InternedString;
3536
use std::collections::HashMap;
3637
use std::collections::hash_map::Entry::{Occupied, Vacant};
3738
use getopts;
38-
use std::cell::{RefCell};
3939
use std::fmt;
4040

4141
use llvm;
@@ -86,7 +86,7 @@ pub struct Options {
8686
// This was mutable for rustpkg, which updates search paths based on the
8787
// parsed code. It remains mutable in case its replacements wants to use
8888
// this.
89-
pub addl_lib_search_paths: RefCell<Vec<Path>>,
89+
pub search_paths: SearchPaths,
9090
pub libs: Vec<(String, cstore::NativeLibraryKind)>,
9191
pub maybe_sysroot: Option<Path>,
9292
pub target_triple: String,
@@ -198,7 +198,7 @@ pub fn basic_options() -> Options {
198198
lint_opts: Vec::new(),
199199
describe_lints: false,
200200
output_types: Vec::new(),
201-
addl_lib_search_paths: RefCell::new(Vec::new()),
201+
search_paths: SearchPaths::new(),
202202
maybe_sysroot: None,
203203
target_triple: host_triple().to_string(),
204204
cfg: Vec::new(),
@@ -1010,9 +1010,10 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10101010
}
10111011
};
10121012

1013-
let addl_lib_search_paths = matches.opt_strs("L").iter().map(|s| {
1014-
Path::new(s[])
1015-
}).collect();
1013+
let mut search_paths = SearchPaths::new();
1014+
for s in matches.opt_strs("L").iter() {
1015+
search_paths.add_path(s[]);
1016+
}
10161017

10171018
let libs = matches.opt_strs("l").into_iter().map(|s| {
10181019
let mut parts = s.rsplitn(1, ':');
@@ -1112,7 +1113,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
11121113
lint_opts: lint_opts,
11131114
describe_lints: describe_lints,
11141115
output_types: output_types,
1115-
addl_lib_search_paths: RefCell::new(addl_lib_search_paths),
1116+
search_paths: search_paths,
11161117
maybe_sysroot: sysroot_opt,
11171118
target_triple: target,
11181119
cfg: cfg,

src/librustc/session/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
// except according to those terms.
1010

1111

12+
use lint;
1213
use metadata::cstore::CStore;
1314
use metadata::filesearch;
14-
use lint;
15+
use session::search_paths::PathKind;
1516
use util::nodemap::NodeMap;
1617

1718
use syntax::ast::NodeId;
@@ -28,6 +29,7 @@ use std::os;
2829
use std::cell::{Cell, RefCell};
2930

3031
pub mod config;
32+
pub mod search_paths;
3133

3234
// Represents the data associated with a compilation
3335
// session for a single crate.
@@ -212,16 +214,18 @@ impl Session {
212214
.expect("missing sysroot and default_sysroot in Session")
213215
}
214216
}
215-
pub fn target_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> {
217+
pub fn target_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
216218
filesearch::FileSearch::new(self.sysroot(),
217219
self.opts.target_triple[],
218-
&self.opts.addl_lib_search_paths)
220+
&self.opts.search_paths,
221+
kind)
219222
}
220-
pub fn host_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> {
223+
pub fn host_filesearch(&self, kind: PathKind) -> filesearch::FileSearch {
221224
filesearch::FileSearch::new(
222225
self.sysroot(),
223226
config::host_triple(),
224-
&self.opts.addl_lib_search_paths)
227+
&self.opts.search_paths,
228+
kind)
225229
}
226230
}
227231

src/librustc/session/search_paths.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::slice;
12+
13+
#[deriving(Clone)]
14+
pub struct SearchPaths {
15+
paths: Vec<(PathKind, Path)>,
16+
}
17+
18+
pub struct Iter<'a> {
19+
kind: PathKind,
20+
iter: slice::Iter<'a, (PathKind, Path)>,
21+
}
22+
23+
#[deriving(Eq, PartialEq, Clone, Copy)]
24+
pub enum PathKind {
25+
Native,
26+
Crate,
27+
Dependency,
28+
All,
29+
}
30+
31+
impl SearchPaths {
32+
pub fn new() -> SearchPaths {
33+
SearchPaths { paths: Vec::new() }
34+
}
35+
36+
pub fn add_path(&mut self, path: &str) {
37+
let (kind, path) = if path.ends_with(":native") {
38+
(PathKind::Native, path.slice_to(path.len() - ":native".len()))
39+
} else if path.ends_with(":crate") {
40+
(PathKind::Crate, path.slice_to(path.len() - ":crate".len()))
41+
} else if path.ends_with(":dependency") {
42+
(PathKind::Dependency,
43+
path.slice_to(path.len() - ":dependency".len()))
44+
} else if path.ends_with(":all") {
45+
(PathKind::All, path.slice_to(path.len() - ":all".len()))
46+
} else {
47+
(PathKind::All, path)
48+
};
49+
self.paths.push((kind, Path::new(path)));
50+
}
51+
52+
pub fn iter(&self, kind: PathKind) -> Iter {
53+
Iter { kind: kind, iter: self.paths.iter() }
54+
}
55+
}
56+
57+
impl<'a> Iterator<&'a Path> for Iter<'a> {
58+
fn next(&mut self) -> Option<&'a Path> {
59+
loop {
60+
match self.iter.next() {
61+
Some(&(kind, ref p)) if self.kind == PathKind::All ||
62+
kind == PathKind::All ||
63+
kind == self.kind => return Some(p),
64+
Some(..) => {}
65+
None => return None,
66+
}
67+
}
68+
}
69+
}

src/librustc_driver/driver.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use rustc::session::Session;
1212
use rustc::session::config::{mod, Input, OutputFilenames};
13+
use rustc::session::search_paths::PathKind;
1314
use rustc::lint;
1415
use rustc::metadata::creader;
1516
use rustc::middle::{stability, ty, reachable};
@@ -266,7 +267,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
266267
let mut _old_path = String::new();
267268
if cfg!(windows) {
268269
_old_path = os::getenv("PATH").unwrap_or(_old_path);
269-
let mut new_path = sess.host_filesearch().get_dylib_search_paths();
270+
let mut new_path = sess.host_filesearch(PathKind::All).get_dylib_search_paths();
270271
new_path.extend(os::split_paths(_old_path[]).into_iter());
271272
os::setenv("PATH", os::join_paths(new_path[]).unwrap());
272273
}
@@ -541,7 +542,7 @@ pub fn phase_6_link_output(sess: &Session,
541542
trans: &trans::CrateTranslation,
542543
outputs: &OutputFilenames) {
543544
let old_path = os::getenv("PATH").unwrap_or_else(||String::new());
544-
let mut new_path = sess.host_filesearch().get_tools_search_paths();
545+
let mut new_path = sess.host_filesearch(PathKind::All).get_tools_search_paths();
545546
new_path.extend(os::split_paths(old_path[]).into_iter());
546547
os::setenv("PATH", os::join_paths(new_path[]).unwrap());
547548

src/librustc_trans/back/link.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ use super::svh::Svh;
1616
use session::config;
1717
use session::config::NoDebugInfo;
1818
use session::config::{OutputFilenames, Input, OutputTypeBitcode, OutputTypeExe, OutputTypeObject};
19+
use session::search_paths::PathKind;
1920
use session::Session;
2021
use metadata::common::LinkMeta;
2122
use metadata::{encoder, cstore, filesearch, csearch, creader};
23+
use metadata::filesearch::FileDoesntMatch;
2224
use trans::{CrateContext, CrateTranslation, gensym_name};
2325
use middle::ty::{mod, Ty};
2426
use util::common::time;
@@ -504,10 +506,11 @@ fn link_binary_output(sess: &Session,
504506
}
505507

506508
fn archive_search_paths(sess: &Session) -> Vec<Path> {
507-
let mut rustpath = filesearch::rust_path();
508-
rustpath.push(sess.target_filesearch().get_lib_path());
509-
let mut search: Vec<Path> = sess.opts.addl_lib_search_paths.borrow().clone();
510-
search.push_all(rustpath[]);
509+
let mut search = Vec::new();
510+
sess.target_filesearch(PathKind::Native).for_each_lib_search_path(|path| {
511+
search.push(path.clone());
512+
FileDoesntMatch
513+
});
511514
return search;
512515
}
513516

@@ -832,7 +835,7 @@ fn link_args(cmd: &mut Command,
832835

833836
// The default library location, we need this to find the runtime.
834837
// The location of crates will be determined as needed.
835-
let lib_path = sess.target_filesearch().get_lib_path();
838+
let lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
836839

837840
// target descriptor
838841
let t = &sess.target.target;
@@ -1040,14 +1043,10 @@ fn link_args(cmd: &mut Command,
10401043
// in the current crate. Upstream crates with native library dependencies
10411044
// may have their native library pulled in above.
10421045
fn add_local_native_libraries(cmd: &mut Command, sess: &Session) {
1043-
for path in sess.opts.addl_lib_search_paths.borrow().iter() {
1044-
cmd.arg("-L").arg(path);
1045-
}
1046-
1047-
let rustpath = filesearch::rust_path();
1048-
for path in rustpath.iter() {
1046+
sess.target_filesearch(PathKind::All).for_each_lib_search_path(|path| {
10491047
cmd.arg("-L").arg(path);
1050-
}
1048+
FileDoesntMatch
1049+
});
10511050

10521051
// Some platforms take hints about whether a library is static or dynamic.
10531052
// For those that support this, we ensure we pass the option if the library

0 commit comments

Comments
 (0)