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.
This commit is contained in:
Jonathan Marler
2024-07-02 06:15:29 -06:00
committed by Andrew Kelley
parent b67caf72e3
commit d9f1a952b8
12 changed files with 96 additions and 47 deletions

View File

@@ -17,8 +17,8 @@ const WriteFile = @This();
step: Step,
// The elements here are pointers because we need stable pointers for the GeneratedFile field.
files: std.ArrayListUnmanaged(*File),
directories: std.ArrayListUnmanaged(*Directory),
files: std.ArrayListUnmanaged(File),
directories: std.ArrayListUnmanaged(Directory),
output_source_files: std.ArrayListUnmanaged(OutputSourceFile),
generated_directory: std.Build.GeneratedFile,
@@ -26,20 +26,14 @@ generated_directory: std.Build.GeneratedFile,
pub const base_id: Step.Id = .write_file;
pub const File = struct {
generated_file: std.Build.GeneratedFile,
sub_path: []const u8,
contents: Contents,
pub fn getPath(file: *File) std.Build.LazyPath {
return .{ .generated = .{ .file = &file.generated_file } };
}
};
pub const Directory = struct {
source: std.Build.LazyPath,
sub_path: []const u8,
options: Options,
generated_dir: std.Build.GeneratedFile,
pub const Options = struct {
/// File paths that end in any of these suffixes will be excluded from copying.
@@ -56,10 +50,6 @@ pub const Directory = struct {
};
}
};
pub fn getPath(dir: *Directory) std.Build.LazyPath {
return .{ .generated = .{ .file = &dir.generated_dir } };
}
};
pub const OutputSourceFile = struct {
@@ -92,15 +82,18 @@ pub fn create(owner: *std.Build) *WriteFile {
pub fn add(write_file: *WriteFile, sub_path: []const u8, bytes: []const u8) std.Build.LazyPath {
const b = write_file.step.owner;
const gpa = b.allocator;
const file = gpa.create(File) catch @panic("OOM");
file.* = .{
.generated_file = .{ .step = &write_file.step },
const file = File{
.sub_path = b.dupePath(sub_path),
.contents = .{ .bytes = b.dupe(bytes) },
};
write_file.files.append(gpa, file) catch @panic("OOM");
write_file.maybeUpdateName();
return file.getPath();
return .{
.generated = .{
.file = &write_file.generated_directory,
.sub_path = file.sub_path,
},
};
}
/// 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.
pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPath {
const b = write_file.step.owner;
const gpa = b.allocator;
const file = gpa.create(File) catch @panic("OOM");
file.* = .{
.generated_file = .{ .step = &write_file.step },
const file = File{
.sub_path = b.dupePath(sub_path),
.contents = .{ .copy = source },
};
@@ -123,7 +114,12 @@ pub fn addCopyFile(write_file: *WriteFile, source: std.Build.LazyPath, sub_path:
write_file.maybeUpdateName();
source.addStepDependencies(&write_file.step);
return file.getPath();
return .{
.generated = .{
.file = &write_file.generated_directory,
.sub_path = file.sub_path,
},
};
}
/// Copy files matching the specified exclude/include patterns to the specified subdirectory
@@ -137,18 +133,21 @@ pub fn addCopyDirectory(
) std.Build.LazyPath {
const b = write_file.step.owner;
const gpa = b.allocator;
const dir = gpa.create(Directory) catch @panic("OOM");
dir.* = .{
const dir = Directory{
.source = source.dupe(b),
.sub_path = b.dupePath(sub_path),
.options = options.dupe(b),
.generated_dir = .{ .step = &write_file.step },
};
write_file.directories.append(gpa, dir) catch @panic("OOM");
write_file.maybeUpdateName();
source.addStepDependencies(&write_file.step);
return dir.getPath();
return .{
.generated = .{
.file = &write_file.generated_directory,
.sub_path = dir.sub_path,
},
};
}
/// A path relative to the package root.
@@ -278,11 +277,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
if (try step.cacheHit(&man)) {
const digest = man.final();
for (write_file.files.items) |file| {
file.generated_file.path = try b.cache_root.join(b.allocator, &.{
"o", &digest, file.sub_path,
});
}
write_file.generated_directory.path = try b.cache_root.join(b.allocator, &.{ "o", &digest });
return;
}
@@ -342,10 +336,6 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
_ = prev_status;
},
}
file.generated_file.path = try b.cache_root.join(b.allocator, &.{
cache_path, file.sub_path,
});
}
for (write_file.directories.items) |dir| {
const full_src_dir_path = dir.source.getPath2(b, step);