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 {
484484 .flags = b.dupeStrings(options.flags),
485485 };
486486 m.link_objects.append(allocator, .{ .c_source_files = c_source_files }) catch @panic("OOM");
487 addLazyPathDependenciesOnly(m, c_source_files.root);
487488}
488489
489490pub fn addCSourceFile(m: *Module, source: CSourceFile) void {
lib/std/Build/Step/WriteFile.zig+23-33
......@@ -17,8 +17,8 @@ const WriteFile = @This();
1717step: Step,
1818
1919// The elements here are pointers because we need stable pointers for the GeneratedFile field.
20files: std.ArrayListUnmanaged(*File),
21directories: std.ArrayListUnmanaged(*Directory),
20files: std.ArrayListUnmanaged(File),
21directories: std.ArrayListUnmanaged(Directory),
2222
2323output_source_files: std.ArrayListUnmanaged(OutputSourceFile),
2424generated_directory: std.Build.GeneratedFile,
......@@ -26,20 +26,14 @@ generated_directory: std.Build.GeneratedFile,
2626pub const base_id: Step.Id = .write_file;
2727
2828pub const File = struct {
29 generated_file: std.Build.GeneratedFile,
3029 sub_path: []const u8,
3130 contents: Contents,
32
33 pub fn getPath(file: *File) std.Build.LazyPath {
34 return .{ .generated = .{ .file = &file.generated_file } };
35 }
3631};
3732
3833pub const Directory = struct {
3934 source: std.Build.LazyPath,
4035 sub_path: []const u8,
4136 options: Options,
42 generated_dir: std.Build.GeneratedFile,
4337
4438 pub const Options = struct {
4539 /// File paths that end in any of these suffixes will be excluded from copying.
......@@ -56,10 +50,6 @@ pub const Directory = struct {
5650 };
5751 }
5852 };
59
60 pub fn getPath(dir: *Directory) std.Build.LazyPath {
61 return .{ .generated = .{ .file = &dir.generated_dir } };
62 }
6353};
6454
6555pub const OutputSourceFile = struct {
......@@ -92,15 +82,18 @@ pub fn create(owner: *std.Build) *WriteFile {
9282pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.LazyPath {
9383 const b = write_file.step.owner;
9484 const gpa = b.allocator;
95 const file = gpa.create(File) catch @panic("OOM");
96 file.* = .{
97 .generated_file = .{ .step = &write_file.step },
85 const file = File{
9886 .sub_path = b.dupePath(sub_path),
9987 .contents = .{ .bytes = b.dupe(bytes) },
10088 };
10189 write_file.files.append(gpa, file) catch @panic("OOM");
10290 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 };
10497}
10598
10699/// 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.
113106pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPath {
114107 const b = write_file.step.owner;
115108 const gpa = b.allocator;
116 const file = gpa.create(File) catch @panic("OOM");
117 file.* = .{
118 .generated_file = .{ .step = &write_file.step },
109 const file = File{
119110 .sub_path = b.dupePath(sub_path),
120111 .contents = .{ .copy = source },
121112 };
......@@ -123,7 +114,12 @@ pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path:
123114
124115 write_file.maybeUpdateName();
125116 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 };
127123}
128124
129125/// Copy files matching the specified exclude/include patterns to the specified subdirectory
......@@ -137,18 +133,21 @@ pub fn addCopyDirectory(
137133) std.Build.LazyPath {
138134 const b = write_file.step.owner;
139135 const gpa = b.allocator;
140 const dir = gpa.create(Directory) catch @panic("OOM");
141 dir.* = .{
136 const dir = Directory{
142137 .source = source.dupe(b),
143138 .sub_path = b.dupePath(sub_path),
144139 .options = options.dupe(b),
145 .generated_dir = .{ .step = &write_file.step },
146140 };
147141 write_file.directories.append(gpa, dir) catch @panic("OOM");
148142
149143 write_file.maybeUpdateName();
150144 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 };
152151}
153152
154153/// A path relative to the package root.
......@@ -278,11 +277,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
278277
279278 if (try step.cacheHit(&man)) {
280279 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 }
286280 write_file.generated_directory.path = try b.cache_root.join(b.allocator, &.{ "o", &digest });
287281 return;
288282 }
......@@ -342,10 +336,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
342336 _ = prev_status;
343337 },
344338 }
345
346 file.generated_file.path = try b.cache_root.join(b.allocator, &.{
347 cache_path, file.sub_path,
348 });
349339 }
350340 for (write_file.directories.items) |dir| {
351341 const full_src_dir_path = dir.source.getPath2(b, step);
test/src/Cases.zig+4-2
......@@ -665,10 +665,12 @@ pub fn lowerToBuildSteps(
665665 const writefiles = b.addWriteFiles();
666666 var file_sources = std.StringHashMap(std.Build.LazyPath).init(b.allocator);
667667 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| {
669672 file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM");
670673 }
671 const root_source_file = writefiles.files.items[0].getPath();
672674
673675 const artifact = if (case.is_test) b.addTest(.{
674676 .root_source_file = root_source_file,
test/src/CompareOutput.zig+6-4
......@@ -81,7 +81,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
8181 const b = self.b;
8282
8383 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| {
8587 _ = write_src.add(src_file.filename, src_file.source);
8688 }
8789
......@@ -99,7 +101,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
99101 .target = b.graph.host,
100102 .optimize = .Debug,
101103 });
102 exe.addAssemblyFile(write_src.files.items[0].getPath());
104 exe.addAssemblyFile(first_file);
103105
104106 const run = b.addRunArtifact(exe);
105107 run.setName(annotated_case_name);
......@@ -119,7 +121,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
119121
120122 const exe = b.addExecutable(.{
121123 .name = "test",
122 .root_source_file = write_src.files.items[0].getPath(),
124 .root_source_file = first_file,
123125 .optimize = optimize,
124126 .target = b.graph.host,
125127 });
......@@ -145,7 +147,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
145147
146148 const exe = b.addExecutable(.{
147149 .name = "test",
148 .root_source_file = write_src.files.items[0].getPath(),
150 .root_source_file = first_file,
149151 .target = b.graph.host,
150152 .optimize = .Debug,
151153 });
test/src/RunTranslatedC.zig+4-2
......@@ -72,11 +72,13 @@ pub fn addCase(self: *RunTranslatedCContext, case: *const TestCase) void {
7272 } else if (self.test_filters.len > 0) return;
7373
7474 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| {
7678 _ = write_src.add(src_file.filename, src_file.source);
7779 }
7880 const translate_c = b.addTranslateC(.{
79 .root_source_file = write_src.files.items[0].getPath(),
81 .root_source_file = root_source_file,
8082 .target = b.graph.host,
8183 .optimize = .Debug,
8284 });
test/src/StackTrace.zig+3-2
......@@ -51,10 +51,11 @@ fn addExpect(
5151 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
5252 } 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);
5556 const exe = b.addExecutable(.{
5657 .name = "test",
57 .root_source_file = write_src.files.items[0].getPath(),
58 .root_source_file = source_zig,
5859 .optimize = optimize_mode,
5960 .target = b.graph.host,
6061 .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 {
9393 } else if (self.test_filters.len > 0) return;
9494
9595 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| {
9799 _ = write_src.add(src_file.filename, src_file.source);
98100 }
99101
100102 const translate_c = b.addTranslateC(.{
101 .root_source_file = write_src.files.items[0].getPath(),
103 .root_source_file = root_source_file,
102104 .target = b.resolveTargetQuery(case.target),
103105 .optimize = .Debug,
104106 });
test/standalone/build.zig.zon+3
......@@ -83,6 +83,9 @@
8383 .dep_shared_builtin = .{
8484 .path = "dep_shared_builtin",
8585 },
86 .dep_lazypath = .{
87 .path = "dep_lazypath",
88 },
8689 .dirname = .{
8790 .path = "dirname",
8891 },
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 {
783783 if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) {
784784 const tmp_path = b.makeTempPath();
785785
786 const writefile = b.addWriteFile("example.zig",
786 const example_zig = b.addWriteFiles().add("example.zig",
787787 \\// Type your code here, or load an example.
788788 \\export fn square(num: i32) i32 {
789789 \\ return num * num;
......@@ -804,7 +804,7 @@ pub fn addCliTests(b: *std.Build) *Step {
804804 "-fno-emit-bin", "-fno-emit-h",
805805 "-fstrip", "-OReleaseFast",
806806 });
807 run.addFileArg(writefile.files.items[0].getPath());
807 run.addFileArg(example_zig);
808808 const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s");
809809
810810 const checkfile = b.addCheckFile(example_s, .{