| ... | @@ -18,6 +18,7 @@ const link = @import("link.zig"); | ... | @@ -18,6 +18,7 @@ const link = @import("link.zig"); |
| 18 | const Package = @import("Package.zig"); | 18 | const Package = @import("Package.zig"); |
| 19 | const build_options = @import("build_options"); | 19 | const build_options = @import("build_options"); |
| 20 | const introspect = @import("introspect.zig"); | 20 | const introspect = @import("introspect.zig"); |
| | 21 | const EnvVar = introspect.EnvVar; |
| 21 | const LibCInstallation = @import("libc_installation.zig").LibCInstallation; | 22 | const LibCInstallation = @import("libc_installation.zig").LibCInstallation; |
| 22 | const wasi_libc = @import("wasi_libc.zig"); | 23 | const wasi_libc = @import("wasi_libc.zig"); |
| 23 | const BuildId = std.Build.CompileStep.BuildId; | 24 | const BuildId = std.Build.CompileStep.BuildId; |
| ... | @@ -231,14 +232,14 @@ pub fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -231,14 +232,14 @@ pub fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 231 | fatal("expected command argument", .{}); | 232 | fatal("expected command argument", .{}); |
| 232 | } | 233 | } |
| 233 | | 234 | |
| 234 | if (std.process.can_execv and std.os.getenvZ("ZIG_IS_DETECTING_LIBC_PATHS") != null) { | 235 | if (process.can_execv and std.os.getenvZ("ZIG_IS_DETECTING_LIBC_PATHS") != null) { |
| 235 | // In this case we have accidentally invoked ourselves as "the system C compiler" | 236 | // In this case we have accidentally invoked ourselves as "the system C compiler" |
| 236 | // to figure out where libc is installed. This is essentially infinite recursion | 237 | // to figure out where libc is installed. This is essentially infinite recursion |
| 237 | // via child process execution due to the CC environment variable pointing to Zig. | 238 | // via child process execution due to the CC environment variable pointing to Zig. |
| 238 | // Here we ignore the CC environment variable and exec `cc` as a child process. | 239 | // Here we ignore the CC environment variable and exec `cc` as a child process. |
| 239 | // However it's possible Zig is installed as *that* C compiler as well, which is | 240 | // However it's possible Zig is installed as *that* C compiler as well, which is |
| 240 | // why we have this additional environment variable here to check. | 241 | // why we have this additional environment variable here to check. |
| 241 | var env_map = try std.process.getEnvMap(arena); | 242 | var env_map = try process.getEnvMap(arena); |
| 242 | | 243 | |
| 243 | const inf_loop_env_key = "ZIG_IS_TRYING_TO_NOT_CALL_ITSELF"; | 244 | const inf_loop_env_key = "ZIG_IS_TRYING_TO_NOT_CALL_ITSELF"; |
| 244 | if (env_map.get(inf_loop_env_key) != null) { | 245 | if (env_map.get(inf_loop_env_key) != null) { |
| ... | @@ -254,11 +255,11 @@ pub fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -254,11 +255,11 @@ pub fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 254 | // CC environment variable. We detect and support this scenario here because of | 255 | // CC environment variable. We detect and support this scenario here because of |
| 255 | // the ZIG_IS_DETECTING_LIBC_PATHS environment variable. | 256 | // the ZIG_IS_DETECTING_LIBC_PATHS environment variable. |
| 256 | if (mem.eql(u8, args[1], "cc")) { | 257 | if (mem.eql(u8, args[1], "cc")) { |
| 257 | return std.process.execve(arena, args[1..], &env_map); | 258 | return process.execve(arena, args[1..], &env_map); |
| 258 | } else { | 259 | } else { |
| 259 | const modified_args = try arena.dupe([]const u8, args); | 260 | const modified_args = try arena.dupe([]const u8, args); |
| 260 | modified_args[0] = "cc"; | 261 | modified_args[0] = "cc"; |
| 261 | return std.process.execve(arena, modified_args, &env_map); | 262 | return process.execve(arena, modified_args, &env_map); |
| 262 | } | 263 | } |
| 263 | } | 264 | } |
| 264 | | 265 | |
| ... | @@ -686,19 +687,6 @@ const Emit = union(enum) { | ... | @@ -686,19 +687,6 @@ const Emit = union(enum) { |
| 686 | } | 687 | } |
| 687 | }; | 688 | }; |
| 688 | | 689 | |
| 689 | fn optionalStringEnvVar(arena: Allocator, name: []const u8) !?[]const u8 { | | |
| 690 | // Env vars aren't used in the bootstrap stage. | | |
| 691 | if (build_options.only_c) { | | |
| 692 | return null; | | |
| 693 | } | | |
| 694 | if (std.process.getEnvVarOwned(arena, name)) |value| { | | |
| 695 | return value; | | |
| 696 | } else |err| switch (err) { | | |
| 697 | error.EnvironmentVariableNotFound => return null, | | |
| 698 | else => |e| return e, | | |
| 699 | } | | |
| 700 | } | | |
| 701 | | | |
| 702 | const ArgMode = union(enum) { | 690 | const ArgMode = union(enum) { |
| 703 | build: std.builtin.OutputMode, | 691 | build: std.builtin.OutputMode, |
| 704 | cc, | 692 | cc, |
| ... | @@ -797,8 +785,10 @@ fn buildOutputType( | ... | @@ -797,8 +785,10 @@ fn buildOutputType( |
| 797 | var no_builtin = false; | 785 | var no_builtin = false; |
| 798 | var listen: Listen = .none; | 786 | var listen: Listen = .none; |
| 799 | var debug_compile_errors = false; | 787 | var debug_compile_errors = false; |
| 800 | var verbose_link = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK"); | 788 | var verbose_link = (builtin.os.tag != .wasi or builtin.link_libc) and |
| 801 | var verbose_cc = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_CC"); | 789 | EnvVar.ZIG_VERBOSE_LINK.isSet(); |
| | 790 | var verbose_cc = (builtin.os.tag != .wasi or builtin.link_libc) and |
| | 791 | EnvVar.ZIG_VERBOSE_CC.isSet(); |
| 802 | var verbose_air = false; | 792 | var verbose_air = false; |
| 803 | var verbose_intern_pool = false; | 793 | var verbose_intern_pool = false; |
| 804 | var verbose_generic_instances = false; | 794 | var verbose_generic_instances = false; |
| ... | @@ -892,15 +882,15 @@ fn buildOutputType( | ... | @@ -892,15 +882,15 @@ fn buildOutputType( |
| 892 | var each_lib_rpath: ?bool = null; | 882 | var each_lib_rpath: ?bool = null; |
| 893 | var build_id: ?BuildId = null; | 883 | var build_id: ?BuildId = null; |
| 894 | var sysroot: ?[]const u8 = null; | 884 | var sysroot: ?[]const u8 = null; |
| 895 | var libc_paths_file: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIBC"); | 885 | var libc_paths_file: ?[]const u8 = try EnvVar.ZIG_LIBC.get(arena); |
| 896 | var machine_code_model: std.builtin.CodeModel = .default; | 886 | var machine_code_model: std.builtin.CodeModel = .default; |
| 897 | var runtime_args_start: ?usize = null; | 887 | var runtime_args_start: ?usize = null; |
| 898 | var test_filter: ?[]const u8 = null; | 888 | var test_filter: ?[]const u8 = null; |
| 899 | var test_name_prefix: ?[]const u8 = null; | 889 | var test_name_prefix: ?[]const u8 = null; |
| 900 | var test_runner_path: ?[]const u8 = null; | 890 | var test_runner_path: ?[]const u8 = null; |
| 901 | var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR"); | 891 | var override_local_cache_dir: ?[]const u8 = try EnvVar.ZIG_LOCAL_CACHE_DIR.get(arena); |
| 902 | var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR"); | 892 | var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena); |
| 903 | var override_lib_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIB_DIR"); | 893 | var override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena); |
| 904 | var main_mod_path: ?[]const u8 = null; | 894 | var main_mod_path: ?[]const u8 = null; |
| 905 | var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no; | 895 | var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no; |
| 906 | var subsystem: ?std.Target.SubSystem = null; | 896 | var subsystem: ?std.Target.SubSystem = null; |
| ... | @@ -960,7 +950,7 @@ fn buildOutputType( | ... | @@ -960,7 +950,7 @@ fn buildOutputType( |
| 960 | // if it exists, default the color setting to .off | 950 | // if it exists, default the color setting to .off |
| 961 | // explicit --color arguments will still override this setting. | 951 | // explicit --color arguments will still override this setting. |
| 962 | // Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162 | 952 | // Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162 |
| 963 | color = if (builtin.os.tag == .wasi or std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto; | 953 | color = if (builtin.os.tag == .wasi or EnvVar.NO_COLOR.isSet()) .off else .auto; |
| 964 | | 954 | |
| 965 | switch (arg_mode) { | 955 | switch (arg_mode) { |
| 966 | .build, .translate_c, .zig_test, .run => { | 956 | .build, .translate_c, .zig_test, .run => { |
| ... | @@ -4059,18 +4049,18 @@ fn runOrTest( | ... | @@ -4059,18 +4049,18 @@ fn runOrTest( |
| 4059 | if (runtime_args_start) |i| { | 4049 | if (runtime_args_start) |i| { |
| 4060 | try argv.appendSlice(all_args[i..]); | 4050 | try argv.appendSlice(all_args[i..]); |
| 4061 | } | 4051 | } |
| 4062 | var env_map = try std.process.getEnvMap(arena); | 4052 | var env_map = try process.getEnvMap(arena); |
| 4063 | try env_map.put("ZIG_EXE", self_exe_path); | 4053 | try env_map.put("ZIG_EXE", self_exe_path); |
| 4064 | | 4054 | |
| 4065 | // We do not execve for tests because if the test fails we want to print | 4055 | // We do not execve for tests because if the test fails we want to print |
| 4066 | // the error message and invocation below. | 4056 | // the error message and invocation below. |
| 4067 | if (std.process.can_execv and arg_mode == .run) { | 4057 | if (process.can_execv and arg_mode == .run) { |
| 4068 | // execv releases the locks; no need to destroy the Compilation here. | 4058 | // execv releases the locks; no need to destroy the Compilation here. |
| 4069 | const err = std.process.execve(gpa, argv.items, &env_map); | 4059 | const err = process.execve(gpa, argv.items, &env_map); |
| 4070 | try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc); | 4060 | try warnAboutForeignBinaries(arena, arg_mode, target_info, link_libc); |
| 4071 | const cmd = try std.mem.join(arena, " ", argv.items); | 4061 | const cmd = try std.mem.join(arena, " ", argv.items); |
| 4072 | fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd }); | 4062 | fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd }); |
| 4073 | } else if (std.process.can_spawn) { | 4063 | } else if (process.can_spawn) { |
| 4074 | var child = std.ChildProcess.init(argv.items, gpa); | 4064 | var child = std.ChildProcess.init(argv.items, gpa); |
| 4075 | child.env_map = &env_map; | 4065 | child.env_map = &env_map; |
| 4076 | child.stdin_behavior = .Inherit; | 4066 | child.stdin_behavior = .Inherit; |
| ... | @@ -4489,7 +4479,7 @@ fn cmdRc(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { | ... | @@ -4489,7 +4479,7 @@ fn cmdRc(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 4489 | try stdout_writer.print("{s}\n\n", .{argv.items[argv.items.len - 1]}); | 4479 | try stdout_writer.print("{s}\n\n", .{argv.items[argv.items.len - 1]}); |
| 4490 | } | 4480 | } |
| 4491 | | 4481 | |
| 4492 | if (std.process.can_spawn) { | 4482 | if (process.can_spawn) { |
| 4493 | var result = std.ChildProcess.exec(.{ | 4483 | var result = std.ChildProcess.exec(.{ |
| 4494 | .allocator = gpa, | 4484 | .allocator = gpa, |
| 4495 | .argv = argv.items, | 4485 | .argv = argv.items, |
| ... | @@ -4922,7 +4912,7 @@ pub const usage_build = | ... | @@ -4922,7 +4912,7 @@ pub const usage_build = |
| 4922 | | 4912 | |
| 4923 | pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { | 4913 | pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 4924 | const work_around_btrfs_bug = builtin.os.tag == .linux and | 4914 | const work_around_btrfs_bug = builtin.os.tag == .linux and |
| 4925 | std.process.hasEnvVarConstant("ZIG_BTRFS_WORKAROUND"); | 4915 | EnvVar.ZIG_BTRFS_WORKAROUND.isSet(); |
| 4926 | var color: Color = .auto; | 4916 | var color: Color = .auto; |
| 4927 | | 4917 | |
| 4928 | // We want to release all the locks before executing the child process, so we make a nice | 4918 | // We want to release all the locks before executing the child process, so we make a nice |
| ... | @@ -4931,10 +4921,10 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -4931,10 +4921,10 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 4931 | const self_exe_path = try introspect.findZigExePath(arena); | 4921 | const self_exe_path = try introspect.findZigExePath(arena); |
| 4932 | | 4922 | |
| 4933 | var build_file: ?[]const u8 = null; | 4923 | var build_file: ?[]const u8 = null; |
| 4934 | var override_lib_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIB_DIR"); | 4924 | var override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena); |
| 4935 | var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR"); | 4925 | var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena); |
| 4936 | var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR"); | 4926 | var override_local_cache_dir: ?[]const u8 = try EnvVar.ZIG_LOCAL_CACHE_DIR.get(arena); |
| 4937 | var override_build_runner: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_BUILD_RUNNER"); | 4927 | var override_build_runner: ?[]const u8 = try EnvVar.ZIG_BUILD_RUNNER.get(arena); |
| 4938 | var child_argv = std.ArrayList([]const u8).init(arena); | 4928 | var child_argv = std.ArrayList([]const u8).init(arena); |
| 4939 | var reference_trace: ?u32 = null; | 4929 | var reference_trace: ?u32 = null; |
| 4940 | var debug_compile_errors = false; | 4930 | var debug_compile_errors = false; |
| ... | @@ -5292,7 +5282,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -5292,7 +5282,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 5292 | break :argv child_argv.items; | 5282 | break :argv child_argv.items; |
| 5293 | }; | 5283 | }; |
| 5294 | | 5284 | |
| 5295 | if (std.process.can_spawn) { | 5285 | if (process.can_spawn) { |
| 5296 | var child = std.ChildProcess.init(child_argv, gpa); | 5286 | var child = std.ChildProcess.init(child_argv, gpa); |
| 5297 | child.stdin_behavior = .Inherit; | 5287 | child.stdin_behavior = .Inherit; |
| 5298 | child.stdout_behavior = .Inherit; | 5288 | child.stdout_behavior = .Inherit; |
| ... | @@ -7003,9 +6993,9 @@ fn cmdFetch( | ... | @@ -7003,9 +6993,9 @@ fn cmdFetch( |
| 7003 | ) !void { | 6993 | ) !void { |
| 7004 | const color: Color = .auto; | 6994 | const color: Color = .auto; |
| 7005 | const work_around_btrfs_bug = builtin.os.tag == .linux and | 6995 | const work_around_btrfs_bug = builtin.os.tag == .linux and |
| 7006 | std.process.hasEnvVarConstant("ZIG_BTRFS_WORKAROUND"); | 6996 | EnvVar.ZIG_BTRFS_WORKAROUND.isSet(); |
| 7007 | var opt_path_or_url: ?[]const u8 = null; | 6997 | var opt_path_or_url: ?[]const u8 = null; |
| 7008 | var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR"); | 6998 | var override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena); |
| 7009 | var debug_hash: bool = false; | 6999 | var debug_hash: bool = false; |
| 7010 | | 7000 | |
| 7011 | { | 7001 | { |