| ... | @@ -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 | } |
| 3334 | | 3330 | |
| | 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; |
| 3534 | | 3534 | |
| 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 | }); | | |
| 3541 | | 3536 | |
| 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); |
| 3749 | | 3755 | 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 | } |
| 3758 | | 3758 | |
| | 3759 | fn 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 | |
| 3759 | fn receiveMessage(in: fs.File, fifo: *std.fifo.LinearFifo(u8, .Dynamic)) !std.zig.Client.Message.Header { | 3766 | fn 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; |
| 3761 | | 3768 | |
| ... | @@ -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 | } |
| 4102 | | 4109 | |
| 4103 | fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void { | 4110 | fn 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", .{}); |
| 4106 | | 4113 | |
| ... | @@ -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 |
| 4111 | | 4118 | |
| 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(); |
| 4115 | | 4122 | |
| 4116 | man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects | 4123 | 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 | }; |
| 4215 | | 4223 | |
| 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| { |