Commit c9863c0a0c

Andrew Kelley <andrew@ziglang.org>
2021-12-16 03:56:32
CLI: helpful error message when libc requested but not provided
1 parent c8af00c
Changed files (2)
src/Compilation.zig
@@ -1586,6 +1586,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
         // If we need to build glibc for the target, add work items for it.
         // We go through the work queue so that building can be done in parallel.
         if (comp.wantBuildGLibCFromSource()) {
+            if (!target_util.canBuildLibC(comp.getTarget())) return error.LibCUnavailable;
+
             if (glibc.needsCrtiCrtn(comp.getTarget())) {
                 try comp.work_queue.write(&[_]Job{
                     .{ .glibc_crt_file = .crti_o },
@@ -1599,6 +1601,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             });
         }
         if (comp.wantBuildMuslFromSource()) {
+            if (!target_util.canBuildLibC(comp.getTarget())) return error.LibCUnavailable;
+
             try comp.work_queue.ensureUnusedCapacity(6);
             if (musl.needsCrtiCrtn(comp.getTarget())) {
                 comp.work_queue.writeAssumeCapacity(&[_]Job{
@@ -1617,6 +1621,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             });
         }
         if (comp.wantBuildWasiLibcFromSource()) {
+            if (!target_util.canBuildLibC(comp.getTarget())) return error.LibCUnavailable;
+
             const wasi_emulated_libs = comp.bin_file.options.wasi_emulated_libs;
             try comp.work_queue.ensureUnusedCapacity(wasi_emulated_libs.len + 2); // worst-case we need all components
             for (wasi_emulated_libs) |crt_file| {
@@ -1630,6 +1636,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
             });
         }
         if (comp.wantBuildMinGWFromSource()) {
+            if (!target_util.canBuildLibC(comp.getTarget())) return error.LibCUnavailable;
+
             const static_lib_jobs = [_]Job{
                 .{ .mingw_crt_file = .mingw32_lib },
                 .{ .mingw_crt_file = .msvcrt_os_lib },
src/main.zig
@@ -2495,8 +2495,28 @@ fn buildOutputType(
         .debug_compile_errors = debug_compile_errors,
         .enable_link_snapshots = enable_link_snapshots,
         .native_darwin_sdk = native_darwin_sdk,
-    }) catch |err| {
-        fatal("unable to create compilation: {s}", .{@errorName(err)});
+    }) catch |err| switch (err) {
+        error.LibCUnavailable => {
+            const target = target_info.target;
+            const triple_name = try target.zigTriple(arena);
+            std.log.err("unable to find or provide libc for target '{s}'", .{triple_name});
+
+            for (target_util.available_libcs) |t| {
+                if (t.arch == target.cpu.arch and t.os == target.os.tag) {
+                    if (t.os_ver) |os_ver| {
+                        std.log.info("zig can provide libc for related target {s}-{s}.{d}-{s}", .{
+                            @tagName(t.arch), @tagName(t.os), os_ver.major, @tagName(t.abi),
+                        });
+                    } else {
+                        std.log.info("zig can provide libc for related target {s}-{s}-{s}", .{
+                            @tagName(t.arch), @tagName(t.os), @tagName(t.abi),
+                        });
+                    }
+                }
+            }
+            process.exit(1);
+        },
+        else => fatal("unable to create compilation: {s}", .{@errorName(err)}),
     };
     var comp_destroyed = false;
     defer if (!comp_destroyed) comp.destroy();