diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index a71fe4430388d..f2637e6014771 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -1380,10 +1380,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::(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 a1ecf4e8528be..cc9a8ac098f62 100644 --- a/src/librustc_session/options.rs +++ b/src/librustc_session/options.rs @@ -968,4 +968,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "link the `.rlink` file generated by `-Z no-link`"), new_llvm_pass_manager: Option = (None, parse_opt_bool, [TRACKED], "use new LLVM pass manager"), + link_native_libraries: Option = (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 0000000000000..2ac22c631ef91 --- /dev/null +++ b/src/test/ui/issues/issue-70093.rs @@ -0,0 +1,7 @@ +// run-pass +// compile-flags: -Zlink-native-libraries=no -Cdefault-linker-libraries=yes + +#[link(name = "some-random-non-existent-library", kind = "static")] +extern "C" {} + +fn main() {}