1
0
Fork 0
forked from wry/wry
wry/build/enums.rs

66 lines
1.7 KiB
Rust

use {
crate::open,
repc::layout::{Type, TypeVariant},
std::{env, io::Write},
};
#[allow(unused_macros)]
macro_rules! cenum {
($name:ident, $uc:ident; $($name2:ident = $val:expr,)*) => {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct $name(pub i32);
impl $name {
pub fn raw(self) -> i32 {
self.0
}
}
pub const $uc: &[i32] = &[$($val,)*];
$(
pub const $name2: $name = $name($val);
)*
}
}
#[path = "fontconfig_consts.rs"]
mod fontconfig;
fn get_target() -> repc::Target {
let rustc_target = env::var("TARGET").unwrap();
repc::TARGET_MAP
.iter()
.cloned()
.find(|t| t.0 == rustc_target)
.unwrap()
.1
}
fn get_enum_ty(variants: Vec<i128>) -> anyhow::Result<u64> {
let target = get_target();
let ty = Type {
layout: (),
annotations: vec![],
variant: TypeVariant::Enum(variants),
};
let ty = repc::compute_layout(target, &ty)?;
assert!(ty.layout.pointer_alignment_bits <= ty.layout.size_bits);
Ok(ty.layout.size_bits)
}
fn write_ty<W: Write>(f: &mut W, vals: &[i32], ty: &str) -> anyhow::Result<()> {
let variants: Vec<_> = vals.iter().cloned().map(|v| v as i128).collect();
let size = get_enum_ty(variants)?;
writeln!(f, "#[allow(clippy::allow_attributes, dead_code)]")?;
writeln!(f, "pub type {} = i{};", ty, size)?;
Ok(())
}
pub fn main() -> anyhow::Result<()> {
let mut f = open("fontconfig_tys.rs")?;
write_ty(&mut f, fontconfig::FC_MATCH_KINDS, "FcMatchKind")?;
write_ty(&mut f, fontconfig::FC_RESULTS, "FcResult")?;
Ok(())
}