use { repc::layout::{Type, TypeVariant}, std::{ env, fs::{File, OpenOptions}, io::{self, BufWriter, Write}, path::PathBuf, }, }; #[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 $name2: $name = $name($val); )* pub const $uc: &[i32] = &[$($val,)*]; }; } #[path = "src/consts.rs"] mod consts; fn open(s: &str) -> io::Result> { let mut path = PathBuf::from(env::var("OUT_DIR").unwrap()); path.push(s); Ok(BufWriter::new( OpenOptions::new() .create(true) .write(true) .truncate(true) .open(path)?, )) } 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) -> anyhow::Result { 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(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(()) } fn main() -> anyhow::Result<()> { let mut f = open("pango_tys.rs")?; write_ty(&mut f, consts::CAIRO_FORMATS, "cairo_format_t")?; write_ty(&mut f, consts::CAIRO_STATUSES, "cairo_status_t")?; write_ty(&mut f, consts::CAIRO_OPERATORS, "cairo_operator_t")?; write_ty(&mut f, consts::PANGO_ELLIPSIZE_MODES, "PangoEllipsizeMode_")?; println!("cargo:rerun-if-changed=src/consts.rs"); Ok(()) }