Skip to content

Commit db10ec5

Browse files
committed
Detect retpolines in build.rs as cfg is never set for them. Fixes #46
1 parent 2862c14 commit db10ec5

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

multiversion-macros/build.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn main() {
2+
// retpolines are not yet recognized by rust as a regular target feature.
3+
// We can't detect them with `cfg(target_feature = "retpoline")`, but we can detect them in
4+
// rustflags, since they shouldn't be the default for any target.
5+
let rustflags = std::env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();
6+
let retpolines_enabled = rustflags.split('\x1f').any(|flag| {
7+
let features = flag
8+
.strip_prefix("target-feature=")
9+
.or(flag.strip_prefix("-Ctarget-feature="));
10+
if let Some(features) = features {
11+
features
12+
.split(',')
13+
.any(|feature| feature.starts_with("+retpoline"))
14+
} else {
15+
false
16+
}
17+
});
18+
19+
if retpolines_enabled {
20+
println!("cargo::rustc-cfg=retpoline")
21+
}
22+
println!("cargo::rustc-check-cfg=cfg(retpoline)");
23+
println!("cargo::rerun-if-changed=build.rs");
24+
}

multiversion-macros/src/dispatcher.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -381,27 +381,11 @@ impl Dispatcher {
381381
if !crate::util::fn_params(&self.func.sig).is_empty()
382382
|| self.func.sig.asyncness.is_some()
383383
|| util::impl_trait_present(&self.func.sig)
384+
|| cfg!(retpoline)
384385
{
385386
self.direct_dispatcher_fn()?
386387
} else {
387-
let indirect = self.indirect_dispatcher_fn()?;
388-
let direct = self.direct_dispatcher_fn()?;
389-
parse_quote! {
390-
{
391-
#[cfg(not(any(
392-
target_feature = "retpoline",
393-
target_feature = "retpoline-indirect-branches",
394-
target_feature = "retpoline-indirect-calls",
395-
)))]
396-
#indirect
397-
#[cfg(any(
398-
target_feature = "retpoline",
399-
target_feature = "retpoline-indirect-branches",
400-
target_feature = "retpoline-indirect-calls",
401-
))]
402-
#direct
403-
}
404-
}
388+
self.indirect_dispatcher_fn()?
405389
}
406390
} else {
407391
self.static_dispatcher_fn()

0 commit comments

Comments
 (0)