authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-04 12:26:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-04 15:03:22-04:00
loge5d479b06e74e04b3ef3108e6098424b2130cbe5
tree6e13e6db93f55d9cac22734b98ab6d71d742db7b
parent52db13738b3fca0ad5d83476e584be1d61a1428f

detect an endless loop when trying to detect native libc installation

closes #4810

4 files changed, 23 insertions(+), 0 deletions(-)

src-self-hosted/libc_installation.zig+19
......@@ -32,6 +32,7 @@ pub const LibCInstallation = struct {
3232 LibCKernel32LibNotFound,
3333 UnsupportedArchitecture,
3434 WindowsSdkNotFound,
35 ZigIsTheCCompiler,
3536 };
3637
3738 pub fn parse(
......@@ -229,10 +230,19 @@ pub const LibCInstallation = struct {
229230 "-xc",
230231 dev_null,
231232 };
233 var env_map = try std.process.getEnvMap(allocator);
234 defer env_map.deinit();
235
236 // Detect infinite loops.
237 const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS";
238 if (env_map.get(inf_loop_env_key) != null) return error.ZigIsTheCCompiler;
239 try env_map.set(inf_loop_env_key, "1");
240
232241 const exec_res = std.ChildProcess.exec(.{
233242 .allocator = allocator,
234243 .argv = &argv,
235244 .max_output_bytes = 1024 * 1024,
245 .env_map = &env_map,
236246 // Some C compilers, such as Clang, are known to rely on argv[0] to find the path
237247 // to their own executable, without even bothering to resolve PATH. This results in the message:
238248 // error: unable to execute command: Executable "" doesn't exist!
......@@ -518,10 +528,19 @@ fn ccPrintFileName(args: CCPrintFileNameOptions) ![:0]u8 {
518528 defer allocator.free(arg1);
519529 const argv = [_][]const u8{ cc_exe, arg1 };
520530
531 var env_map = try std.process.getEnvMap(allocator);
532 defer env_map.deinit();
533
534 // Detect infinite loops.
535 const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS";
536 if (env_map.get(inf_loop_env_key) != null) return error.ZigIsTheCCompiler;
537 try env_map.set(inf_loop_env_key, "1");
538
521539 const exec_res = std.ChildProcess.exec(.{
522540 .allocator = allocator,
523541 .argv = &argv,
524542 .max_output_bytes = 1024 * 1024,
543 .env_map = &env_map,
525544 // Some C compilers, such as Clang, are known to rely on argv[0] to find the path
526545 // to their own executable, without even bothering to resolve PATH. This results in the message:
527546 // error: unable to execute command: Executable "" doesn't exist!
src-self-hosted/stage2.zig+2
......@@ -115,6 +115,7 @@ const Error = extern enum {
115115 InvalidOperatingSystemVersion,
116116 UnknownClangOption,
117117 NestedResponseFile,
118 ZigIsTheCCompiler,
118119};
119120
120121const FILE = std.c.FILE;
......@@ -868,6 +869,7 @@ export fn stage2_libc_find_native(stage1_libc: *Stage2LibCInstallation) Error {
868869 error.LibCKernel32LibNotFound => return .LibCKernel32LibNotFound,
869870 error.UnsupportedArchitecture => return .UnsupportedArchitecture,
870871 error.WindowsSdkNotFound => return .WindowsSdkNotFound,
872 error.ZigIsTheCCompiler => return .ZigIsTheCCompiler,
871873 };
872874 stage1_libc.initFromStage2(libc);
873875 return .None;
src/error.cpp+1
......@@ -85,6 +85,7 @@ const char *err_str(Error err) {
8585 case ErrorInvalidOperatingSystemVersion: return "invalid operating system version";
8686 case ErrorUnknownClangOption: return "unknown Clang option";
8787 case ErrorNestedResponseFile: return "nested response file";
88 case ErrorZigIsTheCCompiler: return "Zig was not provided with libc installation information, and so it does not know where the libc paths are on the system. Zig attempted to use the system C compiler to find out where the libc paths are, but discovered that Zig is being used as the system C compiler.";
8889 }
8990 return "(invalid error)";
9091}
src/stage2.h+1
......@@ -107,6 +107,7 @@ enum Error {
107107 ErrorInvalidOperatingSystemVersion,
108108 ErrorUnknownClangOption,
109109 ErrorNestedResponseFile,
110 ErrorZigIsTheCCompiler,
110111};
111112
112113// ABI warning