|
| 1 | +const std = @import("std"); |
| 2 | +const Build = std.Build; |
| 3 | +const ChildProcess = std.process.Child; |
| 4 | + |
| 5 | +const log = std.log.scoped(.For_0_14_0); |
| 6 | + |
| 7 | +const args = [_][]const u8{ "zig", "build" }; |
| 8 | + |
| 9 | +const version = "15"; |
| 10 | + |
| 11 | +const relative_path = "course/code/" ++ version; |
| 12 | + |
| 13 | +pub fn build(b: *Build) void { |
| 14 | + // get target and optimize |
| 15 | + const target = b.standardTargetOptions(.{}); |
| 16 | + const optimize = b.standardOptimizeOption(.{}); |
| 17 | + |
| 18 | + var lazy_path = b.path(relative_path); |
| 19 | + |
| 20 | + const full_path = lazy_path.getPath(b); |
| 21 | + |
| 22 | + // open dir |
| 23 | + var dir = std.fs.openDirAbsolute(full_path, .{ .iterate = true }) catch |err| { |
| 24 | + log.err("open 14 path failed, err is {}", .{err}); |
| 25 | + std.process.exit(1); |
| 26 | + }; |
| 27 | + defer dir.close(); |
| 28 | + |
| 29 | + // make a iterate for release ath |
| 30 | + var iterate = dir.iterate(); |
| 31 | + |
| 32 | + while (iterate.next()) |val| { |
| 33 | + if (val) |entry| { |
| 34 | + // get the entry name, entry can be file or directory |
| 35 | + const output_name = std.mem.trimRight(u8, entry.name, ".zig"); |
| 36 | + if (entry.kind == .file) { |
| 37 | + |
| 38 | + // connect path |
| 39 | + const path = std.fs.path.join(b.allocator, &[_][]const u8{ relative_path, entry.name }) catch |err| { |
| 40 | + log.err("fmt path for examples failed, err is {}", .{err}); |
| 41 | + std.process.exit(1); |
| 42 | + }; |
| 43 | + |
| 44 | + // build exe |
| 45 | + const exe = b.addExecutable(.{ |
| 46 | + .name = output_name, |
| 47 | + .root_source_file = b.path(path), |
| 48 | + .target = target, |
| 49 | + .optimize = optimize, |
| 50 | + }); |
| 51 | + exe.linkLibC(); |
| 52 | + |
| 53 | + // add to default install |
| 54 | + b.installArtifact(exe); |
| 55 | + |
| 56 | + // build test |
| 57 | + const unit_tests = b.addTest(.{ |
| 58 | + .root_source_file = b.path(path), |
| 59 | + .target = target, |
| 60 | + .optimize = optimize, |
| 61 | + }); |
| 62 | + |
| 63 | + // add to default install |
| 64 | + b.getInstallStep().dependOn(&b.addRunArtifact(unit_tests).step); |
| 65 | + } else if (entry.kind == .directory) { |
| 66 | + |
| 67 | + // build child process |
| 68 | + var child = ChildProcess.init(&args, b.allocator); |
| 69 | + |
| 70 | + // build cwd |
| 71 | + const cwd = std.fs.path.join(b.allocator, &[_][]const u8{ |
| 72 | + full_path, |
| 73 | + entry.name, |
| 74 | + }) catch |err| { |
| 75 | + log.err("fmt path for examples failed, err is {}", .{err}); |
| 76 | + std.process.exit(1); |
| 77 | + }; |
| 78 | + |
| 79 | + // open entry dir |
| 80 | + const entry_dir = std.fs.openDirAbsolute(cwd, .{}) catch unreachable; |
| 81 | + entry_dir.access("build.zig", .{}) catch { |
| 82 | + log.err("not found build.zig in path {s}", .{cwd}); |
| 83 | + std.process.exit(1); |
| 84 | + }; |
| 85 | + |
| 86 | + // set child cwd |
| 87 | + // this api maybe changed in the future |
| 88 | + child.cwd = cwd; |
| 89 | + |
| 90 | + // spawn and wait child process |
| 91 | + _ = child.spawnAndWait() catch unreachable; |
| 92 | + } |
| 93 | + } else { |
| 94 | + // Stop endless loop |
| 95 | + break; |
| 96 | + } |
| 97 | + } else |err| { |
| 98 | + log.err("iterate examples_path failed, err is {}", .{err}); |
| 99 | + std.process.exit(1); |
| 100 | + } |
| 101 | +} |
0 commit comments