authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-05 07:25:51+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-05 11:04:48+01:00
log34f88722cd6c206cbbc312377563340f3c3ab2b2
treefddec7472a4d49dd4dd17b12c07c2eb8512954de
parent4724774433dd7b1a80b572933556b7452c867b08
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

compiler: support --dependency-file linker option

closes https://github.com/ziglang/zig/issues/22213

2 files changed, 54 insertions(+), 0 deletions(-)

src/Compilation.zig+50
......@@ -173,6 +173,7 @@ verbose_llvm_bc: ?[]const u8,
173173verbose_cimport: bool,
174174verbose_llvm_cpu_features: bool,
175175verbose_link: bool,
176link_depfile: ?[]const u8,
176177disable_c_depfile: bool,
177178stack_report: bool,
178179debug_compiler_runtime_libs: bool,
......@@ -1403,6 +1404,7 @@ pub const MiscTask = enum {
14031404 compiler_rt,
14041405 libzigc,
14051406 analyze_mod,
1407 link_depfile,
14061408 docs_copy,
14071409 docs_wasm,
14081410
......@@ -1732,6 +1734,7 @@ pub const CreateOptions = struct {
17321734 verbose_generic_instances: bool = false,
17331735 verbose_llvm_ir: ?[]const u8 = null,
17341736 verbose_llvm_bc: ?[]const u8 = null,
1737 link_depfile: ?[]const u8 = null,
17351738 verbose_cimport: bool = false,
17361739 verbose_llvm_cpu_features: bool = false,
17371740 debug_compiler_runtime_libs: bool = false,
......@@ -2247,6 +2250,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,
22472250 .verbose_generic_instances = options.verbose_generic_instances,
22482251 .verbose_llvm_ir = options.verbose_llvm_ir,
22492252 .verbose_llvm_bc = options.verbose_llvm_bc,
2253 .link_depfile = options.link_depfile,
22502254 .verbose_cimport = options.verbose_cimport,
22512255 .verbose_llvm_cpu_features = options.verbose_llvm_cpu_features,
22522256 .verbose_link = options.verbose_link,
......@@ -3099,6 +3103,15 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE
30993103 }
31003104 }
31013105
3106 if (comp.link_depfile) |depfile_path| if (comp.bin_file) |lf| {
3107 assert(comp.file_system_inputs != null);
3108 comp.createDepFile(depfile_path, lf.emit) catch |err| comp.setMiscFailure(
3109 .link_depfile,
3110 "unable to write linker dependency file: {t}",
3111 .{err},
3112 );
3113 };
3114
31023115 if (anyErrors(comp)) {
31033116 // Skip flushing and keep source files loaded for error reporting.
31043117 return;
......@@ -5208,6 +5221,43 @@ pub fn separateCodegenThreadOk(comp: *const Compilation) bool {
52085221 return zcu.backendSupportsFeature(.separate_thread);
52095222}
52105223
5224fn createDepFile(
5225 comp: *Compilation,
5226 depfile: []const u8,
5227 binfile: Cache.Path,
5228) anyerror!void {
5229 var buf: [4096]u8 = undefined;
5230 var af = try std.fs.cwd().atomicFile(depfile, .{ .write_buffer = &buf });
5231 defer af.deinit();
5232
5233 comp.writeDepFile(binfile, &af.file_writer.interface) catch return af.file_writer.err.?;
5234
5235 try af.finish();
5236}
5237
5238fn writeDepFile(
5239 comp: *Compilation,
5240 binfile: Cache.Path,
5241 w: *std.Io.Writer,
5242) std.Io.Writer.Error!void {
5243 const prefixes = comp.cache_parent.prefixes();
5244 const fsi = comp.file_system_inputs.?.items;
5245
5246 try w.print("{f}:", .{binfile});
5247
5248 {
5249 var it = std.mem.splitScalar(u8, fsi, 0);
5250 while (it.next()) |input| try w.print(" \\\n {f}{s}", .{ prefixes[input[0] - 1], input[1..] });
5251 }
5252
5253 {
5254 var it = std.mem.splitScalar(u8, fsi, 0);
5255 while (it.next()) |input| try w.print("\n\n{f}{s}:", .{ prefixes[input[0] - 1], input[1..] });
5256 }
5257
5258 try w.writeByte('\n');
5259}
5260
52115261fn workerDocsCopy(comp: *Compilation) void {
52125262 docsCopyFallible(comp) catch |err| return comp.lockAndSetMiscFailure(
52135263 .docs_copy,
src/main.zig+4
......@@ -821,6 +821,7 @@ fn buildOutputType(
821821 var verbose_generic_instances = false;
822822 var verbose_llvm_ir: ?[]const u8 = null;
823823 var verbose_llvm_bc: ?[]const u8 = null;
824 var link_depfile: ?[]const u8 = null;
824825 var verbose_cimport = false;
825826 var verbose_llvm_cpu_features = false;
826827 var time_report = false;
......@@ -2704,6 +2705,8 @@ fn buildOutputType(
27042705 {
27052706 emit_implib = .{ .yes = linker_args_it.nextOrFatal() };
27062707 emit_implib_arg_provided = true;
2708 } else if (mem.eql(u8, arg, "--dependency-file")) {
2709 link_depfile = linker_args_it.nextOrFatal();
27072710 } else if (mem.eql(u8, arg, "-Brepro") or mem.eql(u8, arg, "/Brepro")) {
27082711 linker_repro = true;
27092712 } else if (mem.eql(u8, arg, "-undefined")) {
......@@ -3479,6 +3482,7 @@ fn buildOutputType(
34793482 .verbose_generic_instances = verbose_generic_instances,
34803483 .verbose_llvm_ir = verbose_llvm_ir,
34813484 .verbose_llvm_bc = verbose_llvm_bc,
3485 .link_depfile = link_depfile,
34823486 .verbose_cimport = verbose_cimport,
34833487 .verbose_llvm_cpu_features = verbose_llvm_cpu_features,
34843488 .time_report = time_report,