Skip to content

Commit 6515270

Browse files
committed
fix verbatim behavior with upstream dependencies
1 parent a5b58ad commit 6515270

File tree

6 files changed

+66
-13
lines changed

6 files changed

+66
-13
lines changed

Diff for: compiler/rustc_metadata/src/native_libs.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,25 @@ pub fn find_native_static_library(
3333
search_paths: &[PathBuf],
3434
sess: &Session,
3535
) -> PathBuf {
36-
let verbatim = verbatim.unwrap_or(false);
37-
// On Windows, static libraries sometimes show up as libfoo.a and other
38-
// times show up as foo.lib
39-
let oslibname = if verbatim {
40-
name.to_string()
36+
let formats = if verbatim.unwrap_or(false) {
37+
vec![("".into(), "".into())]
4138
} else {
42-
format!("{}{}{}", sess.target.staticlib_prefix, name, sess.target.staticlib_suffix)
39+
let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
40+
// On Windows, static libraries sometimes show up as libfoo.a and other
41+
// times show up as foo.lib
42+
let unix = ("lib".into(), ".a".into());
43+
if os == unix { vec![os] } else { vec![os, unix] }
4344
};
44-
let unixlibname = format!("lib{}.a", name);
4545

4646
for path in search_paths {
47-
let test = path.join(&oslibname);
48-
if test.exists() {
49-
return test;
50-
}
51-
if oslibname != unixlibname {
52-
let test = path.join(&unixlibname);
47+
for (prefix, suffix) in &formats {
48+
let test = path.join(format!("{}{}{}", prefix, name, suffix));
5349
if test.exists() {
5450
return test;
5551
}
5652
}
5753
}
54+
5855
sess.emit_fatal(MissingNativeLibrary { libname: name });
5956
}
6057

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# ignore-cross-compile
2+
# ignore-macos
3+
4+
include ../../run-make-fulldeps/tools.mk
5+
6+
all:
7+
# Verbatim allows specify precise name.
8+
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_some_strange_name.ext
9+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_some_strange_name.ext
10+
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib
11+
$(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep.rlib -Zunstable-options -l static:+verbatim=local_some_strange_name.ext
12+
rm $(TMPDIR)/*some_strange_name.ext
13+
14+
# With verbatim any other name cannot be used (local).
15+
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/liblocal_native_dep.a
16+
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.a
17+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep
18+
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib
19+
$(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep.rlib -Zunstable-options -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep"
20+
rm $(TMPDIR)/*native_dep*
21+
22+
# With verbatim any other name cannot be used (upstream).
23+
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep
24+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/libupstream_native_dep.a
25+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.a
26+
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"
27+
rm $(TMPDIR)/*native_dep*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[no_mangle]
2+
pub fn local_native_f() -> i32 {
3+
return 0;
4+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extern crate rust_dep;
2+
3+
extern "C" {
4+
fn local_native_f() -> i32;
5+
}
6+
7+
pub fn main() {
8+
unsafe {
9+
assert!(local_native_f() == 0);
10+
};
11+
rust_dep::rust_dep()
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern "C" {
2+
fn upstream_native_f() -> i32;
3+
}
4+
5+
pub fn rust_dep() {
6+
unsafe {
7+
assert!(upstream_native_f() == 0);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[no_mangle]
2+
pub fn upstream_native_f() -> i32 {
3+
return 0;
4+
}

0 commit comments

Comments
 (0)