| ... | ... | @@ -35,14 +35,13 @@ pub fn openBinFilePath( |
| 35 | 35 | sub_path: []const u8, |
| 36 | 36 | options: Options, |
| 37 | 37 | ) !*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) }); |
| 39 | 39 | errdefer file.close(); |
| 40 | 40 | |
| 41 | 41 | if (options.cbe) { |
| 42 | 42 | var bin_file = try allocator.create(File.C); |
| 43 | 43 | errdefer allocator.destroy(bin_file); |
| 44 | 44 | bin_file.* = try openCFile(allocator, file, options); |
| 45 | | bin_file.owns_file_handle = true; |
| 46 | 45 | return &bin_file.base; |
| 47 | 46 | } else { |
| 48 | 47 | var bin_file = try allocator.create(File.Elf); |
| ... | ... | @@ -88,17 +87,14 @@ pub fn writeFilePath( |
| 88 | 87 | } |
| 89 | 88 | |
| 90 | 89 | pub fn openCFile(allocator: *Allocator, file: fs.File, options: Options) !File.C { |
| 91 | | var self: File.C = .{ |
| 90 | return File.C{ |
| 92 | 91 | .allocator = allocator, |
| 93 | 92 | .file = file, |
| 94 | 93 | .options = options, |
| 95 | | .owns_file_handle = false, |
| 96 | 94 | .main = std.ArrayList(u8).init(allocator), |
| 97 | 95 | .header = std.ArrayList(u8).init(allocator), |
| 98 | 96 | .called = std.StringHashMap(void).init(allocator), |
| 99 | 97 | }; |
| 100 | | errdefer self.deinit(); |
| 101 | | return self; |
| 102 | 98 | } |
| 103 | 99 | |
| 104 | 100 | /// Attempts incremental linking, if the file already exists. |
| ... | ... | @@ -127,7 +123,7 @@ pub const File = struct { |
| 127 | 123 | pub fn makeWritable(base: *File, dir: fs.Dir, sub_path: []const u8) !void { |
| 128 | 124 | switch (base.tag) { |
| 129 | 125 | .Elf => return @fieldParentPtr(Elf, "base", base).makeWritable(dir, sub_path), |
| 130 | | .C => return @fieldParentPtr(C, "base", base).makeWritable(dir, sub_path), |
| 126 | .C => {}, |
| 131 | 127 | else => unreachable, |
| 132 | 128 | } |
| 133 | 129 | } |
| ... | ... | @@ -210,31 +206,18 @@ pub const File = struct { |
| 210 | 206 | header: std.ArrayList(u8), |
| 211 | 207 | main: std.ArrayList(u8), |
| 212 | 208 | file: ?fs.File, |
| 213 | | owns_file_handle: bool, |
| 214 | 209 | options: Options, |
| 215 | 210 | called: std.StringHashMap(void), |
| 216 | 211 | need_stddef: bool = false, |
| 217 | 212 | need_stdint: bool = false, |
| 218 | 213 | need_noreturn: bool = false, |
| 219 | 214 | |
| 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 | | |
| 230 | 215 | pub fn deinit(self: *File.C) void { |
| 231 | 216 | self.main.deinit(); |
| 232 | 217 | self.header.deinit(); |
| 233 | 218 | 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(); |
| 238 | 221 | } |
| 239 | 222 | |
| 240 | 223 | pub fn updateDecl(self: *File.C, module: *Module, decl: *Module.Decl) !void { |