Skip to content

Commit ca57115

Browse files
committed
Support passing std.zig.BuildId to b.dependency()
1 parent 2c1a349 commit ca57115

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

lib/std/Build.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,13 @@ fn addUserInputOptionFromArg(
450450
.used = false,
451451
}) catch @panic("OOM");
452452
},
453+
std.zig.BuildId => return if (maybe_value) |v| {
454+
map.put(field.name, .{
455+
.name = field.name,
456+
.value = .{ .scalar = std.fmt.allocPrint(arena, "{f}", .{v}) catch @panic("OOM") },
457+
.used = false,
458+
}) catch @panic("OOM");
459+
},
453460
LazyPath => return if (maybe_value) |v| {
454461
map.put(field.name, .{
455462
.name = field.name,

lib/std/zig.zig

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,27 @@ pub const BuildId = union(enum) {
321321
try std.testing.expectError(error.InvalidCharacter, parse("0xfoobbb"));
322322
try std.testing.expectError(error.InvalidBuildIdStyle, parse("yaddaxxx"));
323323
}
324+
325+
pub fn format(id: BuildId, writer: *std.io.Writer) std.io.Writer.Error!void {
326+
switch (id) {
327+
.none, .fast, .uuid, .sha1, .md5 => {
328+
try writer.writeAll(@tagName(id));
329+
},
330+
.hexstring => |hs| {
331+
try writer.print("0x{x}", .{hs.toSlice()});
332+
},
333+
}
334+
}
335+
336+
test format {
337+
try std.testing.expectFmt("none", "{f}", .{@as(BuildId, .none)});
338+
try std.testing.expectFmt("fast", "{f}", .{@as(BuildId, .fast)});
339+
try std.testing.expectFmt("uuid", "{f}", .{@as(BuildId, .uuid)});
340+
try std.testing.expectFmt("sha1", "{f}", .{@as(BuildId, .sha1)});
341+
try std.testing.expectFmt("md5", "{f}", .{@as(BuildId, .md5)});
342+
try std.testing.expectFmt("0x", "{f}", .{BuildId.initHexString("")});
343+
try std.testing.expectFmt("0x1234cdef", "{f}", .{BuildId.initHexString("\x12\x34\xcd\xef")});
344+
}
324345
};
325346

326347
pub const LtoMode = enum { none, full, thin };

test/standalone/dependency_options/build.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ pub fn build(b: *std.Build) !void {
5151
}),
5252
.@"enum" = @as(Enum, .alfa),
5353
.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),
54-
//.build_id = @as(std.zig.BuildId, .uuid),
54+
.build_id = @as(std.zig.BuildId, .uuid),
55+
.hex_build_id = std.zig.BuildId.initHexString("\x12\x34\xcd\xef"),
5556
});
5657

5758
const all_specified_mod = all_specified.module("dummy");
@@ -76,7 +77,8 @@ pub fn build(b: *std.Build) !void {
7677
}),
7778
.@"enum" = @as(?Enum, .alfa),
7879
.enum_list = @as(?[]const Enum, &.{ .alfa, .bravo, .charlie }),
79-
//.build_id = @as(?std.zig.BuildId, .uuid),
80+
.build_id = @as(?std.zig.BuildId, .uuid),
81+
.hex_build_id = @as(?std.zig.BuildId, .initHexString("\x12\x34\xcd\xef")),
8082
});
8183

8284
if (all_specified_optional != all_specified) return error.TestFailed;
@@ -97,7 +99,8 @@ pub fn build(b: *std.Build) !void {
9799
},
98100
.@"enum" = .alfa,
99101
.enum_list = &[_]Enum{ .alfa, .bravo, .charlie },
100-
//.build_id = @as(std.zig.BuildId, .uuid),
102+
.build_id = .uuid,
103+
.hex_build_id = std.zig.BuildId.initHexString("\x12\x34\xcd\xef"),
101104
});
102105

103106
if (all_specified_literal != all_specified) return error.TestFailed;
@@ -130,7 +133,8 @@ pub fn build(b: *std.Build) !void {
130133
.lazy_path_list = mut_lazy_path_list,
131134
.@"enum" = "alfa",
132135
.enum_list = mut_enum_list,
133-
//.build_id = @as(std.zig.BuildId, .uuid),
136+
.build_id = "uuid",
137+
.hex_build_id = "0x1234cdef",
134138
});
135139

136140
if (all_specified_alt != all_specified) return error.TestFailed;

test/standalone/dependency_options/other/build.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn build(b: *std.Build) !void {
2020
const expected_enum: Enum = .alfa;
2121
const expected_enum_list: []const Enum = &.{ .alfa, .bravo, .charlie };
2222
const expected_build_id: std.zig.BuildId = .uuid;
23+
const expected_hex_build_id: std.zig.BuildId = .initHexString("\x12\x34\xcd\xef");
2324

2425
const @"bool" = b.option(bool, "bool", "bool") orelse expected_bool;
2526
const int = b.option(i64, "int", "int") orelse expected_int;
@@ -31,6 +32,7 @@ pub fn build(b: *std.Build) !void {
3132
const @"enum" = b.option(Enum, "enum", "enum") orelse expected_enum;
3233
const enum_list = b.option([]const Enum, "enum_list", "enum_list") orelse expected_enum_list;
3334
const build_id = b.option(std.zig.BuildId, "build_id", "build_id") orelse expected_build_id;
35+
const hex_build_id = b.option(std.zig.BuildId, "hex_build_id", "hex_build_id") orelse expected_hex_build_id;
3436

3537
if (@"bool" != expected_bool) return error.TestFailed;
3638
if (int != expected_int) return error.TestFailed;
@@ -47,6 +49,7 @@ pub fn build(b: *std.Build) !void {
4749
if (@"enum" != expected_enum) return error.TestFailed;
4850
if (!std.mem.eql(Enum, enum_list, expected_enum_list)) return error.TestFailed;
4951
if (!std.meta.eql(build_id, expected_build_id)) return error.TestFailed;
52+
if (!hex_build_id.eql(expected_hex_build_id)) return error.TestFailed;
5053

5154
_ = b.addModule("dummy", .{
5255
.root_source_file = b.path("build.zig"),

0 commit comments

Comments
 (0)