Created
February 14, 2024 22:31
-
-
Save maxsei/3c3bdbcdb95c0474ba3f29647adcd7cf to your computer and use it in GitHub Desktop.
custom zig build step
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const std = @import("std"); | |
| const Step = std.build.Step; | |
| const Allocator = std.mem.Allocator; | |
| pub fn build(b: *std.Build) void { | |
| const target = b.standardTargetOptions(.{}); | |
| const optimize = b.standardOptimizeOption(.{}); | |
| const exe = b.addExecutable(.{ | |
| .name = "output-custom-file-zig-build", | |
| .root_source_file = .{ .path = "src/main.zig" }, | |
| .target = target, | |
| .optimize = optimize, | |
| }); | |
| b.installArtifact(exe); | |
| const unit_tests = b.addTest(.{ | |
| .root_source_file = .{ .path = "src/main.zig" }, | |
| .target = target, | |
| .optimize = optimize, | |
| }); | |
| const run_unit_tests = b.addRunArtifact(unit_tests); | |
| const test_step = b.step("test", "Run unit tests"); | |
| test_step.dependOn(&run_unit_tests.step); | |
| const run_codegen = CodegenStep.init(b, "hello.txt"); | |
| const codegen_step = b.step("codegen", "Run codegen"); | |
| codegen_step.dependOn(&run_codegen.step); | |
| } | |
| const CodegenStep = struct { | |
| step: Step, | |
| filename: []const u8, | |
| const Self = @This(); | |
| pub fn init(b: *std.Build, filename: []const u8) *Self { | |
| var ret = b.allocator.create(Self) catch |e| @panic(@errorName(e)); | |
| ret.* = .{ | |
| .step = Step.init(.{ | |
| .id = .custom, | |
| .name = "codegen", | |
| .owner = b, | |
| .makeFn = Self.doStep, | |
| .max_rss = 4096 * 16, | |
| }), | |
| .filename = filename, | |
| }; | |
| return ret; | |
| } | |
| pub fn doStep(step: *Step, _: *std.Progress.Node) anyerror!void { | |
| const self = @fieldParentPtr(Self, "step", step); | |
| const file = try std.fs.cwd().createFile(self.filename, .{}); | |
| defer file.close(); | |
| try file.writeAll("hello world!\n"); | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment