Skip to content

Commit

Permalink
build: restore the exercise chain
Browse files Browse the repository at this point in the history
The new parallel build support in Zig broke the exercise chain, so that
each esercise check is no longer strictly serialized.

  1. Add the Dexno option, in order to isolate the chain starting from a
     named exercise from the normal chain, thus simplify the code.

     The current code have an additional issue: it added 4 x n steps,
     making reading the help message or the list of steps very hard.

     Add only the `install`, `uninstall`, `zigling`, `test` and `start`
     steps.  The last three steps match the old steps `n`, `n_test` and
     `n_start`.

     The default step is zigling (note the singular form).

     The `install` step override the builtin install step, showing a
     custom description and matches the old `n_install` step.
     The uninstall step was added for consistency, so that the
     description is consistent.

     Setup a new chain starting at `zig build -Dexno=n start` so that it
     is stricly serialized.

     The behavior should be the same as the old one.

  2. Handle the code for all the exercises separately.

     Add only the `ziglings step`, making it the default step, in
     addition to the install and uninstall steps.

     Setup a new chain starting at the first exercise, to that it is
     strictly serialized.

     The behavior should be the same as the old one.

The current code has a know issue: the messages from the ZiglingStep and
the ones from the compiler compilation progress are interleaved, but each
message is written atomically, due to the use of `std.debug.getStderrMutex()`.

Update the README.md file.

Closes #202
  • Loading branch information
perillo committed Apr 6, 2023
1 parent 7b793d8 commit 1a7d636
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 34 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,11 @@ $ git clone https://github.com/ratfactor/ziglings
$ cd ziglings
```

Then run `zig build 1` and follow the instructions to begin!
Then run `zig build` and follow the instructions to begin!

```bash
$ zig build 1
$ zig build
```
## :warning: Attention
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

Expand Down
135 changes: 110 additions & 25 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const Exercise = struct {
while (self.main_file[start_index] == '0') start_index += 1;
return self.main_file[start_index..end_index.?];
}

/// Returns the exercise key as an integer.
pub fn number(self: Exercise) usize {
return std.fmt.parseInt(usize, self.key(), 10) catch unreachable;
}
};

const exercises = [_]Exercise{
Expand Down Expand Up @@ -534,50 +539,130 @@ pub fn build(b: *Build) !void {
\\
;

const header_step = PrintStep.create(b, logo, std.io.getStdErr());

const verify_all = b.step("ziglings", "Check all ziglings");
verify_all.dependOn(&header_step.step);
b.default_step = verify_all;
const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false;
const exno: ?usize = b.option(usize, "exno", "Select exercise");

var prev_chain_verify = verify_all;
const header_step = PrintStep.create(b, logo, std.io.getStdErr());

const use_healed = b.option(bool, "healed", "Run exercises from patches/healed") orelse false;
if (exno) |i| {
const ex = blk: {
for (exercises) |ex| {
if (ex.number() == i) break :blk ex;
}
print("unknown exercise number: {}\n", .{i});
std.os.exit(1);
};

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 } });
build_step.install();
const zigling_step = b.step("zigling", b.fmt("Check the solution of {s}", .{ex.main_file}));
b.default_step = zigling_step;

const verify_step = ZiglingStep.create(b, ex, use_healed);

const key = ex.key();
const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } });
build_step.install();

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("Copy {s} to prefix path", .{ex.main_file}));
named_install.dependOn(&build_step.install_step.?.step);
const test_step = b.step("test", b.fmt("Run {s} without checking output", .{ex.main_file}));
test_step.dependOn(&run_step.step);

const install_step = b.step("install", b.fmt("Install {s} to prefix path", .{ex.main_file}));
install_step.dependOn(b.getInstallStep());

const uninstall_step = b.step("uninstall", b.fmt("Uninstall {s} from prefix path", .{ex.main_file}));
uninstall_step.dependOn(b.getUninstallStep());

const named_step = b.allocator.create(Step) catch unreachable;
named_step.* = Step.init(.{
.id = .custom,
.name = "named",
.owner = b,
});
named_step.dependOn(&verify_step.step);
zigling_step.dependOn(named_step);

const start_step = b.step("start", b.fmt("Check all solutions starting at {s}", .{ex.main_file}));
start_step.dependOn(&header_step.step);
start_step.dependOn(&verify_step.step);

const chain_step = b.allocator.create(Step) catch unreachable;
chain_step.* = Step.init(.{
.id = .custom,
.name = "chain",
.owner = b,
});

const named_verify = b.step(key, b.fmt("Check {s} only", .{ex.main_file}));
named_verify.dependOn(&verify_step.step);
var prev_chain = chain_step;
for (exercises) |exn| {
const n = exn.number();
if (n > i) {
const verify_stepn = ZiglingStep.create(b, exn, use_healed);

const start_stepn = b.allocator.create(Step) catch unreachable;
start_stepn.* = Step.init(.{
.id = .custom,
.name = b.fmt("start_step {}", .{n}),
.owner = b,
});
start_stepn.dependOn(prev_chain);
start_stepn.dependOn(&verify_stepn.step);

prev_chain = start_stepn;
}
}
start_step.dependOn(prev_chain);

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);
return;
}

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 ziglings_step = b.step("ziglings", "Check all ziglings");
ziglings_step.dependOn(&header_step.step);
b.default_step = ziglings_step;

const chain_step = b.allocator.create(Step) catch unreachable;
chain_step.* = Step.init(.{
.id = .custom,
.name = "chain",
.owner = b,
});

var start_step: *Step = undefined;
var prev_chain = chain_step;
for (exercises, 0..) |ex, i| {
const n = ex.number();
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;

prev_chain_verify.dependOn(chain_verify);
prev_chain_verify = chain_verify;
const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } });
build_step.install();

const verify_stepn = ZiglingStep.create(b, ex, use_healed);

const zigling_stepn = b.allocator.create(Step) catch unreachable;
zigling_stepn.* = Step.init(.{
.id = .custom,
.name = b.fmt("zigling_step {}", .{n}),
.owner = b,
});
if (i == 0) {
start_step = zigling_stepn;
zigling_stepn.dependOn(&verify_stepn.step);
} else {
zigling_stepn.dependOn(prev_chain);
zigling_stepn.dependOn(&verify_stepn.step);

prev_chain = zigling_stepn;
}
}
start_step.dependOn(prev_chain);
ziglings_step.dependOn(start_step);
}

var use_color_escapes = false;
Expand Down

0 comments on commit 1a7d636

Please sign in to comment.