Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix verbatim with upstream dependencies #101806

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,25 @@ pub fn find_native_static_library(
search_paths: &[PathBuf],
sess: &Session,
) -> PathBuf {
let verbatim = verbatim.unwrap_or(false);
// On Windows, static libraries sometimes show up as libfoo.a and other
// times show up as foo.lib
let oslibname = if verbatim {
name.to_string()
let formats = if verbatim.unwrap_or(false) {
vec![("".into(), "".into())]
} 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 {
let test = path.join(&oslibname);
if test.exists() {
return test;
}
if oslibname != unixlibname {
let test = path.join(&unixlibname);
for (prefix, suffix) in &formats {
let test = path.join(format!("{}{}{}", prefix, name, suffix));
if test.exists() {
return test;
}
}
}

sess.emit_fatal(MissingNativeLibrary { libname: name });
}

Expand Down
27 changes: 27 additions & 0 deletions src/test/run-make/native-link-modifier-verbatim/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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) 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
$(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep.rlib -Zunstable-options -l static:+verbatim=local_some_strange_name.ext
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
rm $(TMPDIR)/*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) upstream_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/upstream_native_dep
$(RUSTC) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib
$(RUSTC) main.rs --extern rust_dep=$(TMPDIR)/librust_dep.rlib -Zunstable-options -l static:+verbatim=local_native_dep 2>&1 | $(CGREP) "local_native_dep"
rm $(TMPDIR)/*native_dep*

# With verbatim any other name cannot be used (upstream).
$(RUSTC) local_native_dep.rs --crate-type=staticlib -o $(TMPDIR)/local_native_dep
$(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) rust_dep.rs -Zunstable-options -l static:+verbatim=upstream_native_dep --crate-type rlib 2>&1 | $(CGREP) "upstream_native_dep"
rm $(TMPDIR)/*native_dep*
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub fn local_native_f() -> i32 {
return 0;
}
12 changes: 12 additions & 0 deletions src/test/run-make/native-link-modifier-verbatim/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extern crate rust_dep;

extern "C" {
fn local_native_f() -> i32;
}

pub fn main() {
unsafe {
assert!(local_native_f() == 0);
};
rust_dep::rust_dep()
}
9 changes: 9 additions & 0 deletions src/test/run-make/native-link-modifier-verbatim/rust_dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
extern "C" {
fn upstream_native_f() -> i32;
}

pub fn rust_dep() {
unsafe {
assert!(upstream_native_f() == 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub fn upstream_native_f() -> i32 {
return 0;
}