Skip to content

Commit 2114c76

Browse files
authored
Add an option to opt-out of #[used] statics (#1021)
This is not needed for the wasi_snapshot_preview1 adapter in Wasmtime and is causing issues with LLVM 19, so add an option to turn it off which will be enabled in Wasmtime.
1 parent 82793b3 commit 2114c76

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed

crates/guest-rust/macro/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ impl Parse for Config {
137137
Opt::Features(f) => {
138138
features.extend(f.into_iter().map(|f| f.value()));
139139
}
140+
Opt::DisableCustomSectionLinkHelpers(disable) => {
141+
opts.disable_custom_section_link_helpers = disable.value();
142+
}
140143
}
141144
}
142145
} else {
@@ -309,6 +312,7 @@ mod kw {
309312
syn::custom_keyword!(pub_export_macro);
310313
syn::custom_keyword!(generate_unused_types);
311314
syn::custom_keyword!(features);
315+
syn::custom_keyword!(disable_custom_section_link_helpers);
312316
}
313317

314318
#[derive(Clone)]
@@ -361,6 +365,7 @@ enum Opt {
361365
PubExportMacro(syn::LitBool),
362366
GenerateUnusedTypes(syn::LitBool),
363367
Features(Vec<syn::LitStr>),
368+
DisableCustomSectionLinkHelpers(syn::LitBool),
364369
}
365370

366371
impl Parse for Opt {
@@ -504,6 +509,10 @@ impl Parse for Opt {
504509
syn::bracketed!(contents in input);
505510
let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
506511
Ok(Opt::Features(list.into_iter().collect()))
512+
} else if l.peek(kw::disable_custom_section_link_helpers) {
513+
input.parse::<kw::disable_custom_section_link_helpers>()?;
514+
input.parse::<Token![:]>()?;
515+
Ok(Opt::DisableCustomSectionLinkHelpers(input.parse()?))
507516
} else {
508517
Err(l.error())
509518
}

crates/guest-rust/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,12 @@
812812
/// //
813813
/// // By default this is an empty list.
814814
/// features: ["foo", "bar", "baz"],
815+
///
816+
/// // Disables generation of a `#[used]` static to try harder to get the
817+
/// // custom section describing WIT types linked into the binary when
818+
/// // used in library-like situations. This is `false` by default with
819+
/// // `#[used]` statics being emitted.
820+
/// disable_custom_section_link_helpers: false,
815821
/// });
816822
/// ```
817823
///

crates/rust/src/interface.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,23 @@ macro_rules! {macro_name} {{
425425
pub fn finish_append_submodule(mut self, snake: &str, module_path: Vec<String>) {
426426
let module = self.finish();
427427
let path_to_root = self.path_to_root();
428+
let used_static = if self.gen.opts.disable_custom_section_link_helpers {
429+
String::new()
430+
} else {
431+
format!(
432+
"\
433+
#[used]
434+
#[doc(hidden)]
435+
static __FORCE_SECTION_REF: fn() =
436+
{path_to_root}__link_custom_section_describing_imports;
437+
"
438+
)
439+
};
428440
let module = format!(
429441
"\
430442
#[allow(dead_code, clippy::all)]
431443
pub mod {snake} {{
432-
#[used]
433-
#[doc(hidden)]
434-
#[cfg(target_arch = \"wasm32\")]
435-
static __FORCE_SECTION_REF: fn() = {path_to_root}__link_custom_section_describing_imports;
444+
{used_static}
436445
{module}
437446
}}
438447
",

crates/rust/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ pub struct Opts {
226226
/// Whether to generate unused structures, not generated by default (false)
227227
#[cfg_attr(feature = "clap", arg(long))]
228228
pub generate_unused_types: bool,
229+
230+
/// Whether or not to generate helper function/constants to help link custom
231+
/// sections into the final output.
232+
///
233+
/// Disabling this can shave a few bytes off a binary but makes
234+
/// library-based usage of `generate!` prone to breakage.
235+
#[cfg_attr(feature = "clap", arg(long))]
236+
pub disable_custom_section_link_helpers: bool,
229237
}
230238

231239
impl Opts {
@@ -816,7 +824,6 @@ macro_rules! __export_{world_name}_impl {{
816824
"
817825
#[inline(never)]
818826
#[doc(hidden)]
819-
#[cfg(target_arch = \"wasm32\")]
820827
pub fn {func_name}() {{
821828
{rt}::maybe_link_cabi_realloc();
822829
}}
@@ -1075,7 +1082,11 @@ impl WorldGenerator for RustWasm {
10751082
resolve_to_encode,
10761083
world_to_encode,
10771084
"encoded world",
1078-
Some("__link_custom_section_describing_imports"),
1085+
if self.opts.disable_custom_section_link_helpers {
1086+
None
1087+
} else {
1088+
Some("__link_custom_section_describing_imports")
1089+
},
10791090
);
10801091

10811092
if self.opts.stubs {

crates/rust/tests/codegen.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,3 +572,19 @@ mod multiple_paths {
572572
generate_all,
573573
});
574574
}
575+
576+
#[allow(unused)]
577+
mod generate_custom_section_link_helpers {
578+
wit_bindgen::generate!({
579+
inline: r#"
580+
package a:b;
581+
582+
world test {
583+
import a: interface {
584+
x: func();
585+
}
586+
}
587+
"#,
588+
disable_custom_section_link_helpers: true,
589+
});
590+
}

0 commit comments

Comments
 (0)