drm/gen_table_fourcc.zig

114 lines
4.1 KiB
Zig
Raw Normal View History

2024-04-19 18:22:59 -06:00
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const argv = try std.process.argsAlloc(gpa.allocator());
defer std.process.argsFree(gpa.allocator(), argv);
const filename = argv[1];
const contents = try std.fs.cwd().readFileAlloc(gpa.allocator(), filename, 50 * 1024 * 1024);
defer gpa.allocator().free(contents);
const stdout = std.io.getStdOut();
const out = stdout.writer();
try out.writeAll(
\\/* AUTOMATICALLY GENERATED by gen_table_fourcc.py. You should modify
\\ that script instead of adding here entries manually! */
\\static const struct drmFormatModifierInfo drm_format_modifier_table[] = {
\\ { DRM_MODIFIER_INVALID(NONE, INVALID) },
\\ { DRM_MODIFIER_LINEAR(NONE, LINEAR) },
\\
);
var line_iter = std.mem.splitScalar(u8, contents, '\n');
check_each_line: while (line_iter.next()) |line| {
errdefer std.log.debug("line = \"{}\"", .{std.zig.fmtEscapes(line)});
if (matchStart(line, "#define I915_FORMAT_MOD_")) |after| {
const modifier_end = std.mem.indexOfAny(u8, after, "\t ") orelse return error.Invalid;
const modifier = after[0..modifier_end];
for (modifier) |character| {
if (character != '_' and !std.ascii.isAlphanumeric(character)) {
continue :check_each_line;
}
}
try out.print(
\\ {{ DRM_MODIFIER_INTEL({[modifier]s}, {[modifier]s}) }},
\\
, .{
.modifier = modifier,
});
} else if (matchStart(line, "#define DRM_FORMAT_MOD_")) |after| {
const end_of_identifier = std.mem.indexOfAny(u8, after, "\t ") orelse return error.Invalid;
const identifier = after[0..end_of_identifier];
const end_of_vendor = std.mem.indexOfScalar(u8, identifier, '_') orelse continue;
const vendor_str = identifier[0..end_of_vendor];
const modifier = identifier[end_of_vendor + 1 ..];
for (modifier) |character| {
if (character != '_' and !std.ascii.isAlphanumeric(character)) {
continue :check_each_line;
}
}
const Vendor = enum { ARM, SAMSUNG, QCOM, VIVANTE, NVIDIA, BROADCOM, ALLWINNER };
const vendor = std.meta.stringToEnum(Vendor, vendor_str) orelse continue;
if (vendor == .ARM) {
if (std.mem.eql(u8, modifier, "TYPE_AFBC")) continue;
if (std.mem.eql(u8, modifier, "TYPE_MISC")) continue;
if (std.mem.eql(u8, modifier, "TYPE_AFRC")) continue;
}
try out.print(
\\ {{ DRM_MODIFIER({[vendor]s}, {[modifier]s}, {[modifier]s}) }},
\\
, .{
.vendor = @tagName(vendor),
.modifier = modifier,
});
}
}
try out.writeAll(
\\};
\\static const struct drmFormatModifierVendorInfo drm_format_modifier_vendor_table[] = {
\\
);
line_iter.reset();
check_each_line: while (line_iter.next()) |line| {
if (matchStart(line, "#define DRM_FORMAT_MOD_VENDOR_")) |after| {
const modifier_end = std.mem.indexOfAny(u8, after, "\t ") orelse return error.Invalid;
const modifier = after[0..modifier_end];
for (modifier) |character| {
if (character != '_' and !std.ascii.isAlphanumeric(character)) {
continue :check_each_line;
}
}
try out.print(
\\ {{ DRM_FORMAT_MOD_VENDOR_{[modifier]s}, "{[modifier]s}" }},
\\
, .{
.modifier = modifier,
});
}
}
try out.writeAll(
\\};
\\
);
}
fn matchStart(haystack: []const u8, needle: []const u8) ?[]const u8 {
if (std.mem.startsWith(u8, haystack, needle)) {
return haystack[needle.len..];
}
return null;
}