use compact_str::format_compact;
use getset::{CopyGetters, WithSetters};
use crate::{
generate_struct_arr,
os_cmd::{
MiniStr,
presets::{StrVec, cargo_build::ArgConverter},
},
};
#[derive(Debug, Clone, WithSetters, CopyGetters, Default)]
#[getset(set_with = "pub", get_copy = "pub with_prefix")]
pub struct BuildStdFeatures {
panic_immediate_abort: bool,
panic_unwind: bool,
backtrace: bool,
optimize_for_size: bool,
llvm_libunwind: bool,
system_llvm_libunwind: bool,
debug_refcell: bool,
debug_typeid: bool,
std_detect_file_io: bool,
std_detect_dlsym_getauxval: bool,
std_detect_env_override: bool,
windows_raw_dylib: bool,
}
impl ArgConverter for BuildStdFeatures {
type ArgsIter = core::iter::Flatten<core::option::IntoIter<[MiniStr; 2]>>;
fn to_args(&self) -> Self::ArgsIter {
let components = generate_struct_arr! [ self =>
panic_immediate_abort,
panic_unwind,
backtrace,
llvm_libunwind,
system_llvm_libunwind,
optimize_for_size,
debug_refcell,
debug_typeid,
std_detect_file_io,
std_detect_dlsym_getauxval,
std_detect_env_override,
windows_raw_dylib,
];
match components
.into_iter()
.filter_map(|(name, enabled)| enabled.then_some(name))
.collect::<StrVec<12>>()
{
v if !v.is_empty() => [
"-Z".into(),
format_compact!("build-std-features={}", v.join(",")),
]
.into(),
_ => None,
}
.into_iter()
.flatten()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_std_feats() {
let feats = BuildStdFeatures::default();
assert!(!feats.get_llvm_libunwind());
assert!(!feats.get_panic_immediate_abort());
}
#[ignore]
#[test]
fn test_std_feats() {
let feats = BuildStdFeatures::default()
.with_panic_unwind(true)
.with_optimize_for_size(true);
assert!(feats.get_panic_unwind());
assert!(feats.get_optimize_for_size());
assert!(!feats.get_panic_immediate_abort());
let args = feats.to_args();
args
.last()
.map(|x| assert_eq!(x, "build-std-features=panic_unwind,optimize_for_size"));
}
}