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...@@ -1453,7 +1453,7 @@ pub fn execFromStep(b: *Build, argv: []const []const u8, s: *Step, prog_node: *s
1453 const header = @ptrCast(*align(1) const Header, buf[0..@sizeOf(Header)]);1453 const header = @ptrCast(*align(1) const Header, buf[0..@sizeOf(Header)]);
1454 const header_and_msg_len = header.bytes_len + @sizeOf(Header);1454 const header_and_msg_len = header.bytes_len + @sizeOf(Header);
1455 if (buf.len >= header_and_msg_len) {1455 if (buf.len >= header_and_msg_len) {
1456 const body = buf[@sizeOf(Header)..];1456 const body = buf[@sizeOf(Header)..][0..header.bytes_len];
1457 switch (header.tag) {1457 switch (header.tag) {
1458 .zig_version => {1458 .zig_version => {
1459 if (!mem.eql(u8, builtin.zig_version_string, body)) {1459 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 {...@@ -97,6 +97,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
97 try argv_list.append("-lc");97 try argv_list.append("-lc");
9898
99 try argv_list.append("--enable-cache");99 try argv_list.append("--enable-cache");
100 try argv_list.append("--listen=-");
100101
101 if (!self.target.isNative()) {102 if (!self.target.isNative()) {
102 try argv_list.append("-target");103 try argv_list.append("-target");
...@@ -120,8 +121,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -120,8 +121,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
120121
121 try argv_list.append(self.source.getPath(self.builder));122 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 = try self.builder.execFromStep(argv_list.items, &self.step, prog_node);
124 const output_path = mem.trimRight(u8, output_path_nl, "\r\n");
125125
126 self.out_basename = fs.path.basename(output_path);126 self.out_basename = fs.path.basename(output_path);
127 const output_dir = fs.path.dirname(output_path).?;127 const output_dir = fs.path.dirname(output_path).?;
src/main.zig+29-22
...@@ -3287,10 +3287,6 @@ fn buildOutputType(...@@ -3287,10 +3287,6 @@ fn buildOutputType(
3287 if (show_builtin) {3287 if (show_builtin) {
3288 return std.io.getStdOut().writeAll(try comp.generateBuiltinZigSource(arena));3288 return std.io.getStdOut().writeAll(try comp.generateBuiltinZigSource(arena));
3289 }3289 }
3290 if (arg_mode == .translate_c) {
3291 return cmdTranslateC(comp, arena, have_enable_cache);
3292 }
3293
3294 switch (listen) {3290 switch (listen) {
3295 .none => {},3291 .none => {},
3296 .stdio => {3292 .stdio => {
...@@ -3332,6 +3328,10 @@ fn buildOutputType(...@@ -3332,6 +3328,10 @@ fn buildOutputType(
3332 },3328 },
3333 }3329 }
33343330
3331 if (arg_mode == .translate_c) {
3332 return cmdTranslateC(comp, arena, null);
3333 }
3334
3335 const hook: AfterUpdateHook = blk: {3335 const hook: AfterUpdateHook = blk: {
3336 if (!have_enable_cache)3336 if (!have_enable_cache)
3337 break :blk .none;3337 break :blk .none;
...@@ -3532,12 +3532,7 @@ fn serve(...@@ -3532,12 +3532,7 @@ fn serve(
3532) !void {3532) !void {
3533 const gpa = comp.gpa;3533 const gpa = comp.gpa;
35343534
3535 try serveMessage(out, .{3535 try serveStringMessage(out, .zig_version, build_options.version);
3536 .tag = .zig_version,
3537 .bytes_len = build_options.version.len,
3538 }, &.{
3539 build_options.version,
3540 });
35413536
3542 var child_pid: ?i32 = null;3537 var child_pid: ?i32 = null;
3543 var receive_fifo = std.fifo.LinearFifo(u8, .Dynamic).init(gpa);3538 var receive_fifo = std.fifo.LinearFifo(u8, .Dynamic).init(gpa);
...@@ -3570,6 +3565,17 @@ fn serve(...@@ -3570,6 +3565,17 @@ fn serve(
3570 .update => {3565 .update => {
3571 assert(main_progress_node.recently_updated_child == null);3566 assert(main_progress_node.recently_updated_child == null);
3572 tracy.frameMark();3567 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
3573 if (comp.bin_file.options.output_mode == .Exe) {3579 if (comp.bin_file.options.output_mode == .Exe) {
3574 try comp.makeBinFileWritable();3580 try comp.makeBinFileWritable();
3575 }3581 }
...@@ -3746,16 +3752,17 @@ fn serveUpdateResults(out: fs.File, comp: *Compilation) !void {...@@ -3746,16 +3752,17 @@ fn serveUpdateResults(out: fs.File, comp: *Compilation) !void {
3746 } else if (comp.bin_file.options.emit) |emit| {3752 } else if (comp.bin_file.options.emit) |emit| {
3747 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});3753 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});
3748 defer gpa.free(full_path);3754 defer gpa.free(full_path);
37493755 try serveStringMessage(out, .emit_bin_path, full_path);
3750 try serveMessage(out, .{
3751 .tag = .emit_bin_path,
3752 .bytes_len = @intCast(u32, full_path.len),
3753 }, &.{
3754 full_path,
3755 });
3756 }3756 }
3757}3757}
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
3759fn receiveMessage(in: fs.File, fifo: *std.fifo.LinearFifo(u8, .Dynamic)) !std.zig.Client.Message.Header {3766fn receiveMessage(in: fs.File, fifo: *std.fifo.LinearFifo(u8, .Dynamic)) !std.zig.Client.Message.Header {
3760 const Header = std.zig.Client.Message.Header;3767 const Header = std.zig.Client.Message.Header;
37613768
...@@ -4100,7 +4107,7 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void...@@ -4100,7 +4107,7 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
4100 }4107 }
4101}4108}
41024109
4103fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void {4110fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8) !void {
4104 if (!build_options.have_llvm)4111 if (!build_options.have_llvm)
4105 fatal("cannot translate-c: compiler built without LLVM extensions", .{});4112 fatal("cannot translate-c: compiler built without LLVM extensions", .{});
41064113
...@@ -4111,7 +4118,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void...@@ -4111,7 +4118,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
41114118
4112 var man: Cache.Manifest = comp.obtainCObjectCacheManifest();4119 var man: Cache.Manifest = comp.obtainCObjectCacheManifest();
4113 man.want_shared_lock = false;4120 man.want_shared_lock = false;
4114 defer if (enable_cache) man.deinit();4121 defer if (output_path != null) man.deinit();
41154122
4116 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects4123 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
4117 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {4124 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {
...@@ -4169,6 +4176,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void...@@ -4169,6 +4176,7 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
4169 error.OutOfMemory => return error.OutOfMemory,4176 error.OutOfMemory => return error.OutOfMemory,
4170 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", .{}),4177 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", .{}),
4171 error.SemanticAnalyzeFail => {4178 error.SemanticAnalyzeFail => {
4179 // TODO convert these to zig errors
4172 for (clang_errors) |clang_err| {4180 for (clang_errors) |clang_err| {
4173 std.debug.print("{s}:{d}:{d}: {s}\n", .{4181 std.debug.print("{s}:{d}:{d}: {s}\n", .{
4174 if (clang_err.filename_ptr) |p| p[0..clang_err.filename_len] else "(no file)",4182 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...@@ -4213,12 +4221,11 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void
4213 break :digest digest;4221 break :digest digest;
4214 };4222 };
42154223
4216 if (enable_cache) {4224 if (output_path) |out_path| {
4217 const full_zig_path = try comp.local_cache_directory.join(arena, &[_][]const u8{4225 const full_zig_path = try comp.local_cache_directory.join(arena, &[_][]const u8{
4218 "o", &digest, translated_zig_basename,4226 "o", &digest, translated_zig_basename,
4219 });4227 });
4220 try io.getStdOut().writer().print("{s}\n", .{full_zig_path});4228 out_path.* = full_zig_path;
4221 return cleanExit();
4222 } else {4229 } else {
4223 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });4230 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });
4224 const zig_file = comp.local_cache_directory.handle.openFile(out_zig_path, .{}) catch |err| {4231 const zig_file = comp.local_cache_directory.handle.openFile(out_zig_path, .{}) catch |err| {