pub const Directories = struct cwd: []const u8The string returned by introspect.getResolvedCwd. This is typically an absolute path, but on WASI is the empty string "" instead, because WASI does not have absolute paths.
zig_lib: Cache.DirectoryThe Zig 'lib' directory. zig_lib.path is resolved (resolvePath) or null for cwd. Guaranteed to be a different path from global_cache and local_cache.
global_cache: Cache.DirectoryThe global Zig cache directory. global_cache.path is resolved (resolvePath) or null for cwd.
local_cache: Cache.DirectoryThe local Zig cache directory. local_cache.path is resolved (resolvePath) or null for cwd. This may be the same as global_cache.
build_root: Cache.DirectoryThe directory that contains build.zig. This path is provided by the build system, when the build system is used, otherwise, it is null for cwd.
pub fn deinit(dirs: *Directories, io: Io) voidpub fn withoutLocalCache(dirs: Directories) DirectoriesReturns a Directories where local_cache is replaced with global_cache, intended for use by sub-compilations (e.g. compiler_rt). Do not deinit the returned Directories; it shares handles with dirs.
pub fn init(arena: Allocator, io: Io, options: InitOptions) DirectoriesUses std.process.fatal on error conditions.
fn getLocalCacheDirectory(arena: Allocator, io: Io, cwd: []const u8, global_cache: Cache.Directory, local_cache_strat: LocalCacheStrategy) Cache.Directoryfn getPreopen(preopens: std.process.Preopens, name: []const u8) Cache.Directorypub fn openUnresolved(arena: Allocator, io: Io, cwd: []const u8, unresolved_path: []const u8, thing: enum1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517pub const Directories = struct {
/// The string returned by `introspect.getResolvedCwd`. This is typically an absolute path,
/// but on WASI is the empty string "" instead, because WASI does not have absolute paths.
cwd: []const u8,
/// The Zig 'lib' directory.
/// `zig_lib.path` is resolved (`resolvePath`) or `null` for cwd.
/// Guaranteed to be a different path from `global_cache` and `local_cache`.
zig_lib: Cache.Directory,
/// The global Zig cache directory.
/// `global_cache.path` is resolved (`resolvePath`) or `null` for cwd.
global_cache: Cache.Directory,
/// The local Zig cache directory.
/// `local_cache.path` is resolved (`resolvePath`) or `null` for cwd.
/// This may be the same as `global_cache`.
local_cache: Cache.Directory,
/// The directory that contains build.zig. This path is provided by the
/// build system, when the build system is used, otherwise, it is `null`
/// for cwd.
build_root: Cache.Directory,
pub fn deinit(dirs: *Directories, io: Io) void {
// The local and global caches could be the same.
const close_local = dirs.local_cache.handle.handle != dirs.global_cache.handle.handle;
const close_build_root = dirs.build_root.handle.handle != Io.Dir.cwd().handle;
dirs.global_cache.handle.close(io);
if (close_local) dirs.local_cache.handle.close(io);
dirs.zig_lib.handle.close(io);
if (close_build_root) dirs.build_root.handle.close(io);
}
/// Returns a `Directories` where `local_cache` is replaced with `global_cache`, intended for
/// use by sub-compilations (e.g. compiler_rt). Do not `deinit` the returned `Directories`; it
/// shares handles with `dirs`.
pub fn withoutLocalCache(dirs: Directories) Directories {
return .{
.cwd = dirs.cwd,
.zig_lib = dirs.zig_lib,
.global_cache = dirs.global_cache,
.local_cache = dirs.global_cache,
.build_root = dirs.build_root,
};
}
const LocalCacheStrategy = union(enum) {
override: []const u8,
search,
global,
};
pub const InitOptions = struct {
override_zig_lib: ?[]const u8,
override_global_cache: ?[]const u8,
build_root: ?[]const u8,
local_cache_strat: LocalCacheStrategy,
preopens: std.process.Preopens,
self_exe_path: switch (builtin.target.os.tag) {
.wasi => void,
else => []const u8,
},
environ_map: *const std.process.Environ.Map,
cwd: []const u8,
};
/// Uses `std.process.fatal` on error conditions.
pub fn init(arena: Allocator, io: Io, options: InitOptions) Directories {
const wasi = builtin.target.os.tag == .wasi;
const cwd = options.cwd;
const zig_lib: Cache.Directory = d: {
if (options.override_zig_lib) |path| break :d openUnresolved(arena, io, cwd, path, .@"zig lib");
if (wasi) break :d getPreopen(options.preopens, "/lib");
break :d findZigLibDirFromSelfExe(arena, io, cwd, options.self_exe_path) catch |err| {
fatal("unable to find zig installation directory from executable path {q}: {t}", .{
options.self_exe_path, err,
});
};
};
const build_root: Cache.Directory = if (options.build_root) |s|
openUnresolved(arena, io, cwd, s, .@"build root")
else
.cwd();
const global_cache: Cache.Directory = d: {
if (options.override_global_cache) |path| break :d openUnresolved(arena, io, cwd, path, .@"global cache");
if (wasi) break :d getPreopen(options.preopens, "/cache");
const path = resolveGlobalCacheDir(arena, options.environ_map) catch |err| {
fatal("unable to resolve zig cache directory: {t}", .{err});
};
break :d openUnresolved(arena, io, cwd, path, .@"global cache");
};
const local_cache = getLocalCacheDirectory(arena, io, cwd, global_cache, options.local_cache_strat);
if (mem.eql(u8, zig_lib.path orelse "", global_cache.path orelse "")) {
fatal("zig lib directory '{f}' cannot be equal to global cache directory '{f}'", .{ zig_lib, global_cache });
}
if (mem.eql(u8, zig_lib.path orelse "", local_cache.path orelse "")) {
fatal("zig lib directory '{f}' cannot be equal to local cache directory '{f}'", .{ zig_lib, local_cache });
}
return .{
.cwd = cwd,
.zig_lib = zig_lib,
.global_cache = global_cache,
.local_cache = local_cache,
.build_root = build_root,
};
}
fn getLocalCacheDirectory(
arena: Allocator,
io: Io,
cwd: []const u8,
global_cache: Cache.Directory,
local_cache_strat: LocalCacheStrategy,
) Cache.Directory {
return switch (local_cache_strat) {
.override => |path| openUnresolved(arena, io, cwd, path, .@"local cache"),
.search => d: {
const maybe_path = resolveSuitableLocalCacheDir(arena, io, cwd) catch |err|
fatal("unable to resolve zig cache directory: {t}", .{err});
const path = maybe_path orelse break :d global_cache;
break :d openUnresolved(arena, io, cwd, path, .@"local cache");
},
.global => global_cache,
};
}
fn getPreopen(preopens: std.process.Preopens, name: []const u8) Cache.Directory {
return .{
.path = if (mem.eql(u8, name, ".")) null else name,
.handle = switch (preopens.get(name) orelse fatal("preopen not found: {q}", .{name})) {
.file => fatal("preopen {q} is not a directory", .{name}),
.dir => |d| d,
},
};
}
pub fn openUnresolved(
arena: Allocator,
io: Io,
cwd: []const u8,
unresolved_path: []const u8,
thing: enum { @"zig lib", @"global cache", @"local cache", @"build root" },
) Cache.Directory {
const path = resolvePath(arena, cwd, &.{unresolved_path}) catch |err| {
fatal("unable to resolve {t} directory: {t}", .{ thing, err });
};
const nonempty_path = if (path.len == 0) "." else path;
const handle_or_err = switch (thing) {
.@"zig lib", .@"build root" => Dir.cwd().openDir(io, nonempty_path, .{}),
.@"global cache", .@"local cache" => Dir.cwd().createDirPathOpen(io, nonempty_path, .{}),
};
return .{
.path = if (path.len == 0) null else path,
.handle = handle_or_err catch |err| {
const extra_str: []const u8 = e: {
if (thing == .@"global cache") switch (err) {
error.AccessDenied, error.ReadOnlyFileSystem => break :e "\n" ++
"If this location is not writable then consider specifying an alternative with " ++
"the ZIG_GLOBAL_CACHE_DIR environment variable or the --global-cache-dir option.",
else => {},
};
break :e "";
};
fatal("unable to open {t} directory {q}: {t}{s}", .{ thing, nonempty_path, err, extra_str });
},
};
}
}