Skip to content

Commit f74af29

Browse files
committed
Generate proper function impl when cross-compiling
When cross-compiling, using cfg!() in a build script or proc macro will not have the intended behavior: It will check the cfg against the build host instead of the build target. The easiest way to avoid this issue is to use CARGO_CFG_* environment variables, but those are only present for build scripts, not for proc macros. So to work around the issue, we fold the #[cfg] *inside* the generated code so that it is checked when compiling the generated code.
1 parent 510aab2 commit f74af29

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

crates/gen/src/types/function.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,36 @@ impl Function {
5151
link = "onecoreuap";
5252
}
5353

54-
let body = if cfg!(windows) {
55-
if signature.has_query_interface() {
56-
let leading_params = &signature.params[..signature.params.len() - 2];
57-
let args = leading_params.iter().map(|p| p.gen_win32_abi_arg());
54+
let body = if signature.has_query_interface() {
55+
let leading_params = &signature.params[..signature.params.len() - 2];
56+
let args = leading_params.iter().map(|p| p.gen_win32_abi_arg());
5857

59-
quote! {
60-
#[link(name = #link)]
61-
extern "system" {
62-
fn #name(#(#abi_params),*) #abi_return_type;
63-
}
64-
let mut result__ = ::std::option::Option::None;
65-
#name(#(#args,)* &<T as ::windows::Interface>::IID, ::windows::Abi::set_abi(&mut result__)).and_some(result__)
66-
}
67-
} else {
68-
quote! {
69-
#[link(name = #link)]
70-
extern "system" {
71-
fn #name(#(#abi_params),*) #abi_return_type;
72-
}
73-
#name(#(#args),*)
58+
quote! {
59+
#[link(name = #link)]
60+
extern "system" {
61+
fn #name(#(#abi_params),*) #abi_return_type;
7462
}
63+
let mut result__ = ::std::option::Option::None;
64+
#name(#(#args,)* &<T as ::windows::Interface>::IID, ::windows::Abi::set_abi(&mut result__)).and_some(result__)
7565
}
7666
} else {
7767
quote! {
68+
#[link(name = #link)]
69+
extern "system" {
70+
fn #name(#(#abi_params),*) #abi_return_type;
71+
}
72+
#name(#(#args),*)
73+
}
74+
};
75+
76+
// Don't link on windows dlls when generating code for non-windows:
77+
let body = quote! {
78+
#[cfg(windows)]
79+
{
80+
#body
81+
}
82+
#[cfg(not(windows))]
83+
{
7884
unimplemented!("Unsupported target OS");
7985
}
8086
};

0 commit comments

Comments
 (0)