Fixes WriteFile.getFileSource failure on Windows (#15730)

This commit is contained in:
Mason Remaley 2023-05-24 14:26:07 -07:00 committed by GitHub
parent c9dffc842e
commit 5744ceedb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 26 additions and 32 deletions

View file

@ -759,7 +759,7 @@ pub fn dupePath(self: *Build, bytes: []const u8) []u8 {
pub fn addWriteFile(self: *Build, file_path: []const u8, data: []const u8) *Step.WriteFile { pub fn addWriteFile(self: *Build, file_path: []const u8, data: []const u8) *Step.WriteFile {
const write_file_step = self.addWriteFiles(); const write_file_step = self.addWriteFiles();
write_file_step.add(file_path, data); _ = write_file_step.add(file_path, data);
return write_file_step; return write_file_step;
} }

View file

@ -27,6 +27,10 @@ pub const File = struct {
generated_file: std.Build.GeneratedFile, generated_file: std.Build.GeneratedFile,
sub_path: []const u8, sub_path: []const u8,
contents: Contents, contents: Contents,
pub fn getFileSource(self: *File) std.Build.FileSource {
return .{ .generated = &self.generated_file };
}
}; };
pub const OutputSourceFile = struct { pub const OutputSourceFile = struct {
@ -55,7 +59,7 @@ pub fn create(owner: *std.Build) *WriteFile {
return wf; return wf;
} }
pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) void { pub fn add(wf: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.FileSource {
const b = wf.step.owner; const b = wf.step.owner;
const gpa = b.allocator; const gpa = b.allocator;
const file = gpa.create(File) catch @panic("OOM"); 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 {
.contents = .{ .bytes = b.dupe(bytes) }, .contents = .{ .bytes = b.dupe(bytes) },
}; };
wf.files.append(gpa, file) catch @panic("OOM"); wf.files.append(gpa, file) catch @panic("OOM");
wf.maybeUpdateName(); wf.maybeUpdateName();
return file.getFileSource();
} }
/// Place the file into the generated directory within the local cache, /// 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 {
/// include sub-directories, in which case this step will ensure the /// include sub-directories, in which case this step will ensure the
/// required sub-path exists. /// required sub-path exists.
/// This is the option expected to be used most commonly with `addCopyFile`. /// This is the option expected to be used most commonly with `addCopyFile`.
pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void { pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) std.Build.FileSource {
const b = wf.step.owner; const b = wf.step.owner;
const gpa = b.allocator; const gpa = b.allocator;
const file = gpa.create(File) catch @panic("OOM"); const file = gpa.create(File) catch @panic("OOM");
@ -89,6 +93,7 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con
wf.maybeUpdateName(); wf.maybeUpdateName();
source.addStepDependencies(&wf.step); source.addStepDependencies(&wf.step);
return file.getFileSource();
} }
/// A path relative to the package root. /// A path relative to the package root.
@ -96,7 +101,6 @@ pub fn addCopyFile(wf: *WriteFile, source: std.Build.FileSource, sub_path: []con
/// used as part of the normal build process, but as a utility occasionally /// used as part of the normal build process, but as a utility occasionally
/// run by a developer with intent to modify source files and then commit /// run by a developer with intent to modify source files and then commit
/// those changes to version control. /// those changes to version control.
/// A file added this way is not available with `getFileSource`.
pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void { pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_path: []const u8) void {
const b = wf.step.owner; const b = wf.step.owner;
wf.output_source_files.append(b.allocator, .{ wf.output_source_files.append(b.allocator, .{
@ -111,7 +115,6 @@ pub fn addCopyFileToSource(wf: *WriteFile, source: std.Build.FileSource, sub_pat
/// used as part of the normal build process, but as a utility occasionally /// used as part of the normal build process, but as a utility occasionally
/// run by a developer with intent to modify source files and then commit /// run by a developer with intent to modify source files and then commit
/// those changes to version control. /// those changes to version control.
/// A file added this way is not available with `getFileSource`.
pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8) void { pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8) void {
const b = wf.step.owner; const b = wf.step.owner;
wf.output_source_files.append(b.allocator, .{ wf.output_source_files.append(b.allocator, .{
@ -120,15 +123,7 @@ pub fn addBytesToSource(wf: *WriteFile, bytes: []const u8, sub_path: []const u8)
}) catch @panic("OOM"); }) catch @panic("OOM");
} }
/// Gets a file source for the given sub_path. If the file does not exist, returns `null`. pub const getFileSource = @compileError("Deprecated; use the return value from add()/addCopyFile(), or use files[i].getFileSource()");
pub fn getFileSource(wf: *WriteFile, sub_path: []const u8) ?std.Build.FileSource {
for (wf.files.items) |file| {
if (std.mem.eql(u8, file.sub_path, sub_path)) {
return .{ .generated = &file.generated_file };
}
}
return null;
}
/// Returns a `FileSource` representing the base directory that contains all the /// Returns a `FileSource` representing the base directory that contains all the
/// files from this `WriteFile`. /// files from this `WriteFile`.

View file

@ -494,10 +494,12 @@ pub fn lowerToBuildSteps(
} }
const writefiles = b.addWriteFiles(); const writefiles = b.addWriteFiles();
var file_sources = std.StringHashMap(std.Build.FileSource).init(b.allocator);
defer file_sources.deinit();
for (update.files.items) |file| { for (update.files.items) |file| {
writefiles.add(file.path, file.src); file_sources.put(file.path, writefiles.add(file.path, file.src)) catch @panic("OOM");
} }
const root_source_file = writefiles.getFileSource(update.files.items[0].path).?; const root_source_file = writefiles.files.items[0].getFileSource();
const artifact = if (case.is_test) b.addTest(.{ const artifact = if (case.is_test) b.addTest(.{
.root_source_file = root_source_file, .root_source_file = root_source_file,
@ -540,7 +542,7 @@ pub fn lowerToBuildSteps(
for (case.deps.items) |dep| { for (case.deps.items) |dep| {
artifact.addAnonymousModule(dep.name, .{ artifact.addAnonymousModule(dep.name, .{
.source_file = writefiles.getFileSource(dep.path).?, .source_file = file_sources.get(dep.path).?,
}); });
} }

View file

@ -82,7 +82,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
const write_src = b.addWriteFiles(); const write_src = b.addWriteFiles();
for (case.sources.items) |src_file| { for (case.sources.items) |src_file| {
write_src.add(src_file.filename, src_file.source); _ = write_src.add(src_file.filename, src_file.source);
} }
switch (case.special) { switch (case.special) {
@ -99,7 +99,7 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
.target = .{}, .target = .{},
.optimize = .Debug, .optimize = .Debug,
}); });
exe.addAssemblyFileSource(write_src.getFileSource(case.sources.items[0].filename).?); exe.addAssemblyFileSource(write_src.files.items[0].getFileSource());
const run = b.addRunArtifact(exe); const run = b.addRunArtifact(exe);
run.setName(annotated_case_name); run.setName(annotated_case_name);
@ -117,10 +117,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
if (mem.indexOf(u8, annotated_case_name, filter) == null) continue; if (mem.indexOf(u8, annotated_case_name, filter) == null) continue;
} }
const basename = case.sources.items[0].filename;
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "test", .name = "test",
.root_source_file = write_src.getFileSource(basename).?, .root_source_file = write_src.files.items[0].getFileSource(),
.optimize = optimize, .optimize = optimize,
.target = .{}, .target = .{},
}); });
@ -144,10 +143,9 @@ pub fn addCase(self: *CompareOutput, case: TestCase) void {
if (mem.indexOf(u8, annotated_case_name, filter) == null) return; if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
} }
const basename = case.sources.items[0].filename;
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "test", .name = "test",
.root_source_file = write_src.getFileSource(basename).?, .root_source_file = write_src.files.items[0].getFileSource(),
.target = .{}, .target = .{},
.optimize = .Debug, .optimize = .Debug,
}); });

