authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-08 14:39:17-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-08 14:39:17-07:00
log2759313263d58dba324788dbe346ed1506b239dc
treed7cd349ebe71e091992de578ec548ca3a8990d1e
parentc796c4528ec8085178fd3ae402ae80be6358dd38

add support for environment variables to control cache directories

This commit adds: ZIG_LOCAL_CACHE_DIR corresponding to --cache-dir ZIG_GLOBAL_CACHE_DIR corresponding to --global-cache-dir ZIG_LIB_DIR corresponding to --override-lib-dir The main use case is for `zig cc` where we are bound by clang's CLI options and need alternate channels to pass these configuration options.

1 files changed, 13 insertions(+), 7 deletions(-)

src/main.zig+13-7
...@@ -420,6 +420,15 @@ const Emit = union(enum) {...@@ -420,6 +420,15 @@ const Emit = union(enum) {
420 }420 }
421};421};
422422
423fn optionalStringEnvVar(arena: *Allocator, name: []const u8) !?[]const u8 {
424 if (std.process.getEnvVarOwned(arena, name)) |value| {
425 return value;
426 } else |err| switch (err) {
427 error.EnvironmentVariableNotFound => return null,
428 else => |e| return e,
429 }
430}
431
423fn buildOutputType(432fn buildOutputType(
424 gpa: *Allocator,433 gpa: *Allocator,
425 arena: *Allocator,434 arena: *Allocator,
...@@ -499,17 +508,14 @@ fn buildOutputType(...@@ -499,17 +508,14 @@ fn buildOutputType(
499 var link_eh_frame_hdr = false;508 var link_eh_frame_hdr = false;
500 var link_emit_relocs = false;509 var link_emit_relocs = false;
501 var each_lib_rpath: ?bool = null;510 var each_lib_rpath: ?bool = null;
502 var libc_paths_file: ?[]const u8 = std.process.getEnvVarOwned(arena, "ZIG_LIBC") catch |err| switch (err) {511 var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC");
503 error.EnvironmentVariableNotFound => null,
504 else => |e| return e,
505 };
506 var machine_code_model: std.builtin.CodeModel = .default;512 var machine_code_model: std.builtin.CodeModel = .default;
507 var runtime_args_start: ?usize = null;513 var runtime_args_start: ?usize = null;
508 var test_filter: ?[]const u8 = null;514 var test_filter: ?[]const u8 = null;
509 var test_name_prefix: ?[]const u8 = null;515 var test_name_prefix: ?[]const u8 = null;
510 var override_local_cache_dir: ?[]const u8 = null;516 var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR");
511 var override_global_cache_dir: ?[]const u8 = null;517 var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR");
512 var override_lib_dir: ?[]const u8 = null;518 var override_lib_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIB_DIR");
513 var main_pkg_path: ?[]const u8 = null;519 var main_pkg_path: ?[]const u8 = null;
514 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;520 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
515 var subsystem: ?std.Target.SubSystem = null;521 var subsystem: ?std.Target.SubSystem = null;