authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2024-07-02 06:15:29-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-04 13:34:17-04:00
logd9f1a952b8b0e19aafcf568b35cc220adbb4a7b5
treed7333d6953a0fe37ac11b12a2456efebffd2a4c0
parentb67caf72e31eefe08fe2dc8b17b4bca16e0e3cc6

build: fix WriteFile and addCSourceFiles not adding LazyPath deps

Adds a missing call to addLazyPathDependenciesOnly in std.Build.Module.addCSourceFiles. Also fixes an issue in std.Build.Step.WriteFile where it wasn't updating all the GeneratedFile instances for every directory. To fix the second issue, I removed all the GeneratedFile instances and now all files/directories reference the steps main GeneratedFile via sub paths.

12 files changed, 96 insertions(+), 47 deletions(-)

lib/std/Build/Module.zig+1
...@@ -484,6 +484,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {...@@ -484,6 +484,7 @@ pub fn addCSourceFiles(m: *Module, options: AddCSourceFilesOptions) void {
484 .flags = b.dupeStrings(options.flags),484 .flags = b.dupeStrings(options.flags),
485 };485 };
486 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");486 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
487 addLazyPathDependenciesOnly(m, c_source_files.root);
487}488}
488489
489pub fn addCSourceFile(m: *Module, source: CSourceFile) void {490pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
lib/std/Build/Step/WriteFile.zig+23-33
...@@ -17,8 +17,8 @@ const WriteFile = @This();...@@ -17,8 +17,8 @@ const WriteFile = @This();
17step: Step,17step: Step,
1818
19// The elements here are pointers because we need stable pointers for the GeneratedFile field.19// The elements here are pointers because we need stable pointers for the GeneratedFile field.
20files: std.ArrayListUnmanaged(*File),20files: std.ArrayListUnmanaged(File),
21directories: std.ArrayListUnmanaged(*Directory),21directories: std.ArrayListUnmanaged(Directory),
2222
23output_source_files: std.ArrayListUnmanaged(OutputSourceFile),23output_source_files: std.ArrayListUnmanaged(OutputSourceFile),
24generated_directory: std.Build.GeneratedFile,24generated_directory: std.Build.GeneratedFile,
...@@ -26,20 +26,14 @@ generated_directory: std.Build.GeneratedFile,...@@ -26,20 +26,14 @@ generated_directory: std.Build.GeneratedFile,
26pub const base_id: Step.Id = .write_file;26pub const base_id: Step.Id = .write_file;
2727
28pub const File = struct {28pub const File = struct {
29 generated_file: std.Build.GeneratedFile,
30 sub_path: []const u8,29 sub_path: []const u8,
31 contents: Contents,30 contents: Contents,
32
33 pub fn getPath(file: *File) std.Build.LazyPath {
34 return .{ .generated = .{ .file = &file.generated_file } };
35 }
36};31};
3732
38pub const Directory = struct {33pub const Directory = struct {
39 source: std.Build.LazyPath,34 source: std.Build.LazyPath,
40 sub_path: []const u8,35 sub_path: []const u8,
41 options: Options,36 options: Options,
42 generated_dir: std.Build.GeneratedFile,
4337
44 pub const Options = struct {38 pub const Options = struct {
45 /// File paths that end in any of these suffixes will be excluded from copying.39 /// File paths that end in any of these suffixes will be excluded from copying.
...@@ -56,10 +50,6 @@ pub const Directory = struct {...@@ -56,10 +50,6 @@ pub const Directory = struct {
56 };50 };
57 }51 }
58 };52 };
59
60 pub fn getPath(dir: *Directory) std.Build.LazyPath {
61 return .{ .generated = .{ .file = &dir.generated_dir } };
62 }
63};53};
6454
65pub const OutputSourceFile = struct {55pub const OutputSourceFile = struct {
...@@ -92,15 +82,18 @@ pub fn create(owner: *std.Build) *WriteFile {...@@ -92,15 +82,18 @@ pub fn create(owner: *std.Build) *WriteFile {
92pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.LazyPath {82pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.LazyPath {
93 const b = write_file.step.owner;83 const b = write_file.step.owner;
94 const gpa = b.allocator;84 const gpa = b.allocator;
95 const file = gpa.create(File) catch @panic("OOM");85 const file = File{
96 file.* = .{
97 .generated_file = .{ .step = &write_file.step },
98 .sub_path = b.dupePath(sub_path),86 .sub_path = b.dupePath(sub_path),
99 .contents = .{ .bytes = b.dupe(bytes) },87 .contents = .{ .bytes = b.dupe(bytes) },
100 };88 };
101 write_file.files.append(gpa, file) catch @panic("OOM");89 write_file.files.append(gpa, file) catch @panic("OOM");
102 write_file.maybeUpdateName();90 write_file.maybeUpdateName();
103 return file.getPath();91 return .{
92 .generated = .{
93 .file = &write_file.generated_directory,
94 .sub_path = file.sub_path,
95 },
96 };
104}97}
10598
106/// Place the file into the generated directory within the local cache,99/// Place the file into the generated directory within the local cache,
...@@ -113,9 +106,7 @@ pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std....@@ -113,9 +106,7 @@ pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std.
113pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPath {106pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPath {
114 const b = write_file.step.owner;107 const b = write_file.step.owner;
115 const gpa = b.allocator;108 const gpa = b.allocator;
116 const file = gpa.create(File) catch @panic("OOM");109 const file = File{
117 file.* = .{
118 .generated_file = .{ .step = &write_file.step },
119 .sub_path = b.dupePath(sub_path),110 .sub_path = b.dupePath(sub_path),
120 .contents = .{ .copy = source },111 .contents = .{ .copy = source },
121 };112 };
...@@ -123,7 +114,12 @@ pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path:...@@ -123,7 +114,12 @@ pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path:
123114
124 write_file.maybeUpdateName();115 write_file.maybeUpdateName();
125 source.addStepDependencies(&write_file.step);116 source.addStepDependencies(&write_file.step);
126 return file.getPath();117 return .{
118 .generated = .{
119 .file = &write_file.generated_directory,
120 .sub_path = file.sub_path,
121 },
122 };
127}123}
128124
129/// Copy files matching the specified exclude/include patterns to the specified subdirectory125/// Copy files matching the specified exclude/include patterns to the specified subdirectory
...@@ -137,18 +133,21 @@ pub fn addCopyDirectory(...@@ -137,18 +133,21 @@ pub fn addCopyDirectory(
137) std.Build.LazyPath {133) std.Build.LazyPath {
138 const b = write_file.step.owner;134 const b = write_file.step.owner;
139 const gpa = b.allocator;135 const gpa = b.allocator;
140 const dir = gpa.create(Directory) catch @panic("OOM");136 const dir = Directory{
141 dir.* = .{
142 .source = source.dupe(b),137 .source = source.dupe(b),
143 .sub_path = b.dupePath(sub_path),138 .sub_path = b.dupePath(sub_path),
144 .options = options.dupe(b),139 .options = options.dupe(b),
145 .generated_dir = .{ .step = &write_file.step },
146 };140 };
147 write_file.directories.append(gpa, dir) catch @panic("OOM");141 write_file.directories.append(gpa, dir) catch @panic("OOM");
148142
149 write_file.maybeUpdateName();143 write_file.maybeUpdateName();
150 source.addStepDependencies(&write_file.step);144 source.addStepDependencies(&write_file.step);
151 return dir.getPath();145 return .{
146 .generated = .{
147 .file = &write_file.generated_directory,
148 .sub_path = dir.sub_path,
149 },
150 };
152}151}
153152
154/// A path relative to the package root.153/// A path relative to the package root.
...@@ -278,11 +277,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {...@@ -278,11 +277,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
278277
279 if (try step.cacheHit(&man)) {278 if (try step.cacheHit(&man)) {
280 const digest = man.final();279 const digest = man.final();
281 for (write_file.files.items) |file| {
282 file.generated_file.path = try b.cache_root.join(b.allocator, &.{
283 "o", &digest, file.sub_path,
284 });
285 }
286 write_file.generated_directory.path = try b.cache_root.join(b.allocator, &.{ "o", &digest });280 write_file.generated_directory.path = try b.cache_root.join(b.allocator, &.{ "o", &digest });
287 return;281 return;
288 }282 }
...@@ -342,10 +336,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {...@@ -342,10 +336,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
342 _ = prev_status;336 _ = prev_status;
343 },337 },
344 }338 }
345
346 file.generated_file.path = try b.cache_root.join(b.allocator, &.{
347 cache_path, file.sub_path,
348 });
349 }339 }
350 for (write_file.directories.items) |dir| {340 for (write_file.directories.items) |dir| {
351 const full_src_dir_path = dir.source.getPath2(b, step);341 const full_src_dir_path = dir.source.getPath2(b, step);
test/src/Cases.zig+4-2
...@@ -665,10 +665,12 @@ pub fn lowerToBuildSteps(...@@ -665,10 +665,12 @@ pub fn lowerToBuildSteps(
665 const writefiles = b.addWriteFiles();665 const writefiles = b.addWriteFiles();
666 var file_sources = std.StringHashMap(std.Build.LazyPath).init(b.allocator);666 var file_sources = std.StringHashMap(std.Build.LazyPath).init(b.allocator);
667 defer file_sources.deinit();667 defer file_sources.deinit();
668 for (update.files.items) |file| {668 const first_file = update.files.items[0];
669 const root_source_file = writefiles.add(first_file.path, first_file.src);
670 file_sources.put(first_file.path, root_source_file) catch @panic("OOM");
671 for (update.files.items[1..]) |file| {
669 file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM");672 file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM");
670 }673 }
671 const root_source_file = writefiles.files.items[0].getPath();
672674
673 const artifact = if (case.is_test) b.addTest(.{675 const artifact = if (case.is_test) b.addTest(.{
674 .root_source_file = root_source_file,676 .root_source_file = root_source_file,
test/src/CompareOutput.zig+6-4
...@@ -81,7 +81,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -81,7 +81,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
81 const b = self.b;81 const b = self.b;
8282
83 const write_src = b.addWriteFiles();83 const write_src = b.addWriteFiles();
84 for (case.sources.items) |src_file| {84 const first_src = case.sources.items[0];
85 const first_file = write_src.add(first_src.filename, first_src.source);
86 for (case.sources.items[1..]) |src_file| {
85 _ = write_src.add(src_file.filename, src_file.source);87 _ = write_src.add(src_file.filename, src_file.source);
86 }88 }
8789
...@@ -99,7 +101,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -99,7 +101,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
99 .target = b.graph.host,101 .target = b.graph.host,
100 .optimize = .Debug,102 .optimize = .Debug,
101 });103 });
102 exe.addAssemblyFile(write_src.files.items[0].getPath());104 exe.addAssemblyFile(first_file);
103105
104 const run = b.addRunArtifact(exe);106 const run = b.addRunArtifact(exe);
105 run.setName(annotated_case_name);107 run.setName(annotated_case_name);
...@@ -119,7 +121,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -119,7 +121,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
119121
120 const exe = b.addExecutable(.{122 const exe = b.addExecutable(.{
121 .name = "test",123 .name = "test",
122 .root_source_file = write_src.files.items[0].getPath(),124 .root_source_file = first_file,
123 .optimize = optimize,125 .optimize = optimize,
124 .target = b.graph.host,126 .target = b.graph.host,
125 });127 });
...@@ -145,7 +147,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -145,7 +147,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
145147
146 const exe = b.addExecutable(.{148 const exe = b.addExecutable(.{
147 .name = "test",149 .name = "test",
148 .root_source_file = write_src.files.items[0].getPath(),150 .root_source_file = first_file,
149 .target = b.graph.host,151 .target = b.graph.host,
150 .optimize = .Debug,152 .optimize = .Debug,
151 });153 });
test/src/RunTranslatedC.zig+4-2
...@@ -72,11 +72,13 @@ pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void {...@@ -72,11 +72,13 @@ pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void {
72 } else if (self.test_filters.len > 0) return;72 } else if (self.test_filters.len > 0) return;
7373
74 const write_src = b.addWriteFiles();74 const write_src = b.addWriteFiles();
75 for (case.sources.items) |src_file| {75 const first_case = case.sources.items[0];
76 const root_source_file = write_src.add(first_case.filename, first_case.source);
77 for (case.sources.items[1..]) |src_file| {
76 _ = write_src.add(src_file.filename, src_file.source);78 _ = write_src.add(src_file.filename, src_file.source);
77 }79 }
78 const translate_c = b.addTranslateC(.{80 const translate_c = b.addTranslateC(.{
79 .root_source_file = write_src.files.items[0].getPath(),81 .root_source_file = root_source_file,
80 .target = b.graph.host,82 .target = b.graph.host,
81 .optimize = .Debug,83 .optimize = .Debug,
82 });84 });
test/src/StackTrace.zig+3-2
...@@ -51,10 +51,11 @@ fn addExpect(...@@ -51,10 +51,11 @@ fn addExpect(
51 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;51 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
52 } else if (self.test_filters.len > 0) return;52 } else if (self.test_filters.len > 0) return;
5353
54 const write_src = b.addWriteFile("source.zig", source);54 const write_files = b.addWriteFiles();
55 const source_zig = write_files.add("source.zig", source);
55 const exe = b.addExecutable(.{56 const exe = b.addExecutable(.{
56 .name = "test",57 .name = "test",
57 .root_source_file = write_src.files.items[0].getPath(),58 .root_source_file = source_zig,
58 .optimize = optimize_mode,59 .optimize = optimize_mode,
59 .target = b.graph.host,60 .target = b.graph.host,
60 .error_tracing = mode_config.error_tracing,61 .error_tracing = mode_config.error_tracing,
test/src/TranslateC.zig+4-2
...@@ -93,12 +93,14 @@ pub fn addCase(self: *TranslateCContext, case: *const TestCase) void {...@@ -93,12 +93,14 @@ pub fn addCase(self: *TranslateCContext, case: *const TestCase) void {
93 } else if (self.test_filters.len > 0) return;93 } else if (self.test_filters.len > 0) return;
9494
95 const write_src = b.addWriteFiles();95 const write_src = b.addWriteFiles();
96 for (case.sources.items) |src_file| {96 const first_src = case.sources.items[0];
97 const root_source_file = write_src.add(first_src.filename, first_src.source);
98 for (case.sources.items[1..]) |src_file| {
97 _ = write_src.add(src_file.filename, src_file.source);99 _ = write_src.add(src_file.filename, src_file.source);
98 }100 }
99101
100 const translate_c = b.addTranslateC(.{102 const translate_c = b.addTranslateC(.{
101 .root_source_file = write_src.files.items[0].getPath(),103 .root_source_file = root_source_file,
102 .target = b.resolveTargetQuery(case.target),104 .target = b.resolveTargetQuery(case.target),
103 .optimize = .Debug,105 .optimize = .Debug,
104 });106 });
test/standalone/build.zig.zon+3
...@@ -83,6 +83,9 @@...@@ -83,6 +83,9 @@
83 .dep_shared_builtin = .{83 .dep_shared_builtin = .{
84 .path = "dep_shared_builtin",84 .path = "dep_shared_builtin",
85 },85 },
86 .dep_lazypath = .{
87 .path = "dep_lazypath",
88 },
86 .dirname = .{89 .dirname = .{
87 .path = "dirname",90 .path = "dirname",
88 },91 },
test/standalone/dep_lazypath/build.zig created+37
...@@ -0,0 +1,37 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const test_step = b.step("test", "Test it");
5 b.default_step = test_step;
6
7 const optimize: std.builtin.OptimizeMode = .Debug;
8
9 {
10 const write_files = b.addWriteFiles();
11 const generated_main_c = write_files.add("main.c", "");
12 const exe = b.addExecutable(.{
13 .name = "test",
14 .target = b.graph.host,
15 .optimize = optimize,
16 });
17 exe.addCSourceFiles(.{
18 .root = generated_main_c.dirname(),
19 .files = &.{"main.c"},
20 });
21 b.step("csourcefiles", "").dependOn(&exe.step);
22 test_step.dependOn(&exe.step);
23 }
24 {
25 const write_files = b.addWriteFiles();
26 const dir = write_files.addCopyDirectory(b.path("inc"), "", .{});
27 const exe = b.addExecutable(.{
28 .name = "test",
29 .root_source_file = b.path("inctest.zig"),
30 .target = b.graph.host,
31 .optimize = optimize,
32 });
33 exe.addIncludePath(dir);
34 b.step("copydir", "").dependOn(&exe.step);
35 test_step.dependOn(&exe.step);
36 }
37}
test/standalone/dep_lazypath/inc/foo.h created+1
...@@ -0,0 +1 @@
1#define foo_value 42
test/standalone/dep_lazypath/inctest.zig created+8
...@@ -0,0 +1,8 @@
1const std = @import("std");
2const c = @cImport({
3 @cInclude("foo.h");
4});
5comptime {
6 std.debug.assert(c.foo_value == 42);
7}
8pub fn main() void {}
test/tests.zig+2-2
...@@ -783,7 +783,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -783,7 +783,7 @@ pub fn addCliTests(b: *std.Build) *Step {
783 if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) {783 if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) {
784 const tmp_path = b.makeTempPath();784 const tmp_path = b.makeTempPath();
785785
786 const writefile = b.addWriteFile("example.zig",786 const example_zig = b.addWriteFiles().add("example.zig",
787 \\// Type your code here, or load an example.787 \\// Type your code here, or load an example.
788 \\export fn square(num: i32) i32 {788 \\export fn square(num: i32) i32 {
789 \\ return num * num;789 \\ return num * num;
...@@ -804,7 +804,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -804,7 +804,7 @@ pub fn addCliTests(b: *std.Build) *Step {
804 "-fno-emit-bin", "-fno-emit-h",804 "-fno-emit-bin", "-fno-emit-h",
805 "-fstrip", "-OReleaseFast",805 "-fstrip", "-OReleaseFast",
806 });806 });
807 run.addFileArg(writefile.files.items[0].getPath());807 run.addFileArg(example_zig);
808 const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s");808 const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s");
809809
810 const checkfile = b.addCheckFile(example_s, .{810 const checkfile = b.addCheckFile(example_s, .{