Skip to content

Commit 48b6aef

Browse files
committed
rustc: Don't fall back to -L if using --extern
The compiler would previously fall back to using `-L` and normal lookup paths if a `--extern` path was specified but it did not match (wrong architecture, for example). This commit removes this behavior and forces the hand of the crate loader to *always* use the `--extern` path if specified, no matter whether it is correct or not. This fixes a bug today where the compiler's own libraries are favored in cross compilation by accident. For example when a crate using the crates.io version of `log` was cross compiled, Cargo would compile `log` for the target architecture. When loading the macros, however, the compiler currently favors using the *host* architecture (for plugins), and because the `--extern log=...` pointed at an rlib for the target architecture, that lookup failed. The crate loader then fell back on `-L` paths to find the compiler-used `log` crate (the wrong one!) and then a compile failure happened because the logging macros are slightly different.
1 parent 0b56e9b commit 48b6aef

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

src/librustc/metadata/loader.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,8 @@ impl<'a> Context<'a> {
357357
// must be loaded via -L plus some filtering.
358358
if self.hash.is_none() {
359359
self.should_match_name = false;
360-
match self.find_commandline_library() {
361-
Some(l) => return Some(l),
362-
None => {}
360+
if let Some(s) = self.sess.opts.externs.get(self.crate_name) {
361+
return self.find_commandline_library(s);
363362
}
364363
self.should_match_name = true;
365364
}
@@ -596,12 +595,7 @@ impl<'a> Context<'a> {
596595
(t.options.dll_prefix.clone(), t.options.dll_suffix.clone())
597596
}
598597

599-
fn find_commandline_library(&mut self) -> Option<Library> {
600-
let locs = match self.sess.opts.externs.get(self.crate_name) {
601-
Some(s) => s,
602-
None => return None,
603-
};
604-
598+
fn find_commandline_library(&mut self, locs: &[String]) -> Option<Library> {
605599
// First, filter out all libraries that look suspicious. We only accept
606600
// files which actually exist that have the correct naming scheme for
607601
// rlibs/dylibs.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-include ../tools.mk
2+
3+
HOST := $(shell $(RUSTC) -vV | grep 'host:' | sed 's/host: //')
4+
ifeq ($(findstring i686,$(HOST)),i686)
5+
TARGET := $(subst i686,x86_64,$(HOST))
6+
else
7+
TARGET := $(subst x86_64,i686,$(HOST))
8+
endif
9+
10+
all:
11+
$(RUSTC) foo.rs -C extra-filename=-host
12+
$(RUSTC) bar.rs -C extra-filename=-targ --target $(TARGET)
13+
$(RUSTC) baz.rs --extern a=$(TMPDIR)/liba-targ.rlib --target $(TARGET)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 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+
#![feature(no_std)]
12+
#![no_std]
13+
#![crate_type = "lib"]
14+
#![crate_name = "a"]
15+
16+
#[macro_export]
17+
macro_rules! bar {
18+
() => ()
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
#![feature(no_std)]
12+
#![no_std]
13+
#![crate_type = "lib"]
14+
15+
#[macro_use]
16+
extern crate a;
17+
18+
bar!();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2015 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+
#![feature(no_std)]
12+
#![no_std]
13+
#![crate_type = "lib"]
14+
#![crate_name = "a"]
15+
16+
#[macro_export]
17+
macro_rules! foo {
18+
() => ()
19+
}

0 commit comments

Comments
 (0)