about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-09-07 12:31:11 +0000
committerbors <bors@rust-lang.org>2021-09-07 12:31:11 +0000
commit73641cd23ba470c6b4dcd72b8d5f62d27c735254 (patch)
tree8c9e072ae0c9aa7bac43a01e978bb0f5fb93af4c /src
parent385f8e2078b00282a7a7ffcd58aad17b405f57bf (diff)
parent70c7317d71c9b6627b4b39f3381449b1397135f7 (diff)
Auto merge of #88161 - michaelwoerister:fix-whole-archive-no-bundle, r=petrochenkov
Fix handling of +whole-archive native link modifier.

This PR fixes a bug in `add_upstream_native_libraries` that led to the `+whole-archive` modifier being ignored when linking in native libs.

~~Note that the PR does not address the situation when `+whole-archive` is combined with `+bundle`.~~
`@wesleywiser's` commit adds validation code that turns combining `+whole-archive` with `+bundle` into an error.

Fixes https://github.com/rust-lang/rust/issues/88085.

r? `@petrochenkov`
cc `@wesleywiser` `@gcoakes`
Diffstat (limited to 'src')
-rw-r--r--src/test/run-make/native-link-modifier-whole-archive/Makefile39
-rw-r--r--src/test/run-make/native-link-modifier-whole-archive/c_static_lib_with_constructor.cpp11
-rw-r--r--src/test/run-make/native-link-modifier-whole-archive/directly_linked.rs6
-rw-r--r--src/test/run-make/native-link-modifier-whole-archive/indirectly_linked.rs5
-rw-r--r--src/test/run-make/native-link-modifier-whole-archive/indirectly_linked_via_attr.rs5
-rw-r--r--src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs15
-rw-r--r--src/test/run-make/native-link-modifier-whole-archive/rlib_with_cmdline_native_lib.rs6
-rw-r--r--src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs12
-rw-r--r--src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.stderr6
-rw-r--r--src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.rs7
-rw-r--r--src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.stderr6
11 files changed, 118 insertions, 0 deletions
diff --git a/src/test/run-make/native-link-modifier-whole-archive/Makefile b/src/test/run-make/native-link-modifier-whole-archive/Makefile
new file mode 100644
index 00000000000..799b5f6f5fc
--- /dev/null
+++ b/src/test/run-make/native-link-modifier-whole-archive/Makefile
@@ -0,0 +1,39 @@
+# ignore-cross-compile -- compiling C++ code does not work well when cross-compiling
+
+# This test case makes sure that native libraries are linked with --whole-archive semantics
+# when the `-bundle,+whole-archive` modifiers are applied to them.
+#
+# The test works by checking that the resulting executables produce the expected output,
+# part of which is emitted by otherwise unreferenced C code. If +whole-archive didn't work
+# that code would never make it into the final executable and we'd thus be missing some
+# of the output.
+
+-include ../../run-make-fulldeps/tools.mk
+
+all: $(TMPDIR)/$(call BIN,directly_linked) $(TMPDIR)/$(call BIN,indirectly_linked) $(TMPDIR)/$(call BIN,indirectly_linked_via_attr)
+	$(call RUN,directly_linked) | $(CGREP) 'static-initializer.directly_linked.'
+	$(call RUN,indirectly_linked) | $(CGREP) 'static-initializer.indirectly_linked.'
+	$(call RUN,indirectly_linked_via_attr) | $(CGREP) 'static-initializer.native_lib_in_src.'
+
+# Native lib linked directly into executable
+$(TMPDIR)/$(call BIN,directly_linked): $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
+	$(RUSTC) directly_linked.rs -Z unstable-options -l static:+whole-archive=c_static_lib_with_constructor
+
+# Native lib linked into RLIB via `-l static:-bundle,+whole-archive`, RLIB linked into executable
+$(TMPDIR)/$(call BIN,indirectly_linked): $(TMPDIR)/librlib_with_cmdline_native_lib.rlib
+	$(RUSTC) indirectly_linked.rs
+
+# Native lib linked into RLIB via #[link] attribute, RLIB linked into executable
+$(TMPDIR)/$(call BIN,indirectly_linked_via_attr): $(TMPDIR)/libnative_lib_in_src.rlib
+	$(RUSTC) indirectly_linked_via_attr.rs
+
+# Native lib linked into rlib with via commandline
+$(TMPDIR)/librlib_with_cmdline_native_lib.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
+	$(RUSTC) rlib_with_cmdline_native_lib.rs -Z unstable-options --crate-type=rlib -l static:-bundle,+whole-archive=c_static_lib_with_constructor
+
+# Native lib linked into rlib via `#[link()]` attribute on extern block.
+$(TMPDIR)/libnative_lib_in_src.rlib: $(call NATIVE_STATICLIB,c_static_lib_with_constructor)
+	$(RUSTC) native_lib_in_src.rs --crate-type=rlib
+
+$(TMPDIR)/libc_static_lib_with_constructor.o: c_static_lib_with_constructor.cpp
+	$(call COMPILE_OBJ_CXX,$@,$<)
diff --git a/src/test/run-make/native-link-modifier-whole-archive/c_static_lib_with_constructor.cpp b/src/test/run-make/native-link-modifier-whole-archive/c_static_lib_with_constructor.cpp
new file mode 100644
index 00000000000..c687eb0f092
--- /dev/null
+++ b/src/test/run-make/native-link-modifier-whole-archive/c_static_lib_with_constructor.cpp
@@ -0,0 +1,11 @@
+#include <cstdio>
+
+// Since this is a global variable, its constructor will be called before
+// main() is executed. But only if the object file containing it actually
+// gets linked into the executable.
+struct Foo {
+    Foo() {
+        printf("static-initializer.");
+        fflush(stdout);
+    }
+} FOO;
diff --git a/src/test/run-make/native-link-modifier-whole-archive/directly_linked.rs b/src/test/run-make/native-link-modifier-whole-archive/directly_linked.rs
new file mode 100644
index 00000000000..17518e8b2f9
--- /dev/null
+++ b/src/test/run-make/native-link-modifier-whole-archive/directly_linked.rs
@@ -0,0 +1,6 @@
+use std::io::Write;
+
+fn main() {
+    print!("directly_linked.");
+    std::io::stdout().flush().unwrap();
+}
diff --git a/src/test/run-make/native-link-modifier-whole-archive/indirectly_linked.rs b/src/test/run-make/native-link-modifier-whole-archive/indirectly_linked.rs
new file mode 100644
index 00000000000..c8b83fcfe03
--- /dev/null
+++ b/src/test/run-make/native-link-modifier-whole-archive/indirectly_linked.rs
@@ -0,0 +1,5 @@
+extern crate rlib_with_cmdline_native_lib;
+
+fn main() {
+    rlib_with_cmdline_native_lib::hello();
+}
diff --git a/src/test/run-make/native-link-modifier-whole-archive/indirectly_linked_via_attr.rs b/src/test/run-make/native-link-modifier-whole-archive/indirectly_linked_via_attr.rs
new file mode 100644
index 00000000000..b9e347609b2
--- /dev/null
+++ b/src/test/run-make/native-link-modifier-whole-archive/indirectly_linked_via_attr.rs
@@ -0,0 +1,5 @@
+extern crate native_lib_in_src;
+
+fn main() {
+    native_lib_in_src::hello();
+}
diff --git a/src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs b/src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs
new file mode 100644
index 00000000000..373d89b7936
--- /dev/null
+++ b/src/test/run-make/native-link-modifier-whole-archive/native_lib_in_src.rs
@@ -0,0 +1,15 @@
+#![feature(native_link_modifiers_bundle)]
+#![feature(native_link_modifiers_whole_archive)]
+#![feature(native_link_modifiers)]
+
+use std::io::Write;
+
+#[link(name = "c_static_lib_with_constructor",
+       kind = "static",
+       modifiers = "-bundle,+whole-archive")]
+extern {}
+
+pub fn hello() {
+    print!("native_lib_in_src.");
+    std::io::stdout().flush().unwrap();
+}
diff --git a/src/test/run-make/native-link-modifier-whole-archive/rlib_with_cmdline_native_lib.rs b/src/test/run-make/native-link-modifier-whole-archive/rlib_with_cmdline_native_lib.rs
new file mode 100644
index 00000000000..ef2b702dd82
--- /dev/null
+++ b/src/test/run-make/native-link-modifier-whole-archive/rlib_with_cmdline_native_lib.rs
@@ -0,0 +1,6 @@
+use std::io::Write;
+
+pub fn hello() {
+    print!("indirectly_linked.");
+    std::io::stdout().flush().unwrap();
+}
diff --git a/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs
new file mode 100644
index 00000000000..c3714a38451
--- /dev/null
+++ b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.rs
@@ -0,0 +1,12 @@
+// compile-flags: -Zunstable-options --crate-type rlib
+// build-fail
+// error-pattern: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
+
+#![feature(native_link_modifiers)]
+#![feature(native_link_modifiers_bundle)]
+#![feature(native_link_modifiers_whole_archive)]
+
+#[link(name = "mylib", kind = "static", modifiers = "+bundle,+whole-archive")]
+extern "C" { }
+
+fn main() { }
diff --git a/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.stderr b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.stderr
new file mode 100644
index 00000000000..246efb8d627
--- /dev/null
+++ b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive-link-attr.stderr
@@ -0,0 +1,6 @@
+error: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
+
+error: could not find native static library `mylib`, perhaps an -L flag is missing?
+
+error: aborting due to 2 previous errors
+
diff --git a/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.rs b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.rs
new file mode 100644
index 00000000000..1d0768d99cf
--- /dev/null
+++ b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.rs
@@ -0,0 +1,7 @@
+// Mixing +bundle and +whole-archive is not allowed
+
+// compile-flags: -l static:+bundle,+whole-archive=mylib -Zunstable-options --crate-type rlib
+// build-fail
+// error-pattern: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
+
+fn main() { }
diff --git a/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.stderr b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.stderr
new file mode 100644
index 00000000000..246efb8d627
--- /dev/null
+++ b/src/test/ui/native-library-link-flags/mix-bundle-and-whole-archive.stderr
@@ -0,0 +1,6 @@
+error: the linking modifiers `+bundle` and `+whole-archive` are not compatible with each other when generating rlibs
+
+error: could not find native static library `mylib`, perhaps an -L flag is missing?
+
+error: aborting due to 2 previous errors
+