authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-24 18:30:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:21-07:00
log9141e1a29c8e1b3334a1ea3cdf85fe6df9cac2b4
treed9f88a1726c4de8d5b5c562ab741c71505a0f611
parente12e29630664a78a32103d56175162381443b574

CLI: fix logic for sending output file path

via the compiler protocol

2 files changed, 68 insertions(+), 24 deletions(-)

src/Compilation.zig+2-12
...@@ -1198,16 +1198,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1198,16 +1198,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
11981198
1199 const use_llvm = options.config.use_llvm;1199 const use_llvm = options.config.use_llvm;
12001200
1201 // TODO: once we support incremental compilation for the LLVM backend via
1202 // saving the LLVM module into a bitcode file and restoring it, along with
1203 // compiler state, the second clause here can be removed so that incremental
1204 // cache mode is used for LLVM backend too. We need some fuzz testing before
1205 // that can be enabled.
1206 const cache_mode = if ((use_llvm or !have_zcu) and !options.disable_lld_caching)
1207 CacheMode.whole
1208 else
1209 options.cache_mode;
1210
1211 const any_unwind_tables = options.config.any_unwind_tables;1201 const any_unwind_tables = options.config.any_unwind_tables;
12121202
1213 const link_eh_frame_hdr = options.link_eh_frame_hdr or any_unwind_tables;1203 const link_eh_frame_hdr = options.link_eh_frame_hdr or any_unwind_tables;
...@@ -1560,7 +1550,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1560,7 +1550,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1560 .compatibility_version = options.compatibility_version,1550 .compatibility_version = options.compatibility_version,
1561 .each_lib_rpath = each_lib_rpath,1551 .each_lib_rpath = each_lib_rpath,
1562 .build_id = build_id,1552 .build_id = build_id,
1563 .disable_lld_caching = options.disable_lld_caching or cache_mode == .whole,1553 .disable_lld_caching = options.disable_lld_caching or options.cache_mode == .whole,
1564 .subsystem = options.subsystem,1554 .subsystem = options.subsystem,
1565 .hash_style = options.hash_style,1555 .hash_style = options.hash_style,
1566 .enable_link_snapshots = options.enable_link_snapshots,1556 .enable_link_snapshots = options.enable_link_snapshots,
...@@ -1576,7 +1566,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1576,7 +1566,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1576 .entry_addr = null, // CLI does not expose this option (yet?)1566 .entry_addr = null, // CLI does not expose this option (yet?)
1577 };1567 };
15781568
1579 switch (cache_mode) {1569 switch (options.cache_mode) {
1580 .incremental => {1570 .incremental => {
1581 // Options that are specific to zig source files, that cannot be1571 // Options that are specific to zig source files, that cannot be
1582 // modified between incremental updates.1572 // modified between incremental updates.
src/main.zig+66-12
...@@ -3110,6 +3110,21 @@ fn buildOutputType(...@@ -3110,6 +3110,21 @@ fn buildOutputType(
3110 else => false,3110 else => false,
3111 };3111 };
31123112
3113 const disable_lld_caching = !output_to_cache;
3114
3115 const cache_mode: Compilation.CacheMode = b: {
3116 if (disable_lld_caching) break :b .incremental;
3117 if (!create_module.resolved_options.have_zcu) break :b .whole;
3118
3119 // TODO: once we support incremental compilation for the LLVM backend
3120 // via saving the LLVM module into a bitcode file and restoring it,
3121 // along with compiler state, this clause can be removed so that
3122 // incremental cache mode is used for LLVM backend too.
3123 if (create_module.resolved_options.use_llvm) break :b .whole;
3124
3125 break :b .incremental;
3126 };
3127
3113 gimmeMoreOfThoseSweetSweetFileDescriptors();3128 gimmeMoreOfThoseSweetSweetFileDescriptors();
31143129
3115 const comp = Compilation.create(gpa, .{3130 const comp = Compilation.create(gpa, .{
...@@ -3211,7 +3226,8 @@ fn buildOutputType(...@@ -3211,7 +3226,8 @@ fn buildOutputType(
3211 .test_filter = test_filter,3226 .test_filter = test_filter,
3212 .test_name_prefix = test_name_prefix,3227 .test_name_prefix = test_name_prefix,
3213 .test_runner_path = test_runner_path,3228 .test_runner_path = test_runner_path,
3214 .disable_lld_caching = !output_to_cache,3229 .disable_lld_caching = disable_lld_caching,
3230 .cache_mode = cache_mode,
3215 .subsystem = subsystem,3231 .subsystem = subsystem,
3216 .debug_compile_errors = debug_compile_errors,3232 .debug_compile_errors = debug_compile_errors,
3217 .enable_link_snapshots = enable_link_snapshots,3233 .enable_link_snapshots = enable_link_snapshots,
...@@ -3942,7 +3958,7 @@ fn serve(...@@ -3942,7 +3958,7 @@ fn serve(
3942 const hdr = try server.receiveMessage();3958 const hdr = try server.receiveMessage();
39433959
3944 switch (hdr.tag) {3960 switch (hdr.tag) {
3945 .exit => return,3961 .exit => return cleanExit(),
3946 .update => {3962 .update => {
3947 assert(main_progress_node.recently_updated_child == null);3963 assert(main_progress_node.recently_updated_child == null);
3948 tracy.frameMark();3964 tracy.frameMark();
...@@ -4104,25 +4120,63 @@ fn serveUpdateResults(s: *Server, comp: *Compilation) !void {...@@ -4104,25 +4120,63 @@ fn serveUpdateResults(s: *Server, comp: *Compilation) !void {
4104 try s.serveErrorBundle(error_bundle);4120 try s.serveErrorBundle(error_bundle);
4105 return;4121 return;
4106 }4122 }
4107 // This logic is a bit counter-intuitive because the protocol implies that4123
4108 // each emitted artifact could possibly be in a different location, when in4124 // This logic is counter-intuitive because the protocol accounts for each
4109 // reality, there is only one artifact output directory, and the build4125 // emitted artifact possibly being in a different location, which correctly
4110 // system depends on that fact. So, until the protocol is changed to4126 // matches the behavior of the compiler, however, the build system
4111 // reflect this, this logic only needs to ensure that emit_bin_path is4127 // currently always passes flags that makes all build artifacts output to
4112 // emitted for at least one thing, if there are any artifacts.4128 // the same local cache directory, and relies on them all being in the same
4113 if (comp.bin_file) |lf| {4129 // directory.
4114 const emit = lf.emit;4130 //
4131 // So, until the build system and protocol are changed to reflect this,
4132 // this logic must ensure that emit_bin_path is emitted for at least one
4133 // thing, if there are any artifacts.
4134
4135 switch (comp.cache_use) {
4136 .incremental => if (comp.bin_file) |lf| {
4137 const full_path = try lf.emit.directory.join(gpa, &.{lf.emit.sub_path});
4138 defer gpa.free(full_path);
4139 try s.serveEmitBinPath(full_path, .{
4140 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },
4141 });
4142 return;
4143 },
4144 .whole => |whole| if (whole.bin_sub_path) |sub_path| {
4145 const full_path = try comp.local_cache_directory.join(gpa, &.{sub_path});
4146 defer gpa.free(full_path);
4147 try s.serveEmitBinPath(full_path, .{
4148 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },
4149 });
4150 return;
4151 },
4152 }
4153
4154 for ([_]?Compilation.Emit{
4155 comp.docs_emit,
4156 comp.implib_emit,
4157 }) |opt_emit| {
4158 const emit = opt_emit orelse continue;
4115 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});4159 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});
4116 defer gpa.free(full_path);4160 defer gpa.free(full_path);
4117 try s.serveEmitBinPath(full_path, .{4161 try s.serveEmitBinPath(full_path, .{
4118 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },4162 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },
4119 });4163 });
4120 } else if (comp.docs_emit) |emit| {4164 return;
4121 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});4165 }
4166
4167 for ([_]?Compilation.EmitLoc{
4168 comp.emit_asm,
4169 comp.emit_llvm_ir,
4170 comp.emit_llvm_bc,
4171 }) |opt_emit_loc| {
4172 const emit_loc = opt_emit_loc orelse continue;
4173 const directory = emit_loc.directory orelse continue;
4174 const full_path = try directory.join(gpa, &.{emit_loc.basename});
4122 defer gpa.free(full_path);4175 defer gpa.free(full_path);
4123 try s.serveEmitBinPath(full_path, .{4176 try s.serveEmitBinPath(full_path, .{
4124 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },4177 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },
4125 });4178 });
4179 return;
4126 }4180 }
4127}4181}
41284182