From d268e0cf2e3cd152017a165ca80085a5823c45a2 Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Mon, 25 May 2020 11:20:31 +0300 Subject: [PATCH 1/2] Added and id and a cast function to build steps --- lib/std/build.zig | 62 +++++++++++++++++++++++++++-------- lib/std/build/check_file.zig | 2 +- lib/std/build/emit_raw.zig | 2 +- lib/std/build/fmt.zig | 2 +- lib/std/build/run.zig | 2 +- lib/std/build/translate_c.zig | 2 +- lib/std/build/write_file.zig | 2 +- 7 files changed, 55 insertions(+), 19 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index 4fdb64903f..66e8d8a679 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -154,11 +154,11 @@ pub const Builder = struct { .dest_dir = env_map.get("DESTDIR"), .installed_files = ArrayList(InstalledFile).init(allocator), .install_tls = TopLevelStep{ - .step = Step.initNoOp("install", allocator), + .step = Step.initNoOp(.TopLevel, "install", allocator), .description = "Copy build artifacts to prefix path", }, .uninstall_tls = TopLevelStep{ - .step = Step.init("uninstall", allocator, makeUninstall), + .step = Step.init(.TopLevel, "uninstall", allocator, makeUninstall), .description = "Remove build artifacts from prefix path", }, .release_mode = null, @@ -500,7 +500,7 @@ pub const Builder = struct { pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step { const step_info = self.allocator.create(TopLevelStep) catch unreachable; step_info.* = TopLevelStep{ - .step = Step.initNoOp(name, self.allocator), + .step = Step.initNoOp(.TopLevel, name, self.allocator), .description = description, }; self.top_level_steps.append(step_info) catch unreachable; @@ -1286,7 +1286,7 @@ pub const LibExeObjStep = struct { .root_src = root_src, .name = name, .frameworks = BufSet.init(builder.allocator), - .step = Step.init(name, builder.allocator, make), + .step = Step.init(.LibExeObj, name, builder.allocator, make), .version = ver, .out_filename = undefined, .out_h_filename = builder.fmt("{}.h", .{name}), @@ -2223,7 +2223,7 @@ pub const LibExeObjStep = struct { } }; -const InstallArtifactStep = struct { +pub const InstallArtifactStep = struct { step: Step, builder: *Builder, artifact: *LibExeObjStep, @@ -2239,7 +2239,7 @@ const InstallArtifactStep = struct { const self = builder.allocator.create(Self) catch unreachable; self.* = Self{ .builder = builder, - .step = Step.init(builder.fmt("install {}", .{artifact.step.name}), builder.allocator, make), + .step = Step.init(.InstallArtifact, builder.fmt("install {}", .{artifact.step.name}), builder.allocator, make), .artifact = artifact, .dest_dir = switch (artifact.kind) { .Obj => unreachable, @@ -2313,7 +2313,7 @@ pub const InstallFileStep = struct { builder.pushInstalledFile(dir, dest_rel_path); return InstallFileStep{ .builder = builder, - .step = Step.init(builder.fmt("install {}", .{src_path}), builder.allocator, make), + .step = Step.init(.InstallFile, builder.fmt("install {}", .{src_path}), builder.allocator, make), .src_path = src_path, .dir = dir, .dest_rel_path = dest_rel_path, @@ -2347,7 +2347,7 @@ pub const InstallDirStep = struct { builder.pushInstalledFile(options.install_dir, options.install_subdir); return InstallDirStep{ .builder = builder, - .step = Step.init(builder.fmt("install {}/", .{options.source_dir}), builder.allocator, make), + .step = Step.init(.InstallDir, builder.fmt("install {}/", .{options.source_dir}), builder.allocator, make), .options = options, }; } @@ -2383,7 +2383,7 @@ pub const LogStep = struct { pub fn init(builder: *Builder, data: []const u8) LogStep { return LogStep{ .builder = builder, - .step = Step.init(builder.fmt("log {}", .{data}), builder.allocator, make), + .step = Step.init(.Log, builder.fmt("log {}", .{data}), builder.allocator, make), .data = data, }; } @@ -2402,7 +2402,7 @@ pub const RemoveDirStep = struct { pub fn init(builder: *Builder, dir_path: []const u8) RemoveDirStep { return RemoveDirStep{ .builder = builder, - .step = Step.init(builder.fmt("RemoveDir {}", .{dir_path}), builder.allocator, make), + .step = Step.init(.RemoveDir, builder.fmt("RemoveDir {}", .{dir_path}), builder.allocator, make), .dir_path = dir_path, }; } @@ -2418,15 +2418,34 @@ pub const RemoveDirStep = struct { } }; +const ThisModule = @This(); pub const Step = struct { + id: Id, name: []const u8, makeFn: fn (self: *Step) anyerror!void, dependencies: ArrayList(*Step), loop_flag: bool, done_flag: bool, - pub fn init(name: []const u8, allocator: *Allocator, makeFn: fn (*Step) anyerror!void) Step { + pub const Id = enum { + TopLevel, + LibExeObj, + InstallArtifact, + InstallFile, + InstallDir, + Log, + RemoveDir, + Fmt, + TranslateC, + WriteFile, + Run, + CheckFile, + InstallRaw, + }; + + pub fn init(id: Id, name: []const u8, allocator: *Allocator, makeFn: fn (*Step) anyerror!void) Step { return Step{ + .id = id, .name = name, .makeFn = makeFn, .dependencies = ArrayList(*Step).init(allocator), @@ -2434,8 +2453,8 @@ pub const Step = struct { .done_flag = false, }; } - pub fn initNoOp(name: []const u8, allocator: *Allocator) Step { - return init(name, allocator, makeNoOp); + pub fn initNoOp(id: Id, name: []const u8, allocator: *Allocator) Step { + return init(id, name, allocator, makeNoOp); } pub fn make(self: *Step) !void { @@ -2450,6 +2469,23 @@ pub const Step = struct { } fn makeNoOp(self: *Step) anyerror!void {} + + pub fn cast(step: *Step, comptime T: type) ?*T { + if (step.id == comptime typeToId(T)) { + return @fieldParentPtr(T, "step", step); + } + return null; + } + + fn typeToId(comptime T: type) Id { + inline for (@typeInfo(Id).Enum.fields) |f| { + if (std.mem.eql(u8, f.name, "TopLevel")) continue; + if (T == @field(ThisModule, f.name ++ "Step")) { + return @field(Id, f.name); + } + } + unreachable; + } }; fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void { diff --git a/lib/std/build/check_file.zig b/lib/std/build/check_file.zig index 782cdb5600..2d8e58f92c 100644 --- a/lib/std/build/check_file.zig +++ b/lib/std/build/check_file.zig @@ -21,7 +21,7 @@ pub const CheckFileStep = struct { const self = builder.allocator.create(CheckFileStep) catch unreachable; self.* = CheckFileStep{ .builder = builder, - .step = Step.init("CheckFile", builder.allocator, make), + .step = Step.init(.CheckFile, "CheckFile", builder.allocator, make), .source = source, .expected_matches = expected_matches, }; diff --git a/lib/std/build/emit_raw.zig b/lib/std/build/emit_raw.zig index 275a444bef..30174a9af4 100644 --- a/lib/std/build/emit_raw.zig +++ b/lib/std/build/emit_raw.zig @@ -182,7 +182,7 @@ pub const InstallRawStep = struct { pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *Self { const self = builder.allocator.create(Self) catch unreachable; self.* = Self{ - .step = Step.init(builder.fmt("install raw binary {}", .{artifact.step.name}), builder.allocator, make), + .step = Step.init(.InstallRaw, builder.fmt("install raw binary {}", .{artifact.step.name}), builder.allocator, make), .builder = builder, .artifact = artifact, .dest_dir = switch (artifact.kind) { diff --git a/lib/std/build/fmt.zig b/lib/std/build/fmt.zig index f3b8e08180..8fb12df824 100644 --- a/lib/std/build/fmt.zig +++ b/lib/std/build/fmt.zig @@ -14,7 +14,7 @@ pub const FmtStep = struct { const self = builder.allocator.create(FmtStep) catch unreachable; const name = "zig fmt"; self.* = FmtStep{ - .step = Step.init(name, builder.allocator, make), + .step = Step.init(.Fmt, name, builder.allocator, make), .builder = builder, .argv = builder.allocator.alloc([]u8, paths.len + 2) catch unreachable, }; diff --git a/lib/std/build/run.zig b/lib/std/build/run.zig index 3a2dc2a2a3..5af2c2d075 100644 --- a/lib/std/build/run.zig +++ b/lib/std/build/run.zig @@ -49,7 +49,7 @@ pub const RunStep = struct { const self = builder.allocator.create(RunStep) catch unreachable; self.* = RunStep{ .builder = builder, - .step = Step.init(name, builder.allocator, make), + .step = Step.init(.Run, name, builder.allocator, make), .argv = ArrayList(Arg).init(builder.allocator), .cwd = null, .env_map = null, diff --git a/lib/std/build/translate_c.zig b/lib/std/build/translate_c.zig index 73e395d951..2f3ad18144 100644 --- a/lib/std/build/translate_c.zig +++ b/lib/std/build/translate_c.zig @@ -20,7 +20,7 @@ pub const TranslateCStep = struct { pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep { const self = builder.allocator.create(TranslateCStep) catch unreachable; self.* = TranslateCStep{ - .step = Step.init("translate-c", builder.allocator, make), + .step = Step.init(.TranslateC, "translate-c", builder.allocator, make), .builder = builder, .source = source, .output_dir = null, diff --git a/lib/std/build/write_file.zig b/lib/std/build/write_file.zig index 7389923dc3..ad0e13bed2 100644 --- a/lib/std/build/write_file.zig +++ b/lib/std/build/write_file.zig @@ -20,7 +20,7 @@ pub const WriteFileStep = struct { pub fn init(builder: *Builder) WriteFileStep { return WriteFileStep{ .builder = builder, - .step = Step.init("writefile", builder.allocator, make), + .step = Step.init(.WriteFile, "writefile", builder.allocator, make), .files = ArrayList(File).init(builder.allocator), .output_dir = undefined, }; From 65d827183bd521cd402826382541d8b16d7718bb Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Mon, 25 May 2020 11:36:12 +0300 Subject: [PATCH 2/2] Added custom build step id, made tests.zig steps use it --- lib/std/build.zig | 5 ++++- test/tests.zig | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index 66e8d8a679..d98ef71a59 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -2441,6 +2441,7 @@ pub const Step = struct { Run, CheckFile, InstallRaw, + Custom, }; pub fn init(id: Id, name: []const u8, allocator: *Allocator, makeFn: fn (*Step) anyerror!void) Step { @@ -2479,7 +2480,9 @@ pub const Step = struct { fn typeToId(comptime T: type) Id { inline for (@typeInfo(Id).Enum.fields) |f| { - if (std.mem.eql(u8, f.name, "TopLevel")) continue; + if (std.mem.eql(u8, f.name, "TopLevel") or + std.mem.eql(u8, f.name, "Custom")) continue; + if (T == @field(ThisModule, f.name ++ "Step")) { return @field(Id, f.name); } diff --git a/test/tests.zig b/test/tests.zig index 74aa9b105a..577f48b03a 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -609,7 +609,7 @@ pub const StackTracesContext = struct { const allocator = context.b.allocator; const ptr = allocator.create(RunAndCompareStep) catch unreachable; ptr.* = RunAndCompareStep{ - .step = build.Step.init("StackTraceCompareOutputStep", allocator, make), + .step = build.Step.init(.Custom, "StackTraceCompareOutputStep", allocator, make), .context = context, .exe = exe, .name = name, @@ -808,7 +808,7 @@ pub const CompileErrorContext = struct { const allocator = context.b.allocator; const ptr = allocator.create(CompileCmpOutputStep) catch unreachable; ptr.* = CompileCmpOutputStep{ - .step = build.Step.init("CompileCmpOutput", allocator, make), + .step = build.Step.init(.Custom, "CompileCmpOutput", allocator, make), .context = context, .name = name, .test_index = context.test_index, @@ -1156,7 +1156,7 @@ pub const GenHContext = struct { const allocator = context.b.allocator; const ptr = allocator.create(GenHCmpOutputStep) catch unreachable; ptr.* = GenHCmpOutputStep{ - .step = build.Step.init("ParseCCmpOutput", allocator, make), + .step = build.Step.init(.Custom, "ParseCCmpOutput", allocator, make), .context = context, .obj = obj, .name = name,