Skip to content

Commit 80627cd

Browse files
committed
Auto merge of #22023 - alexcrichton:oops-picked-the-wrong-plugin, r=nikomatsakis
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.
2 parents a08504b + 48b6aef commit 80627cd

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
@@ -370,9 +370,8 @@ impl<'a> Context<'a> {
370370
// must be loaded via -L plus some filtering.
371371
if self.hash.is_none() {
372372
self.should_match_name = false;
373-
match self.find_commandline_library() {
374-
Some(l) => return Some(l),
375-
None => {}
373+
if let Some(s) = self.sess.opts.externs.get(self.crate_name) {
374+
return self.find_commandline_library(s);
376375
}
377376
self.should_match_name = true;
378377
}
@@ -619,12 +618,7 @@ impl<'a> Context<'a> {
619618
(t.options.dll_prefix.clone(), t.options.dll_suffix.clone())
620619
}
621620

622-
fn find_commandline_library(&mut self) -> Option<Library> {
623-
let locs = match self.sess.opts.externs.get(self.crate_name) {
624-
Some(s) => s,
625-
None => return None,
626-
};
627-
621+
fn find_commandline_library(&mut self, locs: &[String]) -> Option<Library> {
628622
// First, filter out all libraries that look suspicious. We only accept
629623
// files which actually exist that have the correct naming scheme for
630624
// 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)