Skip to content

Commit 86730e4

Browse files
committed
auto merge of rust-lang#16904 : inrustwetrust/rust/link-path-order, r=alexcrichton
Issue can be reproduced by the following: ``` $ cat main.rs fn main() {} $ rustc -Z print-link-args -Lfoo -Lbar main.rs ``` Run the rustc command a few times and observe that the order of the '-L' 'foo' '-L' 'bar' options randomly changes. Actually hit this issue in practice on Windows when specifying two -L directories to rustc, one with rust-sdl2 in it and one with the C SDL2.dll. Since Windows file systems aren't case-sensitive, gcc randomly attempted to link against the rust sdl2.dll instead of SDL2.dll if that -L directory happened to come first. The randomness was due to addl_lib_search_paths being a HashSet. Changed it to a Vec instead which maintains the ordering. Unsure how to test this though since it is random by nature; suggestions very welcome.
2 parents d7502ac + e7a000e commit 86730e4

File tree

11 files changed

+60
-24
lines changed

11 files changed

+60
-24
lines changed

src/librustc/back/link.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use util::ppaux;
2828
use util::sha2::{Digest, Sha256};
2929

3030
use std::char;
31-
use std::collections::HashSet;
3231
use std::io::{fs, TempDir, Command};
3332
use std::io;
3433
use std::mem;
@@ -570,10 +569,7 @@ fn link_binary_output(sess: &Session,
570569
fn archive_search_paths(sess: &Session) -> Vec<Path> {
571570
let mut rustpath = filesearch::rust_path();
572571
rustpath.push(sess.target_filesearch().get_lib_path());
573-
// FIXME: Addl lib search paths are an unordered HashSet?
574-
// Shouldn't this search be done in some order?
575-
let addl_lib_paths: HashSet<Path> = sess.opts.addl_lib_search_paths.borrow().clone();
576-
let mut search: Vec<Path> = addl_lib_paths.move_iter().collect();
572+
let mut search: Vec<Path> = sess.opts.addl_lib_search_paths.borrow().clone();
577573
search.push_all(rustpath.as_slice());
578574
return search;
579575
}

src/librustc/driver/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use syntax::diagnostic::{ColorConfig, Auto, Always, Never};
3030
use syntax::parse;
3131
use syntax::parse::token::InternedString;
3232

33-
use std::collections::{HashSet, HashMap};
33+
use std::collections::HashMap;
3434
use getopts::{optopt, optmulti, optflag, optflagopt};
3535
use getopts;
3636
use std::cell::{RefCell};
@@ -76,7 +76,7 @@ pub struct Options {
7676
// This was mutable for rustpkg, which updates search paths based on the
7777
// parsed code. It remains mutable in case its replacements wants to use
7878
// this.
79-
pub addl_lib_search_paths: RefCell<HashSet<Path>>,
79+
pub addl_lib_search_paths: RefCell<Vec<Path>>,
8080
pub maybe_sysroot: Option<Path>,
8181
pub target_triple: String,
8282
// User-specified cfg meta items. The compiler itself will add additional
@@ -113,7 +113,7 @@ pub fn basic_options() -> Options {
113113
lint_opts: Vec::new(),
114114
describe_lints: false,
115115
output_types: Vec::new(),
116-
addl_lib_search_paths: RefCell::new(HashSet::new()),
116+
addl_lib_search_paths: RefCell::new(Vec::new()),
117117
maybe_sysroot: None,
118118
target_triple: driver::host_triple().to_string(),
119119
cfg: Vec::new(),

src/librustc/metadata/filesearch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub type pick<'a> = |path: &Path|: 'a -> FileMatch;
3030

3131
pub struct FileSearch<'a> {
3232
pub sysroot: &'a Path,
33-
pub addl_lib_search_paths: &'a RefCell<HashSet<Path>>,
33+
pub addl_lib_search_paths: &'a RefCell<Vec<Path>>,
3434
pub triple: &'a str,
3535
}
3636

@@ -125,7 +125,7 @@ impl<'a> FileSearch<'a> {
125125

126126
pub fn new(sysroot: &'a Path,
127127
triple: &'a str,
128-
addl_lib_search_paths: &'a RefCell<HashSet<Path>>) -> FileSearch<'a> {
128+
addl_lib_search_paths: &'a RefCell<Vec<Path>>) -> FileSearch<'a> {
129129
debug!("using sysroot = {}, triple = {}", sysroot.display(), triple);
130130
FileSearch {
131131
sysroot: sysroot,

src/librustdoc/core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -80,7 +80,7 @@ pub struct CrateAnalysis {
8080
pub type Externs = HashMap<String, Vec<String>>;
8181

8282
/// Parses, resolves, and typechecks the given crate
83-
fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>,
83+
fn get_ast_and_resolve(cpath: &Path, libs: Vec<Path>, cfgs: Vec<String>,
8484
externs: Externs, triple: Option<String>)
8585
-> (DocContext, CrateAnalysis) {
8686
use syntax::codemap::dummy_spanned;
@@ -153,7 +153,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>,
153153
})
154154
}
155155

156-
pub fn run_core(libs: HashSet<Path>, cfgs: Vec<String>, externs: Externs,
156+
pub fn run_core(libs: Vec<Path>, cfgs: Vec<String>, externs: Externs,
157157
path: &Path, triple: Option<String>)
158158
-> (clean::Crate, CrateAnalysis) {
159159
let (ctxt, analysis) = get_ast_and_resolve(path, libs, cfgs, externs, triple);

src/librustdoc/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,7 @@ fn rust_input(cratefile: &str, externs: core::Externs, matches: &getopts::Matche
369369
info!("starting to run rustc");
370370
let (mut krate, analysis) = std::task::try(proc() {
371371
let cr = cr;
372-
core::run_core(libs.move_iter().collect(),
373-
cfgs,
374-
externs,
375-
&cr,
376-
triple)
372+
core::run_core(libs, cfgs, externs, &cr, triple)
377373
}).map_err(|boxed_any|format!("{:?}", boxed_any)).unwrap();
378374
info!("finished with rustc");
379375
analysiskey.replace(Some(analysis));

src/librustdoc/markdown.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::collections::HashSet;
1211
use std::io;
1312
use std::string::String;
1413

@@ -136,7 +135,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
136135
}
137136

138137
/// Run any tests/code examples in the markdown file `input`.
139-
pub fn test(input: &str, libs: HashSet<Path>, externs: core::Externs,
138+
pub fn test(input: &str, libs: Vec<Path>, externs: core::Externs,
140139
mut test_args: Vec<String>) -> int {
141140
let input_str = load_or_return!(input, 1, 2);
142141

src/librustdoc/test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use visit_ast::RustdocVisitor;
3939

4040
pub fn run(input: &str,
4141
cfgs: Vec<String>,
42-
libs: HashSet<Path>,
42+
libs: Vec<Path>,
4343
externs: core::Externs,
4444
mut test_args: Vec<String>,
4545
crate_name: Option<String>)
@@ -109,7 +109,7 @@ pub fn run(input: &str,
109109
0
110110
}
111111

112-
fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, externs: core::Externs,
112+
fn runtest(test: &str, cratename: &str, libs: Vec<Path>, externs: core::Externs,
113113
should_fail: bool, no_run: bool, as_test_harness: bool) {
114114
// the test harness wants its own `main` & top level functions, so
115115
// never wrap the test in `fn main() { ... }`
@@ -244,7 +244,7 @@ pub fn maketest(s: &str, cratename: Option<&str>, lints: bool, dont_insert_main:
244244
pub struct Collector {
245245
pub tests: Vec<testing::TestDescAndFn>,
246246
names: Vec<String>,
247-
libs: HashSet<Path>,
247+
libs: Vec<Path>,
248248
externs: core::Externs,
249249
cnt: uint,
250250
use_headers: bool,
@@ -253,7 +253,7 @@ pub struct Collector {
253253
}
254254

255255
impl Collector {
256-
pub fn new(cratename: String, libs: HashSet<Path>, externs: core::Externs,
256+
pub fn new(cratename: String, libs: Vec<Path>, externs: core::Externs,
257257
use_headers: bool) -> Collector {
258258
Collector {
259259
tests: Vec::new(),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-include ../tools.mk
2+
3+
# Verifies that the -L arguments given to the linker is in the same order
4+
# as the -L arguments on the rustc command line.
5+
6+
CORRECT_DIR=$(TMPDIR)/correct
7+
WRONG_DIR=$(TMPDIR)/wrong
8+
9+
all: $(TMPDIR)/libcorrect.a $(TMPDIR)/libwrong.a
10+
mkdir -p $(CORRECT_DIR) $(WRONG_DIR)
11+
mv $(TMPDIR)/libcorrect.a $(CORRECT_DIR)/libfoo.a
12+
mv $(TMPDIR)/libwrong.a $(WRONG_DIR)/libfoo.a
13+
$(RUSTC) main.rs -o $(TMPDIR)/should_succeed -L $(CORRECT_DIR) -L $(WRONG_DIR)
14+
$(call RUN,should_succeed)
15+
$(RUSTC) main.rs -o $(TMPDIR)/should_fail -L $(WRONG_DIR) -L $(CORRECT_DIR)
16+
$(call FAIL,should_fail)
17+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int should_return_one() { return 1; }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
extern crate libc;
12+
13+
#[link(name="foo")]
14+
extern {
15+
fn should_return_one() -> libc::c_int;
16+
}
17+
18+
fn main() {
19+
let result = unsafe {
20+
should_return_one()
21+
};
22+
23+
if result != 1 {
24+
std::os::set_exit_status(255);
25+
}
26+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int should_return_one() { return 0; }

0 commit comments

Comments
 (0)