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

native lib: defer the duplicate check after relevant_lib check. #85554

Merged
merged 2 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,13 +1805,14 @@ fn add_local_native_libraries(
let search_path = archive_search_paths(sess);
let mut last = (NativeLibKind::Unspecified, None);
for lib in relevant_libs {
// Skip if this library is the same as the last.
last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };

let name = match lib.name {
Some(l) => l,
None => continue,
};

// Skip if this library is the same as the last.
last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not strictly necessary for the fix, but let's leave it for consistency.
It also improves deduplication in the (unlikely) case of #[link(name = "foo")] #[link(wasm_import_module = "...")] #[link(name = "foo")].


let verbatim = lib.verbatim.unwrap_or(false);
match lib.kind {
NativeLibKind::Dylib { as_needed } => {
Expand Down Expand Up @@ -2144,16 +2145,17 @@ fn add_upstream_native_libraries(
let mut last = (NativeLibKind::Unspecified, None);
for &(cnum, _) in crates {
for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
// Skip if this library is the same as the last.
last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };

let name = match lib.name {
Some(l) => l,
None => continue,
};
if !relevant_lib(sess, &lib) {
continue;
}

// Skip if this library is the same as the last.
last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };

let verbatim = lib.verbatim.unwrap_or(false);
match lib.kind {
NativeLibKind::Dylib { as_needed } => {
Expand Down
12 changes: 12 additions & 0 deletions src/test/run-make-fulldeps/link-dedup/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# ignore-msvc

-include ../tools.mk

all:
$(RUSTC) depa.rs
$(RUSTC) depb.rs
$(RUSTC) depc.rs
$(RUSTC) empty.rs --cfg bar 2>&1 | $(CGREP) '"-ltesta" "-ltestb" "-ltesta"'
$(RUSTC) empty.rs 2>&1 | $(CGREP) '"-ltesta"'
$(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltestb"'
$(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltesta" "-ltesta"'
7 changes: 7 additions & 0 deletions src/test/run-make-fulldeps/link-dedup/depa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![crate_type = "rlib"]

#[link(name = "testa")]
extern "C" {}

#[link(name = "testa")]
extern "C" {}
8 changes: 8 additions & 0 deletions src/test/run-make-fulldeps/link-dedup/depb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(link_cfg)]
#![crate_type = "rlib"]

#[link(name = "testb", cfg(foo))]
extern "C" {}

#[link(name = "testb", cfg(bar))]
extern "C" {}
4 changes: 4 additions & 0 deletions src/test/run-make-fulldeps/link-dedup/depc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![crate_type = "rlib"]

#[link(name = "testa")]
extern "C" {}
5 changes: 5 additions & 0 deletions src/test/run-make-fulldeps/link-dedup/empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate depa;
extern crate depb;
extern crate depc;

fn main() {}