From e61c5f775d5f03c03fb14478c162e0df528280da Mon Sep 17 00:00:00 2001 From: adrien Date: Thu, 21 May 2026 23:33:54 +0200 Subject: [PATCH] Prevent building example when use as module --- build.zig | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/build.zig b/build.zig index 4c0cf8d..b610f0d 100644 --- a/build.zig +++ b/build.zig @@ -29,31 +29,33 @@ pub fn build(b: *std.Build) !void { mod.linkSystemLibrary("gcc_s", .{}); } - var threaded: std.Io.Threaded = .init_single_threaded; - const io = threaded.io(); + if (b.pkg_hash.len == 0) { + var threaded: std.Io.Threaded = .init_single_threaded; + const io = threaded.io(); - var buf: [1024]u8 = undefined; - const exemples = try std.Io.Dir.cwd().openDir(io, "examples", .{ .access_sub_paths = false, .iterate = true }); - var iter = exemples.iterate(); - while (try iter.next(io)) |entry| { - if (entry.kind != .file) continue; - if (!std.mem.eql(u8, entry.name[entry.name.len - 4 ..], ".zig")) continue; + var buf: [1024]u8 = undefined; + const exemples = try std.Io.Dir.cwd().openDir(io, "examples", .{ .access_sub_paths = false, .iterate = true }); + var iter = exemples.iterate(); + while (try iter.next(io)) |entry| { + if (entry.kind != .file) continue; + if (!std.mem.eql(u8, entry.name[entry.name.len - 4 ..], ".zig")) continue; - const exe = b.addExecutable(.{ - .name = entry.name[0 .. entry.name.len - 4], - .root_module = b.createModule(.{ - .root_source_file = b.path(try std.fmt.bufPrint(&buf, "examples/{s}", .{entry.name})), - .target = target, - .optimize = optimize, - .imports = &.{}, - }), - }); - exe.root_module.addImport("gpu", mod); + const exe = b.addExecutable(.{ + .name = entry.name[0 .. entry.name.len - 4], + .root_module = b.createModule(.{ + .root_source_file = b.path(try std.fmt.bufPrint(&buf, "examples/{s}", .{entry.name})), + .target = target, + .optimize = optimize, + .imports = &.{}, + }), + }); + exe.root_module.addImport("gpu", mod); - b.installArtifact(exe); + b.installArtifact(exe); - const run_step = b.step(entry.name[0 .. entry.name.len - 4], try std.fmt.bufPrint(&buf, "Run {s} demo", .{entry.name})); - const run_cmd = b.addRunArtifact(exe); - run_step.dependOn(&run_cmd.step); + const run_step = b.step(entry.name[0 .. entry.name.len - 4], try std.fmt.bufPrint(&buf, "Run {s} demo", .{entry.name})); + const run_cmd = b.addRunArtifact(exe); + run_step.dependOn(&run_cmd.step); + } } }