std.Build: introduce ConfigHeader.getOutputDir, small refactor

`std.Build.Step.ConfigHeader` emits a *directory* containing a config
header under a given sub path, but there's no good way to actually
access that directory as a `LazyPath` in the configure phase. This is
silly; it's perfectly valid to refer to that directory, perhaps to
explicitly pass as a "-I" flag to a different toolchain invoked via a
`Step.Run`. So now, instead of the `GeneratedFile` being the actual
*file*, it should be that *directory*, i.e. `cache/o/<digest>`. We can
then easily get the *file* if needed just by using `LazyPath.path` to go
"deeper", which there is a helper function for.

The legacy `getOutput` function is now a deprecated alias for
`getOutputFile`, and `getOutputDir` is introduced.

`std.Build.Module.IncludeDir.appendZigProcessFlags` needed a fix after
this change, so I took the opportunity to refactor it a little. I was
looking at this function while working on ziglang/translate-c yesterday
and realised it could be expressed much more simply -- particularly
after the `ConfigHeader` change here.

I had to update the test `standalone/cmakedefine/` -- it turns out this
test was well and truly reaching into build system internals, and doing
horrible not-really-allowed stuff like overriding the `makeFn` of a
`TopLevelStep`. To top it all off, the test forgot to set
`b.default_step` to its "test" step, so the test never even ran. I've
refactored it to follow accepted practices and to actually, like, work.
This commit is contained in:
mlugg
2025-06-17 11:24:58 +01:00
parent a92427bf27
commit f3c0555975
4 changed files with 83 additions and 71 deletions

View File

@@ -39,7 +39,8 @@ pub const Value = union(enum) {
step: Step,
values: std.StringArrayHashMap(Value),
output_file: std.Build.GeneratedFile,
/// This directory contains the generated file under the name `include_path`.
generated_dir: std.Build.GeneratedFile,
style: Style,
max_bytes: usize,
@@ -99,7 +100,7 @@ pub fn create(owner: *std.Build, options: Options) *ConfigHeader {
.max_bytes = options.max_bytes,
.include_path = include_path,
.include_guard_override = options.include_guard_override,
.output_file = .{ .step = &config_header.step },
.generated_dir = .{ .step = &config_header.step },
};
return config_header;
@@ -115,9 +116,15 @@ pub fn addValues(config_header: *ConfigHeader, values: anytype) void {
}
}
pub fn getOutput(config_header: *ConfigHeader) std.Build.LazyPath {
return .{ .generated = .{ .file = &config_header.output_file } };
pub fn getOutputDir(ch: *ConfigHeader) std.Build.LazyPath {
return .{ .generated = .{ .file = &ch.generated_dir } };
}
pub fn getOutputFile(ch: *ConfigHeader) std.Build.LazyPath {
return ch.getOutputDir().path(ch.step.owner, ch.include_path);
}
/// Deprecated; use `getOutputFile`.
pub const getOutput = getOutputFile;
fn addValueInner(config_header: *ConfigHeader, name: []const u8, comptime T: type, value: T) !void {
switch (@typeInfo(T)) {
@@ -234,9 +241,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
if (try step.cacheHit(&man)) {
const digest = man.final();
config_header.output_file.path = try b.cache_root.join(arena, &.{
"o", &digest, config_header.include_path,
});
config_header.generated_dir.path = try b.cache_root.join(arena, &.{ "o", &digest });
return;
}
@@ -262,7 +267,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
});
};
config_header.output_file.path = try b.cache_root.join(arena, &.{sub_path});
config_header.generated_dir.path = try b.cache_root.join(arena, &.{ "o", &digest });
try man.writeManifest();
}