authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-18 14:26:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-24 20:01:18-07:00
log01d993b2305961100d2e898270b7a6be832d0fa7
treefe4c303c821978dd449325983ada3a5b7a496b24
parent8e27821b604ebbeadecc42efa4e42098f6662f10

add translate-c CLI args


2 files changed, 51 insertions(+), 6 deletions(-)

src/Compilation.zig+14
......@@ -6600,6 +6600,20 @@ pub fn tmpFilePath(comp: Compilation, ally: Allocator, suffix: []const u8) error
66006600 }
66016601}
66026602
6603pub fn addTranslateCCArgs(
6604 comp: *Compilation,
6605 arena: Allocator,
6606 argv: *std.ArrayList([]const u8),
6607 ext: FileExt,
6608 out_dep_path: ?[]const u8,
6609 owner_mod: *Package.Module,
6610) !void {
6611 try argv.appendSlice(&.{ "-x", "c" });
6612 try comp.addCCArgs(arena, argv, ext, out_dep_path, owner_mod);
6613 // This gives us access to preprocessing entities, presumably at the cost of performance.
6614 try argv.appendSlice(&.{ "-Xclang", "-detailed-preprocessing-record" });
6615}
6616
66036617/// Add common C compiler args between translate-c and C object compilation.
66046618pub fn addCCArgs(
66056619 comp: *const Compilation,
src/main.zig+37-6
......@@ -4542,9 +4542,22 @@ fn cmdTranslateC(
45424542 assert(comp.c_source_files.len == 1);
45434543 const c_source_file = comp.c_source_files[0];
45444544
4545 var argv: std.ArrayListUnmanaged([]const u8) = .empty;
4546 try argv.append(arena, c_source_file.src_path);
4545 var zig_cache_tmp_dir = try comp.dirs.local_cache.handle.makeOpenPath("tmp", .{});
4546 defer zig_cache_tmp_dir.close();
4547
4548 const ext = Compilation.classifyFileExt(c_source_file.src_path);
4549 const out_dep_path: ?[]const u8 = blk: {
4550 if (comp.disable_c_depfile) break :blk null;
4551 const c_src_basename = fs.path.basename(c_source_file.src_path);
4552 const dep_basename = try std.fmt.allocPrint(arena, "{s}.d", .{c_src_basename});
4553 const out_dep_path = try comp.tmpFilePath(arena, dep_basename);
4554 break :blk out_dep_path;
4555 };
45474556
4557 var argv = std.ArrayList([]const u8).init(arena);
4558 try argv.append("arocc");
4559 try comp.addTranslateCCArgs(arena, &argv, ext, out_dep_path, comp.root_mod);
4560 try argv.append(c_source_file.src_path);
45484561 if (comp.verbose_cc) Compilation.dump_argv(argv.items);
45494562
45504563 try jitCmd(comp.gpa, arena, argv.items, .{
......@@ -4553,6 +4566,23 @@ fn cmdTranslateC(
45534566 .depend_on_aro = true,
45544567 .progress_node = prog_node,
45554568 });
4569
4570 if (out_dep_path) |dep_file_path| {
4571 const dep_basename = fs.path.basename(dep_file_path);
4572 // Add the files depended on to the cache system.
4573 //man.addDepFilePost(zig_cache_tmp_dir, dep_basename) catch |err| switch (err) {
4574 // error.FileNotFound => {
4575 // // Clang didn't emit the dep file; nothing to add to the manifest.
4576 // break :add_deps;
4577 // },
4578 // else => |e| return e,
4579 //};
4580 // Just to save disk space, we delete the file because it is never needed again.
4581 zig_cache_tmp_dir.deleteFile(dep_basename) catch |err| {
4582 warn("failed to delete '{s}': {t}", .{ dep_file_path, err });
4583 };
4584 }
4585
45564586 return cleanExit();
45574587}
45584588
......@@ -5512,12 +5542,13 @@ fn jitCmd(
55125542 child_argv.appendSliceAssumeCapacity(args);
55135543
55145544 if (process.can_execv and options.capture == null) {
5545 if (EnvVar.ZIG_DEBUG_CMD.isSet()) {
5546 const cmd = try std.mem.join(arena, " ", child_argv.items);
5547 std.debug.print("{s}\n", .{cmd});
5548 }
55155549 const err = process.execv(gpa, child_argv.items);
55165550 const cmd = try std.mem.join(arena, " ", child_argv.items);
5517 fatal("the following command failed to execve with '{s}':\n{s}", .{
5518 @errorName(err),
5519 cmd,
5520 });
5551 fatal("the following command failed to execve with '{t}':\n{s}", .{ err, cmd });
55215552 }
55225553
55235554 if (!process.can_spawn) {