use std::path::{Path, PathBuf};
use crcbl_wl_scanner::{Options, Source, parse_protocol};
const PROTOCOLS: &[(&str, &str)] = &[
("wayland", "wayland-protocols/wayland.xml"),
("xdg_shell", "wayland-protocols/xdg-shell.xml"),
("viewporter", "wayland-protocols/viewporter.xml"),
(
"fractional_scale",
"wayland-protocols/fractional-scale-v1.xml",
),
("xdg_output", "wayland-protocols/xdg-output-unstable-v1.xml"),
(
"xdg_decoration",
"wayland-protocols/xdg-decoration-unstable-v1.xml",
),
(
"relative_pointer",
"wayland-protocols/relative-pointer-unstable-v1.xml",
),
(
"pointer_constraints",
"wayland-protocols/pointer-constraints-unstable-v1.xml",
),
(
"layer_shell",
"wayland-protocols/wlr-layer-shell-unstable-v1.xml",
),
("text_input", "wayland-protocols/text-input-unstable-v3.xml"),
("cursor_shape", "wayland-protocols/cursor-shape-v1.xml"),
("linux_dmabuf", "wayland-protocols/linux-dmabuf-v1.xml"),
("xdg_activation", "wayland-protocols/xdg-activation-v1.xml"),
("tablet", "wayland-protocols/tablet-v2.xml"),
];
const E2E_PROTOCOLS: &[(&str, &str)] = &[
(
"virtual_keyboard",
"wayland-protocols/virtual-keyboard-unstable-v1.xml",
),
(
"virtual_pointer",
"wayland-protocols/wlr-virtual-pointer-unstable-v1.xml",
),
(
"input_method",
"wayland-protocols/input-method-unstable-v2.xml",
),
];
fn main() {
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rustc-check-cfg=cfg(desktop_unix)");
let os = std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
if matches!(os.as_str(), "linux" | "freebsd") {
println!("cargo::rustc-cfg=desktop_unix");
}
let e2e = std::env::var_os("CARGO_FEATURE_WAYLAND_E2E").is_some();
let protocols: Vec<(&str, &str)> = PROTOCOLS
.iter()
.chain(if e2e { E2E_PROTOCOLS } else { &[] })
.copied()
.collect();
let manifest = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").expect("cargo sets this"));
let mut sources = Vec::with_capacity(protocols.len());
for (module, relative) in &protocols {
let path = manifest.join(relative);
println!("cargo::rerun-if-changed={}", path.display());
let xml = std::fs::read_to_string(&path)
.unwrap_or_else(|error| panic!("cannot read {}: {error}", path.display()));
let protocol = parse_protocol(&xml)
.unwrap_or_else(|error| panic!("cannot parse {}: {error}", path.display()));
sources.push(Source { module, protocol });
}
let generated = crcbl_wl_scanner::emit(
&sources,
&Options {
ffi_path: "crate::wayland::ffi",
},
)
.unwrap_or_else(|error| panic!("cannot generate Wayland protocol code: {error}"));
let out = Path::new(&std::env::var_os("OUT_DIR").expect("cargo sets this"))
.join("wayland_protocol.rs");
std::fs::write(&out, generated)
.unwrap_or_else(|error| panic!("cannot write {}: {error}", out.display()));
}