Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bindgen-tests/tests/headers/extern-fn-block-attrs-many.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bindgen-flags: --extern-fn-block-attrs '#[allow(dead_code)]' --extern-fn-block-attrs '#[cfg_attr(not(windows), link(wasm_import_module = "test-module"))]'

void test_function();
3 changes: 3 additions & 0 deletions bindgen-tests/tests/headers/extern-fn-block-attrs-wasm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bindgen-flags: --extern-fn-block-attrs '#[allow(dead_code)]' --wasm-import-module-name test-module

void test_function();
3 changes: 3 additions & 0 deletions bindgen-tests/tests/headers/extern-fn-block-attrs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// bindgen-flags: --extern-fn-block-attrs '#[allow(dead_code)]'

void test_function();
20 changes: 13 additions & 7 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4665,13 +4665,19 @@ impl CodeGenerator for Function {
}
}

// Unfortunately this can't piggyback on the `attributes` list because
// the #[link(wasm_import_module)] needs to happen before the `extern
// "C"` block. It doesn't get picked up properly otherwise
let wasm_link_attribute =
ctx.options().wasm_import_module_name.as_ref().map(|name| {
quote! { #[link(wasm_import_module = #name)] }
let mut block_attributes = quote! {};
for attr in &ctx.options().extern_fn_block_attrs {
let parsed_attr = proc_macro2::TokenStream::from_str(attr).unwrap_or_else(
|err| {
panic!(
"Error parsing extern fn block attribute `{attr}`: {err}"
)
},
);
block_attributes.extend(quote! {
#parsed_attr
});
}

let should_wrap = is_internal &&
ctx.options().wrap_static_fns &&
Expand Down Expand Up @@ -4725,7 +4731,7 @@ impl CodeGenerator for Function {
.then(|| quote!(unsafe));

let tokens = quote! {
#wasm_link_attribute
#block_attributes
#safety extern #abi {
#(#attributes)*
pub fn #ident ( #( #args ),* ) #ret;
Expand Down
5 changes: 5 additions & 0 deletions bindgen/options/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ struct BindgenCommand {
/// The NAME to be used in a #[link(wasm_import_module = ...)] statement
#[arg(long, value_name = "NAME")]
wasm_import_module_name: Option<String>,
/// Attributes to apply to the extern function block.
#[arg(long, value_name = "ATTRS")]
extern_fn_block_attrs: Vec<String>,
/// Use dynamic loading mode with the given library NAME.
#[arg(long, value_name = "NAME")]
dynamic_loading: Option<String>,
Expand Down Expand Up @@ -647,6 +650,7 @@ where
enable_function_attribute_detection,
use_array_pointers_in_arguments,
wasm_import_module_name,
extern_fn_block_attrs,
dynamic_loading,
dynamic_link_require_all,
prefix_link_name,
Expand Down Expand Up @@ -907,6 +911,7 @@ where
time_phases,
use_array_pointers_in_arguments => Builder::array_pointers_in_arguments,
wasm_import_module_name,
extern_fn_block_attrs => Builder::extern_fn_block_attrs,
ctypes_prefix,
anon_fields_prefix,
generate => Builder::with_codegen_config,
Expand Down
23 changes: 22 additions & 1 deletion bindgen/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,25 @@ options! {
},
as_args: "--use-array-pointers-in-arguments",
},
/// Attributes to add to all `extern` blocks.
extern_fn_block_attrs: Vec<String> {
methods: {
/// Add an attribute to all the `extern` blocks generated by `bindgen`.
///
/// This can be used to add attributes such as `#[link(...)]` to all
/// the `extern` blocks.
pub fn extern_fn_block_attrs<T: Into<String>>(mut self, attr: T) -> Self {
self.options.extern_fn_block_attrs.push(attr.into());
self
}
},
as_args: |attrs, args| {
for attr in attrs {
args.push("--extern-fn-block-attrs".to_owned());
args.push(attr.clone());
}
},
},
/// The name of the `wasm_import_module`.
wasm_import_module_name: Option<String> {
methods: {
Expand All @@ -1927,7 +1946,9 @@ options! {
mut self,
import_name: T,
) -> Self {
self.options.wasm_import_module_name = Some(import_name.into());
self.options.extern_fn_block_attrs.push(format!(
"#[link(wasm_import_module = \"{}\")]", import_name.into()
));
self
}
},
Expand Down