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,...@@ -173,6 +173,7 @@ verbose_llvm_bc: ?[]const u8,
173verbose_cimport: bool,173verbose_cimport: bool,
174verbose_llvm_cpu_features: bool,174verbose_llvm_cpu_features: bool,
175verbose_link: bool,175verbose_link: bool,
176link_depfile: ?[]const u8,
176disable_c_depfile: bool,177disable_c_depfile: bool,
177stack_report: bool,178stack_report: bool,
178debug_compiler_runtime_libs: bool,179debug_compiler_runtime_libs: bool,
...@@ -1403,6 +1404,7 @@ pub const MiscTask = enum {...@@ -1403,6 +1404,7 @@ pub const MiscTask = enum {
1403 compiler_rt,1404 compiler_rt,
1404 libzigc,1405 libzigc,
1405 analyze_mod,1406 analyze_mod,
1407 link_depfile,
1406 docs_copy,1408 docs_copy,
1407 docs_wasm,1409 docs_wasm,
14081410
...@@ -1732,6 +1734,7 @@ pub const CreateOptions = struct {...@@ -1732,6 +1734,7 @@ pub const CreateOptions = struct {
1732 verbose_generic_instances: bool = false,1734 verbose_generic_instances: bool = false,
1733 verbose_llvm_ir: ?[]const u8 = null,1735 verbose_llvm_ir: ?[]const u8 = null,
1734 verbose_llvm_bc: ?[]const u8 = null,1736 verbose_llvm_bc: ?[]const u8 = null,
1737 link_depfile: ?[]const u8 = null,
1735 verbose_cimport: bool = false,1738 verbose_cimport: bool = false,
1736 verbose_llvm_cpu_features: bool = false,1739 verbose_llvm_cpu_features: bool = false,
1737 debug_compiler_runtime_libs: bool = false,1740 debug_compiler_runtime_libs: bool = false,
...@@ -2247,6 +2250,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,...@@ -2247,6 +2250,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,
2247 .verbose_generic_instances = options.verbose_generic_instances,2250 .verbose_generic_instances = options.verbose_generic_instances,
2248 .verbose_llvm_ir = options.verbose_llvm_ir,2251 .verbose_llvm_ir = options.verbose_llvm_ir,
2249 .verbose_llvm_bc = options.verbose_llvm_bc,2252 .verbose_llvm_bc = options.verbose_llvm_bc,
2253 .link_depfile = options.link_depfile,
2250 .verbose_cimport = options.verbose_cimport,2254 .verbose_cimport = options.verbose_cimport,
2251 .verbose_llvm_cpu_features = options.verbose_llvm_cpu_features,2255 .verbose_llvm_cpu_features = options.verbose_llvm_cpu_features,
2252 .verbose_link = options.verbose_link,2256 .verbose_link = options.verbose_link,
...@@ -3099,6 +3103,15 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE...@@ -3099,6 +3103,15 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE
3099 }3103 }
3100 }3104 }
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
3102 if (anyErrors(comp)) {3115 if (anyErrors(comp)) {
3103 // Skip flushing and keep source files loaded for error reporting.3116 // Skip flushing and keep source files loaded for error reporting.
3104 return;3117 return;
...@@ -5208,6 +5221,43 @@ pub fn separateCodegenThreadOk(comp: *const Compilation) bool {...@@ -5208,6 +5221,43 @@ pub fn separateCodegenThreadOk(comp: *const Compilation) bool {
5208 return zcu.backendSupportsFeature(.separate_thread);5221 return zcu.backendSupportsFeature(.separate_thread);
5209}5222}
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
5211fn workerDocsCopy(comp: *Compilation) void {5261fn workerDocsCopy(comp: *Compilation) void {
5212 docsCopyFallible(comp) catch |err| return comp.lockAndSetMiscFailure(5262 docsCopyFallible(comp) catch |err| return comp.lockAndSetMiscFailure(
5213 .docs_copy,5263 .docs_copy,
src/main.zig+4
...@@ -821,6 +821,7 @@ fn buildOutputType(...@@ -821,6 +821,7 @@ fn buildOutputType(
821 var verbose_generic_instances = false;821 var verbose_generic_instances = false;
822 var verbose_llvm_ir: ?[]const u8 = null;822 var verbose_llvm_ir: ?[]const u8 = null;
823 var verbose_llvm_bc: ?[]const u8 = null;823 var verbose_llvm_bc: ?[]const u8 = null;
824 var link_depfile: ?[]const u8 = null;
824 var verbose_cimport = false;825 var verbose_cimport = false;
825 var verbose_llvm_cpu_features = false;826 var verbose_llvm_cpu_features = false;
826 var time_report = false;827 var time_report = false;
...@@ -2704,6 +2705,8 @@ fn buildOutputType(...@@ -2704,6 +2705,8 @@ fn buildOutputType(
2704 {2705 {
2705 emit_implib = .{ .yes = linker_args_it.nextOrFatal() };2706 emit_implib = .{ .yes = linker_args_it.nextOrFatal() };
2706 emit_implib_arg_provided = true;2707 emit_implib_arg_provided = true;
2708 } else if (mem.eql(u8, arg, "--dependency-file")) {
2709 link_depfile = linker_args_it.nextOrFatal();
2707 } else if (mem.eql(u8, arg, "-Brepro") or mem.eql(u8, arg, "/Brepro")) {2710 } else if (mem.eql(u8, arg, "-Brepro") or mem.eql(u8, arg, "/Brepro")) {
2708 linker_repro = true;2711 linker_repro = true;
2709 } else if (mem.eql(u8, arg, "-undefined")) {2712 } else if (mem.eql(u8, arg, "-undefined")) {
...@@ -3479,6 +3482,7 @@ fn buildOutputType(...@@ -3479,6 +3482,7 @@ fn buildOutputType(
3479 .verbose_generic_instances = verbose_generic_instances,3482 .verbose_generic_instances = verbose_generic_instances,
3480 .verbose_llvm_ir = verbose_llvm_ir,3483 .verbose_llvm_ir = verbose_llvm_ir,
3481 .verbose_llvm_bc = verbose_llvm_bc,3484 .verbose_llvm_bc = verbose_llvm_bc,
3485 .link_depfile = link_depfile,
3482 .verbose_cimport = verbose_cimport,3486 .verbose_cimport = verbose_cimport,
3483 .verbose_llvm_cpu_features = verbose_llvm_cpu_features,3487 .verbose_llvm_cpu_features = verbose_llvm_cpu_features,
3484 .time_report = time_report,3488 .time_report = time_report,