| ... | @@ -2152,15 +2152,6 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor | ... | @@ -2152,15 +2152,6 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor |
| 2152 | defer main_progress_node.end(); | 2152 | defer main_progress_node.end(); |
| 2153 | if (self.color == .off) progress.terminal = null; | 2153 | if (self.color == .off) progress.terminal = null; |
| 2154 | | 2154 | |
| 2155 | // If we need to write out builtin.zig, it needs to be done before starting | | |
| 2156 | // the AstGen tasks. | | |
| 2157 | if (self.bin_file.options.module) |mod| { | | |
| 2158 | if (mod.job_queued_update_builtin_zig) { | | |
| 2159 | mod.job_queued_update_builtin_zig = false; | | |
| 2160 | try self.updateBuiltinZigFile(mod); | | |
| 2161 | } | | |
| 2162 | } | | |
| 2163 | | | |
| 2164 | // Here we queue up all the AstGen tasks first, followed by C object compilation. | 2155 | // Here we queue up all the AstGen tasks first, followed by C object compilation. |
| 2165 | // We wait until the AstGen tasks are all completed before proceeding to the | 2156 | // We wait until the AstGen tasks are all completed before proceeding to the |
| 2166 | // (at least for now) single-threaded main work queue. However, C object compilation | 2157 | // (at least for now) single-threaded main work queue. However, C object compilation |
| ... | @@ -2185,6 +2176,21 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor | ... | @@ -2185,6 +2176,21 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor |
| 2185 | self.astgen_wait_group.reset(); | 2176 | self.astgen_wait_group.reset(); |
| 2186 | defer self.astgen_wait_group.wait(); | 2177 | defer self.astgen_wait_group.wait(); |
| 2187 | | 2178 | |
| | 2179 | // builtin.zig is handled specially for two reasons: |
| | 2180 | // 1. to avoid race condition of zig processes truncating each other's builtin.zig files |
| | 2181 | // 2. optimization; in the hot path it only incurs a stat() syscall, which happens |
| | 2182 | // in the `astgen_wait_group`. |
| | 2183 | if (self.bin_file.options.module) |mod| { |
| | 2184 | if (mod.job_queued_update_builtin_zig) { |
| | 2185 | mod.job_queued_update_builtin_zig = false; |
| | 2186 | |
| | 2187 | self.astgen_wait_group.start(); |
| | 2188 | try self.thread_pool.spawn(workerUpdateBuiltinZigFile, .{ |
| | 2189 | self, mod, &self.astgen_wait_group, |
| | 2190 | }); |
| | 2191 | } |
| | 2192 | } |
| | 2193 | |
| 2188 | while (self.astgen_work_queue.readItem()) |file| { | 2194 | while (self.astgen_work_queue.readItem()) |file| { |
| 2189 | self.astgen_wait_group.start(); | 2195 | self.astgen_wait_group.start(); |
| 2190 | try self.thread_pool.spawn(workerAstGenFile, .{ | 2196 | try self.thread_pool.spawn(workerAstGenFile, .{ |
| ... | @@ -2748,6 +2754,8 @@ fn workerAstGenFile( | ... | @@ -2748,6 +2754,8 @@ fn workerAstGenFile( |
| 2748 | extra_index = item.end; | 2754 | extra_index = item.end; |
| 2749 | | 2755 | |
| 2750 | const import_path = file.zir.nullTerminatedString(item.data.name); | 2756 | const import_path = file.zir.nullTerminatedString(item.data.name); |
| | 2757 | // `@import("builtin")` is handled specially. |
| | 2758 | if (mem.eql(u8, import_path, "builtin")) continue; |
| 2751 | | 2759 | |
| 2752 | const import_result = blk: { | 2760 | const import_result = blk: { |
| 2753 | comp.mutex.lock(); | 2761 | comp.mutex.lock(); |
| ... | @@ -2775,6 +2783,29 @@ fn workerAstGenFile( | ... | @@ -2775,6 +2783,29 @@ fn workerAstGenFile( |
| 2775 | } | 2783 | } |
| 2776 | } | 2784 | } |
| 2777 | | 2785 | |
| | 2786 | fn workerUpdateBuiltinZigFile( |
| | 2787 | comp: *Compilation, |
| | 2788 | mod: *Module, |
| | 2789 | wg: *WaitGroup, |
| | 2790 | ) void { |
| | 2791 | defer wg.finish(); |
| | 2792 | |
| | 2793 | mod.populateBuiltinFile() catch |err| { |
| | 2794 | const dir_path: []const u8 = mod.zig_cache_artifact_directory.path orelse "."; |
| | 2795 | |
| | 2796 | comp.mutex.lock(); |
| | 2797 | defer comp.mutex.unlock(); |
| | 2798 | |
| | 2799 | comp.setMiscFailure(.write_builtin_zig, "unable to write builtin.zig to {s}: {s}", .{ |
| | 2800 | dir_path, @errorName(err), |
| | 2801 | }) catch |oom| switch (oom) { |
| | 2802 | error.OutOfMemory => log.err("unable to write builtin.zig to {s}: {s}", .{ |
| | 2803 | dir_path, @errorName(err), |
| | 2804 | }), |
| | 2805 | }; |
| | 2806 | }; |
| | 2807 | } |
| | 2808 | |
| 2778 | fn workerCheckEmbedFile( | 2809 | fn workerCheckEmbedFile( |
| 2779 | comp: *Compilation, | 2810 | comp: *Compilation, |
| 2780 | embed_file: *Module.EmbedFile, | 2811 | embed_file: *Module.EmbedFile, |
| ... | @@ -4046,22 +4077,6 @@ fn wantBuildLibUnwindFromSource(comp: *Compilation) bool { | ... | @@ -4046,22 +4077,6 @@ fn wantBuildLibUnwindFromSource(comp: *Compilation) bool { |
| 4046 | comp.bin_file.options.object_format != .c; | 4077 | comp.bin_file.options.object_format != .c; |
| 4047 | } | 4078 | } |
| 4048 | | 4079 | |
| 4049 | fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) Allocator.Error!void { | | |
| 4050 | const tracy_trace = trace(@src()); | | |
| 4051 | defer tracy_trace.end(); | | |
| 4052 | | | |
| 4053 | const source = try comp.generateBuiltinZigSource(comp.gpa); | | |
| 4054 | defer comp.gpa.free(source); | | |
| 4055 | | | |
| 4056 | mod.zig_cache_artifact_directory.handle.writeFile("builtin.zig", source) catch |err| { | | |
| 4057 | const dir_path: []const u8 = mod.zig_cache_artifact_directory.path orelse "."; | | |
| 4058 | try comp.setMiscFailure(.write_builtin_zig, "unable to write builtin.zig to {s}: {s}", .{ | | |
| 4059 | dir_path, | | |
| 4060 | @errorName(err), | | |
| 4061 | }); | | |
| 4062 | }; | | |
| 4063 | } | | |
| 4064 | | | |
| 4065 | fn setMiscFailure( | 4080 | fn setMiscFailure( |
| 4066 | comp: *Compilation, | 4081 | comp: *Compilation, |
| 4067 | tag: MiscTask, | 4082 | tag: MiscTask, |
| ... | @@ -4084,7 +4099,7 @@ pub fn dump_argv(argv: []const []const u8) void { | ... | @@ -4084,7 +4099,7 @@ pub fn dump_argv(argv: []const []const u8) void { |
| 4084 | std.debug.print("{s}\n", .{argv[argv.len - 1]}); | 4099 | std.debug.print("{s}\n", .{argv[argv.len - 1]}); |
| 4085 | } | 4100 | } |
| 4086 | | 4101 | |
| 4087 | pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![]u8 { | 4102 | pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![:0]u8 { |
| 4088 | const tracy_trace = trace(@src()); | 4103 | const tracy_trace = trace(@src()); |
| 4089 | defer tracy_trace.end(); | 4104 | defer tracy_trace.end(); |
| 4090 | | 4105 | |
| ... | @@ -4290,7 +4305,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Alloca | ... | @@ -4290,7 +4305,7 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Alloca |
| 4290 | } | 4305 | } |
| 4291 | } | 4306 | } |
| 4292 | | 4307 | |
| 4293 | return buffer.toOwnedSlice(); | 4308 | return buffer.toOwnedSliceSentinel(0); |
| 4294 | } | 4309 | } |
| 4295 | | 4310 | |
| 4296 | pub fn updateSubCompilation(sub_compilation: *Compilation) !void { | 4311 | pub fn updateSubCompilation(sub_compilation: *Compilation) !void { |