Skip to content

Commit 9cff3c5

Browse files
committed
add translate-c CLI args
1 parent c6e3138 commit 9cff3c5

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/Compilation.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6140,6 +6140,20 @@ pub fn tmpFilePath(comp: Compilation, ally: Allocator, suffix: []const u8) error
61406140
}
61416141
}
61426142

6143+
pub fn addTranslateCCArgs(
6144+
comp: *Compilation,
6145+
arena: Allocator,
6146+
argv: *std.ArrayList([]const u8),
6147+
ext: FileExt,
6148+
out_dep_path: ?[]const u8,
6149+
owner_mod: *Package.Module,
6150+
) !void {
6151+
try argv.appendSlice(&.{ "-x", "c" });
6152+
try comp.addCCArgs(arena, argv, ext, out_dep_path, owner_mod);
6153+
// This gives us access to preprocessing entities, presumably at the cost of performance.
6154+
try argv.appendSlice(&.{ "-Xclang", "-detailed-preprocessing-record" });
6155+
}
6156+
61436157
/// Add common C compiler args between translate-c and C object compilation.
61446158
pub fn addCCArgs(
61456159
comp: *const Compilation,

src/main.zig

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,9 +4438,22 @@ fn cmdTranslateC(
44384438
assert(comp.c_source_files.len == 1);
44394439
const c_source_file = comp.c_source_files[0];
44404440

4441-
var argv: std.ArrayListUnmanaged([]const u8) = .empty;
4442-
try argv.append(arena, c_source_file.src_path);
4441+
var zig_cache_tmp_dir = try comp.dirs.local_cache.handle.makeOpenPath("tmp", .{});
4442+
defer zig_cache_tmp_dir.close();
4443+
4444+
const ext = Compilation.classifyFileExt(c_source_file.src_path);
4445+
const out_dep_path: ?[]const u8 = blk: {
4446+
if (comp.disable_c_depfile) break :blk null;
4447+
const c_src_basename = fs.path.basename(c_source_file.src_path);
4448+
const dep_basename = try std.fmt.allocPrint(arena, "{s}.d", .{c_src_basename});
4449+
const out_dep_path = try comp.tmpFilePath(arena, dep_basename);
4450+
break :blk out_dep_path;
4451+
};
44434452

4453+
var argv = std.ArrayList([]const u8).init(arena);
4454+
try argv.append("arocc");
4455+
try comp.addTranslateCCArgs(arena, &argv, ext, out_dep_path, comp.root_mod);
4456+
try argv.append(c_source_file.src_path);
44444457
if (comp.verbose_cc) Compilation.dump_argv(argv.items);
44454458

44464459
try jitCmd(comp.gpa, arena, argv.items, .{
@@ -4449,6 +4462,23 @@ fn cmdTranslateC(
44494462
.depend_on_aro = true,
44504463
.progress_node = prog_node,
44514464
});
4465+
4466+
if (out_dep_path) |dep_file_path| {
4467+
const dep_basename = fs.path.basename(dep_file_path);
4468+
// Add the files depended on to the cache system.
4469+
//man.addDepFilePost(zig_cache_tmp_dir, dep_basename) catch |err| switch (err) {
4470+
// error.FileNotFound => {
4471+
// // Clang didn't emit the dep file; nothing to add to the manifest.
4472+
// break :add_deps;
4473+
// },
4474+
// else => |e| return e,
4475+
//};
4476+
// Just to save disk space, we delete the file because it is never needed again.
4477+
zig_cache_tmp_dir.deleteFile(dep_basename) catch |err| {
4478+
warn("failed to delete '{s}': {t}", .{ dep_file_path, err });
4479+
};
4480+
}
4481+
44524482
return cleanExit();
44534483
}
44544484

@@ -5361,12 +5391,13 @@ fn jitCmd(
53615391
child_argv.appendSliceAssumeCapacity(args);
53625392

53635393
if (process.can_execv and options.capture == null) {
5394+
if (EnvVar.ZIG_DEBUG_CMD.isSet()) {
5395+
const cmd = try std.mem.join(arena, " ", child_argv.items);
5396+
std.debug.print("{s}\n", .{cmd});
5397+
}
53645398
const err = process.execv(gpa, child_argv.items);
53655399
const cmd = try std.mem.join(arena, " ", child_argv.items);
5366-
fatal("the following command failed to execve with '{s}':\n{s}", .{
5367-
@errorName(err),
5368-
cmd,
5369-
});
5400+
fatal("the following command failed to execve with '{t}':\n{s}", .{ err, cmd });
53705401
}
53715402

53725403
if (!process.can_spawn) {

0 commit comments

Comments
 (0)