pub const LazyPath = union(enum) src_path: struct { owner: *std.Build, sub_path: []const u8, }A source file path relative to build root.
generated: struct { index: Configuration.GeneratedFileIndex, /// The number of parent directories to go up. /// 0 means the generated file itself. /// 1 means the directory of the generated file. /// 2 means the parent of that directory, and so on. up: usize = 0, /// Applied after `up`. sub_path: []const u8 = "", }cwd_relative: []const u8Deprecated; call Graph.cwdRelativePath instead.
dependency: struct { dependency: *Dependency, sub_path: []const u8, }relative: struct { base: Configuration.LazyPath.Relative.Base, sub_path: []const u8 = "", pub fn eql(a: @This(), b: @This()) bool { return a.base == b.base and mem.eql(u8, a.sub_path, b.sub_path); } }Path to the Zig executable being used to execute "zig build".
Path to the "lib/" directory from the Zig installation being used to execute "zig build".
Path to the project's local cache directory (usually called ".zig-cache").
pub fn dirname(lazy_path: LazyPath) LazyPathReturns a lazy path referring to the directory containing this path.
The dirname is not allowed to escape the logical root for underlying path. For example, if the path is relative to the build root, the dirname is not allowed to traverse outside of the build root. Similarly, if the path is a generated file inside zig-cache, the dirname is not allowed to traverse outside of zig-cache.
pub fn path(lazy_path: LazyPath, b: *Build, sub_path: []const u8) LazyPathpub fn join(lazy_path: LazyPath, arena: Allocator, sub_path: []const u8) Allocator.Error!LazyPathpub fn getDisplayName(lazy_path: LazyPath) []const u8Deprecated, use format instead.
pub fn format(lp: LazyPath, w: *Io.Writer) Io.Writer.Error!voidpub fn addStepDependencies(lazy_path: LazyPath, other_step: *Step) voidAdds dependencies this file source implies to the given step.
pub fn dupe(lazy_path: LazyPath, graph: *const Graph) LazyPathCopies the internal strings.
The graph parameter is only used for the global arena allocator.
pub fn dupeList(lazy_paths: []const LazyPath, graph: *const Graph) []const LazyPathCopies the slice of paths and all internal strings.
The graph parameter is only used for the global arena allocator.
fn dupeInner(lazy_path: LazyPath, arena: Allocator) LazyPathfn eql(a: LazyPath, b: LazyPath) boolfn hash(lp: LazyPath, hasher: *std.hash.Wyhash) void2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459pub const LazyPath = union(enum) {
/// A source file path relative to build root.
src_path: struct {
owner: *std.Build,
sub_path: []const u8,
},
generated: struct {
index: Configuration.GeneratedFileIndex,
/// The number of parent directories to go up.
/// 0 means the generated file itself.
/// 1 means the directory of the generated file.
/// 2 means the parent of that directory, and so on.
up: usize = 0,
/// Applied after `up`.
sub_path: []const u8 = "",
},
/// Deprecated; call `Graph.cwdRelativePath` instead.
cwd_relative: []const u8,
dependency: struct {
dependency: *Dependency,
sub_path: []const u8,
},
relative: struct {
base: Configuration.LazyPath.Relative.Base,
sub_path: []const u8 = "",
pub fn eql(a: @This(), b: @This()) bool {
return a.base == b.base and mem.eql(u8, a.sub_path, b.sub_path);
}
},
/// Path to the Zig executable being used to execute "zig build".
pub const zig_exe: LazyPath = .{ .relative = .{ .base = .zig_exe } };
/// Path to the "lib/" directory from the Zig installation being used to
/// execute "zig build".
pub const zig_lib: LazyPath = .{ .relative = .{ .base = .zig_lib } };
/// Path to the project's local cache directory (usually called ".zig-cache").
pub const cache_root: LazyPath = .{ .relative = .{ .base = .local_cache } };
/// Returns a lazy path referring to the directory containing this path.
///
/// The dirname is not allowed to escape the logical root for underlying
/// path. For example, if the path is relative to the build root, the
/// dirname is not allowed to traverse outside of the build root.
/// Similarly, if the path is a generated file inside zig-cache, the
/// dirname is not allowed to traverse outside of zig-cache.
pub fn dirname(lazy_path: LazyPath) LazyPath {
return switch (lazy_path) {
.src_path => |sp| .{ .src_path = .{
.owner = sp.owner,
.sub_path = dirnameAllowEmpty(sp.sub_path) orelse {
dumpBadDirnameHelp(null, null, "dirname() attempted to traverse outside the build root\n", .{}) catch {};
@panic("misconfigured build script");
},
} },
.generated => |generated| .{ .generated = if (dirnameAllowEmpty(generated.sub_path)) |sub_dirname| .{
.index = generated.index,
.up = generated.up,
.sub_path = sub_dirname,
} else .{
.index = generated.index,
.up = generated.up + 1,
.sub_path = "",
} },
.cwd_relative => |rel_path| .{
.cwd_relative = dirnameAllowEmpty(rel_path) orelse {
// If we get null, it means one of two things:
// - rel_path was absolute, and is now root
// - rel_path was relative, and is now ""
// In either case, the build script tried to go too far
// and we should panic.
if (fs.path.isAbsolute(rel_path)) {
dumpBadDirnameHelp(null, null,
\\dirname() attempted to traverse outside the root.
\\No more directories left to go up.
\\
, .{}) catch {};
@panic("misconfigured build script");
} else {
dumpBadDirnameHelp(null, null,
\\dirname() attempted to traverse outside the current working directory.
\\
, .{}) catch {};
@panic("misconfigured build script");
}
},
},
.relative => |r| .{ .relative = .{
.base = r.base,
.sub_path = dirnameAllowEmpty(r.sub_path) orelse {
dumpBadDirnameHelp(null, null, "dirname() attempted to traverse outside the base path\n", .{}) catch {};
@panic("misconfigured build script");
},
} },
.dependency => |dep| .{ .dependency = .{
.dependency = dep.dependency,
.sub_path = dirnameAllowEmpty(dep.sub_path) orelse {
dumpBadDirnameHelp(null, null,
\\dirname() attempted to traverse outside the dependency root.
\\
, .{}) catch {};
@panic("misconfigured build script");
},
} },
};
}
pub fn path(lazy_path: LazyPath, b: *Build, sub_path: []const u8) LazyPath {
const graph = b.graph;
const arena = graph.arena;
return lazy_path.join(arena, sub_path) catch @panic("OOM");
}
pub fn join(lazy_path: LazyPath, arena: Allocator, sub_path: []const u8) Allocator.Error!LazyPath {
return switch (lazy_path) {
.src_path => |src| .{ .src_path = .{
.owner = src.owner,
.sub_path = try fs.path.resolve(arena, &.{ src.sub_path, sub_path }),
} },
.generated => |gen| .{ .generated = .{
.index = gen.index,
.up = gen.up,
.sub_path = try fs.path.resolve(arena, &.{ gen.sub_path, sub_path }),
} },
.cwd_relative => |cwd_relative| .{
.cwd_relative = try fs.path.resolve(arena, &.{ cwd_relative, sub_path }),
},
.relative => |r| .{ .relative = .{
.base = r.base,
.sub_path = try fs.path.resolve(arena, &.{ r.sub_path, sub_path }),
} },
.dependency => |dep| .{ .dependency = .{
.dependency = dep.dependency,
.sub_path = try fs.path.resolve(arena, &.{ dep.sub_path, sub_path }),
} },
};
}
/// Deprecated, use `format` instead.
pub fn getDisplayName(lazy_path: LazyPath) []const u8 {
return switch (lazy_path) {
.src_path => |sp| sp.sub_path,
.cwd_relative => |p| p,
.generated => "generated",
.dependency => "dependency",
.relative => |r| @tagName(r.base),
};
}
pub fn format(lp: LazyPath, w: *Io.Writer) Io.Writer.Error!void {
switch (lp) {
.src_path => |sp| try w.writeAll(sp.sub_path),
.cwd_relative => |p| try w.writeAll(p),
.generated => try w.writeAll("generated"),
.dependency => try w.writeAll("dependency"),
.relative => |r| try w.print("{t} {s}", .{ r.base, r.sub_path }),
}
}
/// Adds dependencies this file source implies to the given step.
pub fn addStepDependencies(lazy_path: LazyPath, other_step: *Step) void {
switch (lazy_path) {
.src_path, .cwd_relative, .relative, .dependency => {},
.generated => |gen| {
const graph = other_step.owner.graph;
const generated_owner_step = graph.generated_files.items[@backingInt(gen.index)];
other_step.dependOn(generated_owner_step);
},
}
}
/// Copies the internal strings.
///
/// The `graph` parameter is only used for the global arena allocator.
pub fn dupe(lazy_path: LazyPath, graph: *const Graph) LazyPath {
return dupeInner(lazy_path, graph.arena);
}
/// Copies the slice of paths and all internal strings.
///
/// The `graph` parameter is only used for the global arena allocator.
pub fn dupeList(lazy_paths: []const LazyPath, graph: *const Graph) []const LazyPath {
const arena = graph.arena;
const result = graph.alloc(LazyPath, lazy_paths.len);
for (result, lazy_paths) |*d, s| d.* = dupeInner(s, arena);
return result;
}
fn dupeInner(lazy_path: LazyPath, arena: Allocator) LazyPath {
return switch (lazy_path) {
.src_path => |sp| .{ .src_path = .{
.owner = sp.owner,
.sub_path = sp.owner.graph.dupePath(sp.sub_path),
} },
.cwd_relative => |p| .{ .cwd_relative = Graph.dupePathInner(arena, p) },
.relative => |r| .{ .relative = r },
.generated => |gen| .{ .generated = .{
.index = gen.index,
.up = gen.up,
.sub_path = Graph.dupePathInner(arena, gen.sub_path),
} },
.dependency => |dep| .{ .dependency = .{
.dependency = dep.dependency,
.sub_path = Graph.dupePathInner(arena, dep.sub_path),
} },
};
}
fn eql(a: LazyPath, b: LazyPath) bool {
if (std.meta.activeTag(a) != b) return false;
switch (a) {
.src_path => |a_sp| {
const b_sp = b.src_path;
if (a_sp.owner != b_sp.owner) return false;
if (mem.eql(u8, a_sp.sub_path, b_sp.sub_path)) return false;
},
.generated => |*a_gen| {
const b_gen = &b.generated;
if (a_gen.index != b_gen.index) return false;
if (a_gen.up != b_gen.up) return false;
if (mem.eql(u8, a_gen.sub_path, b_gen.sub_path)) return false;
},
.cwd_relative => |a_rel_path| {
const b_rel_path = b.cwd_relative;
if (!mem.eql(u8, a_rel_path, b_rel_path)) return false;
},
.relative => |a_relative| return a_relative.eql(b.relative),
.dependency => |a_dep| {
const b_dep = b.dependency;
if (a_dep.dependency != b_dep.dependency) return false;
if (!mem.eql(u8, a_dep.sub_path, b_dep.sub_path)) return false;
},
}
return true;
}
fn hash(lp: LazyPath, hasher: *std.hash.Wyhash) void {
switch (lp) {
.src_path => |sp| {
hasher.update(sp.owner.pkg_hash);
hasher.update(sp.sub_path);
},
.generated => |gen| {
hasher.update(@ptrCast(&gen.index));
hasher.update(@ptrCast(&gen.up));
hasher.update(gen.sub_path);
},
.cwd_relative => |rel_path| {
hasher.update(rel_path);
},
.relative => |r| {
hasher.update(@ptrCast(&r.base));
hasher.update(@ptrCast(&r.sub_path));
},
.dependency => |dep| {
hasher.update(dep.dependency.builder.pkg_hash);
hasher.update(dep.sub_path);
},
}
}
}
A reference to an existing or future path.