From 4f437e4d2450dd2aca719ec0dcafa5aed141ca01 Mon Sep 17 00:00:00 2001 From: mlugg Date: Tue, 20 Aug 2024 20:56:53 +0100 Subject: [PATCH 1/2] translate-c: populate file system inputs on error Resolves: #20648 --- src/main.zig | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main.zig b/src/main.zig index 1267baf9a9..226ec79467 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4526,7 +4526,12 @@ fn cmdTranslateC( Compilation.dump_argv(argv.items); } - const formatted = switch (comp.config.c_frontend) { + const Result = union(enum) { + success: []const u8, + error_bundle: std.zig.ErrorBundle, + }; + + const result: Result = switch (comp.config.c_frontend) { .aro => f: { var stdout: []u8 = undefined; try jitCmd(comp.gpa, arena, argv.items, .{ @@ -4536,7 +4541,7 @@ fn cmdTranslateC( .capture = &stdout, .progress_node = prog_node, }); - break :f stdout; + break :f .{ .success = stdout }; }, .clang => f: { if (!build_options.have_llvm) unreachable; @@ -4564,31 +4569,43 @@ fn cmdTranslateC( c_headers_dir_path_z, ) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, - error.SemanticAnalyzeFail => { - if (fancy_output) |p| { - p.errors = errors; - return; - } else { - errors.renderToStdErr(color.renderOptions()); - process.exit(1); - } - }, + error.SemanticAnalyzeFail => break :f .{ .error_bundle = errors }, }; defer tree.deinit(comp.gpa); - break :f try tree.render(arena); + break :f .{ .success = try tree.render(arena) }; }, }; - if (out_dep_path) |dep_file_path| { + if (out_dep_path) |dep_file_path| add_deps: { const dep_basename = fs.path.basename(dep_file_path); // Add the files depended on to the cache system. - try man.addDepFilePost(zig_cache_tmp_dir, dep_basename); + man.addDepFilePost(zig_cache_tmp_dir, dep_basename) catch |err| switch (err) { + error.FileNotFound => { + // Clang didn't emit the dep file; nothing to add to the manifest. + break :add_deps; + }, + else => |e| return e, + }; // Just to save disk space, we delete the file because it is never needed again. zig_cache_tmp_dir.deleteFile(dep_basename) catch |err| { warn("failed to delete '{s}': {s}", .{ dep_file_path, @errorName(err) }); }; } + const formatted = switch (result) { + .success => |formatted| formatted, + .error_bundle => |eb| { + if (file_system_inputs) |buf| try man.populateFileSystemInputs(buf); + if (fancy_output) |p| { + p.errors = eb; + return; + } else { + eb.renderToStdErr(color.renderOptions()); + process.exit(1); + } + }, + }; + const bin_digest = man.finalBin(); const hex_digest = Cache.binToHex(bin_digest); From a0b03d7ffffdf89b14ad6f1d5cd6537935996338 Mon Sep 17 00:00:00 2001 From: mlugg Date: Tue, 20 Aug 2024 21:04:42 +0100 Subject: [PATCH 2/2] std.Build.Step.TranslateC: propagate target, optimize, link_libc to created module --- lib/std/Build/Step/TranslateC.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig index 77fd3e39ad..9ef5f7acdf 100644 --- a/lib/std/Build/Step/TranslateC.zig +++ b/lib/std/Build/Step/TranslateC.zig @@ -29,7 +29,7 @@ pub const Options = struct { pub fn create(owner: *std.Build, options: Options) *TranslateC { const translate_c = owner.allocator.create(TranslateC) catch @panic("OOM"); const source = options.root_source_file.dupe(owner); - translate_c.* = TranslateC{ + translate_c.* = .{ .step = Step.init(.{ .id = base_id, .name = "translate-c", @@ -42,7 +42,7 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC { .out_basename = undefined, .target = options.target, .optimize = options.optimize, - .output_file = std.Build.GeneratedFile{ .step = &translate_c.step }, + .output_file = .{ .step = &translate_c.step }, .link_libc = options.link_libc, .use_clang = options.use_clang, }; @@ -89,6 +89,9 @@ pub fn addModule(translate_c: *TranslateC, name: []const u8) *std.Build.Module { pub fn createModule(translate_c: *TranslateC) *std.Build.Module { return translate_c.step.owner.createModule(.{ .root_source_file = translate_c.getOutput(), + .target = translate_c.target, + .optimize = translate_c.optimize, + .link_libc = translate_c.link_libc, }); }