about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-05-23 19:42:19 +0000
committerbors <bors@rust-lang.org>2021-05-23 19:42:19 +0000
commitf64503eb555475d65ae5503ef22439ca5dd394fd (patch)
tree4caa83f52b8b3fd06eb95779bbc90ad87cc88f8e
parentd8af907491e20339e41d048d6a32b41ddfa91dfe (diff)
parent8e42fa55dbaec9a6560b59fb023e50713ef94e93 (diff)
downloadrust-f64503eb555475d65ae5503ef22439ca5dd394fd.tar.gz
rust-f64503eb555475d65ae5503ef22439ca5dd394fd.zip
Auto merge of #85554 - 12101111:fix-dedup-native-libs, r=petrochenkov
native lib: defer the duplicate check after relevant_lib check.

https://github.com/rust-lang/rust/pull/84794 break code using conditional-compilation with `#[link]` attributes.

```rust
#[cfg(target_env = "musl")]
cfg_if::cfg_if! {
    if #[cfg(any(target_feature = "crt-static", feature = "llvm-libunwind"))] {
        #[link(name = "unwind", kind = "static", modifiers = "-bundle")]
        extern "C" {}
    } else {
        #[link(name = "unwind", cfg(feature = "system-llvm-libunwind"))]
        #[link(name = "gcc_s", cfg(not(feature = "system-llvm-libunwind")))]
        extern "C" {}
    }
}

```
-rw-r--r--compiler/rustc_codegen_ssa/src/back/link.rs14
-rw-r--r--src/test/run-make-fulldeps/link-dedup/Makefile12
-rw-r--r--src/test/run-make-fulldeps/link-dedup/depa.rs7
-rw-r--r--src/test/run-make-fulldeps/link-dedup/depb.rs8
-rw-r--r--src/test/run-make-fulldeps/link-dedup/depc.rs4
-rw-r--r--src/test/run-make-fulldeps/link-dedup/empty.rs5
6 files changed, 44 insertions, 6 deletions
diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs
index e330b5e703b..32275e9b073 100644
--- a/compiler/rustc_codegen_ssa/src/back/link.rs
+++ b/compiler/rustc_codegen_ssa/src/back/link.rs
@@ -1805,13 +1805,14 @@ fn add_local_native_libraries(
     let search_path = archive_search_paths(sess);
     let mut last = (NativeLibKind::Unspecified, None);
     for lib in relevant_libs {
-        // Skip if this library is the same as the last.
-        last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };
-
         let name = match lib.name {
             Some(l) => l,
             None => continue,
         };
+
+        // Skip if this library is the same as the last.
+        last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };
+
         let verbatim = lib.verbatim.unwrap_or(false);
         match lib.kind {
             NativeLibKind::Dylib { as_needed } => {
@@ -2144,9 +2145,6 @@ fn add_upstream_native_libraries(
     let mut last = (NativeLibKind::Unspecified, None);
     for &(cnum, _) in crates {
         for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
-            // Skip if this library is the same as the last.
-            last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };
-
             let name = match lib.name {
                 Some(l) => l,
                 None => continue,
@@ -2154,6 +2152,10 @@ fn add_upstream_native_libraries(
             if !relevant_lib(sess, &lib) {
                 continue;
             }
+
+            // Skip if this library is the same as the last.
+            last = if (lib.kind, lib.name) == last { continue } else { (lib.kind, lib.name) };
+
             let verbatim = lib.verbatim.unwrap_or(false);
             match lib.kind {
                 NativeLibKind::Dylib { as_needed } => {
diff --git a/src/test/run-make-fulldeps/link-dedup/Makefile b/src/test/run-make-fulldeps/link-dedup/Makefile
new file mode 100644
index 00000000000..4e7ce0f02d4
--- /dev/null
+++ b/src/test/run-make-fulldeps/link-dedup/Makefile
@@ -0,0 +1,12 @@
+# ignore-msvc
+
+-include ../tools.mk
+
+all:
+	$(RUSTC) depa.rs
+	$(RUSTC) depb.rs
+	$(RUSTC) depc.rs
+	$(RUSTC) empty.rs --cfg bar 2>&1 | $(CGREP) '"-ltesta" "-ltestb" "-ltesta"'
+	$(RUSTC) empty.rs 2>&1 | $(CGREP) '"-ltesta"'
+	$(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltestb"'
+	$(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltesta" "-ltesta"'
diff --git a/src/test/run-make-fulldeps/link-dedup/depa.rs b/src/test/run-make-fulldeps/link-dedup/depa.rs
new file mode 100644
index 00000000000..e48ffd6413c
--- /dev/null
+++ b/src/test/run-make-fulldeps/link-dedup/depa.rs
@@ -0,0 +1,7 @@
+#![crate_type = "rlib"]
+
+#[link(name = "testa")]
+extern "C" {}
+
+#[link(name = "testa")]
+extern "C" {}
diff --git a/src/test/run-make-fulldeps/link-dedup/depb.rs b/src/test/run-make-fulldeps/link-dedup/depb.rs
new file mode 100644
index 00000000000..b1be21fe005
--- /dev/null
+++ b/src/test/run-make-fulldeps/link-dedup/depb.rs
@@ -0,0 +1,8 @@
+#![feature(link_cfg)]
+#![crate_type = "rlib"]
+
+#[link(name = "testb", cfg(foo))]
+extern "C" {}
+
+#[link(name = "testb", cfg(bar))]
+extern "C" {}
diff --git a/src/test/run-make-fulldeps/link-dedup/depc.rs b/src/test/run-make-fulldeps/link-dedup/depc.rs
new file mode 100644
index 00000000000..8dcb3dee5a2
--- /dev/null
+++ b/src/test/run-make-fulldeps/link-dedup/depc.rs
@@ -0,0 +1,4 @@
+#![crate_type = "rlib"]
+
+#[link(name = "testa")]
+extern "C" {}
diff --git a/src/test/run-make-fulldeps/link-dedup/empty.rs b/src/test/run-make-fulldeps/link-dedup/empty.rs
new file mode 100644
index 00000000000..e00ae18f4af
--- /dev/null
+++ b/src/test/run-make-fulldeps/link-dedup/empty.rs
@@ -0,0 +1,5 @@
+extern crate depa;
+extern crate depb;
+extern crate depc;
+
+fn main() {}