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 {...@@ -359,7 +359,7 @@ pub const Builder = struct {
359 pub fn dupePkg(self: *Builder, package: Pkg) Pkg {359 pub fn dupePkg(self: *Builder, package: Pkg) Pkg {
360 var the_copy = Pkg{360 var the_copy = Pkg{
361 .name = self.dupe(package.name),361 .name = self.dupe(package.name),
362 .path = self.dupePath(package.path),362 .path = package.path.dupe(self),
363 };363 };
364364
365 if (package.dependencies) |dependencies| {365 if (package.dependencies) |dependencies| {
...@@ -1245,7 +1245,7 @@ pub const Target = std.zig.CrossTarget;...@@ -1245,7 +1245,7 @@ pub const Target = std.zig.CrossTarget;
12451245
1246pub const Pkg = struct {1246pub const Pkg = struct {
1247 name: []const u8,1247 name: []const u8,
1248 path: []const u8,1248 path: FileSource,
1249 dependencies: ?[]const Pkg = null,1249 dependencies: ?[]const Pkg = null,
1250};1250};
12511251
...@@ -1284,6 +1284,20 @@ fn isLibCppLibrary(name: []const u8) bool {...@@ -1284,6 +1284,20 @@ fn isLibCppLibrary(name: []const u8) bool {
1284 return false;1284 return false;
1285}1285}
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
1287pub const FileSource = union(enum) {1301pub const FileSource = union(enum) {
1288 /// Relative to build root1302 /// Relative to build root
1289 path: []const u8,1303 path: []const u8,
...@@ -1291,13 +1305,17 @@ pub const FileSource = union(enum) {...@@ -1291,13 +1305,17 @@ pub const FileSource = union(enum) {
1291 step: *WriteFileStep,1305 step: *WriteFileStep,
1292 basename: []const u8,1306 basename: []const u8,
1293 },1307 },
1294 translate_c: *TranslateCStep,1308 generated: *const GeneratedFile,
1309
1310 pub fn relative(path: []const u8) FileSource {
1311 return FileSource{ .path = path };
1312 }
12951313
1296 pub fn addStepDependencies(self: FileSource, step: *Step) void {1314 pub fn addStepDependencies(self: FileSource, step: *Step) void {
1297 switch (self) {1315 switch (self) {
1298 .path => {},1316 .path => {},
1299 .write_file => |wf| step.dependOn(&wf.step.step),1317 .write_file => |wf| step.dependOn(&wf.step.step),
1300 .translate_c => |tc| step.dependOn(&tc.step),1318 .generated => |gen| step.dependOn(gen.step),
1301 }1319 }
1302 }1320 }
13031321
...@@ -1306,18 +1324,20 @@ pub const FileSource = union(enum) {...@@ -1306,18 +1324,20 @@ pub const FileSource = union(enum) {
1306 return switch (self) {1324 return switch (self) {
1307 .path => |p| builder.pathFromRoot(p),1325 .path => |p| builder.pathFromRoot(p),
1308 .write_file => |wf| wf.step.getOutputPath(wf.basename),1326 .write_file => |wf| wf.step.getOutputPath(wf.basename),
1309 .translate_c => |tc| tc.getOutputPath(),1327 .generated => |gen| gen.getPath(),
1310 };1328 };
1311 }1329 }
13121330
1313 pub fn dupe(self: FileSource, b: *Builder) FileSource {1331 pub fn dupe(self: FileSource, b: *Builder) FileSource {
1314 return switch (self) {1332 return switch (self) {
1315 .path => |p| .{ .path = b.dupe(p) },1333 .path => |p| .{ .path = b.dupePath(p) },
1316 .write_file => |wf| .{ .write_file = .{1334 .write_file => |wf| .{
1317 .step = wf.step,1335 .write_file = .{
1318 .basename = b.dupe(wf.basename),1336 .step = wf.step,
1319 } },1337 .basename = b.dupe(wf.basename),
1320 .translate_c => |tc| .{ .translate_c = tc },1338 },
1339 },
1340 .generated => |gen| .{ .generated = gen },
1321 };1341 };
1322 }1342 }
1323};1343};
...@@ -2107,13 +2127,14 @@ pub const LibExeObjStep = struct {...@@ -2107,13 +2127,14 @@ pub const LibExeObjStep = struct {
2107 }2127 }
21082128
2109 pub fn addPackage(self: *LibExeObjStep, package: Pkg) void {2129 pub fn addPackage(self: *LibExeObjStep, package: Pkg) void {
2130 package.path.addStepDependencies(&self.step);
2110 self.packages.append(self.builder.dupePkg(package)) catch unreachable;2131 self.packages.append(self.builder.dupePkg(package)) catch unreachable;
2111 }2132 }
21122133
2113 pub fn addPackagePath(self: *LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {2134 pub fn addPackagePath(self: *LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void {
2114 self.packages.append(Pkg{2135 self.packages.append(Pkg{
2115 .name = self.builder.dupe(name),2136 .name = self.builder.dupe(name),
2116 .path = self.builder.dupe(pkg_index_path),2137 .path = .{ .path = self.builder.dupe(pkg_index_path) },
2117 }) catch unreachable;2138 }) catch unreachable;
2118 }2139 }
21192140
...@@ -2190,7 +2211,7 @@ pub const LibExeObjStep = struct {...@@ -2190,7 +2211,7 @@ pub const LibExeObjStep = struct {
21902211
2191 try zig_args.append("--pkg-begin");2212 try zig_args.append("--pkg-begin");
2192 try zig_args.append(pkg.name);2213 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
2195 if (pkg.dependencies) |dependencies| {2216 if (pkg.dependencies) |dependencies| {
2196 for (dependencies) |sub_pkg| {2217 for (dependencies) |sub_pkg| {
...@@ -3137,11 +3158,11 @@ test "Builder.dupePkg()" {...@@ -3137,11 +3158,11 @@ test "Builder.dupePkg()" {
31373158
3138 var pkg_dep = Pkg{3159 var pkg_dep = Pkg{
3139 .name = "pkg_dep",3160 .name = "pkg_dep",
3140 .path = "/not/a/pkg_dep.zig",3161 .path = FileSource.relative("/not/a/pkg_dep.zig"),
3141 };3162 };
3142 var pkg_top = Pkg{3163 var pkg_top = Pkg{
3143 .name = "pkg_top",3164 .name = "pkg_top",
3144 .path = "/not/a/pkg_top.zig",3165 .path = FileSource.relative("/not/a/pkg_top.zig"),
3145 .dependencies = &[_]Pkg{pkg_dep},3166 .dependencies = &[_]Pkg{pkg_dep},
3146 };3167 };
3147 const dupe = builder.dupePkg(pkg_top);3168 const dupe = builder.dupePkg(pkg_top);
...@@ -3160,9 +3181,9 @@ test "Builder.dupePkg()" {...@@ -3160,9 +3181,9 @@ test "Builder.dupePkg()" {
3160 // the same as those in stack allocated package's fields3181 // the same as those in stack allocated package's fields
3161 try std.testing.expect(dupe_deps.ptr != original_deps.ptr);3182 try std.testing.expect(dupe_deps.ptr != original_deps.ptr);
3162 try std.testing.expect(dupe.name.ptr != pkg_top.name.ptr);3183 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);
3164 try std.testing.expect(dupe_deps[0].name.ptr != pkg_dep.name.ptr);3185 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);
3166}3187}
31673188
3168test "LibExeObjStep.addBuildOption" {3189test "LibExeObjStep.addBuildOption" {
...@@ -3219,11 +3240,11 @@ test "LibExeObjStep.addPackage" {...@@ -3219,11 +3240,11 @@ test "LibExeObjStep.addPackage" {
32193240
3220 const pkg_dep = Pkg{3241 const pkg_dep = Pkg{
3221 .name = "pkg_dep",3242 .name = "pkg_dep",
3222 .path = "/not/a/pkg_dep.zig",3243 .path = FileSource.relative("/not/a/pkg_dep.zig"),
3223 };3244 };
3224 const pkg_top = Pkg{3245 const pkg_top = Pkg{
3225 .name = "pkg_dep",3246 .name = "pkg_dep",
3226 .path = "/not/a/pkg_top.zig",3247 .path = FileSource.relative("/not/a/pkg_top.zig"),
3227 .dependencies = &[_]Pkg{pkg_dep},3248 .dependencies = &[_]Pkg{pkg_dep},
3228 };3249 };
32293250
lib/std/build/translate_c.zig+12-2
...@@ -21,6 +21,7 @@ pub const TranslateCStep = struct {...@@ -21,6 +21,7 @@ pub const TranslateCStep = struct {
21 output_dir: ?[]const u8,21 output_dir: ?[]const u8,
22 out_basename: []const u8,22 out_basename: []const u8,
23 target: CrossTarget = CrossTarget{},23 target: CrossTarget = CrossTarget{},
24 output_file: build.GeneratedFile,
2425
25 pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {26 pub fn create(builder: *Builder, source: build.FileSource) *TranslateCStep {
26 const self = builder.allocator.create(TranslateCStep) catch unreachable;27 const self = builder.allocator.create(TranslateCStep) catch unreachable;
...@@ -31,11 +32,20 @@ pub const TranslateCStep = struct {...@@ -31,11 +32,20 @@ pub const TranslateCStep = struct {
31 .include_dirs = std.ArrayList([]const u8).init(builder.allocator),32 .include_dirs = std.ArrayList([]const u8).init(builder.allocator),
32 .output_dir = null,33 .output_dir = null,
33 .out_basename = undefined,34 .out_basename = undefined,
35 .output_file = build.GeneratedFile{
36 .step = &self.step,
37 .getPathFn = getGeneratedFilePath,
38 },
34 };39 };
35 source.addStepDependencies(&self.step);40 source.addStepDependencies(&self.step);
36 return self;41 return self;
37 }42 }
3843
44 fn getGeneratedFilePath(file: *const build.GeneratedFile) []const u8 {
45 const self = @fieldParentPtr(TranslateCStep, "step", file.step);
46 return self.getOutputPath();
47 }
48
39 /// Unless setOutputDir was called, this function must be called only in49 /// Unless setOutputDir was called, this function must be called only in
40 /// the make step, from a step that has declared a dependency on this one.50 /// the make step, from a step that has declared a dependency on this one.
41 /// To run an executable built with zig build, use `run`, or create an install step and invoke it.51 /// 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 {...@@ -52,7 +62,7 @@ pub const TranslateCStep = struct {
5262
53 /// Creates a step to build an executable from the translated source.63 /// Creates a step to build an executable from the translated source.
54 pub fn addExecutable(self: *TranslateCStep) *LibExeObjStep {64 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 }));
56 }66 }
5767
58 pub fn addIncludeDir(self: *TranslateCStep, include_dir: []const u8) void {68 pub fn addIncludeDir(self: *TranslateCStep, include_dir: []const u8) void {
...@@ -60,7 +70,7 @@ pub const TranslateCStep = struct {...@@ -60,7 +70,7 @@ pub const TranslateCStep = struct {
60 }70 }
6171
62 pub fn addCheckFile(self: *TranslateCStep, expected_matches: []const []const u8) *CheckFileStep {72 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));
64 }74 }
6575
66 fn make(step: *Step) !void {76 fn make(step: *Step) !void {