Skip to content

Commit 70c7317

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

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ignore-cross-compile -- compiling C++ code does not work well when cross-compiling
2+
3+
# This test case makes sure that native libraries are linked with --whole-archive semantics
4+
# when the `-bundle,+whole-archive` modifiers are applied to them.
5+
#
6+
# The test works by checking that the resulting executables produce the expected output,
7+
# part of which is emitted by otherwise unreferenced C code. If +whole-archive didn't work
8+
# that code would never make it into the final executable and we'd thus be missing some
9+
# of the output.
10+
11+
-include ../../run-make-fulldeps/tools.mk
12+
13+
all: $(TMPDIR)/$(call BIN,directly_linked) $(TMPDIR)/$(call BIN,indirectly_linked) $(TMPDIR)/$(call BIN,indirectly_linked_via_attr)
14+
$(call RUN,directly_linked) | $(CGREP) 'static-initializer.directly_linked.'
15+
$(call RUN,indirectly_linked) | $(CGREP) 'static-initializer.indirectly_linked.'
16+
$(call RUN,indirectly_linked_via_attr) | $(CGREP) 'static-initializer.native_lib_in_src.'
17+
18+
# Native lib linked directly into executable
19+
$(TMPDIR)/$(call BIN,directly_linked): $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
20+
$(RUSTC) directly_linked.rs -Z unstable-options -l static:+whole-archive=c_static_lib_with_constructor
21+
22+
# Native lib linked into RLIB via `-l static:-bundle,+whole-archive`, RLIB linked into executable
23+
$(TMPDIR)/$(call BIN,indirectly_linked): $(TMPDIR)/librlib_with_cmdline_native_lib.rlib
24+
$(RUSTC) indirectly_linked.rs
25+
26+
# Native lib linked into RLIB via #[link] attribute, RLIB linked into executable
27+
$(TMPDIR)/$(call BIN,indirectly_linked_via_attr): $(TMPDIR)/libnative_lib_in_src.rlib
28+
$(RUSTC) indirectly_linked_via_attr.rs
29+
30+
# Native lib linked into rlib with via commandline
31+
$(TMPDIR)/librlib_with_cmdline_native_lib.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
32+
$(RUSTC) rlib_with_cmdline_native_lib.rs -Z unstable-options --crate-type=rlib -l static:-bundle,+whole-archive=c_static_lib_with_constructor
33+
34+
# Native lib linked into rlib via `#[link()]` attribute on extern block.
35+
$(TMPDIR)/libnative_lib_in_src.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
36+
$(RUSTC) native_lib_in_src.rs --crate-type=rlib
37+
38+
$(TMPDIR)/libc_static_lib_with_constructor.o: c_static_lib_with_constructor.cpp
39+
$(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)