diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2023-10-17 21:27:09 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-10-17 21:27:09 -0700 |
| commit | 09dea957ed74f87b7f5f88bc2c760f3f93d0165a (patch) | |
| tree | 3b517ec2e5efdedc3fbbeaf3f6a6249ccead7325 /src/introspect.zig | |
| parent | 053119083c2c93cb1fc4129bc647c03301f4010d (diff) | |
| download | zig-09dea957ed74f87b7f5f88bc2c760f3f93d0165a.tar.gz zig-09dea957ed74f87b7f5f88bc2c760f3f93d0165a.zip | |
rework zig env
Introduce introspect.EnvVar which tracks all the environment variables
that are observed by the compiler, so that we can print them with `zig
env`.
The `zig env` command now prints both the resolved values as well as all
the possibly observed environment variables.
Diffstat (limited to 'src/introspect.zig')
| -rw-r--r-- | src/introspect.zig | 56 |
1 files changed, 45 insertions, 11 deletions
diff --git a/src/introspect.zig b/src/introspect.zig index 2eeae956ec..2cd19b798f 100644 --- a/src/introspect.zig +++ b/src/introspect.zig @@ -4,6 +4,7 @@ const mem = std.mem; const os = std.os; const fs = std.fs; const Compilation = @import("Compilation.zig"); +const build_options = @import("build_options"); /// Returns the sub_path that worked, or `null` if none did. /// The path of the returned Directory is relative to `base`. @@ -80,23 +81,17 @@ pub fn findZigLibDirFromSelfExe( /// Caller owns returned memory. pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 { - if (builtin.os.tag == .wasi) { + if (builtin.os.tag == .wasi) @compileError("on WASI the global cache dir must be resolved with preopens"); - } - if (std.process.getEnvVarOwned(allocator, "ZIG_GLOBAL_CACHE_DIR")) |value| { - if (value.len > 0) { - return value; - } else { - allocator.free(value); - } - } else |_| {} + + if (try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(allocator)) |value| return value; const appname = "zig"; if (builtin.os.tag != .windows) { - if (std.os.getenv("XDG_CACHE_HOME")) |cache_root| { + if (EnvVar.XDG_CACHE_HOME.getPosix()) |cache_root| { return fs.path.join(allocator, &[_][]const u8{ cache_root, appname }); - } else if (std.os.getenv("HOME")) |home| { + } else if (EnvVar.HOME.getPosix()) |home| { return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname }); } } @@ -146,3 +141,42 @@ pub fn resolvePath( pub fn isUpDir(p: []const u8) bool { return mem.startsWith(u8, p, "..") and (p.len == 2 or p[2] == fs.path.sep); } + +/// Collects all the environment variables that Zig could possibly inspect, so +/// that we can do reflection on this and print them with `zig env`. +pub const EnvVar = enum { + ZIG_GLOBAL_CACHE_DIR, + ZIG_LOCAL_CACHE_DIR, + ZIG_LIB_DIR, + ZIG_LIBC, + ZIG_BUILD_RUNNER, + ZIG_VERBOSE_LINK, + ZIG_VERBOSE_CC, + ZIG_BTRFS_WORKAROUND, + CC, + NO_COLOR, + XDG_CACHE_HOME, + HOME, + /// https://github.com/ziglang/zig/issues/17585 + INCLUDE, + + pub fn isSet(comptime ev: EnvVar) bool { + return std.process.hasEnvVarConstant(@tagName(ev)); + } + + pub fn get(ev: EnvVar, arena: mem.Allocator) !?[]u8 { + // Env vars aren't used in the bootstrap stage. + if (build_options.only_c) return null; + + if (std.process.getEnvVarOwned(arena, @tagName(ev))) |value| { + return value; + } else |err| switch (err) { + error.EnvironmentVariableNotFound => return null, + else => |e| return e, + } + } + + pub fn getPosix(comptime ev: EnvVar) ?[:0]const u8 { + return std.os.getenvZ(@tagName(ev)); + } +}; |
