authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-01 00:34:30-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:13-07:00
log8b2d872020bf8139404d0655e5ab70792cc67873
treeecfe266b968cb13d860daae80275ea3df0e1e387
parent0e078790feaf49964d7a0da3042117ebd10de13b

fix std.Build.TranslateCStep


3 files changed, 32 insertions(+), 25 deletions(-)

lib/std/Build.zig+1-1
......@@ -1453,7 +1453,7 @@ pub fn execFromStep(b: *Build, argv: []const []const u8, s: *Step, prog_node: *s
14531453 const header = @ptrCast(*align(1) const Header, buf[0..@sizeOf(Header)]);
14541454 const header_and_msg_len = header.bytes_len + @sizeOf(Header);
14551455 if (buf.len >= header_and_msg_len) {
1456 const body = buf[@sizeOf(Header)..];
1456 const body = buf[@sizeOf(Header)..][0..header.bytes_len];
14571457 switch (header.tag) {
14581458 .zig_version => {
14591459 if (!mem.eql(u8, builtin.zig_version_string, body)) {
lib/std/Build/TranslateCStep.zig+2-2
......@@ -97,6 +97,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
9797 try argv_list.append("-lc");
9898
9999 try argv_list.append("--enable-cache");
100 try argv_list.append("--listen=-");
100101
101102 if (!self.target.isNative()) {
102103 try argv_list.append("-target");
......@@ -120,8 +121,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
120121
121122 try argv_list.append(self.source.getPath(self.builder));
122123
123 const output_path_nl = try self.builder.execFromStep(argv_list.items, &self.step, prog_node);
124 const output_path = mem.trimRight(u8, output_path_nl, "\r\n");
124 const output_path = try self.builder.execFromStep(argv_list.items, &self.step, prog_node);
125125
126126 self.out_basename = fs.path.basename(output_path);
127127 const output_dir = fs.path.dirname(output_path).?;
src/main.zig+29-22
......@@ -3287,10 +3287,6 @@ fn buildOutputType(
32873287 if (show_builtin) {
32883288 return std.io.getStdOut().writeAll(try comp.generateBuiltinZigSource(arena));
32893289 }
3290 if (arg_mode == .translate_c) {
3291 return cmdTranslateC(comp, arena, have_enable_cache);
3292 }
3293
32943290 switch (listen) {
32953291 .none => {},
32963292 .stdio => {
......@@ -3332,6 +3328,10 @@ fn buildOutputType(
33323328 },
33333329 }
33343330
3331 if (arg_mode == .translate_c) {
3332 return cmdTranslateC(comp, arena, null);
3333 }
3334
33353335 const hook: AfterUpdateHook = blk: {
33363336 if (!have_enable_cache)
33373337 break :blk .none;
......@@ -3532,12 +3532,7 @@ fn serve(
35323532) !void {
35333533 const gpa = comp.gpa;
35343534
3535 try serveMessage(out, .{
3536 .tag = .zig_version,
3537 .bytes_len = build_options.version.len,
3538 }, &.{
3539 build_options.version,
3540 });
3535 try serveStringMessage(out, .zig_version, build_options.version);
35413536
35423537 var child_pid: ?i32 = null;
35433538 var receive_fifo = std.fifo.LinearFifo(u8, .Dynamic).init(gpa);
......@@ -3570,6 +3565,17 @@ fn serve(
35703565 .update => {
35713566 assert(main_progress_node.recently_updated_child == null);
35723567 tracy.frameMark();
3568
3569 if (arg_mode == .translate_c) {
3570 var arena_instance = std.heap.ArenaAllocator.init(gpa);
3571 defer arena_instance.deinit();
3572 const arena = arena_instance.allocator();
3573 var output_path: []const u8 = undefined;
3574 try cmdTranslateC(comp, arena, &output_path);
3575 try serveStringMessage(out, .emit_bin_path, output_path);
3576 continue;
3577 }
3578
35733579 if (comp.bin_file.options.output_mode == .Exe) {
35743580 try comp.makeBinFileWritable();
35753581 }
......@@ -3746,16 +3752,17 @@ fn serveUpdateResults(out: fs.File, comp: *Compilation) !void {
37463752 } else if (comp.bin_file.options.emit) |emit| {
37473753 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});
37483754 defer gpa.free(full_path);
3749
3750 try serveMessage(out, .{
3751 .tag = .emit_bin_path,
3752 .bytes_len = @intCast(u32, full_path.len),
3753 }, &.{
3754 full_path,
3755 });
3755 try serveStringMessage(out, .emit_bin_path, full_path);
37563756 }
37573757}
37583758
3759fn serveStringMessage(out: fs.File, tag: std.zig.Server.Message.Tag, s: []const u8) !void {
3760 try serveMessage(out, .{
3761 .tag = tag,
3762 .bytes_len = @intCast(u32, s.len),
3763 }, &.{s});
3764}
3765
37593766fn receiveMessage(in: fs.File, fifo: *std.fifo.LinearFifo(u8, .Dynamic)) !std.zig.Client.Message.Header {
37603767 const Header = std.zig.Client.Message.Header;
37613768
......@@ -4100,7 +4107,7 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
41004107 }
41014108}
41024109
4103fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void {
4110fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8) !void {
41044111 if (!build_options.have_llvm)
41054112 fatal("cannot translate-c: compiler built without LLVM extensions", .{});
41064113
......@@ -4111,7 +4118,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
41114118
41124119 var man: Cache.Manifest = comp.obtainCObjectCacheManifest();
41134120 man.want_shared_lock = false;
4114 defer if (enable_cache) man.deinit();
4121 defer if (output_path != null) man.deinit();
41154122
41164123 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
41174124 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {
......@@ -4169,6 +4176,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
41694176 error.OutOfMemory => return error.OutOfMemory,
41704177 error.ASTUnitFailure => fatal("clang API returned errors but due to a clang bug, it is not exposing the errors for zig to see. For more details: https://github.com/ziglang/zig/issues/4455", .{}),
41714178 error.SemanticAnalyzeFail => {
4179 // TODO convert these to zig errors
41724180 for (clang_errors) |clang_err| {
41734181 std.debug.print("{s}:{d}:{d}: {s}\n", .{
41744182 if (clang_err.filename_ptr) |p| p[0..clang_err.filename_len] else "(no file)",
......@@ -4213,12 +4221,11 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
42134221 break :digest digest;
42144222 };
42154223
4216 if (enable_cache) {
4224 if (output_path) |out_path| {
42174225 const full_zig_path = try comp.local_cache_directory.join(arena, &[_][]const u8{
42184226 "o", &digest, translated_zig_basename,
42194227 });
4220 try io.getStdOut().writer().print("{s}\n", .{full_zig_path});
4221 return cleanExit();
4228 out_path.* = full_zig_path;
42224229 } else {
42234230 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });
42244231 const zig_file = comp.local_cache_directory.handle.openFile(out_zig_path, .{}) catch |err| {