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 {
11981198
11991199 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
12111201 const any_unwind_tables = options.config.any_unwind_tables;
12121202
12131203 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 {
15601550 .compatibility_version = options.compatibility_version,
15611551 .each_lib_rpath = each_lib_rpath,
15621552 .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,
15641554 .subsystem = options.subsystem,
15651555 .hash_style = options.hash_style,
15661556 .enable_link_snapshots = options.enable_link_snapshots,
......@@ -1576,7 +1566,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15761566 .entry_addr = null, // CLI does not expose this option (yet?)
15771567 };
15781568
1579 switch (cache_mode) {
1569 switch (options.cache_mode) {
15801570 .incremental => {
15811571 // Options that are specific to zig source files, that cannot be
15821572 // modified between incremental updates.
src/main.zig+66-12
......@@ -3110,6 +3110,21 @@ fn buildOutputType(
31103110 else => false,
31113111 };
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
31133128 gimmeMoreOfThoseSweetSweetFileDescriptors();
31143129
31153130 const comp = Compilation.create(gpa, .{
......@@ -3211,7 +3226,8 @@ fn buildOutputType(
32113226 .test_filter = test_filter,
32123227 .test_name_prefix = test_name_prefix,
32133228 .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,
32153231 .subsystem = subsystem,
32163232 .debug_compile_errors = debug_compile_errors,
32173233 .enable_link_snapshots = enable_link_snapshots,
......@@ -3942,7 +3958,7 @@ fn serve(
39423958 const hdr = try server.receiveMessage();
39433959
39443960 switch (hdr.tag) {
3945 .exit => return,
3961 .exit => return cleanExit(),
39463962 .update => {
39473963 assert(main_progress_node.recently_updated_child == null);
39483964 tracy.frameMark();
......@@ -4104,25 +4120,63 @@ fn serveUpdateResults(s: *Server, comp: *Compilation) !void {
41044120 try s.serveErrorBundle(error_bundle);
41054121 return;
41064122 }
4107 // This logic is a bit counter-intuitive because the protocol implies that
4108 // each emitted artifact could possibly be in a different location, when in
4109 // reality, there is only one artifact output directory, and the build
4110 // system depends on that fact. So, until the protocol is changed to
4111 // reflect this, this logic only needs to ensure that emit_bin_path is
4112 // emitted for at least one thing, if there are any artifacts.
4113 if (comp.bin_file) |lf| {
4114 const emit = lf.emit;
4123
4124 // This logic is counter-intuitive because the protocol accounts for each
4125 // emitted artifact possibly being in a different location, which correctly
4126 // matches the behavior of the compiler, however, the build system
4127 // currently always passes flags that makes all build artifacts output to
4128 // the same local cache directory, and relies on them all being in the same
4129 // directory.
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;
41154159 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});
41164160 defer gpa.free(full_path);
41174161 try s.serveEmitBinPath(full_path, .{
41184162 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },
41194163 });
4120 } else if (comp.docs_emit) |emit| {
4121 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});
4164 return;
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});
41224175 defer gpa.free(full_path);
41234176 try s.serveEmitBinPath(full_path, .{
41244177 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },
41254178 });
4179 return;
41264180 }
41274181}
41284182