Skip to content

Commit 1b67f8b

Browse files
committed
Auto merge of #111675 - Urgau:fix-local-libs-for-native-static-libs, r=bjorn3
Fix local libs not included when printing native static libs This PR fixes #111643 by adding the local used libs to the printed `--print=native-static-libs` output. It seems that `--print=native-static-libs` doesn't have any test, so I added one. It's very simple and doesn't even tries to compile the result to a binary as I don't know how to handle external library linking in CI. (Note that https://github.com/rust-lang/rust/blob/master/tests/run-make/staticlib-dylib-linkage/Makefile does compile to a binary) r? `@bjorn3`
2 parents fe76e14 + 3281060 commit 1b67f8b

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,8 @@ fn link_staticlib<'a>(
574574
}
575575
}
576576

577+
all_native_libs.extend_from_slice(&codegen_results.crate_info.used_libraries);
578+
577579
if sess.opts.prints.contains(&PrintRequest::NativeStaticLibs) {
578580
print_native_static_libs(sess, &all_native_libs, &all_rust_dylibs);
579581
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
include ../tools.mk
2+
3+
# ignore-cross-compile
4+
# ignore-wasm
5+
6+
all:
7+
$(RUSTC) --crate-type rlib -lbar_cli bar.rs
8+
$(RUSTC) foo.rs -lfoo_cli --crate-type staticlib --print native-static-libs 2>&1 \
9+
| grep 'note: native-static-libs: ' \
10+
| sed 's/note: native-static-libs: \(.*\)/\1/' > $(TMPDIR)/libs.txt
11+
12+
cat $(TMPDIR)/libs.txt | grep -F "glib-2.0" # in bar.rs
13+
cat $(TMPDIR)/libs.txt | grep -F "systemd" # in foo.rs
14+
cat $(TMPDIR)/libs.txt | grep -F "bar_cli"
15+
cat $(TMPDIR)/libs.txt | grep -F "foo_cli"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[no_mangle]
2+
pub extern "C" fn my_bar_add(left: i32, right: i32) -> i32 {
3+
// Obviously makes no sense but...
4+
unsafe {
5+
g_free(std::ptr::null_mut());
6+
}
7+
left + right
8+
}
9+
10+
#[link(name = "glib-2.0")]
11+
extern "C" {
12+
fn g_free(p: *mut ());
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern crate bar;
2+
3+
#[no_mangle]
4+
pub extern "C" fn my_foo_add(left: i32, right: i32) -> i32 {
5+
// Obviously makes no sense but...
6+
unsafe {
7+
init(std::ptr::null_mut());
8+
}
9+
bar::my_bar_add(left, right)
10+
}
11+
12+
#[link(name = "systemd")]
13+
extern "C" {
14+
fn init(p: *mut ());
15+
}

0 commit comments

Comments
 (0)