Skip to content

Commit a38a082

Browse files
committed
fix verbatim with upstream dependencies
rust-lang#99425 (comment) r? `@petrochenkov`
1 parent a5b58ad commit a38a082

File tree

8 files changed

+75
-19
lines changed

8 files changed

+75
-19
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ impl<'a> Linker for GccLinker<'a> {
439439
}
440440
}
441441
self.hint_dynamic();
442-
self.cmd.arg(format!("-l{}{}", if verbatim { ":" } else { "" }, lib));
442+
self.cmd.arg(format!(
443+
"-l{}{lib}",
444+
if verbatim && self.sess.target.linker_is_gnu { ":" } else { "" },
445+
));
443446
if !as_needed {
444447
if self.sess.target.is_like_osx {
445448
// See above FIXME comment
@@ -450,7 +453,10 @@ impl<'a> Linker for GccLinker<'a> {
450453
}
451454
fn link_staticlib(&mut self, lib: &str, verbatim: bool) {
452455
self.hint_static();
453-
self.cmd.arg(format!("-l{}{}", if verbatim { ":" } else { "" }, lib));
456+
self.cmd.arg(format!(
457+
"-l{}{lib}",
458+
if verbatim && self.sess.target.linker_is_gnu { ":" } else { "" },
459+
));
454460
}
455461
fn link_rlib(&mut self, lib: &Path) {
456462
self.hint_static();
@@ -504,10 +510,10 @@ impl<'a> Linker for GccLinker<'a> {
504510
self.hint_static();
505511
let target = &self.sess.target;
506512
if !target.is_like_osx {
507-
self.linker_arg("--whole-archive").cmd.arg(format!(
508-
"-l{}{}",
509-
if verbatim { ":" } else { "" },
510-
lib
513+
self.linker_arg("--whole-archive");
514+
self.cmd.arg(format!(
515+
"-l{}{lib}",
516+
if verbatim && self.sess.target.linker_is_gnu { ":" } else { "" },
511517
));
512518
self.linker_arg("--no-whole-archive");
513519
} else {

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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) main.rs -Zunstable-options -l static:+verbatim=local_some_strange_name.ext
10+
11+
# With verbatim any other name cannot be used (local).
12+
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/liblocal_native_dep.a
13+
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.a
14+
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.lib
15+
$(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern "C" {
2+
fn local_native_f() -> i32;
3+
}
4+
5+
pub fn main() {
6+
unsafe {
7+
assert!(local_native_f() == 0);
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
include ../../run-make-fulldeps/tools.mk
2+
3+
all:
4+
# Verbatim allows specify precise name.
5+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_some_strange_name.ext
6+
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib
7+
8+
# With verbatim any other name cannot be used (upstream).
9+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/libupstream_native_dep.a
10+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.a
11+
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.lib
12+
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"
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)