|
| 1 | +const std = @import("std"); |
| 2 | +const ChildProcess = std.process.Child; |
| 3 | + |
| 4 | +const args = [_][]const u8{ "zig", "build" }; |
| 5 | + |
| 6 | +pub fn build(b: *std.Build) !void { |
| 7 | + const full_path = try std.process.getCwdAlloc(b.allocator); |
| 8 | + |
| 9 | + var dir = std.fs.openDirAbsolute(full_path, .{ .iterate = true }) catch |err| { |
| 10 | + std.log.err("open path failed {s}, err is {}", .{ full_path, err }); |
| 11 | + std.process.exit(1); |
| 12 | + }; |
| 13 | + defer dir.close(); |
| 14 | + |
| 15 | + var iterate = dir.iterate(); |
| 16 | + |
| 17 | + while (iterate.next()) |val| { |
| 18 | + if (val) |entry| { |
| 19 | + // get the entry name, entry can be file or directory |
| 20 | + const name = entry.name; |
| 21 | + if (entry.kind == .directory) { |
| 22 | + if (eqlu8(name, ".zig-cache") or eqlu8(name, "zig-out") or eqlu8(name, "zig-cache")) |
| 23 | + continue; |
| 24 | + |
| 25 | + // build child process |
| 26 | + var child = ChildProcess.init(&args, b.allocator); |
| 27 | + |
| 28 | + // build cwd |
| 29 | + const cwd = std.fs.path.join(b.allocator, &[_][]const u8{ |
| 30 | + full_path, |
| 31 | + name, |
| 32 | + }) catch |err| { |
| 33 | + std.log.err("fmt path failed, err is {}", .{err}); |
| 34 | + std.process.exit(1); |
| 35 | + }; |
| 36 | + |
| 37 | + // open entry dir |
| 38 | + const entry_dir = std.fs.openDirAbsolute(cwd, .{}) catch unreachable; |
| 39 | + entry_dir.access("build.zig", .{}) catch { |
| 40 | + std.log.err("not found build.zig in path {s}", .{cwd}); |
| 41 | + std.process.exit(1); |
| 42 | + }; |
| 43 | + |
| 44 | + // set child cwd |
| 45 | + // this api maybe changed in the future |
| 46 | + child.cwd = cwd; |
| 47 | + |
| 48 | + // spawn and wait child process |
| 49 | + _ = child.spawnAndWait() catch unreachable; |
| 50 | + } |
| 51 | + } else { |
| 52 | + // Stop endless loop |
| 53 | + break; |
| 54 | + } |
| 55 | + } else |err| { |
| 56 | + std.log.err("iterate examples_path failed, err is {}", .{err}); |
| 57 | + std.process.exit(1); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn eqlu8(a: []const u8, b: []const u8) bool { |
| 62 | + return std.mem.eql(u8, a, b); |
| 63 | +} |
0 commit comments