Skip to content

Commit

Permalink
Detect retpolines in build.rs as cfg is never set for them. Fixes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
calebzulawski committed Nov 23, 2024
1 parent 2862c14 commit db10ec5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
24 changes: 24 additions & 0 deletions multiversion-macros/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
fn main() {
// retpolines are not yet recognized by rust as a regular target feature.
// We can't detect them with `cfg(target_feature = "retpoline")`, but we can detect them in
// rustflags, since they shouldn't be the default for any target.
let rustflags = std::env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();
let retpolines_enabled = rustflags.split('\x1f').any(|flag| {
let features = flag
.strip_prefix("target-feature=")
.or(flag.strip_prefix("-Ctarget-feature="));
if let Some(features) = features {
features
.split(',')
.any(|feature| feature.starts_with("+retpoline"))
} else {
false
}
});

if retpolines_enabled {
println!("cargo::rustc-cfg=retpoline")
}
println!("cargo::rustc-check-cfg=cfg(retpoline)");
println!("cargo::rerun-if-changed=build.rs");
}
20 changes: 2 additions & 18 deletions multiversion-macros/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,27 +381,11 @@ impl Dispatcher {
if !crate::util::fn_params(&self.func.sig).is_empty()
|| self.func.sig.asyncness.is_some()
|| util::impl_trait_present(&self.func.sig)
|| cfg!(retpoline)
{
self.direct_dispatcher_fn()?
} else {
let indirect = self.indirect_dispatcher_fn()?;
let direct = self.direct_dispatcher_fn()?;
parse_quote! {
{
#[cfg(not(any(
target_feature = "retpoline",
target_feature = "retpoline-indirect-branches",
target_feature = "retpoline-indirect-calls",
)))]
#indirect
#[cfg(any(
target_feature = "retpoline",
target_feature = "retpoline-indirect-branches",
target_feature = "retpoline-indirect-calls",
))]
#direct
}
}
self.indirect_dispatcher_fn()?
}
} else {
self.static_dispatcher_fn()
Expand Down

0 comments on commit db10ec5

Please sign in to comment.