Skip to content

Commit 1a599d7

Browse files
Rollup merge of #107675 - jsgf:link-directives, r=davidtwco
Implement -Zlink-directives=yes/no `-Zlink-directives=no` will ignored `#[link]` directives while compiling a crate, so nothing is emitted into the crate's metadata. The assumption is that the build system already knows about the crate's native dependencies and can provide them at link time without these directives. This is another way to address issue # #70093, which is currently addressed by `-Zlink-native-libraries` (implemented in #70095). The latter is implemented at link time, which has the effect of ignoring `#[link]` in *every* crate. This makes it a very large hammer as it requires all native dependencies to be known to the build system to be at all usable, including those in sysroot libraries. I think this means its effectively unused, and definitely under-used. Being able to control this on a crate-by-crate basis should make it much easier to apply when needed. I'm not sure if we need both mechanisms, but we can decide that later. cc `@pcwalton` `@cramertj`
2 parents a4119ba + fde2e40 commit 1a599d7

File tree

6 files changed

+20
-1
lines changed

6 files changed

+20
-1
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ fn test_unstable_options_tracking_hash() {
756756
tracked!(instrument_coverage, Some(InstrumentCoverage::All));
757757
tracked!(instrument_mcount, true);
758758
tracked!(instrument_xray, Some(InstrumentXRay::default()));
759+
tracked!(link_directives, false);
759760
tracked!(link_only, true);
760761
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
761762
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });

compiler/rustc_metadata/src/native_libs.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ impl<'tcx> Collector<'tcx> {
103103
}
104104

105105
// Process all of the #[link(..)]-style arguments
106-
let sess = &self.tcx.sess;
106+
let sess = self.tcx.sess;
107107
let features = self.tcx.features();
108+
109+
if !sess.opts.unstable_opts.link_directives {
110+
return;
111+
}
112+
108113
for m in self.tcx.hir().attrs(it.hir_id()).iter().filter(|a| a.has_name(sym::link)) {
109114
let Some(items) = m.meta_item_list() else {
110115
continue;

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,8 @@ options! {
14891489
"keep hygiene data after analysis (default: no)"),
14901490
layout_seed: Option<u64> = (None, parse_opt_number, [TRACKED],
14911491
"seed layout randomization"),
1492+
link_directives: bool = (true, parse_bool, [TRACKED],
1493+
"honor #[link] directives in the compiled crate (default: yes)"),
14921494
link_native_libraries: bool = (true, parse_bool, [UNTRACKED],
14931495
"link native libraries in the linker invocation (default: yes)"),
14941496
link_only: bool = (false, parse_bool, [TRACKED],

tests/rustdoc-ui/z-help.stdout

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
Multiple options can be combined with commas.
8282
-Z keep-hygiene-data=val -- keep hygiene data after analysis (default: no)
8383
-Z layout-seed=val -- seed layout randomization
84+
-Z link-directives=val -- honor #[link] directives in the compiled crate (default: yes)
8485
-Z link-native-libraries=val -- link native libraries in the linker invocation (default: yes)
8586
-Z link-only=val -- link the `.rlink` file generated by `-Z no-link` (default: no)
8687
-Z llvm-plugins=val -- a list LLVM plugins to enable (space separated)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-pass
2+
// compile-flags: -Zlink-directives=no
3+
// ignore-windows - this will probably only work on unixish systems
4+
// ignore-fuchsia - missing __libc_start_main for some reason (#84733)
5+
// ignore-cross-compile - default-linker-libraries=yes doesn't play well with cross compiling
6+
7+
#[link(name = "some-random-non-existent-library", kind = "static")]
8+
extern "C" {}
9+
10+
fn main() {}

0 commit comments

Comments
 (0)