Skip to content

Commit 89dbdc7

Browse files
authored
Rollup 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
2 parents cde3df2 + bb55741 commit 89dbdc7

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/librustc_codegen_ssa/back/link.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1380,10 +1380,17 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
13801380
// link line. And finally upstream native libraries can't depend on anything
13811381
// in this DAG so far because they're only dylibs and dylibs can only depend
13821382
// on other dylibs (e.g., other native deps).
1383-
add_local_native_libraries(cmd, sess, codegen_results);
1383+
//
1384+
// If -Zlink-native-libraries=false is set, then the assumption is that an
1385+
// external build system already has the native dependencies defined, and it
1386+
// will provide them to the linker itself.
1387+
if sess.opts.debugging_opts.link_native_libraries.unwrap_or(true) {
1388+
add_local_native_libraries(cmd, sess, codegen_results);
1389+
}
13841390
add_upstream_rust_crates::<B>(cmd, sess, codegen_results, crate_type, tmpdir);
1385-
add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
1386-
1391+
if sess.opts.debugging_opts.link_native_libraries.unwrap_or(true) {
1392+
add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
1393+
}
13871394
// Tell the linker what we're doing.
13881395
if crate_type != config::CrateType::Executable {
13891396
cmd.build_dylib(out_filename);

src/librustc_session/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -968,4 +968,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
968968
"link the `.rlink` file generated by `-Z no-link`"),
969969
new_llvm_pass_manager: Option<bool> = (None, parse_opt_bool, [TRACKED],
970970
"use new LLVM pass manager"),
971+
link_native_libraries: Option<bool> = (None, parse_opt_bool, [UNTRACKED],
972+
"Link native libraries in the linker invocation."),
971973
}

src/test/ui/issues/issue-70093.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-pass
2+
// compile-flags: -Zlink-native-libraries=no -Cdefault-linker-libraries=yes
3+
4+
#[link(name = "some-random-non-existent-library", kind = "static")]
5+
extern "C" {}
6+
7+
fn main() {}

0 commit comments

Comments
 (0)