Skip to content

Commit eff79f3

Browse files
committed
feat: Refactor specific attribute solution to general solution
1 parent 27f8b73 commit eff79f3

File tree

11 files changed

+61
-77
lines changed

11 files changed

+61
-77
lines changed

bindgen-tests/tests/expectations/tests/extern-fn-block-attrs-many.rs

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/windows-raw-dylib-verbatim.rs renamed to bindgen-tests/tests/expectations/tests/extern-fn-block-attrs-wasm.rs

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/windows-raw-dylib.rs renamed to bindgen-tests/tests/expectations/tests/extern-fn-block-attrs.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// bindgen-flags: --extern-fn-block-attrs '#[cfg_attr(windows, link(name = "<name>", kind = "raw-dylib"))]' --extern-fn-block-attrs '#[cfg_attr(not(windows), link(name = "<name>"))]'
2+
3+
void test_function();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// bindgen-flags: --extern-fn-block-attrs '#[cfg_attr(windows, link(name = "<name>", kind = "raw-dylib"))]' --wasm-import-module-name test-module
2+
3+
void test_function();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// bindgen-flags: --extern-fn-block-attrs '#[cfg_attr(windows, link(name = "<name>", kind = "raw-dylib"))]'
2+
3+
void test_function();

bindgen-tests/tests/headers/windows-raw-dylib-verbatim.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

bindgen-tests/tests/headers/windows-raw-dylib.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

bindgen/codegen/mod.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4666,28 +4666,19 @@ impl CodeGenerator for Function {
46664666
}
46674667
}
46684668

4669-
// Unfortunately this can't piggyback on the `attributes` list because
4670-
// the #[link(wasm_import_module)] needs to happen before the `extern
4671-
// "C"` block. It doesn't get picked up properly otherwise
4672-
let mut link_attribute = quote! {};
4673-
if let Some(ref wasm_name) = ctx.options().wasm_import_module_name {
4674-
link_attribute.extend(quote! {
4675-
#[link(wasm_import_module = #wasm_name)]
4669+
let mut block_attributes = quote! {};
4670+
for attr in &ctx.options().extern_fn_block_attrs {
4671+
let parsed_attr = proc_macro2::TokenStream::from_str(attr).unwrap_or_else(
4672+
|err| {
4673+
panic!(
4674+
"Error parsing extern fn block attribute `{attr}`: {err}"
4675+
)
4676+
},
4677+
);
4678+
block_attributes.extend(quote! {
4679+
#parsed_attr
46764680
});
46774681
}
4678-
if let (Some(ref windows_name), verbatim) =
4679-
ctx.options().windows_link_as_raw_dylib
4680-
{
4681-
if verbatim {
4682-
link_attribute.extend(quote! {
4683-
#[cfg_attr(windows, link(name = #windows_name, kind = "raw-dylib", modifiers = "+verbatim"))]
4684-
});
4685-
} else {
4686-
link_attribute.extend(quote! {
4687-
#[cfg_attr(windows, link(name = #windows_name, kind = "raw-dylib"))]
4688-
});
4689-
}
4690-
}
46914682

46924683
let should_wrap = is_internal
46934684
&& ctx.options().wrap_static_fns
@@ -4741,7 +4732,7 @@ impl CodeGenerator for Function {
47414732
.then(|| quote!(unsafe));
47424733

47434734
let tokens = quote! {
4744-
#link_attribute
4735+
#block_attributes
47454736
#safety extern #abi {
47464737
#(#attributes)*
47474738
pub fn #ident ( #( #args ),* ) #ret;

bindgen/options/cli.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,9 @@ struct BindgenCommand {
423423
/// The NAME to be used in a #[link(wasm_import_module = ...)] statement
424424
#[arg(long, value_name = "NAME")]
425425
wasm_import_module_name: Option<String>,
426-
/// On Windows, link against NAME with kind `raw-dylib`.
427-
#[arg(long, value_name = "NAME")]
428-
windows_link_as_raw_dylib: Option<String>,
429-
/// Do not append `.dll` suffix to the NAME provided to `--windows-link-as-raw-dylib`.
430-
#[arg(long, requires = "windows_link_as_raw_dylib")]
431-
windows_link_as_raw_dylib_verbatim: bool,
426+
/// Attributes to apply to the extern function block.
427+
#[arg(long, value_name = "ATTRS")]
428+
extern_fn_block_attrs: Vec<String>,
432429
/// Use dynamic loading mode with the given library NAME.
433430
#[arg(long, value_name = "NAME")]
434431
dynamic_loading: Option<String>,
@@ -653,8 +650,7 @@ where
653650
enable_function_attribute_detection,
654651
use_array_pointers_in_arguments,
655652
wasm_import_module_name,
656-
windows_link_as_raw_dylib,
657-
windows_link_as_raw_dylib_verbatim,
653+
extern_fn_block_attrs,
658654
dynamic_loading,
659655
dynamic_link_require_all,
660656
prefix_link_name,
@@ -772,8 +768,8 @@ where
772768
}
773769

774770
fn add_derives(&self, info: &DeriveInfo<'_>) -> Vec<String> {
775-
if self.kind.map_or(true, |kind| kind == info.kind) &&
776-
self.regex_set.matches(info.name)
771+
if self.kind.map_or(true, |kind| kind == info.kind)
772+
&& self.regex_set.matches(info.name)
777773
{
778774
return self.derives.clone();
779775
}
@@ -812,8 +808,8 @@ where
812808
}
813809

814810
fn add_attributes(&self, info: &AttributeInfo<'_>) -> Vec<String> {
815-
if self.kind.map_or(true, |kind| kind == info.kind) &&
816-
self.regex_set.matches(info.name)
811+
if self.kind.map_or(true, |kind| kind == info.kind)
812+
&& self.regex_set.matches(info.name)
817813
{
818814
return self.attributes.clone();
819815
}
@@ -915,6 +911,7 @@ where
915911
time_phases,
916912
use_array_pointers_in_arguments => Builder::array_pointers_in_arguments,
917913
wasm_import_module_name,
914+
extern_fn_block_attrs => Builder::extern_fn_block_attrs,
918915
ctypes_prefix,
919916
anon_fields_prefix,
920917
generate => Builder::with_codegen_config,
@@ -1096,13 +1093,6 @@ where
10961093
builder = builder.emit_diagnostics();
10971094
}
10981095

1099-
if let Some(windows_link_as_raw_dylib) = windows_link_as_raw_dylib {
1100-
builder = builder.windows_link_as_raw_dylib(
1101-
windows_link_as_raw_dylib,
1102-
windows_link_as_raw_dylib_verbatim,
1103-
);
1104-
}
1105-
11061096
Ok((builder, output, verbose))
11071097
}
11081098

0 commit comments

Comments
 (0)