Basic setup for examples

This commit is contained in:
Robin Voetter
2020-07-01 03:07:55 +02:00
parent 023b46751a
commit f0813e99d2
7 changed files with 13754 additions and 91 deletions

View File

@@ -1,21 +1,35 @@
const std = @import("std"); const std = @import("std");
const vkgen = @import("generator/generator.zig");
const Builder = std.build.Builder; const Builder = std.build.Builder;
const FmtStep = std.build.FmtStep;
pub fn generateVk(b: *Builder) []const u8 {
const spec = std.fs.cwd().readFileAlloc(b.allocator, "examples/vk.xml", std.math.maxInt(usize)) catch unreachable;
const output = std.fs.path.join(
b.allocator,
&[_][]const u8{b.cache_root, "vk.zig"},
) catch unreachable;
const output_file = std.fs.cwd().createFile(output, .{}) catch unreachable;
defer output_file.close();
vkgen.generate(b.allocator, spec, output_file.writer()) catch unreachable;
return output;
}
pub fn build(b: *Builder) void { pub fn build(b: *Builder) void {
const generator = b.addExecutable("vulkan-zig-gen", "generator/main.zig");
generator.setBuildMode(b.standardReleaseOptions());
var test_step = b.step("test", "Run all the tests"); var test_step = b.step("test", "Run all the tests");
test_step.dependOn(&b.addTest("generator/main.zig").step); test_step.dependOn(&b.addTest("generator/generator.zig").step);
const run_cmd = generator.run(); const target = b.standardTargetOptions(.{});
if (b.args) |args| { const mode = b.standardReleaseOptions();
run_cmd.addArgs(args); const exe = b.addExecutable("example", "examples/main.zig");
} exe.setTarget(target);
exe.setBuildMode(mode);
const run_step = b.step("run", ""); exe.install();
run_step.dependOn(&run_cmd.step);
const vk_path = generateVk(b);
b.default_step.dependOn(&generator.step); const fmt_step = b.addFmt(&[_][]const u8{vk_path});
b.installArtifact(generator); exe.step.dependOn(&fmt_step.step);
exe.addPackagePath("vk", vk_path);
} }

39
examples/build.zig Normal file
View File

@@ -0,0 +1,39 @@
const std = @import("std");
const Builder = std.build.Builder;
const FmtStep = std.build.FmtStep;
const vkgen = @import("../generator/generate.zig");
pub fn generateVk(b: *Builder) []const u8 {
const spec = std.fs.cwd().readFileAlloc(b.allocator, "vk.xml", std.math.maxInt(usize)) catch unreachable;
const output = std.fs.path.join(
b.allocator,
&[_][]const u8{b.cache_root, "vk.zig"},
) catch unreachable;
const output_file = std.fs.cwd().openFile(output, .{}) catch unreachable;
defer output_file.close();
vkgen.generate(b.allocator, spec, output_file.writer());
return output;
}
pub fn build(b: *Builder) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("example", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const vk_path = generateVk(b);
const fmt_step = b.addFmt(&[_][]const u8{vk_path});
exe.step.dependOn(&fmt_step.step);
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}

5
examples/main.zig Normal file
View File

@@ -0,0 +1,5 @@
const std = @import("std");
pub fn main() anyerror!void {
std.debug.warn("All your codebase are belong to us.\n", .{});
}

13672
examples/vk.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,67 +0,0 @@
const std = @import("std");
const testing = std.testing;
fn FlagTraits(comptime Self: type) type {
return struct {
pub fn init(flags: var) Self {
var self: u32 = 0;
inline for (std.meta.fields(@TypeOf(flags))) |field| {
self |= @enumToInt(@as(Self, @field(flags, field.name)));
}
return @intToEnum(Self, self);
}
pub fn merge(lhs: Self, rhs: Self) Self {
return @intToEnum(Self, @enumToInt(lhs) | @enumToInt(rhs));
}
pub fn intersect(lhs: Self, rhs: Self) Self {
return @intToEnum(Self, @enumToInt(lhs) & @enumToInt(rhs));
}
pub fn subtract(lhs: Self, rhs: Self) Self {
return @intToEnum(Self, @enumToInt(lhs) & @enumToInt(rhs.complement()));
}
pub fn complement(self: Self) Self {
const all = comptime blk: {
var flags: u32 = 0;
for (std.meta.fields(Self)) |field| {
flags |= field.value;
}
break :blk flags;
};
return @intToEnum(Self, ~@enumToInt(self) & all);
}
pub fn contains(lhs: Self, rhs: Self) bool {
return @enumToInt(lhs) & @enumToInt(rhs) == @enumToInt(rhs);
}
pub fn format(
self: Self,
fmt: []const u8,
options: std.fmt.FormatOptions,
out_stream: var
) @TypeOf(out_stream).Error!void {
const bits = @enumToInt(self);
var first = true;
inline for (std.meta.fields(Self)) |field| {
if (@popCount(u32, field.value) != 1) continue;
if (bits & field.value == field.value) {
if (first) {
first = false;
} else {
try std.fmt.formatBuf(" | ", options, out_stream);
}
try std.fmt.formatBuf(field.name, options, out_stream);
}
}
}
};
}

View File

@@ -193,11 +193,8 @@ pub const Generator = struct {
} }
}; };
pub fn generate(allocator: *Allocator, xml_reader: var, writer: var) !void { pub fn generate(allocator: *Allocator, spec_xml: []const u8, writer: var) !void {
const source = try xml_reader.readAllAlloc(allocator, std.math.maxInt(usize)); const spec = try xml.parse(allocator, spec_xml);
defer allocator.free(source);
const spec = try xml.parse(allocator, source);
defer spec.deinit(); defer spec.deinit();
var gen = try Generator.init(allocator, spec.root); var gen = try Generator.init(allocator, spec.root);
@@ -207,3 +204,8 @@ pub fn generate(allocator: *Allocator, xml_reader: var, writer: var) !void {
try gen.resolveDeclarations(); try gen.resolveDeclarations();
try gen.render(writer); try gen.render(writer);
} }
test "main" {
_ = @import("xml.zig");
_ = @import("registry/c-parse.zig");
}

View File

@@ -120,13 +120,11 @@ pub fn main() !void {
const file = try std.fs.cwd().openFileZ(std.os.argv[1], .{}); const file = try std.fs.cwd().openFileZ(std.os.argv[1], .{});
defer file.close(); defer file.close();
const spec = try file.reader().readAllAlloc(allocator, std.math.maxInt(usize));
defer allocator.free(spec);
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
try vkgen.generate(&prof_alloc.allocator, file.reader(), stdout); try vkgen.generate(allocator, spec, stdout);
std.debug.print("Total memory usage: {} KiB\n", .{@divTrunc(prof_alloc.max_usage, 1024)}); std.debug.print("Total memory usage: {} KiB\n", .{@divTrunc(prof_alloc.max_usage, 1024)});
} }
test "main" {
_ = @import("xml.zig");
_ = @import("registry/c-parse.zig");
}