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 {...@@ -759,7 +759,7 @@ pub fn dupePath(self: *Build, bytes: []const u8) []u8 {
759759
760pub fn addWriteFile(self: *Build, file_path: []const u8, data: []const u8) *Step.WriteFile {760pub fn addWriteFile(self: *Build, file_path: []const u8, data: []const u8) *Step.WriteFile {
761 const write_file_step = self.addWriteFiles();761 const write_file_step = self.addWriteFiles();
762 write_file_step.add(file_path, data);762 _ = write_file_step.add(file_path, data);
763 return write_file_step;763 return write_file_step;
764}764}
765765
lib/std/Build/Step/WriteFile.zig+9-14
...@@ -27,6 +27,10 @@ pub const File = struct {...@@ -27,6 +27,10 @@ pub const File = struct {
27 generated_file: std.Build.GeneratedFile,27 generated_file: std.Build.GeneratedFile,
28 sub_path: []const u8,28 sub_path: []const u8,
29 contents: Contents,29 contents: Contents,
30
31 pub fn getFileSource(self: *File) std.Build.FileSource {
32 return .{ .generated = &self.generated_file };
33 }
30};34};
3135
32pub const OutputSourceFile = struct {36pub const OutputSourceFile = struct {
...@@ -55,7 +59,7 @@ pub fn create(owner: *std.Build) *WriteFile {...@@ -55,7 +59,7 @@ pub fn create(owner: *std.Build) *WriteFile {
55 return wf;59 return wf;
56}60}
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 {
59 const b = wf.step.owner;63 const b = wf.step.owner;
60 const gpa = b.allocator;64 const gpa = b.allocator;
61 const file = gpa.create(File) catch @panic("OOM");65 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 {...@@ -65,8 +69,8 @@ pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) void {
65 .contents = .{ .bytes = b.dupe(bytes) },69 .contents = .{ .bytes = b.dupe(bytes) },
66 };70 };
67 wf.files.append(gpa, file) catch @panic("OOM");71 wf.files.append(gpa, file) catch @panic("OOM");
68
69 wf.maybeUpdateName();72 wf.maybeUpdateName();
73 return file.getFileSource();
70}74}
7175
72/// Place the file into the generated directory within the local cache,76/// 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 {...@@ -76,7 +80,7 @@ pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) void {
76/// include sub-directories, in which case this step will ensure the80/// include sub-directories, in which case this step will ensure the
77/// required sub-path exists.81/// required sub-path exists.
78/// This is the option expected to be used most commonly with `addCopyFile`.82/// 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 {
80 const b = wf.step.owner;84 const b = wf.step.owner;
81 const gpa = b.allocator;85 const gpa = b.allocator;
82 const file = gpa.create(File) catch @panic("OOM");86 const file = gpa.create(File) catch @panic("OOM");
...@@ -89,6 +93,7 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con...@@ -89,6 +93,7 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con
8993
90 wf.maybeUpdateName();94 wf.maybeUpdateName();
91 source.addStepDependencies(&wf.step);95 source.addStepDependencies(&wf.step);
96 return file.getFileSource();
92}97}
9398
94/// A path relative to the package root.99/// A path relative to the package root.
...@@ -96,7 +101,6 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con...@@ -96,7 +101,6 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con
96/// used as part of the normal build process, but as a utility occasionally101/// used as part of the normal build process, but as a utility occasionally
97/// run by a developer with intent to modify source files and then commit102/// run by a developer with intent to modify source files and then commit
98/// those changes to version control.103/// those changes to version control.
99/// A file added this way is not available with `getFileSource`.
100pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void {104pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void {
101 const b = wf.step.owner;105 const b = wf.step.owner;
102 wf.output_source_files.append(b.allocator, .{106 wf.output_source_files.append(b.allocator, .{
...@@ -111,7 +115,6 @@ pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_pat...@@ -111,7 +115,6 @@ pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_pat
111/// used as part of the normal build process, but as a utility occasionally115/// used as part of the normal build process, but as a utility occasionally
112/// run by a developer with intent to modify source files and then commit116/// run by a developer with intent to modify source files and then commit
113/// those changes to version control.117/// those changes to version control.
114/// A file added this way is not available with `getFileSource`.
115pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8) void {118pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8) void {
116 const b = wf.step.owner;119 const b = wf.step.owner;
117 wf.output_source_files.append(b.allocator, .{120 wf.output_source_files.append(b.allocator, .{
...@@ -120,15 +123,7 @@ pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8)...@@ -120,15 +123,7 @@ pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8)
120 }) catch @panic("OOM");123 }) catch @panic("OOM");
121}124}
122125
123/// Gets a file source for the given sub_path. If the file does not exist, returns `null`.126pub const getFileSource = @compileError("Deprecated; use the return value from add()/addCopyFile(), or use files[i].getFileSource()");
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}
132127
133/// Returns a `FileSource` representing the base directory that contains all the128/// Returns a `FileSource` representing the base directory that contains all the
134/// files from this `WriteFile`.129/// files from this `WriteFile`.
test/src/Cases.zig+5-3
...@@ -494,10 +494,12 @@ pub fn lowerToBuildSteps(...@@ -494,10 +494,12 @@ pub fn lowerToBuildSteps(
494 }494 }
495495
496 const writefiles = b.addWriteFiles();496 const writefiles = b.addWriteFiles();
497 var file_sources = std.StringHashMap(std.Build.FileSource).init(b.allocator);
498 defer file_sources.deinit();
497 for (update.files.items) |file| {499 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");
499 }501 }
500 const root_source_file = writefiles.getFileSource(update.files.items[0].path).?;502 const root_source_file = writefiles.files.items[0].getFileSource();
501503
502 const artifact = if (case.is_test) b.addTest(.{504 const artifact = if (case.is_test) b.addTest(.{
503 .root_source_file = root_source_file,505 .root_source_file = root_source_file,
...@@ -540,7 +542,7 @@ pub fn lowerToBuildSteps(...@@ -540,7 +542,7 @@ pub fn lowerToBuildSteps(
540542
541 for (case.deps.items) |dep| {543 for (case.deps.items) |dep| {
542 artifact.addAnonymousModule(dep.name, .{544 artifact.addAnonymousModule(dep.name, .{
543 .source_file = writefiles.getFileSource(dep.path).?,545 .source_file = file_sources.get(dep.path).?,
544 });546 });
545 }547 }
546548
test/src/CompareOutput.zig+4-6
...@@ -82,7 +82,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -82,7 +82,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
8282
83 const write_src = b.addWriteFiles();83 const write_src = b.addWriteFiles();
84 for (case.sources.items) |src_file| {84 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);
86 }86 }
8787
88 switch (case.special) {88 switch (case.special) {
...@@ -99,7 +99,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -99,7 +99,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
99 .target = .{},99 .target = .{},
100 .optimize = .Debug,100 .optimize = .Debug,
101 });101 });
102 exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?);102 exe.addAssemblyFileSource(write_src.files.items[0].getFileSource());
103103
104 const run = b.addRunArtifact(exe);104 const run = b.addRunArtifact(exe);
105 run.setName(annotated_case_name);105 run.setName(annotated_case_name);
...@@ -117,10 +117,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -117,10 +117,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
117 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;117 if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
118 }118 }
119119
120 const basename = case.sources.items[0].filename;
121 const exe = b.addExecutable(.{120 const exe = b.addExecutable(.{
122 .name = "test",121 .name = "test",
123 .root_source_file = write_src.getFileSource(basename).?,122 .root_source_file = write_src.files.items[0].getFileSource(),
124 .optimize = optimize,123 .optimize = optimize,
125 .target = .{},124 .target = .{},
126 });125 });
...@@ -144,10 +143,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {...@@ -144,10 +143,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
144 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;143 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
145 }144 }
146145
147 const basename = case.sources.items[0].filename;
148 const exe = b.addExecutable(.{146 const exe = b.addExecutable(.{
149 .name = "test",147 .name = "test",
150 .root_source_file = write_src.getFileSource(basename).?,148 .root_source_file = write_src.files.items[0].getFileSource(),
151 .target = .{},149 .target = .{},
152 .optimize = .Debug,150 .optimize = .Debug,
153 });151 });
test/src/StackTrace.zig+2-3
...@@ -72,11 +72,10 @@ fn addExpect(...@@ -72,11 +72,10 @@ fn addExpect(
72 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;72 if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
73 }73 }
7474
75 const src_basename = "source.zig";75 const write_src = b.addWriteFile("source.zig", source);
76 const write_src = b.addWriteFile(src_basename, source);
77 const exe = b.addExecutable(.{76 const exe = b.addExecutable(.{
78 .name = "test",77 .name = "test",
79 .root_source_file = write_src.getFileSource(src_basename).?,78 .root_source_file = write_src.files.items[0].getFileSource(),
80 .optimize = optimize_mode,79 .optimize = optimize_mode,
81 .target = .{},80 .target = .{},
82 });81 });
test/src/run_translated_c.zig+2-2
...@@ -82,10 +82,10 @@ pub const RunTranslatedCContext = struct {...@@ -82,10 +82,10 @@ pub const RunTranslatedCContext = struct {
8282
83 const write_src = b.addWriteFiles();83 const write_src = b.addWriteFiles();
84 for (case.sources.items) |src_file| {84 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);
86 }86 }
87 const translate_c = b.addTranslateC(.{87 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(),
89 .target = .{},89 .target = .{},
90 .optimize = .Debug,90 .optimize = .Debug,
91 });91 });
test/src/translate_c.zig+2-2
...@@ -104,11 +104,11 @@ pub const TranslateCContext = struct {...@@ -104,11 +104,11 @@ pub const TranslateCContext = struct {
104104
105 const write_src = b.addWriteFiles();105 const write_src = b.addWriteFiles();
106 for (case.sources.items) |src_file| {106 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);
108 }108 }
109109
110 const translate_c = b.addTranslateC(.{110 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(),
112 .target = case.target,112 .target = case.target,
113 .optimize = .Debug,113 .optimize = .Debug,
114 });114 });
test/tests.zig+1-1
...@@ -759,7 +759,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -759,7 +759,7 @@ pub fn addCliTests(b: *std.Build) *Step {
759 "-fno-emit-bin", "-fno-emit-h",759 "-fno-emit-bin", "-fno-emit-h",
760 "-fstrip", "-OReleaseFast",760 "-fstrip", "-OReleaseFast",
761 });761 });
762 run.addFileSourceArg(writefile.getFileSource("example.zig").?);762 run.addFileSourceArg(writefile.files.items[0].getFileSource());
763 const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s");763 const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s");
764764
765 const checkfile = b.addCheckFile(example_s, .{765 const checkfile = b.addCheckFile(example_s, .{