View file

@ -72,11 +72,10 @@ fn addExpect(
if (mem.indexOf(u8, annotated_case_name, filter) == null) return; if (mem.indexOf(u8, annotated_case_name, filter) == null) return;
} }
const src_basename = "source.zig"; const write_src = b.addWriteFile("source.zig", source);
const write_src = b.addWriteFile(src_basename, source);
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "test", .name = "test",
.root_source_file = write_src.getFileSource(src_basename).?, .root_source_file = write_src.files.items[0].getFileSource(),
.optimize = optimize_mode, .optimize = optimize_mode,
.target = .{}, .target = .{},
}); });

View file

@ -82,10 +82,10 @@ pub const RunTranslatedCContext = struct {
const write_src = b.addWriteFiles(); const write_src = b.addWriteFiles();
for (case.sources.items) |src_file| { for (case.sources.items) |src_file| {
write_src.add(src_file.filename, src_file.source); _ = write_src.add(src_file.filename, src_file.source);
} }
const translate_c = b.addTranslateC(.{ const translate_c = b.addTranslateC(.{
.source_file = write_src.getFileSource(case.sources.items[0].filename).?, .source_file = write_src.files.items[0].getFileSource(),
.target = .{}, .target = .{},
.optimize = .Debug, .optimize = .Debug,
}); });

View file

@ -104,11 +104,11 @@ pub const TranslateCContext = struct {
const write_src = b.addWriteFiles(); const write_src = b.addWriteFiles();
for (case.sources.items) |src_file| { for (case.sources.items) |src_file| {
write_src.add(src_file.filename, src_file.source); _ = write_src.add(src_file.filename, src_file.source);
} }
const translate_c = b.addTranslateC(.{ const translate_c = b.addTranslateC(.{
.source_file = write_src.getFileSource(case.sources.items[0].filename).?, .source_file = write_src.files.items[0].getFileSource(),
.target = case.target, .target = case.target,
.optimize = .Debug, .optimize = .Debug,
}); });

View file

@ -759,7 +759,7 @@ pub fn addCliTests(b: *std.Build) *Step {
"-fno-emit-bin", "-fno-emit-h", "-fno-emit-bin", "-fno-emit-h",
"-fstrip", "-OReleaseFast", "-fstrip", "-OReleaseFast",
}); });
run.addFileSourceArg(writefile.getFileSource("example.zig").?); run.addFileSourceArg(writefile.files.items[0].getFileSource());
const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s"); const example_s = run.addPrefixedOutputFileArg("-femit-asm=", "example.s");
const checkfile = b.addCheckFile(example_s, .{ const checkfile = b.addCheckFile(example_s, .{