From 78e856f6021beae1a360f321be9ce5b0ca25d517 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Sun, 19 Mar 2023 00:59:42 +0100 Subject: [PATCH 1/2] first test for new build system --- build.zig | 120 +++++++++++++++++++++++++++--------------------------- 1 file changed, 59 insertions(+), 61 deletions(-) diff --git a/build.zig b/build.zig index 4f9f046..9196bea 100644 --- a/build.zig +++ b/build.zig @@ -8,7 +8,7 @@ const print = std.debug.print; // When changing this version, be sure to also update README.md in two places: // 1) Getting Started // 2) Version Changes -const needed_version = std.SemanticVersion.parse("0.11.0-dev.1844") catch unreachable; +const needed_version = std.SemanticVersion.parse("0.11.0-dev.2157") catch unreachable; const Exercise = struct { /// main_file must have the format key_name.zig. @@ -505,7 +505,7 @@ fn checkVersion() bool { return order != .lt; } -pub fn build(b: *Builder) void { +pub fn build(b: *Builder) !void { // Use a comptime branch for the version check. // If this fails, code after this block is not compiled. // It is parsed though, so versions of zig from before 0.6.0 @@ -533,30 +533,24 @@ pub fn build(b: *Builder) void { } use_color_escapes = false; - switch (b.color) { - .on => use_color_escapes = true, - .off => use_color_escapes = false, - .auto => { - if (std.io.getStdErr().supportsAnsiEscapeCodes()) { - use_color_escapes = true; - } else if (builtin.os.tag == .windows) { - const w32 = struct { - const WINAPI = std.os.windows.WINAPI; - const DWORD = std.os.windows.DWORD; - const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; - const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12)); - extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque; - extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32; - extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32; - }; - const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE); - var mode: w32.DWORD = 0; - if (w32.GetConsoleMode(handle, &mode) != 0) { - mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING; - use_color_escapes = w32.SetConsoleMode(handle, mode) != 0; - } - } - }, + if (std.io.getStdErr().supportsAnsiEscapeCodes()) { + use_color_escapes = true; + } else if (builtin.os.tag == .windows) { + const w32 = struct { + const WINAPI = std.os.windows.WINAPI; + const DWORD = std.os.windows.DWORD; + const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004; + const STD_ERROR_HANDLE = @bitCast(DWORD, @as(i32, -12)); + extern "kernel32" fn GetStdHandle(id: DWORD) callconv(WINAPI) ?*anyopaque; + extern "kernel32" fn GetConsoleMode(console: ?*anyopaque, out_mode: *DWORD) callconv(WINAPI) u32; + extern "kernel32" fn SetConsoleMode(console: ?*anyopaque, mode: DWORD) callconv(WINAPI) u32; + }; + const handle = w32.GetStdHandle(w32.STD_ERROR_HANDLE); + var mode: w32.DWORD = 0; + if (w32.GetConsoleMode(handle, &mode) != 0) { + mode |= w32.ENABLE_VIRTUAL_TERMINAL_PROCESSING; + use_color_escapes = w32.SetConsoleMode(handle, mode) != 0; + } } if (use_color_escapes) { @@ -566,7 +560,7 @@ pub fn build(b: *Builder) void { reset_text = "\x1b[0m"; } - const header_step = b.addLog( + const logo = \\ \\ _ _ _ \\ ___(_) __ _| (_)_ __ __ _ ___ @@ -576,50 +570,57 @@ pub fn build(b: *Builder) void { \\ |___/ |___/ \\ \\ - , .{}); + ; + const header_step = b.step("header", logo); + print("{s}\n", .{logo}); const verify_all = b.step("ziglings", "Check all ziglings"); - verify_all.dependOn(&header_step.step); + verify_all.dependOn(header_step); b.default_step = verify_all; var prev_chain_verify = verify_all; const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false; - for (exercises) |ex| { - const base_name = ex.baseName(); - const file_path = std.fs.path.join(b.allocator, &[_][]const u8{ - if (use_healed) "patches/healed" else "exercises", ex.main_file, - }) catch unreachable; - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); + // for (exercises) |ex| { + const ex = exercises[0]; + const base_name = ex.baseName(); + const file_path = std.fs.path.join(b.allocator, &[_][]const u8{ + if (use_healed) "patches/healed" else "exercises", ex.main_file, + }) catch unreachable; + const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - build_step.install(); + build_step.install(); + // std.time.sleep(1000000); - const verify_step = ZiglingStep.create(b, ex, use_healed); + const verify_step = ZiglingStep.create(b, ex, use_healed); - const key = ex.key(); + const key = ex.key(); - const named_test = b.step(b.fmt("{s}_test", .{key}), b.fmt("Run {s} without checking output", .{ex.main_file})); - const run_step = build_step.run(); - named_test.dependOn(&run_step.step); + const named_test = b.step(b.fmt("{s}_test", .{key}), b.fmt("Run {s} without checking output", .{ex.main_file})); + const run_step = build_step.run(); + named_test.dependOn(&run_step.step); - const named_install = b.step(b.fmt("{s}_install", .{key}), b.fmt("Install {s} to zig-cache/bin", .{ex.main_file})); - named_install.dependOn(&build_step.install_step.?.step); + const named_install = b.step(b.fmt("{s}_install", .{key}), b.fmt("Install {s} to zig-cache/bin", .{ex.main_file})); + named_install.dependOn(&build_step.install_step.?.step); - const named_verify = b.step(key, b.fmt("Check {s} only", .{ex.main_file})); - named_verify.dependOn(&verify_step.step); + const named_verify = b.step(key, b.fmt("Check {s} only", .{ex.main_file})); + named_verify.dependOn(&verify_step.step); - const chain_verify = b.allocator.create(Step) catch unreachable; - chain_verify.* = Step.initNoOp(.custom, b.fmt("chain {s}", .{key}), b.allocator); - chain_verify.dependOn(&verify_step.step); + const chain_verify = b.allocator.create(Step) catch unreachable; + chain_verify.* = Step.init(Step.Options{ .id = .custom, .name = b.fmt("chain {s}", .{key}), .owner = b }); + chain_verify.dependOn(&verify_step.step); - const named_chain = b.step(b.fmt("{s}_start", .{key}), b.fmt("Check all solutions starting at {s}", .{ex.main_file})); - named_chain.dependOn(&header_step.step); - named_chain.dependOn(chain_verify); + const named_chain = b.step(b.fmt("{s}_start", .{key}), b.fmt("Check all solutions starting at {s}", .{ex.main_file})); + named_chain.dependOn(header_step); + named_chain.dependOn(chain_verify); - prev_chain_verify.dependOn(chain_verify); - prev_chain_verify = chain_verify; - } + prev_chain_verify.dependOn(chain_verify); + prev_chain_verify = chain_verify; + // std.os.exit(0); + // while (true) {} + + // } } var use_color_escapes = false; @@ -637,7 +638,8 @@ const ZiglingStep = struct { pub fn create(builder: *Builder, exercise: Exercise, use_healed: bool) *@This() { const self = builder.allocator.create(@This()) catch unreachable; self.* = .{ - .step = Step.init(.custom, exercise.main_file, builder.allocator, make), + // .step = Step.init(.custom, exercise.main_file, builder.allocator, make), + .step = Step.init(Step.Options{ .id = .custom, .name = exercise.main_file, .owner = builder, .makeFn = make }), .exercise = exercise, .builder = builder, .use_healed = use_healed, @@ -645,7 +647,8 @@ const ZiglingStep = struct { return self; } - fn make(step: *Step) anyerror!void { + fn make(step: *Step, prog_node: *std.Progress.Node) anyerror!void { + _ = prog_node; const self = @fieldParentPtr(@This(), "step", step); self.makeInternal() catch { if (self.exercise.hint.len > 0) { @@ -756,11 +759,6 @@ const ZiglingStep = struct { zig_args.append("-lc") catch unreachable; } - if (builder.color != .auto) { - zig_args.append("--color") catch unreachable; - zig_args.append(@tagName(builder.color)) catch unreachable; - } - const zig_file = std.fs.path.join(builder.allocator, &[_][]const u8{ if (self.use_healed) "patches/healed" else "exercises", self.exercise.main_file }) catch unreachable; zig_args.append(builder.pathFromRoot(zig_file)) catch unreachable; From 3b85c24694721c957c336cf8eae128d2191f3ba8 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Sun, 19 Mar 2023 18:23:35 +0100 Subject: [PATCH 2/2] workaround for parallel processing of the build steps --- README.md | 16 +++++++++++---- build.zig | 58 +++++++++++++++++++++++++------------------------------ 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 229fe3f..918602a 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Verify the installation and build number of `zig` like so: ```bash $ zig version -0.11.0-dev.1844+xxxxxxxxx +0.11.0-dev.2157+xxxxxxxxx ``` Clone this repository with Git: @@ -52,11 +52,18 @@ $ git clone https://github.com/ratfactor/ziglings $ cd ziglings ``` -Then run `zig build` and follow the instructions to begin! +Then run `zig build 1` and follow the instructions to begin! ```bash -$ zig build +$ zig build 1 ``` +## Note +Due to Zig's new build system, exercises can currently only be run manually with their number! + +```bash +$ zig build xy +``` +We hope to be able to offer this again soon in the automatic way. ## A Note About Versions @@ -82,7 +89,8 @@ about input: ### Version Changes -Version-0.11.0-dev.1844+xxxxxxxxx +Version-0.11.0-dev.2157+xxxxxxxxx +* *2023-02-21* zig 0.11.0-dev.2157 - changes in `build system` - new: parallel processing of the build steps * *2023-02-21* zig 0.11.0-dev.1711 - changes in `for loops` - new: Multi-Object For-Loops + Struct-of-Arrays * *2023-02-12* zig 0.11.0-dev.1638 - changes in `std.Build` cache_root now returns a directory struct * *2023-02-04* zig 0.11.0-dev.1568 - changes in `std.Build` (combine `std.build` and `std.build.Builder` into `std.Build`) diff --git a/build.zig b/build.zig index 9196bea..5ade0c4 100644 --- a/build.zig +++ b/build.zig @@ -571,7 +571,7 @@ pub fn build(b: *Builder) !void { \\ \\ ; - const header_step = b.step("header", logo); + const header_step = b.step("info", logo); print("{s}\n", .{logo}); const verify_all = b.step("ziglings", "Check all ziglings"); @@ -582,45 +582,40 @@ pub fn build(b: *Builder) !void { const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false; - // for (exercises) |ex| { - const ex = exercises[0]; - const base_name = ex.baseName(); - const file_path = std.fs.path.join(b.allocator, &[_][]const u8{ - if (use_healed) "patches/healed" else "exercises", ex.main_file, - }) catch unreachable; - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - - build_step.install(); - // std.time.sleep(1000000); + for (exercises) |ex| { + const base_name = ex.baseName(); + const file_path = std.fs.path.join(b.allocator, &[_][]const u8{ + if (use_healed) "patches/healed" else "exercises", ex.main_file, + }) catch unreachable; + const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - const verify_step = ZiglingStep.create(b, ex, use_healed); + build_step.install(); - const key = ex.key(); + const verify_step = ZiglingStep.create(b, ex, use_healed); - const named_test = b.step(b.fmt("{s}_test", .{key}), b.fmt("Run {s} without checking output", .{ex.main_file})); - const run_step = build_step.run(); - named_test.dependOn(&run_step.step); + const key = ex.key(); - const named_install = b.step(b.fmt("{s}_install", .{key}), b.fmt("Install {s} to zig-cache/bin", .{ex.main_file})); - named_install.dependOn(&build_step.install_step.?.step); + const named_test = b.step(b.fmt("{s}_test", .{key}), b.fmt("Run {s} without checking output", .{ex.main_file})); + const run_step = build_step.run(); + named_test.dependOn(&run_step.step); - const named_verify = b.step(key, b.fmt("Check {s} only", .{ex.main_file})); - named_verify.dependOn(&verify_step.step); + const named_install = b.step(b.fmt("{s}_install", .{key}), b.fmt("Install {s} to zig-cache/bin", .{ex.main_file})); + named_install.dependOn(&build_step.install_step.?.step); - const chain_verify = b.allocator.create(Step) catch unreachable; - chain_verify.* = Step.init(Step.Options{ .id = .custom, .name = b.fmt("chain {s}", .{key}), .owner = b }); - chain_verify.dependOn(&verify_step.step); + const named_verify = b.step(key, b.fmt("Check {s} only", .{ex.main_file})); + named_verify.dependOn(&verify_step.step); - const named_chain = b.step(b.fmt("{s}_start", .{key}), b.fmt("Check all solutions starting at {s}", .{ex.main_file})); - named_chain.dependOn(header_step); - named_chain.dependOn(chain_verify); + const chain_verify = b.allocator.create(Step) catch unreachable; + chain_verify.* = Step.init(Step.Options{ .id = .custom, .name = b.fmt("chain {s}", .{key}), .owner = b }); + chain_verify.dependOn(&verify_step.step); - prev_chain_verify.dependOn(chain_verify); - prev_chain_verify = chain_verify; - // std.os.exit(0); - // while (true) {} + const named_chain = b.step(b.fmt("{s}_start", .{key}), b.fmt("Check all solutions starting at {s}", .{ex.main_file})); + named_chain.dependOn(header_step); + named_chain.dependOn(chain_verify); - // } + prev_chain_verify.dependOn(chain_verify); + prev_chain_verify = chain_verify; + } } var use_color_escapes = false; @@ -638,7 +633,6 @@ const ZiglingStep = struct { pub fn create(builder: *Builder, exercise: Exercise, use_healed: bool) *@This() { const self = builder.allocator.create(@This()) catch unreachable; self.* = .{ - // .step = Step.init(.custom, exercise.main_file, builder.allocator, make), .step = Step.init(Step.Options{ .id = .custom, .name = exercise.main_file, .owner = builder, .makeFn = make }), .exercise = exercise, .builder = builder,