diff options
| author | Manlio Perillo <[email protected]> | 2023-04-30 17:47:16 +0200 |
|---|---|---|
| committer | Manlio Perillo <[email protected]> | 2023-05-01 18:39:54 +0200 |
| commit | 69103a3b821d8b8955eac59489b0dabb6f4735ff (patch) | |
| tree | 1dd02eb087f01eff8c45233a81acef607f232d82 /build.zig | |
| parent | 123fd4b1057783de7b8c9b7b536f61133dbad016 (diff) | |
| download | ziglings-69103a3b821d8b8955eac59489b0dabb6f4735ff.tar.xz ziglings-69103a3b821d8b8955eac59489b0dabb6f4735ff.zip | |
build: add the Exercise.addExecutable method
Currently addExecutable is called 3 times, unnecessarily making the code
more complex.
The method takes as argument the path to the exercises directory.
Additionally, use the new std.Build.ExecutableOptions.link_libc field.
The new field was added in ziglang/zig@adc9b77d5f on 2023-04-13.
Update the required Zig compiler version.
Note that I added the **current** zig version to the changelog, since
the reason for the change is known only to the person updating the
version.
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 36 |
1 files changed, 16 insertions, 20 deletions
@@ -5,6 +5,7 @@ const ipc = @import("src/ipc.zig"); const tests = @import("test/tests.zig"); const Build = compat.Build; +const CompileStep = compat.build.CompileStep; const Step = compat.build.Step; const Child = std.process.Child; @@ -60,6 +61,18 @@ pub const Exercise = struct { pub fn number(self: Exercise) usize { return std.fmt.parseInt(usize, self.key(), 10) catch unreachable; } + + /// Returns the CompileStep for this exercise. + pub fn addExecutable(self: Exercise, b: *Build, work_path: []const u8) *CompileStep { + const file_path = join(b.allocator, &.{ work_path, self.main_file }) catch + @panic("OOM"); + + return b.addExecutable(.{ + .name = self.baseName(), + .root_source_file = .{ .path = file_path }, + .link_libc = self.link_libc, + }); + } }; pub fn build(b: *Build) !void { @@ -121,14 +134,8 @@ pub fn build(b: *Build) !void { } const ex = exercises[n - 1]; - const base_name = ex.baseName(); - const file_path = join(b.allocator, &.{ work_path, ex.main_file }) catch - @panic("OOM"); - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - if (ex.link_libc) { - build_step.linkLibC(); - } + const build_step = ex.addExecutable(b, work_path); b.installArtifact(build_step); const run_step = b.addRunArtifact(build_step); @@ -178,14 +185,7 @@ pub fn build(b: *Build) !void { b.default_step = test_step; for (exercises) |ex| { - const base_name = ex.baseName(); - const file_path = join(b.allocator, &.{ healed_path, ex.main_file }) catch - @panic("OOM"); - - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); - if (ex.link_libc) { - build_step.linkLibC(); - } + const build_step = ex.addExecutable(b, healed_path); b.installArtifact(build_step); const run_step = b.addRunArtifact(build_step); @@ -207,11 +207,7 @@ pub fn build(b: *Build) !void { // error with old Zig compilers. var prev_step = &header_step.step; for (exercises) |ex| { - const base_name = ex.baseName(); - const file_path = join(b.allocator, &.{ "exercises", ex.main_file }) catch - @panic("OOM"); - - const build_step = b.addExecutable(.{ .name = base_name, .root_source_file = .{ .path = file_path } }); + const build_step = ex.addExecutable(b, "exercises"); b.installArtifact(build_step); const verify_stepn = ZiglingStep.create(b, ex, work_path); |
