Skip to content

Commit 89b43dc

Browse files
committed
feat: Implement support for link type 'raw-dylib' on Windows
1 parent de9627f commit 89b43dc

File tree

7 files changed

+78
-5
lines changed

7 files changed

+78
-5
lines changed

bindgen-tests/tests/expectations/tests/windows-raw-dylib-verbatim.rs

Lines changed: 5 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.rs

Lines changed: 5 additions & 0 deletions
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: --windows-link-as-raw-dylib foo.exe --windows-link-as-raw-dylib-verbatim
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: --windows-link-as-raw-dylib foolib
2+
3+
void test_function();

bindgen/codegen/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,10 +4717,24 @@ impl CodeGenerator for Function {
47174717
// Unfortunately this can't piggyback on the `attributes` list because
47184718
// the #[link(wasm_import_module)] needs to happen before the `extern
47194719
// "C"` block. It doesn't get picked up properly otherwise
4720-
let wasm_link_attribute =
4721-
ctx.options().wasm_import_module_name.as_ref().map(|name| {
4722-
quote! { #[link(wasm_import_module = #name)] }
4723-
});
4720+
let link_attribute = match (
4721+
ctx.options().wasm_import_module_name.as_ref(),
4722+
&ctx.options().windows_link_as_raw_dylib,
4723+
) {
4724+
(Some(_), (Some(_), _)) => {
4725+
panic!("Cannot link against a wasm import module and a raw dylib at the same time");
4726+
}
4727+
(Some(name), (None, _)) => {
4728+
Some(quote! { #[link(wasm_import_module = #name)] })
4729+
}
4730+
(None, (Some(name), false)) => Some(
4731+
quote! { #[cfg_attr(windows, link(name = #name, kind = "raw-dylib"))] },
4732+
),
4733+
(None, (Some(name), true)) => Some(
4734+
quote! { #[cfg_attr(windows, link(name = #name, kind = "raw-dylib", modifiers = "+verbatim"))] },
4735+
),
4736+
_ => None,
4737+
};
47244738

47254739
let should_wrap = is_internal &&
47264740
ctx.options().wrap_static_fns &&
@@ -4774,7 +4788,7 @@ impl CodeGenerator for Function {
47744788
.then(|| quote!(unsafe));
47754789

47764790
let tokens = quote! {
4777-
#wasm_link_attribute
4791+
#link_attribute
47784792
#safety extern #abi {
47794793
#(#attributes)*
47804794
pub fn #ident ( #( #args ),* ) #ret;

bindgen/options/cli.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,12 @@ struct BindgenCommand {
420420
/// The NAME to be used in a #[link(wasm_import_module = ...)] statement
421421
#[arg(long, value_name = "NAME")]
422422
wasm_import_module_name: Option<String>,
423+
/// On Windows, link against NAME with kind `raw-dylib`.
424+
#[arg(long, value_name = "NAME")]
425+
windows_link_as_raw_dylib: Option<String>,
426+
/// Do not append `.dll` suffix to the NAME provided to `--windows-link-as-raw-dylib`.
427+
#[arg(long, requires = "windows_link_as_raw_dylib")]
428+
windows_link_as_raw_dylib_verbatim: bool,
423429
/// Use dynamic loading mode with the given library NAME.
424430
#[arg(long, value_name = "NAME")]
425431
dynamic_loading: Option<String>,
@@ -643,6 +649,8 @@ where
643649
enable_function_attribute_detection,
644650
use_array_pointers_in_arguments,
645651
wasm_import_module_name,
652+
windows_link_as_raw_dylib,
653+
windows_link_as_raw_dylib_verbatim,
646654
dynamic_loading,
647655
dynamic_link_require_all,
648656
prefix_link_name,
@@ -1083,6 +1091,13 @@ where
10831091
builder = builder.emit_diagnostics();
10841092
}
10851093

1094+
if let Some(windows_link_as_raw_dylib) = windows_link_as_raw_dylib {
1095+
builder = builder.windows_link_as_raw_dylib(
1096+
windows_link_as_raw_dylib,
1097+
windows_link_as_raw_dylib_verbatim,
1098+
);
1099+
}
1100+
10861101
Ok((builder, output, verbose))
10871102
}
10881103

bindgen/options/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,34 @@ options! {
19081908
},
19091909
as_args: "--wasm-import-module-name",
19101910
},
1911+
/// On Windows, link with kind `raw-dylib`
1912+
windows_link_as_raw_dylib: (Option<String>, bool) {
1913+
methods: {
1914+
/// Adds the `#[cfg_attr(windows, link(name = "<dylib>", kind = "raw-dylib"))]` attribute to all the `extern`
1915+
/// blocks generated by `bindgen`.
1916+
///
1917+
/// This attribute is not added by default.
1918+
///
1919+
/// If `verbatim` is set to `true`, instead `#[cfg_attr(windows, link(name = "<dylib>", kind = "raw-dylib", modifiers = "+verbatim"))]` is used.
1920+
pub fn windows_link_as_raw_dylib<T: Into<String>>(
1921+
mut self,
1922+
dylib: T,
1923+
verbatim: bool,
1924+
) -> Self {
1925+
self.options.windows_link_as_raw_dylib = (Some(dylib.into()), verbatim);
1926+
self
1927+
}
1928+
},
1929+
as_args: |(dylib, verbatim), args| {
1930+
if let Some(dylib) = dylib {
1931+
args.push("--windows-link-as-raw-dylib".to_owned());
1932+
args.push(dylib.into());
1933+
if *verbatim {
1934+
args.push("--windows-link-as-raw-dylib-verbatim".to_owned());
1935+
}
1936+
}
1937+
},
1938+
},
19111939
/// The name of the dynamic library (if we are generating bindings for a shared library).
19121940
dynamic_library_name: Option<String> {
19131941
methods: {

0 commit comments

Comments
 (0)