authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-05-25 11:20:31+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2020-05-25 11:20:31+03:00
logd268e0cf2e3cd152017a165ca80085a5823c45a2
treed90b74798f0417c82131894f240ed1a21504e68c
parent3052fd84c812280334245513d1edc631ae5e4ae2

Added and id and a cast function to build steps


7 files changed, 55 insertions(+), 19 deletions(-)

lib/std/build.zig+49-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,34 @@ pub const RemoveDirStep = struct {...@@ -2418,15 +2418,34 @@ 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 };
2445
2446 pub fn init(id: Id, name: []const u8, allocator: *Allocator, makeFn: fn (*Step) anyerror!void) Step {
2429 return Step{2447 return Step{
2448 .id = id,
2430 .name = name,2449 .name = name,
2431 .makeFn = makeFn,2450 .makeFn = makeFn,
2432 .dependencies = ArrayList(*Step).init(allocator),2451 .dependencies = ArrayList(*Step).init(allocator),
...@@ -2434,8 +2453,8 @@ pub const Step = struct {...@@ -2434,8 +2453,8 @@ pub const Step = struct {
2434 .done_flag = false,2453 .done_flag = false,
2435 };2454 };
2436 }2455 }
2437 pub fn initNoOp(name: []const u8, allocator: *Allocator) Step {2456 pub fn initNoOp(id: Id, name: []const u8, allocator: *Allocator) Step {
2438 return init(name, allocator, makeNoOp);2457 return init(id, name, allocator, makeNoOp);
2439 }2458 }
24402459
2441 pub fn make(self: *Step) !void {2460 pub fn make(self: *Step) !void {
...@@ -2450,6 +2469,23 @@ pub const Step = struct {...@@ -2450,6 +2469,23 @@ pub const Step = struct {
2450 }2469 }
24512470
2452 fn makeNoOp(self: *Step) anyerror!void {}2471 fn makeNoOp(self: *Step) anyerror!void {}
2472
2473 pub fn cast(step: *Step, comptime T: type) ?*T {
2474 if (step.id == comptime typeToId(T)) {
2475 return @fieldParentPtr(T, "step", step);
2476 }
2477 return null;
2478 }
2479
2480 fn typeToId(comptime T: type) Id {
2481 inline for (@typeInfo(Id).Enum.fields) |f| {
2482 if (std.mem.eql(u8, f.name, "TopLevel")) continue;
2483 if (T == @field(ThisModule, f.name ++ "Step")) {
2484 return @field(Id, f.name);
2485 }
2486 }
2487 unreachable;
2488 }
2453};2489};
24542490
2455fn doAtomicSymLinks(allocator: *Allocator, output_path: []const u8, filename_major_only: []const u8, filename_name_only: []const u8) !void {2491fn 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 };