Skip to content

De-duplicate consecutive libs when printing native-static-libs #113626

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

Merged
merged 1 commit into from
Jul 16, 2023
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
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,11 @@ fn print_native_static_libs(
let mut lib_args: Vec<_> = all_native_libs
.iter()
.filter(|l| relevant_lib(sess, l))
// Deduplication of successive repeated libraries, see rust-lang/rust#113209
//
// note: we don't use PartialEq/Eq because NativeLib transitively depends on local
// elements like spans, which we don't care about and would make the deduplication impossible
.dedup_by(|l1, l2| l1.name == l2.name && l1.kind == l2.kind && l1.verbatim == l2.verbatim)
.filter_map(|lib| {
let name = lib.name;
match lib.kind {
Expand Down
6 changes: 5 additions & 1 deletion tests/run-make/print-native-static-libs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ include ../tools.mk

all:
$(RUSTC) --crate-type rlib -lbar_cli bar.rs
$(RUSTC) foo.rs -lfoo_cli --crate-type staticlib --print native-static-libs 2>&1 \
$(RUSTC) foo.rs -lfoo_cli -lfoo_cli --crate-type staticlib --print native-static-libs 2>&1 \
| grep 'note: native-static-libs: ' \
| sed 's/note: native-static-libs: \(.*\)/\1/' > $(TMPDIR)/libs.txt

cat $(TMPDIR)/libs.txt | grep -F "glib-2.0" # in bar.rs
cat $(TMPDIR)/libs.txt | grep -F "systemd" # in foo.rs
cat $(TMPDIR)/libs.txt | grep -F "bar_cli"
cat $(TMPDIR)/libs.txt | grep -F "foo_cli"

# make sure that foo_cli and glib-2.0 are not consecutively present
cat $(TMPDIR)/libs.txt | grep -Fv "foo_cli -lfoo_cli"
cat $(TMPDIR)/libs.txt | grep -Fv "glib-2.0 -lglib-2.0"
6 changes: 6 additions & 0 deletions tests/run-make/print-native-static-libs/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub extern "C" fn my_bar_add(left: i32, right: i32) -> i32 {
// Obviously makes no sense but...
unsafe {
g_free(std::ptr::null_mut());
g_free2(std::ptr::null_mut());
}
left + right
}
Expand All @@ -11,3 +12,8 @@ pub extern "C" fn my_bar_add(left: i32, right: i32) -> i32 {
extern "C" {
fn g_free(p: *mut ());
}

#[link(name = "glib-2.0")]
extern "C" {
fn g_free2(p: *mut ());
}