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 {...@@ -154,11 +154,11 @@ pub const Builder = struct {
154 .dest_dir = env_map.get("DESTDIR"),154 .dest_dir = env_map.get("DESTDIR"),
155 .installed_files = ArrayList(InstalledFile).init(allocator),155 .installed_files = ArrayList(InstalledFile).init(allocator),
156 .install_tls = TopLevelStep{156 .install_tls = TopLevelStep{
157 .step = Step.initNoOp("install", allocator),157 .step = Step.initNoOp(.TopLevel, "install", allocator),
158 .description = "Copy build artifacts to prefix path",158 .description = "Copy build artifacts to prefix path",
159 },159 },
160 .uninstall_tls = TopLevelStep{160 .uninstall_tls = TopLevelStep{
161 .step = Step.init("uninstall", allocator, makeUninstall),161 .step = Step.init(.TopLevel, "uninstall", allocator, makeUninstall),
162 .description = "Remove build artifacts from prefix path",162 .description = "Remove build artifacts from prefix path",
163 },163 },
164 .release_mode = null,164 .release_mode = null,
...@@ -500,7 +500,7 @@ pub const Builder = struct {...@@ -500,7 +500,7 @@ pub const Builder = struct {
500 pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step {500 pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step {
501 const step_info = self.allocator.create(TopLevelStep) catch unreachable;501 const step_info = self.allocator.create(TopLevelStep) catch unreachable;
502 step_info.* = TopLevelStep{502 step_info.* = TopLevelStep{
503 .step = Step.initNoOp(name, self.allocator),503 .step = Step.initNoOp(.TopLevel, name, self.allocator),
504 .description = description,504 .description = description,
505 };505 };
506 self.top_level_steps.append(step_info) catch unreachable;506 self.top_level_steps.append(step_info) catch unreachable;
...@@ -1286,7 +1286,7 @@ pub const LibExeObjStep = struct {...@@ -1286,7 +1286,7 @@ pub const LibExeObjStep = struct {
1286 .root_src = root_src,1286 .root_src = root_src,
1287 .name = name,1287 .name = name,
1288 .frameworks = BufSet.init(builder.allocator),1288 .frameworks = BufSet.init(builder.allocator),
1289 .step = Step.init(name, builder.allocator, make),1289 .step = Step.init(.LibExeObj, name, builder.allocator, make),
1290 .version = ver,1290 .version = ver,
1291 .out_filename = undefined,1291 .out_filename = undefined,
1292 .out_h_filename = builder.fmt("{}.h", .{name}),1292 .out_h_filename = builder.fmt("{}.h", .{name}),
...@@ -2223,7 +2223,7 @@ pub const LibExeObjStep = struct {...@@ -2223,7 +2223,7 @@ pub const LibExeObjStep = struct {
2223 }2223 }
2224};2224};
22252225
2226const InstallArtifactStep = struct {2226pub const InstallArtifactStep = struct {
2227 step: Step,2227 step: Step,
2228 builder: *Builder,2228 builder: *Builder,
2229 artifact: *LibExeObjStep,2229 artifact: *LibExeObjStep,
...@@ -2239,7 +2239,7 @@ const InstallArtifactStep = struct {...@@ -2239,7 +2239,7 @@ const InstallArtifactStep = struct {
2239 const self = builder.allocator.create(Self) catch unreachable;2239 const self = builder.allocator.create(Self) catch unreachable;
2240 self.* = Self{2240 self.* = Self{
2241 .builder = builder,2241 .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),
2243 .artifact = artifact,2243 .artifact = artifact,
2244 .dest_dir = switch (artifact.kind) {2244 .dest_dir = switch (artifact.kind) {
2245 .Obj => unreachable,2245 .Obj => unreachable,
...@@ -2313,7 +2313,7 @@ pub const InstallFileStep = struct {...@@ -2313,7 +2313,7 @@ pub const InstallFileStep = struct {
2313 builder.pushInstalledFile(dir, dest_rel_path);2313 builder.pushInstalledFile(dir, dest_rel_path);
2314 return InstallFileStep{2314 return InstallFileStep{
2315 .builder = builder,2315 .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),
2317 .src_path = src_path,2317 .src_path = src_path,
2318 .dir = dir,2318 .dir = dir,
2319 .dest_rel_path = dest_rel_path,2319 .dest_rel_path = dest_rel_path,
...@@ -2347,7 +2347,7 @@ pub const InstallDirStep = struct {...@@ -2347,7 +2347,7 @@ pub const InstallDirStep = struct {
2347 builder.pushInstalledFile(options.install_dir, options.install_subdir);2347 builder.pushInstalledFile(options.install_dir, options.install_subdir);
2348 return InstallDirStep{2348 return InstallDirStep{
2349 .builder = builder,2349 .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),
2351 .options = options,2351 .options = options,
2352 };2352 };
2353 }2353 }
...@@ -2383,7 +2383,7 @@ pub const LogStep = struct {...@@ -2383,7 +2383,7 @@ pub const LogStep = struct {
2383 pub fn init(builder: *Builder, data: []const u8) LogStep {2383 pub fn init(builder: *Builder, data: []const u8) LogStep {
2384 return LogStep{2384 return LogStep{
2385 .builder = builder,2385 .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),
2387 .data = data,2387 .data = data,
2388 };2388 };
2389 }2389 }
...@@ -2402,7 +2402,7 @@ pub const RemoveDirStep = struct {...@@ -2402,7 +2402,7 @@ pub const RemoveDirStep = struct {
2402 pub fn init(builder: *Builder, dir_path: []const u8) RemoveDirStep {2402 pub fn init(builder: *Builder, dir_path: []const u8) RemoveDirStep {
2403 return RemoveDirStep{2403 return RemoveDirStep{
2404 .builder = builder,2404 .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),
2406 .dir_path = dir_path,2406 .dir_path = dir_path,
2407 };2407 };
2408 }2408 }
...@@ -2418,15 +2418,35 @@ pub const RemoveDirStep = struct {...@@ -2418,15 +2418,35 @@ pub const RemoveDirStep = struct {
2418 }2418 }
2419};2419};
24202420
2421const ThisModule = @This();
2421pub const Step = struct {2422pub const Step = struct {
2423 id: Id,
2422 name: []const u8,2424 name: []const u8,
2423 makeFn: fn (self: *Step) anyerror!void,2425 makeFn: fn (self: *Step) anyerror!void,
2424 dependencies: ArrayList(*Step),2426 dependencies: ArrayList(*Step),
2425 loop_flag: bool,2427 loop_flag: bool,
2426 done_flag: bool,2428 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 {
2429 return Step{2448 return Step{
2449 .id = id,
2430 .name = name,2450 .name = name,
2431 .makeFn = makeFn,2451 .makeFn = makeFn,
2432 .dependencies = ArrayList(*Step).init(allocator),2452 .dependencies = ArrayList(*Step).init(allocator),
...@@ -2434,8 +2454,8 @@ pub const Step = struct {...@@ -2434,8 +2454,8 @@ pub const Step = struct {
2434 .done_flag = false,2454 .done_flag = false,
2435 };2455 };
2436 }2456 }
2437 pub fn initNoOp(name: []const u8, allocator: *Allocator) Step {2457 pub fn initNoOp(id: Id, name: []const u8, allocator: *Allocator) Step {
2438 return init(name, allocator, makeNoOp);2458 return init(id, name, allocator, makeNoOp);
2439 }2459 }
24402460
2441 pub fn make(self: *Step) !void {2461 pub fn make(self: *Step) !void {
...@@ -2450,6 +2470,25 @@ pub const Step = struct {...@@ -2450,6 +2470,25 @@ pub const Step = struct {
2450 }2470 }
24512471
2452 fn makeNoOp(self: *Step) anyerror!void {}2472 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 }
2453};2492};
24542493
2455fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void {2494fn 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 {...@@ -21,7 +21,7 @@ pub const CheckFileStep = struct {
21 const self = builder.allocator.create(CheckFileStep) catch unreachable;21 const self = builder.allocator.create(CheckFileStep) catch unreachable;
22 self.* = CheckFileStep{22 self.* = CheckFileStep{
23 .builder = builder,23 .builder = builder,
24 .step = Step.init("CheckFile", builder.allocator, make),24 .step = Step.init(.CheckFile, "CheckFile", builder.allocator, make),
25 .source = source,25 .source = source,
26 .expected_matches = expected_matches,26 .expected_matches = expected_matches,
27 };27 };
lib/std/build/emit_raw.zig+1-1
...@@ -182,7 +182,7 @@ pub const InstallRawStep = struct {...@@ -182,7 +182,7 @@ pub const InstallRawStep = struct {
182 pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *Self {182 pub fn create(builder: *Builder, artifact: *LibExeObjStep, dest_filename: []const u8) *Self {
183 const self = builder.allocator.create(Self) catch unreachable;183 const self = builder.allocator.create(Self) catch unreachable;
184 self.* = Self{184 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),
186 .builder = builder,186 .builder = builder,
187 .artifact = artifact,187 .artifact = artifact,
188 .dest_dir = switch (artifact.kind) {188 .dest_dir = switch (artifact.kind) {
lib/std/build/fmt.zig+1-1
...@@ -14,7 +14,7 @@ pub const FmtStep = struct {...@@ -14,7 +14,7 @@ pub const FmtStep = struct {
14 const self = builder.allocator.create(FmtStep) catch unreachable;14 const self = builder.allocator.create(FmtStep) catch unreachable;
15 const name = "zig fmt";15 const name = "zig fmt";
16 self.* = FmtStep{16 self.* = FmtStep{
17 .step = Step.init(name, builder.allocator, make),17 .step = Step.init(.Fmt, name, builder.allocator, make),
18 .builder = builder,18 .builder = builder,
19 .argv = builder.allocator.alloc([]u8, paths.len + 2) catch unreachable,19 .argv = builder.allocator.alloc([]u8, paths.len + 2) catch unreachable,
20 };20 };
lib/std/build/run.zig+1-1
...@@ -49,7 +49,7 @@ pub const RunStep = struct {...@@ -49,7 +49,7 @@ pub const RunStep = struct {
49 const self = builder.allocator.create(RunStep) catch unreachable;49 const self = builder.allocator.create(RunStep) catch unreachable;
50 self.* = RunStep{50 self.* = RunStep{
51 .builder = builder,51 .builder = builder,
52 .step = Step.init(name, builder.allocator, make),52 .step = Step.init(.Run, name, builder.allocator, make),
53 .argv = ArrayList(Arg).init(builder.allocator),53 .argv = ArrayList(Arg).init(builder.allocator),
54 .cwd = null,54 .cwd = null,
55 .env_map = null,55 .env_map = null,
lib/std/build/translate_c.zig+1-1
...@@ -20,7 +20,7 @@ pub const TranslateCStep = struct {...@@ -20,7 +20,7 @@ pub const TranslateCStep = struct {
20 pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {20 pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
21 const self = builder.allocator.create(TranslateCStep) catch unreachable;21 const self = builder.allocator.create(TranslateCStep) catch unreachable;
22 self.* = TranslateCStep{22 self.* = TranslateCStep{
23 .step = Step.init("translate-c", builder.allocator, make),23 .step = Step.init(.TranslateC, "translate-c", builder.allocator, make),
24 .builder = builder,24 .builder = builder,
25 .source = source,25 .source = source,
26 .output_dir = null,26 .output_dir = null,
lib/std/build/write_file.zig+1-1
...@@ -20,7 +20,7 @@ pub const WriteFileStep = struct {...@@ -20,7 +20,7 @@ pub const WriteFileStep = struct {
20 pub fn init(builder: *Builder) WriteFileStep {20 pub fn init(builder: *Builder) WriteFileStep {
21 return WriteFileStep{21 return WriteFileStep{
22 .builder = builder,22 .builder = builder,
23 .step = Step.init("writefile", builder.allocator, make),23 .step = Step.init(.WriteFile, "writefile", builder.allocator, make),
24 .files = ArrayList(File).init(builder.allocator),24 .files = ArrayList(File).init(builder.allocator),
25 .output_dir = undefined,25 .output_dir = undefined,
26 };26 };
test/tests.zig+3-3
...@@ -609,7 +609,7 @@ pub const StackTracesContext = struct {...@@ -609,7 +609,7 @@ pub const StackTracesContext = struct {
609 const allocator = context.b.allocator;609 const allocator = context.b.allocator;
610 const ptr = allocator.create(RunAndCompareStep) catch unreachable;610 const ptr = allocator.create(RunAndCompareStep) catch unreachable;
611 ptr.* = RunAndCompareStep{611 ptr.* = RunAndCompareStep{
612 .step = build.Step.init("StackTraceCompareOutputStep", allocator, make),612 .step = build.Step.init(.Custom, "StackTraceCompareOutputStep", allocator, make),
613 .context = context,613 .context = context,
614 .exe = exe,614 .exe = exe,
615 .name = name,615 .name = name,
...@@ -808,7 +808,7 @@ pub const CompileErrorContext = struct {...@@ -808,7 +808,7 @@ pub const CompileErrorContext = struct {
808 const allocator = context.b.allocator;808 const allocator = context.b.allocator;
809 const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;809 const ptr = allocator.create(CompileCmpOutputStep) catch unreachable;
810 ptr.* = CompileCmpOutputStep{810 ptr.* = CompileCmpOutputStep{
811 .step = build.Step.init("CompileCmpOutput", allocator, make),811 .step = build.Step.init(.Custom, "CompileCmpOutput", allocator, make),
812 .context = context,812 .context = context,
813 .name = name,813 .name = name,
814 .test_index = context.test_index,814 .test_index = context.test_index,
...@@ -1156,7 +1156,7 @@ pub const GenHContext = struct {...@@ -1156,7 +1156,7 @@ pub const GenHContext = struct {
1156 const allocator = context.b.allocator;1156 const allocator = context.b.allocator;
1157 const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;1157 const ptr = allocator.create(GenHCmpOutputStep) catch unreachable;
1158 ptr.* = GenHCmpOutputStep{1158 ptr.* = GenHCmpOutputStep{
1159 .step = build.Step.init("ParseCCmpOutput", allocator, make),1159 .step = build.Step.init(.Custom, "ParseCCmpOutput", allocator, make),
1160 .context = context,1160 .context = context,
1161 .obj = obj,1161 .obj = obj,
1162 .name = name,1162 .name = name,