| ... | ... | @@ -359,7 +359,7 @@ pub const Builder = struct { |
| 359 | 359 | pub fn dupePkg(self: *Builder, package: Pkg) Pkg { |
| 360 | 360 | var the_copy = Pkg{ |
| 361 | 361 | .name = self.dupe(package.name), |
| 362 | | .path = self.dupePath(package.path), |
| 362 | .path = package.path.dupe(self), |
| 363 | 363 | }; |
| 364 | 364 | |
| 365 | 365 | if (package.dependencies) |dependencies| { |
| ... | ... | @@ -1245,7 +1245,7 @@ pub const Target = std.zig.CrossTarget; |
| 1245 | 1245 | |
| 1246 | 1246 | pub const Pkg = struct { |
| 1247 | 1247 | name: []const u8, |
| 1248 | | path: []const u8, |
| 1248 | path: FileSource, |
| 1249 | 1249 | dependencies: ?[]const Pkg = null, |
| 1250 | 1250 | }; |
| 1251 | 1251 | |
| ... | ... | @@ -1284,6 +1284,20 @@ fn isLibCppLibrary(name: []const u8) bool { |
| 1284 | 1284 | return false; |
| 1285 | 1285 | } |
| 1286 | 1286 | |
| 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. |
| 1289 | pub 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 | |
| 1287 | 1301 | pub const FileSource = union(enum) { |
| 1288 | 1302 | /// Relative to build root |
| 1289 | 1303 | path: []const u8, |
| ... | ... | @@ -1291,13 +1305,17 @@ pub const FileSource = union(enum) { |
| 1291 | 1305 | step: *WriteFileStep, |
| 1292 | 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 | } |
| 1295 | 1313 | |
| 1296 | 1314 | pub fn addStepDependencies(self: FileSource, step: *Step) void { |
| 1297 | 1315 | switch (self) { |
| 1298 | 1316 | .path => {}, |
| 1299 | 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 | } |
| 1303 | 1321 | |
| ... | ... | @@ -1306,18 +1324,20 @@ pub const FileSource = union(enum) { |
| 1306 | 1324 | return switch (self) { |
| 1307 | 1325 | .path => |p| builder.pathFromRoot(p), |
| 1308 | 1326 | .write_file => |wf| wf.step.getOutputPath(wf.basename), |
| 1309 | | .translate_c => |tc| tc.getOutputPath(), |
| 1327 | .generated => |gen| gen.getPath(), |
| 1310 | 1328 | }; |
| 1311 | 1329 | } |
| 1312 | 1330 | |
| 1313 | 1331 | pub fn dupe(self: FileSource, b: *Builder) FileSource { |
| 1314 | 1332 | 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 }, |
| 1321 | 1341 | }; |
| 1322 | 1342 | } |
| 1323 | 1343 | }; |
| ... | ... | @@ -2107,13 +2127,14 @@ pub const LibExeObjStep = struct { |
| 2107 | 2127 | } |
| 2108 | 2128 | |
| 2109 | 2129 | pub fn addPackage(self: *LibExeObjStep, package: Pkg) void { |
| 2130 | package.path.addStepDependencies(&self.step); |
| 2110 | 2131 | self.packages.append(self.builder.dupePkg(package)) catch unreachable; |
| 2111 | 2132 | } |
| 2112 | 2133 | |
| 2113 | 2134 | pub fn addPackagePath(self: *LibExeObjStep, name: []const u8, pkg_index_path: []const u8) void { |
| 2114 | 2135 | self.packages.append(Pkg{ |
| 2115 | 2136 | .name = self.builder.dupe(name), |
| 2116 | | .path = self.builder.dupe(pkg_index_path), |
| 2137 | .path = .{ .path = self.builder.dupe(pkg_index_path) }, |
| 2117 | 2138 | }) catch unreachable; |
| 2118 | 2139 | } |
| 2119 | 2140 | |
| ... | ... | @@ -2190,7 +2211,7 @@ pub const LibExeObjStep = struct { |
| 2190 | 2211 | |
| 2191 | 2212 | try zig_args.append("--pkg-begin"); |
| 2192 | 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))); |
| 2194 | 2215 | |
| 2195 | 2216 | if (pkg.dependencies) |dependencies| { |
| 2196 | 2217 | for (dependencies) |sub_pkg| { |
| ... | ... | @@ -3137,11 +3158,11 @@ test "Builder.dupePkg()" { |
| 3137 | 3158 | |
| 3138 | 3159 | var pkg_dep = Pkg{ |
| 3139 | 3160 | .name = "pkg_dep", |
| 3140 | | .path = "/not/a/pkg_dep.zig", |
| 3161 | .path = FileSource.relative("/not/a/pkg_dep.zig"), |
| 3141 | 3162 | }; |
| 3142 | 3163 | var pkg_top = Pkg{ |
| 3143 | 3164 | .name = "pkg_top", |
| 3144 | | .path = "/not/a/pkg_top.zig", |
| 3165 | .path = FileSource.relative("/not/a/pkg_top.zig"), |
| 3145 | 3166 | .dependencies = &[_]Pkg{pkg_dep}, |
| 3146 | 3167 | }; |
| 3147 | 3168 | const dupe = builder.dupePkg(pkg_top); |
| ... | ... | @@ -3160,9 +3181,9 @@ test "Builder.dupePkg()" { |
| 3160 | 3181 | // the same as those in stack allocated package's fields |
| 3161 | 3182 | try std.testing.expect(dupe_deps.ptr != original_deps.ptr); |
| 3162 | 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 | 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 | } |
| 3167 | 3188 | |
| 3168 | 3189 | test "LibExeObjStep.addBuildOption" { |
| ... | ... | @@ -3219,11 +3240,11 @@ test "LibExeObjStep.addPackage" { |
| 3219 | 3240 | |
| 3220 | 3241 | const pkg_dep = Pkg{ |
| 3221 | 3242 | .name = "pkg_dep", |
| 3222 | | .path = "/not/a/pkg_dep.zig", |
| 3243 | .path = FileSource.relative("/not/a/pkg_dep.zig"), |
| 3223 | 3244 | }; |
| 3224 | 3245 | const pkg_top = Pkg{ |
| 3225 | 3246 | .name = "pkg_dep", |
| 3226 | | .path = "/not/a/pkg_top.zig", |
| 3247 | .path = FileSource.relative("/not/a/pkg_top.zig"), |
| 3227 | 3248 | .dependencies = &[_]Pkg{pkg_dep}, |
| 3228 | 3249 | }; |
| 3229 | 3250 | |