Files

150 lines
4.9 KiB
Plaintext
Raw Permalink Normal View History

2026-11-11 12:59:00 -06:00
#! Writes the files of `malunal::vulkan`, into `vulkan/`, from the Vulkan
#! registry, `vk.xml`: the Vulkan SDK's, under `VULKAN_SDK`, unless another is
#! named. Run it from the package's root whenever the SDK is updated:
#!
#! ```sh
#! blossom run malunal.vulkan.bindings
#! ```
#!
#! It binds the newest version of Vulkan's core the registry has, unless
#! `--version` names an older one, and the extensions of `extensions`, with any
#! `--extension` names besides. The files it writes are whole: change this, or
#! `tools/writing.sakura`, rather than them. A file of a platform's it no longer
#! writes, as one an extension named once made, is left as it was, to be removed.
import std::args;
import std::containers;
import std::io;
import std::process;
# The extensions the library binds: surfaces and swapchains, then naming objects
# and hearing the validation layers.
const extensions: strview[12] = [
"VK_KHR_surface",
"VK_KHR_swapchain",
"VK_KHR_display",
"VK_KHR_display_swapchain",
"VK_KHR_get_surface_capabilities2",
"VK_KHR_get_display_properties2",
"VK_EXT_headless_surface",
"VK_KHR_win32_surface",
"VK_KHR_xlib_surface",
"VK_KHR_xcb_surface",
"VK_KHR_wayland_surface",
"VK_EXT_debug_utils",
];
# The extensions whose commands the loader exports, as the core's, so they're
# C's functions: the window system's, which `vulkan-1.dll` and `libvulkan.so.1`
# export. Any other's are found by `vkGetInstanceProcAddr`.
const exported: strview[11] = [
"VK_KHR_surface",
"VK_KHR_swapchain",
"VK_KHR_display",
"VK_KHR_display_swapchain",
"VK_KHR_get_surface_capabilities2",
"VK_KHR_get_display_properties2",
"VK_EXT_headless_surface",
"VK_KHR_win32_surface",
"VK_KHR_xlib_surface",
"VK_KHR_xcb_surface",
"VK_KHR_wayland_surface",
];
@args::command{ name = "bindings", help = "Writes the files of malunal::vulkan from the Vulkan registry, vk.xml." }
struct asked {
@args::option{ value = "FILE", help = "The registry read: by default, the Vulkan SDK's, under VULKAN_SDK." }
var from: maybe<strview>;
@args::option{ value = "FOLDER", help = "Where the files are written: by default, vulkan." }
var into: maybe<strview>;
@args::option{ value = "MAJOR.MINOR", help = "The newest version of Vulkan's core bound: by default, the registry's newest." }
var version: maybe<strview>;
@args::option{ value = "NAME", help = "An extension bound besides the library's own, as VK_KHR_present_wait. Write it once for each." }
var extension: vector<strview>;
}
def fail(why: strview): void {
var out := error_output_locked();
_ = out.writer().write($"bindings: {why}\n");
system::exit(1);
}
def main(args: span<strview>): void {
val given := args::parse_or_exit::<asked>(args);
var wanted := wanting{ extensions = vector::<strview>::new(), exported = vector::<strview>::new() };
for &each in extensions {
wanted.extensions.append(each);
}
for &each in given.extension {
if !holds_name(&wanted.extensions, each) {
wanted.extensions.append(each);
}
}
for &each in exported {
wanted.exported.append(each);
}
if given.version is some(version) {
val dot := version.find(".") else {
fail($"--version {version} isn't a version, as 1.3 is.");
return;
};
wanted.major = number_of(version.substr(0, dot)) as u32;
wanted.minor = number_of(version.substr(dot + 1, version.length - dot - 1)) as u32;
}
var file := string::new(nothing{});
if given.from is some(from) {
file.append(from);
} else {
val sdk := process::variable("VULKAN_SDK") else {
fail("VULKAN_SDK isn't set, so there's no registry to read: name one with --from.");
return;
};
file.append(sdk);
file.append("/share/vulkan/registry/vk.xml");
}
val at := path::parse(file.span) else (_) {
fail($"{file} isn't a path.");
return;
};
val text := read_file(&at) else (e) {
fail($"{file} couldn't be read ({e}).");
return;
};
var made := vector::<made_file>::new();
var problems := vector::<string>::new();
if !bindings_of(text.span, &wanted, &mut made, &mut problems) {
var out := error_output_locked();
for &each in problems {
_ = out.writer().write($"bindings: {each}\n");
}
system::exit(1);
}
val folder := given.into else "vulkan";
val into := path::parse(folder) else (_) {
fail($"{folder} isn't a path.");
return;
};
create_directories(&into) else (e) {
fail($"{folder} couldn't be made ({e}).");
};
for &each in made {
val name := $"{folder}/{each.name}";
val written := path::parse(name.span) else (_) {
fail($"{name} isn't a path.");
return;
};
write_file(&written, each.text.span) else (e) {
fail($"{name} couldn't be written ({e}).");
};
var lines: u64 = 0;
for i in 0..each.text.length {
if each.text.span[i] == '\n' {
lines++;
}
}
println($"Wrote {name}, of {lines} lines.");
}
}