add std.build.ConfigHeaderStep

This API converts a config.h.in file into config.h. This is useful when
introducing a build.zig file to an existing C/C++ project that is
configured with autotools or cmake.

The cmake syntax is not implemented yet.
This commit is contained in:
Andrew Kelley
2023-01-07 18:07:10 -07:00
parent e579c88f4c
commit ea792011d1
5 changed files with 260 additions and 12 deletions

View File

@@ -63,14 +63,15 @@ fn make(step: *Step) !void {
// files, then two WriteFileSteps executing in parallel might clobber each other.
// TODO port the cache system from the compiler to zig std lib. Until then
// we use blake2b directly and construct the path, and no "cache hit"
// detection happens; the files are always written.
var hash = std.crypto.hash.blake2.Blake2b384.init(.{});
// we directly construct the path, and no "cache hit" detection happens;
// the files are always written.
// Note there is similar code over in ConfigHeaderStep.
const Hasher = std.crypto.auth.siphash.SipHash128(1, 3);
// Random bytes to make WriteFileStep unique. Refresh this with
// new random bytes when WriteFileStep implementation is modified
// in a non-backwards-compatible way.
hash.update("eagVR1dYXoE7ARDP");
var hash = Hasher.init("eagVR1dYXoE7ARDP");
{
var it = self.files.first;
while (it) |node| : (it = node.next) {
@@ -79,21 +80,23 @@ fn make(step: *Step) !void {
hash.update("|");
}
}
var digest: [48]u8 = undefined;
var digest: [16]u8 = undefined;
hash.final(&digest);
var hash_basename: [64]u8 = undefined;
_ = fs.base64_encoder.encode(&hash_basename, &digest);
_ = std.fmt.bufPrint(
&hash_basename,
"{s}",
.{std.fmt.fmtSliceHexLower(&digest)},
) catch unreachable;
self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
self.builder.cache_root,
"o",
&hash_basename,
});
// TODO replace with something like fs.makePathAndOpenDir
fs.cwd().makePath(self.output_dir) catch |err| {
var dir = fs.cwd().makeOpenPath(self.output_dir, .{}) catch |err| {
std.debug.print("unable to make path {s}: {s}\n", .{ self.output_dir, @errorName(err) });
return err;
};
var dir = try fs.cwd().openDir(self.output_dir, .{});
defer dir.close();
{
var it = self.files.first;