Skip to content

Commit 07241e6

Browse files
Add test case for no-bundle/whole-archive native libs linking.
1 parent 846c372 commit 07241e6

File tree

7 files changed

+85
-0
lines changed

7 files changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This test case makes sure that native libraries are linked with --whole-archive semantics
2+
# when the `-bundle,+whole-archive` modifiers are applied to them.
3+
#
4+
# The test works by checking that the resulting executables produce the expected output,
5+
# part of which is emitted by otherwise unreferenced C code. If +whole-archive didn't work
6+
# that code would never make it into the final executable and we'd thus be missing some
7+
# of the output.
8+
9+
-include ../../run-make-fulldeps/tools.mk
10+
11+
all: $(TMPDIR)/$(call BIN,directly_linked) $(TMPDIR)/$(call BIN,indirectly_linked) $(TMPDIR)/$(call BIN,indirectly_linked_via_attr)
12+
$(call RUN,directly_linked) | $(CGREP) 'static-initializer.directly_linked.'
13+
$(call RUN,indirectly_linked) | $(CGREP) 'static-initializer.indirectly_linked.'
14+
$(call RUN,indirectly_linked_via_attr) | $(CGREP) 'static-initializer.native_lib_in_src.'
15+
16+
# Native lib linked directly into executable
17+
$(TMPDIR)/$(call BIN,directly_linked): $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
18+
$(RUSTC) directly_linked.rs -Z unstable-options -l static:+whole-archive=c_static_lib_with_constructor
19+
20+
# Native lib linked into RLIB via `-l static:-bundle,+whole-archive`, RLIB linked into executable
21+
$(TMPDIR)/$(call BIN,indirectly_linked): $(TMPDIR)/librlib_with_cmdline_native_lib.rlib
22+
$(RUSTC) indirectly_linked.rs
23+
24+
# Native lib linked into RLIB via #[link] attribute, RLIB linked into executable
25+
$(TMPDIR)/$(call BIN,indirectly_linked_via_attr): $(TMPDIR)/libnative_lib_in_src.rlib
26+
$(RUSTC) indirectly_linked_via_attr.rs
27+
28+
# Native lib linked into rlib with via commandline
29+
$(TMPDIR)/librlib_with_cmdline_native_lib.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
30+
$(RUSTC) rlib_with_cmdline_native_lib.rs -Z unstable-options --crate-type=rlib -l static:-bundle,+whole-archive=c_static_lib_with_constructor
31+
32+
# Native lib linked into rlib via `#[link()]` attribute on extern block.
33+
$(TMPDIR)/libnative_lib_in_src.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
34+
$(RUSTC) native_lib_in_src.rs --crate-type=rlib
35+
36+
$(TMPDIR)/libc_static_lib_with_constructor.o: c_static_lib_with_constructor.cpp
37+
$(call COMPILE_OBJ_CXX,$@,$<)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <cstdio>
2+
3+
// Since this is a global variable, its constructor will be called before
4+
// main() is executed. But only if the object file containing it actually
5+
// gets linked into the executable.
6+
struct Foo {
7+
Foo() {
8+
printf("static-initializer.");
9+
fflush(stdout);
10+
}
11+
} FOO;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::io::Write;
2+
3+
fn main() {
4+
print!("directly_linked.");
5+
std::io::stdout().flush().unwrap();
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate rlib_with_cmdline_native_lib;
2+
3+
fn main() {
4+
rlib_with_cmdline_native_lib::hello();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate native_lib_in_src;
2+
3+
fn main() {
4+
native_lib_in_src::hello();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(native_link_modifiers_bundle)]
2+
#![feature(native_link_modifiers_whole_archive)]
3+
#![feature(native_link_modifiers)]
4+
5+
use std::io::Write;
6+
7+
#[link(name = "c_static_lib_with_constructor",
8+
kind = "static",
9+
modifiers = "-bundle,+whole-archive")]
10+
extern {}
11+
12+
pub fn hello() {
13+
print!("native_lib_in_src.");
14+
std::io::stdout().flush().unwrap();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::io::Write;
2+
3+
pub fn hello() {
4+
print!("indirectly_linked.");
5+
std::io::stdout().flush().unwrap();
6+
}

0 commit comments

Comments
 (0)