Skip to content

Commit b76238a

Browse files
committed
Auto merge of rust-lang#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 rust-lang#70093
2 parents 2acf32d + 53c4e0c commit b76238a

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
@@ -1391,10 +1391,17 @@ fn link_args<'a, B: ArchiveBuilder<'a>>(
13911391
// link line. And finally upstream native libraries can't depend on anything
13921392
// in this DAG so far because they're only dylibs and dylibs can only depend
13931393
// on other dylibs (e.g., other native deps).
1394-
add_local_native_libraries(cmd, sess, codegen_results);
1394+
//
1395+
// If -Zlink-native-libraries=false is set, then the assumption is that an
1396+
// external build system already has the native dependencies defined, and it
1397+
// will provide them to the linker itself.
1398+
if sess.opts.debugging_opts.link_native_libraries.unwrap_or(true) {
1399+
add_local_native_libraries(cmd, sess, codegen_results);
1400+
}
13951401
add_upstream_rust_crates::<B>(cmd, sess, codegen_results, crate_type, tmpdir);
1396-
add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
1397-
1402+
if sess.opts.debugging_opts.link_native_libraries.unwrap_or(true) {
1403+
add_upstream_native_libraries(cmd, sess, codegen_results, crate_type);
1404+
}
13981405
// Tell the linker what we're doing.
13991406
if crate_type != config::CrateType::Executable {
14001407
cmd.build_dylib(out_filename);

src/librustc_session/options.rs

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

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)