Auto merge of #101806 - BelovDV:issue-fix-fn-find_library, r=petrochenkov

fix verbatim with upstream dependencies

https://github.com/rust-lang/rust/issues/99425#issuecomment-1207224161

r? `@petrochenkov`
This commit is contained in:
bors 2022-09-20 07:10:18 +00:00
commit 8fd6d03e22
8 changed files with 75 additions and 19 deletions

View File

@ -439,7 +439,10 @@ fn link_dylib(&mut self, lib: &str, verbatim: bool, as_needed: bool) {
} }
} }
self.hint_dynamic(); self.hint_dynamic();
self.cmd.arg(format!("-l{}{}", if verbatim { ":" } else { "" }, lib)); self.cmd.arg(format!(
"-l{}{lib}",
if verbatim && self.sess.target.linker_is_gnu { ":" } else { "" },
));
if !as_needed { if !as_needed {
if self.sess.target.is_like_osx { if self.sess.target.is_like_osx {
// See above FIXME comment // See above FIXME comment
@ -450,7 +453,10 @@ fn link_dylib(&mut self, lib: &str, verbatim: bool, as_needed: bool) {
} }
fn link_staticlib(&mut self, lib: &str, verbatim: bool) { fn link_staticlib(&mut self, lib: &str, verbatim: bool) {
self.hint_static(); self.hint_static();
self.cmd.arg(format!("-l{}{}", if verbatim { ":" } else { "" }, lib)); self.cmd.arg(format!(
"-l{}{lib}",
if verbatim && self.sess.target.linker_is_gnu { ":" } else { "" },
));
} }
fn link_rlib(&mut self, lib: &Path) { fn link_rlib(&mut self, lib: &Path) {
self.hint_static(); self.hint_static();
@ -504,10 +510,10 @@ fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, search_path: &[Pat
self.hint_static(); self.hint_static();
let target = &self.sess.target; let target = &self.sess.target;
if !target.is_like_osx { if !target.is_like_osx {
self.linker_arg("--whole-archive").cmd.arg(format!( self.linker_arg("--whole-archive");
"-l{}{}", self.cmd.arg(format!(
if verbatim { ":" } else { "" }, "-l{}{lib}",
lib if verbatim && self.sess.target.linker_is_gnu { ":" } else { "" },
)); ));
self.linker_arg("--no-whole-archive"); self.linker_arg("--no-whole-archive");
} else { } else {

View File

@ -33,28 +33,25 @@ pub fn find_native_static_library(
search_paths: &[PathBuf], search_paths: &[PathBuf],
sess: &Session, sess: &Session,
) -> PathBuf { ) -> PathBuf {
let verbatim = verbatim.unwrap_or(false); let formats = if verbatim.unwrap_or(false) {
// On Windows, static libraries sometimes show up as libfoo.a and other vec![("".into(), "".into())]
// times show up as foo.lib
let oslibname = if verbatim {
name.to_string()
} else { } else {
format!("{}{}{}", sess.target.staticlib_prefix, name, sess.target.staticlib_suffix) let os = (sess.target.staticlib_prefix.clone(), sess.target.staticlib_suffix.clone());
// On Windows, static libraries sometimes show up as libfoo.a and other
// times show up as foo.lib
let unix = ("lib".into(), ".a".into());
if os == unix { vec![os] } else { vec![os, unix] }
}; };
let unixlibname = format!("lib{}.a", name);
for path in search_paths { for path in search_paths {
let test = path.join(&oslibname); for (prefix, suffix) in &formats {
if test.exists() { let test = path.join(format!("{}{}{}", prefix, name, suffix));
return test;
}
if oslibname != unixlibname {
let test = path.join(&unixlibname);
if test.exists() { if test.exists() {
return test; return test;
} }
} }
} }
sess.emit_fatal(MissingNativeLibrary { libname: name }); sess.emit_fatal(MissingNativeLibrary { libname: name });
} }

View File

@ -0,0 +1,15 @@
# ignore-cross-compile
# ignore-macos
include ../../run-make-fulldeps/tools.mk
all:
# Verbatim allows specify precise name.
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_some_strange_name.ext
$(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_some_strange_name.ext
# With verbatim any other name cannot be used (local).
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/liblocal_native_dep.a
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.a
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep.lib
$(RUSTC) main.rs -Zunstable-options -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep"

View File

@ -0,0 +1,4 @@
#[no_mangle]
pub fn local_native_f() -> i32 {
return 0;
}

View File

@ -0,0 +1,9 @@
extern "C" {
fn local_native_f() -> i32;
}
pub fn main() {
unsafe {
assert!(local_native_f() == 0);
};
}

View File

@ -0,0 +1,12 @@
include ../../run-make-fulldeps/tools.mk
all:
# Verbatim allows specify precise name.
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_some_strange_name.ext
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_some_strange_name.ext --crate-type rlib
# With verbatim any other name cannot be used (upstream).
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/libupstream_native_dep.a
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.a
$(RUSTC) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep.lib
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"

View File

@ -0,0 +1,9 @@
extern "C" {
fn upstream_native_f() -> i32;
}
pub fn rust_dep() {
unsafe {
assert!(upstream_native_f() == 0);
}
}

View File

@ -0,0 +1,4 @@
#[no_mangle]
pub fn upstream_native_f() -> i32 {
return 0;
}