authorgravatar for xq@random-projects.netFelix "xq" Queißner <xq@random-projects.net> 2021-02-05 15:45:18+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-06-11 10:33:10+03:00
log437f81aa9a963af2dce654cd0ac7ac0e19a4922a
tree4c7c72f672d84af8e07d92ef0e2da5503967b10f
parent138afd5cbfbe17829082efa3084f63de88aa1c90

Starts to replace special cases in std.build.FileSource.


2 files changed, 52 insertions(+), 21 deletions(-)

lib/std/build.zig+40-19
......@@ -359,7 +359,7 @@ pub const Builder = struct {
359359 pub fn dupePkg(self: *Builder, package: Pkg) Pkg {
360360 var the_copy = Pkg{
361361 .name = self.dupe(package.name),
362 .path = self.dupePath(package.path),
362 .path = package.path.dupe(self),
363363 };
364364
365365 if (package.dependencies) |dependencies| {
......@@ -1245,7 +1245,7 @@ pub const Target = std.zig.CrossTarget;
12451245
12461246pub const Pkg = struct {
12471247 name: []const u8,
1248 path: []const u8,
1248 path: FileSource,
12491249 dependencies: ?[]const Pkg = null,
12501250};
12511251
......@@ -1284,6 +1284,20 @@ fn isLibCppLibrary(name: []const u8) bool {
12841284 return false;
12851285}
12861286
1287/// A file that is generated by a build step.
1288/// This struct is an interface that is meant to be used with `@fieldParentPtr` to implement the actual path logic.
1289pub const GeneratedFile = struct {
1290 /// The step that generates the file
1291 step: *Step,
1292
1293 /// A function that returns the absolute path to the generated file.
1294 getPathFn: fn (self: *const GeneratedFile) []const u8,
1295
1296 pub fn getPath(self: *const GeneratedFile) []const u8 {
1297 return self.getPathFn(self);
1298 }
1299};
1300
12871301pub const FileSource = union(enum) {
12881302 /// Relative to build root
12891303 path: []const u8,
......@@ -1291,13 +1305,17 @@ pub const FileSource = union(enum) {
12911305 step: *WriteFileStep,
12921306 basename: []const u8,
12931307 },
1294 translate_c: *TranslateCStep,
1308 generated: *const GeneratedFile,
1309
1310 pub fn relative(path: []const u8) FileSource {
1311 return FileSource{ .path = path };
1312 }
12951313
12961314 pub fn addStepDependencies(self: FileSource, step: *Step) void {
12971315 switch (self) {
12981316 .path => {},
12991317 .write_file => |wf| step.dependOn(&wf.step.step),
1300 .translate_c => |tc| step.dependOn(&tc.step),
1318 .generated => |gen| step.dependOn(gen.step),
13011319 }
13021320 }
13031321
......@@ -1306,18 +1324,20 @@ pub const FileSource = union(enum) {
13061324 return switch (self) {
13071325 .path => |p| builder.pathFromRoot(p),
13081326 .write_file => |wf| wf.step.getOutputPath(wf.basename),
1309 .translate_c => |tc| tc.getOutputPath(),
1327 .generated => |gen| gen.getPath(),
13101328 };
13111329 }
13121330
13131331 pub fn dupe(self: FileSource, b: *Builder) FileSource {
13141332 return switch (self) {
1315 .path => |p| .{ .path = b.dupe(p) },
1316 .write_file => |wf| .{ .write_file = .{
1317 .step = wf.step,
1318 .basename = b.dupe(wf.basename),
1319 } },
1320 .translate_c => |tc| .{ .translate_c = tc },
1333 .path => |p| .{ .path = b.dupePath(p) },
1334 .write_file => |wf| .{
1335 .write_file = .{
1336 .step = wf.step,
1337 .basename = b.dupe(wf.basename),
1338 },
1339 },
1340 .generated => |gen| .{ .generated = gen },
13211341 };
13221342 }
13231343};
......@@ -2107,13 +2127,14 @@ pub const LibExeObjStep = struct {
21072127 }
21082128
21092129 pub fn addPackage(self: *LibExeObjStep, package: Pkg) void {
2130 package.path.addStepDependencies(&self.step);
21102131 self.packages.append(self.builder.dupePkg(package)) catch unreachable;
21112132 }
21122133
21132134 pub fn addPackagePath(self: *LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {
21142135 self.packages.append(Pkg{
21152136 .name = self.builder.dupe(name),
2116 .path = self.builder.dupe(pkg_index_path),
2137 .path = .{ .path = self.builder.dupe(pkg_index_path) },
21172138 }) catch unreachable;
21182139 }
21192140
......@@ -2190,7 +2211,7 @@ pub const LibExeObjStep = struct {
21902211
21912212 try zig_args.append("--pkg-begin");
21922213 try zig_args.append(pkg.name);
2193 try zig_args.append(builder.pathFromRoot(pkg.path));
2214 try zig_args.append(builder.pathFromRoot(pkg.path.getPath(self.builder)));
21942215
21952216 if (pkg.dependencies) |dependencies| {
21962217 for (dependencies) |sub_pkg| {
......@@ -3137,11 +3158,11 @@ test "Builder.dupePkg()" {
31373158
31383159 var pkg_dep = Pkg{
31393160 .name = "pkg_dep",
3140 .path = "/not/a/pkg_dep.zig",
3161 .path = FileSource.relative("/not/a/pkg_dep.zig"),
31413162 };
31423163 var pkg_top = Pkg{
31433164 .name = "pkg_top",
3144 .path = "/not/a/pkg_top.zig",
3165 .path = FileSource.relative("/not/a/pkg_top.zig"),
31453166 .dependencies = &[_]Pkg{pkg_dep},
31463167 };
31473168 const dupe = builder.dupePkg(pkg_top);
......@@ -3160,9 +3181,9 @@ test "Builder.dupePkg()" {
31603181 // the same as those in stack allocated package's fields
31613182 try std.testing.expect(dupe_deps.ptr != original_deps.ptr);
31623183 try std.testing.expect(dupe.name.ptr != pkg_top.name.ptr);
3163 try std.testing.expect(dupe.path.ptr != pkg_top.path.ptr);
3184 try std.testing.expect(dupe.path.path.ptr != pkg_top.path.path.ptr);
31643185 try std.testing.expect(dupe_deps[0].name.ptr != pkg_dep.name.ptr);
3165 try std.testing.expect(dupe_deps[0].path.ptr != pkg_dep.path.ptr);
3186 try std.testing.expect(dupe_deps[0].path.path.ptr != pkg_dep.path.path.ptr);
31663187}
31673188
31683189test "LibExeObjStep.addBuildOption" {
......@@ -3219,11 +3240,11 @@ test "LibExeObjStep.addPackage" {
32193240
32203241 const pkg_dep = Pkg{
32213242 .name = "pkg_dep",
3222 .path = "/not/a/pkg_dep.zig",
3243 .path = FileSource.relative("/not/a/pkg_dep.zig"),
32233244 };
32243245 const pkg_top = Pkg{
32253246 .name = "pkg_dep",
3226 .path = "/not/a/pkg_top.zig",
3247 .path = FileSource.relative("/not/a/pkg_top.zig"),
32273248 .dependencies = &[_]Pkg{pkg_dep},
32283249 };
32293250
lib/std/build/translate_c.zig+12-2
......@@ -21,6 +21,7 @@ pub const TranslateCStep = struct {
2121 output_dir: ?[]const u8,
2222 out_basename: []const u8,
2323 target: CrossTarget = CrossTarget{},
24 output_file: build.GeneratedFile,
2425
2526 pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
2627 const self = builder.allocator.create(TranslateCStep) catch unreachable;
......@@ -31,11 +32,20 @@ pub const TranslateCStep = struct {
3132 .include_dirs = std.ArrayList([]const u8).init(builder.allocator),
3233 .output_dir = null,
3334 .out_basename = undefined,
35 .output_file = build.GeneratedFile{
36 .step = &self.step,
37 .getPathFn = getGeneratedFilePath,
38 },
3439 };
3540 source.addStepDependencies(&self.step);
3641 return self;
3742 }
3843
44 fn getGeneratedFilePath(file: *const build.GeneratedFile) []const u8 {
45 const self = @fieldParentPtr(TranslateCStep, "step", file.step);
46 return self.getOutputPath();
47 }
48
3949 /// Unless setOutputDir was called, this function must be called only in
4050 /// the make step, from a step that has declared a dependency on this one.
4151 /// To run an executable built with zig build, use `run`, or create an install step and invoke it.
......@@ -52,7 +62,7 @@ pub const TranslateCStep = struct {
5262
5363 /// Creates a step to build an executable from the translated source.
5464 pub fn addExecutable(self: *TranslateCStep) *LibExeObjStep {
55 return self.builder.addExecutableSource("translated_c", @as(build.FileSource, .{ .translate_c = self }));
65 return self.builder.addExecutableSource("translated_c", @as(build.FileSource, .{ .generated = &self.output_file }));
5666 }
5767
5868 pub fn addIncludeDir(self: *TranslateCStep, include_dir: []const u8) void {
......@@ -60,7 +70,7 @@ pub const TranslateCStep = struct {
6070 }
6171
6272 pub fn addCheckFile(self: *TranslateCStep, expected_matches: []const []const u8) *CheckFileStep {
63 return CheckFileStep.create(self.builder, .{ .translate_c = self }, self.builder.dupeStrings(expected_matches));
73 return CheckFileStep.create(self.builder, .{ .generated = &self.output_file }, self.builder.dupeStrings(expected_matches));
6474 }
6575
6676 fn make(step: *Step) !void {