Skip to content

Commit 53c4e0c

Browse files
committedMar 27, 2020
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 rust-lang#70093
1 parent 0a2df62 commit 53c4e0c

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed
 

‎src/librustc_codegen_ssa/back/link.rs

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

‎src/librustc_session/options.rs

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

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.