authorgravatar for mason@anthropicstudios.comMason Remaley <mason@anthropicstudios.com> 2023-05-24 14:26:07-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-24 14:26:07-07:00
log5744ceedb8ea4b3e5906175033f634b17287f3ca
tree5e5178c8f53a7bf6ffadfc713051661e04220de8
parentc9dffc842e5b9875066f012daaa0888d073ba584
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Fixes `WriteFile.getFileSource` failure on Windows (#15730)


8 files changed, 26 insertions(+), 32 deletions(-)

lib/std/Build.zig+1-1
......@@ -759,7 +759,7 @@ pub fn dupePath(self: *Build, bytes: []const u8) []u8 {
759759
760760pub fn addWriteFile(self: *Build, file_path: []const u8, data: []const u8) *Step.WriteFile {
761761 const write_file_step = self.addWriteFiles();
762 write_file_step.add(file_path, data);
762 _ = write_file_step.add(file_path, data);
763763 return write_file_step;
764764}
765765
lib/std/Build/Step/WriteFile.zig+9-14
......@@ -27,6 +27,10 @@ pub const File = struct {
2727 generated_file: std.Build.GeneratedFile,
2828 sub_path: []const u8,
2929 contents: Contents,
30
31 pub fn getFileSource(self: *File) std.Build.FileSource {
32 return .{ .generated = &self.generated_file };
33 }
3034};
3135
3236pub const OutputSourceFile = struct {
......@@ -55,7 +59,7 @@ pub fn create(owner: *std.Build) *WriteFile {
5559 return wf;
5660}
5761
58pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) void {
62pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.FileSource {
5963 const b = wf.step.owner;
6064 const gpa = b.allocator;
6165 const file = gpa.create(File) catch @panic("OOM");
......@@ -65,8 +69,8 @@ pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) void {
6569 .contents = .{ .bytes = b.dupe(bytes) },
6670 };
6771 wf.files.append(gpa, file) catch @panic("OOM");
68
6972 wf.maybeUpdateName();
73 return file.getFileSource();
7074}
7175
7276/// Place the file into the generated directory within the local cache,
......@@ -76,7 +80,7 @@ pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) void {
7680/// include sub-directories, in which case this step will ensure the
7781/// required sub-path exists.
7882/// This is the option expected to be used most commonly with `addCopyFile`.
79pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void {
83pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) std.Build.FileSource {
8084 const b = wf.step.owner;
8185 const gpa = b.allocator;
8286 const file = gpa.create(File) catch @panic("OOM");
......@@ -89,6 +93,7 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con
8993
9094 wf.maybeUpdateName();
9195 source.addStepDependencies(&wf.step);
96 return file.getFileSource();
9297}
9398
9499/// A path relative to the package root.
......@@ -96,7 +101,6 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con
96101/// used as part of the normal build process, but as a utility occasionally
97102/// run by a developer with intent to modify source files and then commit
98103/// those changes to version control.
99/// A file added this way is not available with `getFileSource`.
100104pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void {
101105 const b = wf.step.owner;
102106 wf.output_source_files.append(b.allocator, .{
......@@ -111,7 +115,6 @@ pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_pat
111115/// used as part of the normal build process, but as a utility occasionally
112116/// run by a developer with intent to modify source files and then commit
113117/// those changes to version control.
114/// A file added this way is not available with `getFileSource`.
115118pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8) void {
116119 const b = wf.step.owner;
117120 wf.output_source_files.append(b.allocator, .{
......@@ -120,15 +123,7 @@ pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8)
120123 }) catch @panic("OOM");
121124}
122125
123/// Gets a file source for the given sub_path. If the file does not exist, returns `null`.
124pub fn getFileSource(wf: *WriteFile, sub_path: []const u8) ?std.Build.FileSource {
125 for (wf.files.items) |file| {
126 if (std.mem.eql(u8, file.sub_path, sub_path)) {
127 return .{ .generated = &file.generated_file };
128 }
129 }
130 return null;
131}
126pub const getFileSource = @compileError("Deprecated; use the return value from add()/addCopyFile(), or use files[i].getFileSource()");
132127
133128/// Returns a `FileSource` representing the base directory that contains all the
134129/// files from this `WriteFile`.
test/src/Cases.zig+5-3
......@@ -494,10 +494,12 @@ pub fn lowerToBuildSteps(
494494 }
495495
496496 const writefiles = b.addWriteFiles();
497 var file_sources = std.StringHashMap(std.Build.FileSource).init(b.allocator);
498 defer file_sources.deinit();
497499 for (update.files.items) |file| {
498 writefiles.add(file.path, file.src);
500 file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM");
499501 }
500 const root_source_file = writefiles.getFileSource(update.files.items[0].path).?;
502 const root_source_file = writefiles.files.items[0].getFileSource();
501503
502504 const artifact = if (case.is_test) b.addTest(.{
503505 .root_source_file = root_source_file,
......@@ -540,7 +542,7 @@ pub fn lowerToBuildSteps(
540542
541543 for (case.deps.items) |dep| {
542544 artifact.addAnonymousModule(dep.name, .{
543 .source_file = writefiles.getFileSource(dep.path).?,
545 .source_file = file_sources.get(dep.path).?,
544546 });
545547 }
546548
test/src/CompareOutput.zig+4-6
......@@ -82,7 +82,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
8282
8383 const write_src = b.addWriteFiles();
8484 for (case.sources.items) |src_file| {
85 write_src.add(src_file.filename, src_file.source);
85 _ = write_src.add(src_file.filename, src_file.source);
8686 }
8787
8888 switch (case.special) {
......@@ -99,7 +99,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
9999 .target = .{},
100100 .optimize = .Debug,
101101 });
102 exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?);
102 exe.addAssemblyFileSource(write_src.files.items[0].getFileSource());
103103
104104 const run = b.addRunArtifact(exe);
105105 run.setName(annotated_case_name);
......@@ -117,10 +117,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
117117 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
118118 }
119119
120 const basename = case.sources.items[0].filename;
121120 const exe = b.addExecutable(.{
122121 .name = "test",
123 .root_source_file = write_src.getFileSource(basename).?,
122 .root_source_file = write_src.files.items[0].getFileSource(),
124123 .optimize = optimize,
125124 .target = .{},
126125 });
......@@ -144,10 +143,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
144143 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
145144 }
146145
147 const basename = case.sources.items[0].filename;
148146 const exe = b.addExecutable(.{
149147 .name = "test",
150 .root_source_file = write_src.getFileSource(basename).?,
148 .root_source_file = write_src.files.items[0].getFileSource(),
151149 .target = .{},
152150 .optimize = .Debug,
153151 });
test/src/StackTrace.zig+2-3
......@@ -72,11 +72,10 @@ fn addExpect(
7272 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
7373 }
7474
75 const src_basename = "source.zig";
76 const write_src = b.addWriteFile(src_basename, source);
75 const write_src = b.addWriteFile("source.zig", source);
7776 const exe = b.addExecutable(.{
7877 .name = "test",
79 .root_source_file = write_src.getFileSource(src_basename).?,
78 .root_source_file = write_src.files.items[0].getFileSource(),
8079 .optimize = optimize_mode,
8180 .target = .{},
8281 });
test/src/run_translated_c.zig+2-2
......@@ -82,10 +82,10 @@ pub const RunTranslatedCContext = struct {
8282
8383 const write_src = b.addWriteFiles();
8484 for (case.sources.items) |src_file| {
85 write_src.add(src_file.filename, src_file.source);
85 _ = write_src.add(src_file.filename, src_file.source);
8686 }
8787 const translate_c = b.addTranslateC(.{
88 .source_file = write_src.getFileSource(case.sources.items[0].filename).?,
88 .source_file = write_src.files.items[0].getFileSource(),
8989 .target = .{},
9090 .optimize = .Debug,
9191 });
test/src/translate_c.zig+2-2
......@@ -104,11 +104,11 @@ pub const TranslateCContext = struct {
104104
105105 const write_src = b.addWriteFiles();
106106 for (case.sources.items) |src_file| {
107 write_src.add(src_file.filename, src_file.source);
107 _ = write_src.add(src_file.filename, src_file.source);
108108 }
109109
110110 const translate_c = b.addTranslateC(.{
111 .source_file = write_src.getFileSource(case.sources.items[0].filename).?,
111 .source_file = write_src.files.items[0].getFileSource(),
112112 .target = case.target,
113113 .optimize = .Debug,
114114 });
test/tests.zig+1-1
......@@ -759,7 +759,7 @@ pub fn addCliTests(b: *std.Build) *Step {
759759 "-fno-emit-bin", "-fno-emit-h",
760760 "-fstrip", "-OReleaseFast",
761761 });
762 run.addFileSourceArg(writefile.getFileSource("example.zig").?);
762 run.addFileSourceArg(writefile.files.items[0].getFileSource());
763763 const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s");
764764
765765 const checkfile = b.addCheckFile(example_s, .{