From cf21c48771fd3e186f41d64d04c43d109a4a64d8 Mon Sep 17 00:00:00 2001 From: torque Date: Thu, 7 Mar 2024 22:33:57 -0800 Subject: [PATCH] build: extract example executable from default target --- build.zig | 13 ++++++++----- {src => example}/main.zig | 7 +++++-- 2 files changed, 13 insertions(+), 7 deletions(-) rename {src => example}/main.zig (89%) diff --git a/build.zig b/build.zig index 2197715..654b80a 100644 --- a/build.zig +++ b/build.zig @@ -20,14 +20,17 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); - const exe = b.addExecutable(.{ + const example_step = b.step("example", "build example"); + + const ex_exe = b.addExecutable(.{ .name = "yamltest", - .root_source_file = .{ .path = "src/main.zig" }, + .root_source_file = .{ .path = "example/main.zig" }, .target = target, .optimize = optimize, }); - exe.linkLibrary(libyaml); - exe.addModule("yaml", yaml_zig); + ex_exe.linkLibrary(libyaml); + ex_exe.addModule("yaml", yaml_zig); - b.installArtifact(exe); + const install = b.addInstallArtifact(ex_exe, .{}); + example_step.dependOn(&install.step); } diff --git a/src/main.zig b/example/main.zig similarity index 89% rename from src/main.zig rename to example/main.zig index fb46313..10d7201 100644 --- a/src/main.zig +++ b/example/main.zig @@ -7,11 +7,14 @@ pub fn main() !void { defer _ = gpa.deinit(); const allocator = gpa.allocator(); - const slurp = try std.fs.cwd().readFileAlloc( + const slurp = std.fs.cwd().readFileAlloc( allocator, "test.yaml", 1024 * 1024 * 1024, - ); + ) catch |err| { + std.debug.print("couldn't open test.yaml in the cwd\n", .{}); + return err; + }; defer allocator.free(slurp); var diag = yaml.ParseDiagnostic{ .message = "?????" };