authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 11:24:15-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-25 11:24:15-04:00
log0fd77c2de3b0355a6e66bf7919cd25612c73654f
tree1d92c4a61ab5733cebf4bf9a3325be14e6a0b0fc
parent3052fd84c812280334245513d1edc631ae5e4ae2
parent65d827183bd521cd402826382541d8b16d7718bb
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5431 from alexnask/build_step_ids

Added and id and a cast function to build steps

8 files changed, 61 insertions(+), 22 deletions(-)

lib/std/build.zig+52-13
......@@ -154,11 +154,11 @@ pub const Builder = struct {
154154 .dest_dir = env_map.get("DESTDIR"),
155155 .installed_files = ArrayList(InstalledFile).init(allocator),
156156 .install_tls = TopLevelStep{
157 .step = Step.initNoOp("install", allocator),
157 .step = Step.initNoOp(.TopLevel, "install", allocator),
158158 .description = "Copy build artifacts to prefix path",
159159 },
160160 .uninstall_tls = TopLevelStep{
161 .step = Step.init("uninstall", allocator, makeUninstall),
161 .step = Step.init(.TopLevel, "uninstall", allocator, makeUninstall),
162162 .description = "Remove build artifacts from prefix path",
163163 },
164164 .release_mode = null,
......@@ -500,7 +500,7 @@ pub const Builder = struct {
500500 pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step {
501501 const step_info = self.allocator.create(TopLevelStep) catch unreachable;
502502 step_info.* = TopLevelStep{
503 .step = Step.initNoOp(name, self.allocator),
503 .step = Step.initNoOp(.TopLevel, name, self.allocator),
504504 .description = description,
505505 };
506506 self.top_level_steps.append(step_info) catch unreachable;
......@@ -1286,7 +1286,7 @@ pub const LibExeObjStep = struct {
12861286 .root_src = root_src,
12871287 .name = name,
12881288 .frameworks = BufSet.init(builder.allocator),
1289 .step = Step.init(name, builder.allocator, make),
1289 .step = Step.init(.LibExeObj, name, builder.allocator, make),
12901290 .version = ver,
12911291 .out_filename = undefined,
12921292 .out_h_filename = builder.fmt("{}.h", .{name}),
......@@ -2223,7 +2223,7 @@ pub const LibExeObjStep = struct {
22232223 }
22242224};
22252225
2226const InstallArtifactStep = struct {
2226pub const InstallArtifactStep = struct {
22272227 step: Step,
22282228 builder: *Builder,
22292229 artifact: *LibExeObjStep,
......@@ -2239,7 +2239,7 @@ const InstallArtifactStep = struct {
22392239 const self = builder.allocator.create(Self) catch unreachable;
22402240 self.* = Self{
22412241 .builder = builder,
2242 .step = Step.init(builder.fmt("install {}", .{artifact.step.name}), builder.allocator, make),
2242 .step = Step.init(.InstallArtifact, builder.fmt("install {}", .{artifact.step.name}), builder.allocator, make),
22432243 .artifact = artifact,
22442244 .dest_dir = switch (artifact.kind) {
22452245 .Obj => unreachable,
......@@ -2313,7 +2313,7 @@ pub const InstallFileStep = struct {
23132313 builder.pushInstalledFile(dir, dest_rel_path);
23142314 return InstallFileStep{
23152315 .builder = builder,
2316 .step = Step.init(builder.fmt("install {}", .{src_path}), builder.allocator, make),
2316 .step = Step.init(.InstallFile, builder.fmt("install {}", .{src_path}), builder.allocator, make),
23172317 .src_path = src_path,
23182318 .dir = dir,
23192319 .dest_rel_path = dest_rel_path,
......@@ -2347,7 +2347,7 @@ pub const InstallDirStep = struct {
23472347 builder.pushInstalledFile(options.install_dir, options.install_subdir);
23482348 return InstallDirStep{
23492349 .builder = builder,
2350 .step = Step.init(builder.fmt("install {}/", .{options.source_dir}), builder.allocator, make),
2350 .step = Step.init(.InstallDir, builder.fmt("install {}/", .{options.source_dir}), builder.allocator, make),
23512351 .options = options,
23522352 };
23532353 }
......@@ -2383,7 +2383,7 @@ pub const LogStep = struct {
23832383 pub fn init(builder: *Builder, data: []const u8) LogStep {
23842384 return LogStep{
23852385 .builder = builder,
2386 .step = Step.init(builder.fmt("log {}", .{data}), builder.allocator, make),
2386 .step = Step.init(.Log, builder.fmt("log {}", .{data}), builder.allocator, make),
23872387 .data = data,
23882388 };
23892389 }
......@@ -2402,7 +2402,7 @@ pub const RemoveDirStep = struct {
24022402 pub fn init(builder: *Builder, dir_path: []const u8) RemoveDirStep {
24032403 return RemoveDirStep{
24042404 .builder = builder,
2405 .step = Step.init(builder.fmt("RemoveDir {}", .{dir_path}), builder.allocator, make),
2405 .step = Step.init(.RemoveDir, builder.fmt("RemoveDir {}", .{dir_path}), builder.allocator, make),
24062406 .dir_path = dir_path,
24072407 };
24082408 }
......@@ -2418,15 +2418,35 @@ pub const RemoveDirStep = struct {
24182418 }
24192419};
24202420
2421const ThisModule = @This();
24212422pub const Step = struct {
2423 id: Id,
24222424 name: []const u8,
24232425 makeFn: fn (self: *Step) anyerror!void,
24242426 dependencies: ArrayList(*Step),
24252427 loop_flag: bool,
24262428 done_flag: bool,
24272429
2428 pub fn init(name: []const u8, allocator: *Allocator, makeFn: fn (*Step) anyerror!void) Step {
2430 pub const Id = enum {
2431 TopLevel,
2432 LibExeObj,
2433 InstallArtifact,
2434 InstallFile,
2435 InstallDir,
2436 Log,
2437 RemoveDir,
2438 Fmt,
2439 TranslateC,
2440 WriteFile,
2441 Run,
2442 CheckFile,
2443 InstallRaw,
2444 Custom,
2445 };
2446
2447 pub fn init(id: Id, name: []const u8, allocator: *Allocator, makeFn: fn (*Step) anyerror!void) Step {
24292448 return Step{
2449 .id = id,
24302450 .name = name,
24312451 .makeFn = makeFn,
24322452 .dependencies = ArrayList(*Step).init(allocator),
......@@ -2434,8 +2454,8 @@ pub const Step = struct {
24342454 .done_flag = false,
24352455 };
24362456 }
2437 pub fn initNoOp(name: []const u8, allocator: *Allocator) Step {
2438 return init(name, allocator, makeNoOp);
2457 pub fn initNoOp(id: Id, name: []const u8, allocator: *Allocator) Step {
2458 return init(id, name, allocator, makeNoOp);
24392459 }
24402460
24412461 pub fn make(self: *Step) !void {
......@@ -2450,6 +2470,25 @@ pub const Step = struct {
24502470 }
24512471
24522472 fn makeNoOp(self: *Step) anyerror!void {}
2473
2474 pub fn cast(step: *Step, comptime T: type) ?*T {
2475 if (step.id == comptime typeToId(T)) {
2476 return @fieldParentPtr(T, "step", step);
2477 }
2478 return null;
2479 }
2480
2481 fn typeToId(comptime T: type) Id {
2482 inline for (@typeInfo(Id).Enum.fields) |f| {
2483 if (std.mem.eql(u8, f.name, "TopLevel") or
2484 std.mem.eql(u8, f.name, "Custom")) continue;
2485
2486 if (T == @field(ThisModule, f.name ++ "Step")) {
2487 return @field(Id, f.name);
2488 }
2489 }
2490 unreachable;
2491 }
24532492};
24542493
24552494fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void {
lib/std/build/check_file.zig+1-1
......@@ -21,7 +21,7 @@ pub const CheckFileStep = struct {
2121 const self = builder.allocator.create(CheckFileStep) catch unreachable;
2222 self.* = CheckFileStep{
2323 .builder = builder,
24 .step = Step.init("CheckFile", builder.allocator, make),
24 .step = Step.init(.CheckFile, "CheckFile", builder.allocator, make),
2525 .source = source,
2626 .expected_matches = expected_matches,
2727 };
lib/std/build/emit_raw.zig+1-1
......@@ -182,7 +182,7 @@ pub const InstallRawStep = struct {
182182 pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *Self {
183183 const self = builder.allocator.create(Self) catch unreachable;
184184 self.* = Self{
185 .step = Step.init(builder.fmt("install raw binary {}", .{artifact.step.name}), builder.allocator, make),
185 .step = Step.init(.InstallRaw, builder.fmt("install raw binary {}", .{artifact.step.name}), builder.allocator, make),
186186 .builder = builder,
187187 .artifact = artifact,
188188 .dest_dir = switch (artifact.kind) {
lib/std/build/fmt.zig+1-1
......@@ -14,7 +14,7 @@ pub const FmtStep = struct {
1414 const self = builder.allocator.create(FmtStep) catch unreachable;
1515 const name = "zig fmt";
1616 self.* = FmtStep{
17 .step = Step.init(name, builder.allocator, make),
17 .step = Step.init(.Fmt, name, builder.allocator, make),
1818 .builder = builder,
1919 .argv = builder.allocator.alloc([]u8, paths.len + 2) catch unreachable,
2020 };
lib/std/build/run.zig+1-1
......@@ -49,7 +49,7 @@ pub const RunStep = struct {
4949 const self = builder.allocator.create(RunStep) catch unreachable;
5050 self.* = RunStep{
5151 .builder = builder,
52 .step = Step.init(name, builder.allocator, make),
52 .step = Step.init(.Run, name, builder.allocator, make),
5353 .argv = ArrayList(Arg).init(builder.allocator),
5454 .cwd = null,
5555 .env_map = null,
lib/std/build/translate_c.zig+1-1
......@@ -20,7 +20,7 @@ pub const TranslateCStep = struct {
2020 pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
2121 const self = builder.allocator.create(TranslateCStep) catch unreachable;
2222 self.* = TranslateCStep{
23 .step = Step.init("translate-c", builder.allocator, make),
23 .step = Step.init(.TranslateC, "translate-c", builder.allocator, make),
2424 .builder = builder,
2525 .source = source,
2626 .output_dir = null,
lib/std/build/write_file.zig+1-1
......@@ -20,7 +20,7 @@ pub const WriteFileStep = struct {
2020 pub fn init(builder: *Builder) WriteFileStep {
2121 return WriteFileStep{
2222 .builder = builder,
23 .step = Step.init("writefile", builder.allocator, make),
23 .step = Step.init(.WriteFile, "writefile", builder.allocator, make),
2424 .files = ArrayList(File).init(builder.allocator),
2525 .output_dir = undefined,
2626 };
test/tests.zig+3-3
......@@ -609,7 +609,7 @@ pub const StackTracesContext = struct {
609609 const allocator = context.b.allocator;
610610 const ptr = allocator.create(RunAndCompareStep) catch unreachable;
611611 ptr.* = RunAndCompareStep{
612 .step = build.Step.init("StackTraceCompareOutputStep", allocator, make),
612 .step = build.Step.init(.Custom, "StackTraceCompareOutputStep", allocator, make),
613613 .context = context,
614614 .exe = exe,
615615 .name = name,
......@@ -808,7 +808,7 @@ pub const CompileErrorContext = struct {
808808 const allocator = context.b.allocator;
809809 const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
810810 ptr.* = CompileCmpOutputStep{
811 .step = build.Step.init("CompileCmpOutput", allocator, make),
811 .step = build.Step.init(.Custom, "CompileCmpOutput", allocator, make),
812812 .context = context,
813813 .name = name,
814814 .test_index = context.test_index,
......@@ -1156,7 +1156,7 @@ pub const GenHContext = struct {
11561156 const allocator = context.b.allocator;
11571157 const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
11581158 ptr.* = GenHCmpOutputStep{
1159 .step = build.Step.init("ParseCCmpOutput", allocator, make),
1159 .step = build.Step.init(.Custom, "ParseCCmpOutput", allocator, make),
11601160 .context = context,
11611161 .obj = obj,
11621162 .name = name,