authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-07 23:19:25-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-07 23:19:25-04:00
log7a6104929b2d140235597d866dc0fabc93df036f
tree96d96d5b667b0f86377e3549b72356d40546c264
parent173e6712417c8102ff6bbd28fd964b89eda14f36
signature Commit is signed but in an unrecognized format.

CBE: truncate output file


1 files changed, 5 insertions(+), 22 deletions(-)

src-self-hosted/link.zig+5-22
......@@ -35,14 +35,13 @@ pub fn openBinFilePath(
3535 sub_path: []const u8,
3636 options: Options,
3737) !*File {
38 const file = try dir.createFile(sub_path, .{ .truncate = false, .read = true, .mode = determineMode(options) });
38 const file = try dir.createFile(sub_path, .{ .truncate = options.cbe, .read = true, .mode = determineMode(options) });
3939 errdefer file.close();
4040
4141 if (options.cbe) {
4242 var bin_file = try allocator.create(File.C);
4343 errdefer allocator.destroy(bin_file);
4444 bin_file.* = try openCFile(allocator, file, options);
45 bin_file.owns_file_handle = true;
4645 return &bin_file.base;
4746 } else {
4847 var bin_file = try allocator.create(File.Elf);
......@@ -88,17 +87,14 @@ pub fn writeFilePath(
8887}
8988
9089pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C {
91 var self: File.C = .{
90 return File.C{
9291 .allocator = allocator,
9392 .file = file,
9493 .options = options,
95 .owns_file_handle = false,
9694 .main = std.ArrayList(u8).init(allocator),
9795 .header = std.ArrayList(u8).init(allocator),
9896 .called = std.StringHashMap(void).init(allocator),
9997 };
100 errdefer self.deinit();
101 return self;
10298}
10399
104100/// Attempts incremental linking, if the file already exists.
......@@ -127,7 +123,7 @@ pub const File = struct {
127123 pub fn makeWritable(base: *File, dir: fs.Dir, sub_path: []const u8) !void {
128124 switch (base.tag) {
129125 .Elf => return @fieldParentPtr(Elf, "base", base).makeWritable(dir, sub_path),
130 .C => return @fieldParentPtr(C, "base", base).makeWritable(dir, sub_path),
126 .C => {},
131127 else => unreachable,
132128 }
133129 }
......@@ -210,31 +206,18 @@ pub const File = struct {
210206 header: std.ArrayList(u8),
211207 main: std.ArrayList(u8),
212208 file: ?fs.File,
213 owns_file_handle: bool,
214209 options: Options,
215210 called: std.StringHashMap(void),
216211 need_stddef: bool = false,
217212 need_stdint: bool = false,
218213 need_noreturn: bool = false,
219214
220 pub fn makeWritable(self: *File.C, dir: fs.Dir, sub_path: []const u8) !void {
221 assert(self.owns_file_handle);
222 if (self.file != null) return;
223 self.file = try dir.createFile(sub_path, .{
224 .truncate = false,
225 .read = true,
226 .mode = determineMode(self.options),
227 });
228 }
229
230215 pub fn deinit(self: *File.C) void {
231216 self.main.deinit();
232217 self.header.deinit();
233218 self.called.deinit();
234 if (self.owns_file_handle) {
235 if (self.file) |f|
236 f.close();
237 }
219 if (self.file) |f|
220 f.close();
238221 }
239222
240223 pub fn updateDecl(self: *File.C, module: *Module, decl: *Module.Decl) !void {