about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-03-28 08:16:47 +0000
committerbors <bors@rust-lang.org>2020-03-28 08:16:47 +0000
commitb76238a3ee5ebffbad8a3aefd36012f3bceb7069 (patch)
tree4a89d67bf0865579c6f29aea7342dac3b1fe1650
parent2acf32d9adff836a3111c039e4e10a48ee5c79b5 (diff)
parent53c4e0c19ac220085638e856999639fe26cb2a00 (diff)
downloadrust-b76238a3ee5ebffbad8a3aefd36012f3bceb7069.tar.gz
rust-b76238a3ee5ebffbad8a3aefd36012f3bceb7069.zip
Auto merge of #70095 - jsgf:link-native, r=nagisa
Implement -Zlink-native-libraries

This implements a flag `-Zlink-native-libraries=yes/no`. If set to true/yes, or unspecified, then
native libraries referenced via `#[link]` attributes will be put on the linker line (ie, unchanged
behaviour).

If `-Zlink-native-libraries=no` is specified then rustc will not add the native libraries to the link
line. The assumption is that the outer build system driving the build already knows about the native
libraries and will specify them to the linker directly (for example via `-Clink-arg=`).

Addresses issue #70093
-rw-r--r--src/librustc_codegen_ssa/back/link.rs13
-rw-r--r--src/librustc_session/options.rs2
-rw-r--r--src/test/ui/issues/issue-70093.rs8
3 files changed, 20 insertions, 3 deletions
diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs
index 1fb08a05803..7169b79c3bc 100644
--- a/src/librustc_codegen_ssa/back/link.rs
+++ b/src/librustc_codegen_ssa/back/link.rs
@@ -1391,10 +1391,17 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
     // link line. And finally upstream native libraries can't depend on anything
     // in this DAG so far because they're only dylibs and dylibs can only depend
     // on other dylibs (e.g., other native deps).
-    add_local_native_libraries(cmd, sess, codegen_results);
+    //
+    // If -Zlink-native-libraries=false is set, then the assumption is that an
+    // external build system already has the native dependencies defined, and it
+    // will provide them to the linker itself.
+    if sess.opts.debugging_opts.link_native_libraries.unwrap_or(true) {
+        add_local_native_libraries(cmd, sess, codegen_results);
+    }
     add_upstream_rust_crates::<B>(cmd, sess, codegen_results, crate_type, tmpdir);
-    add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
-
+    if sess.opts.debugging_opts.link_native_libraries.unwrap_or(true) {
+        add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
+    }
     // Tell the linker what we're doing.
     if crate_type != config::CrateType::Executable {
         cmd.build_dylib(out_filename);
diff --git a/src/librustc_session/options.rs b/src/librustc_session/options.rs
index 9d49075ebbc..e43908a7914 100644
--- a/src/librustc_session/options.rs
+++ b/src/librustc_session/options.rs
@@ -957,4 +957,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
         "link the `.rlink` file generated by `-Z no-link`"),
     new_llvm_pass_manager: Option<bool> = (None, parse_opt_bool, [TRACKED],
         "use new LLVM pass manager"),
+    link_native_libraries: Option<bool> = (None, parse_opt_bool, [UNTRACKED],
+        "Link native libraries in the linker invocation."),
 }
diff --git a/src/test/ui/issues/issue-70093.rs b/src/test/ui/issues/issue-70093.rs
new file mode 100644
index 00000000000..95ab86ebcb1
--- /dev/null
+++ b/src/test/ui/issues/issue-70093.rs
@@ -0,0 +1,8 @@
+// run-pass
+// compile-flags: -Zlink-native-libraries=no -Cdefault-linker-libraries=yes
+// ignore-windows - this will probably only work on unixish systems
+
+#[link(name = "some-random-non-existent-library", kind = "static")]
+extern "C" {}
+
+fn main() {}