authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2025-03-24 14:25:47+01:00
committergravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2025-07-20 18:28:36+02:00
logca57115da7c4603dbcefce1dc9395617e28a86f8
tree04a179eddd93b043e65cf3db5eb2fea955e739a2
parent2c1a349fb9bc9965e309257262665564cff64a79

Support passing `std.zig.BuildId` to `b.dependency()`


4 files changed, 39 insertions(+), 4 deletions(-)

lib/std/Build.zig+7
...@@ -450,6 +450,13 @@ fn addUserInputOptionFromArg(...@@ -450,6 +450,13 @@ fn addUserInputOptionFromArg(
450 .used = false,450 .used = false,
451 }) catch @panic("OOM");451 }) catch @panic("OOM");
452 },452 },
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 },
453 LazyPath => return if (maybe_value) |v| {460 LazyPath => return if (maybe_value) |v| {
454 map.put(field.name, .{461 map.put(field.name, .{
455 .name = field.name,462 .name = field.name,
lib/std/zig.zig+21
...@@ -321,6 +321,27 @@ pub const BuildId = union(enum) {...@@ -321,6 +321,27 @@ pub const BuildId = union(enum) {
321 try std.testing.expectError(error.InvalidCharacter, parse("0xfoobbb"));321 try std.testing.expectError(error.InvalidCharacter, parse("0xfoobbb"));
322 try std.testing.expectError(error.InvalidBuildIdStyle, parse("yaddaxxx"));322 try std.testing.expectError(error.InvalidBuildIdStyle, parse("yaddaxxx"));
323 }323 }
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 }
324};345};
325346
326pub const LtoMode = enum { none, full, thin };347pub const LtoMode = enum { none, full, thin };
test/standalone/dependency_options/build.zig+8-4
...@@ -51,7 +51,8 @@ pub fn build(b: *std.Build) !void {...@@ -51,7 +51,8 @@ pub fn build(b: *std.Build) !void {
51 }),51 }),
52 .@"enum" = @as(Enum, .alfa),52 .@"enum" = @as(Enum, .alfa),
53 .enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),53 .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"),
55 });56 });
5657
57 const all_specified_mod = all_specified.module("dummy");58 const all_specified_mod = all_specified.module("dummy");
...@@ -76,7 +77,8 @@ pub fn build(b: *std.Build) !void {...@@ -76,7 +77,8 @@ pub fn build(b: *std.Build) !void {
76 }),77 }),
77 .@"enum" = @as(?Enum, .alfa),78 .@"enum" = @as(?Enum, .alfa),
78 .enum_list = @as(?[]const Enum, &.{ .alfa, .bravo, .charlie }),79 .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")),
80 });82 });
8183
82 if (all_specified_optional != all_specified) return error.TestFailed;84 if (all_specified_optional != all_specified) return error.TestFailed;
...@@ -97,7 +99,8 @@ pub fn build(b: *std.Build) !void {...@@ -97,7 +99,8 @@ pub fn build(b: *std.Build) !void {
97 },99 },
98 .@"enum" = .alfa,100 .@"enum" = .alfa,
99 .enum_list = &[_]Enum{ .alfa, .bravo, .charlie },101 .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"),
101 });104 });
102105
103 if (all_specified_literal != all_specified) return error.TestFailed;106 if (all_specified_literal != all_specified) return error.TestFailed;
...@@ -130,7 +133,8 @@ pub fn build(b: *std.Build) !void {...@@ -130,7 +133,8 @@ pub fn build(b: *std.Build) !void {
130 .lazy_path_list = mut_lazy_path_list,133 .lazy_path_list = mut_lazy_path_list,
131 .@"enum" = "alfa",134 .@"enum" = "alfa",
132 .enum_list = mut_enum_list,135 .enum_list = mut_enum_list,
133 //.build_id = @as(std.zig.BuildId, .uuid),136 .build_id = "uuid",
137 .hex_build_id = "0x1234cdef",
134 });138 });
135139
136 if (all_specified_alt != all_specified) return error.TestFailed;140 if (all_specified_alt != all_specified) return error.TestFailed;
test/standalone/dependency_options/other/build.zig+3
...@@ -20,6 +20,7 @@ pub fn build(b: *std.Build) !void {...@@ -20,6 +20,7 @@ pub fn build(b: *std.Build) !void {
20 const expected_enum: Enum = .alfa;20 const expected_enum: Enum = .alfa;
21 const expected_enum_list: []const Enum = &.{ .alfa, .bravo, .charlie };21 const expected_enum_list: []const Enum = &.{ .alfa, .bravo, .charlie };
22 const expected_build_id: std.zig.BuildId = .uuid;22 const expected_build_id: std.zig.BuildId = .uuid;
23 const expected_hex_build_id: std.zig.BuildId = .initHexString("\x12\x34\xcd\xef");
2324
24 const @"bool" = b.option(bool, "bool", "bool") orelse expected_bool;25 const @"bool" = b.option(bool, "bool", "bool") orelse expected_bool;
25 const int = b.option(i64, "int", "int") orelse expected_int;26 const int = b.option(i64, "int", "int") orelse expected_int;
...@@ -31,6 +32,7 @@ pub fn build(b: *std.Build) !void {...@@ -31,6 +32,7 @@ pub fn build(b: *std.Build) !void {
31 const @"enum" = b.option(Enum, "enum", "enum") orelse expected_enum;32 const @"enum" = b.option(Enum, "enum", "enum") orelse expected_enum;
32 const enum_list = b.option([]const Enum, "enum_list", "enum_list") orelse expected_enum_list;33 const enum_list = b.option([]const Enum, "enum_list", "enum_list") orelse expected_enum_list;
33 const build_id = b.option(std.zig.BuildId, "build_id", "build_id") orelse expected_build_id;34 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
35 if (@"bool" != expected_bool) return error.TestFailed;37 if (@"bool" != expected_bool) return error.TestFailed;
36 if (int != expected_int) return error.TestFailed;38 if (int != expected_int) return error.TestFailed;
...@@ -47,6 +49,7 @@ pub fn build(b: *std.Build) !void {...@@ -47,6 +49,7 @@ pub fn build(b: *std.Build) !void {
47 if (@"enum" != expected_enum) return error.TestFailed;49 if (@"enum" != expected_enum) return error.TestFailed;
48 if (!std.mem.eql(Enum, enum_list, expected_enum_list)) return error.TestFailed;50 if (!std.mem.eql(Enum, enum_list, expected_enum_list)) return error.TestFailed;
49 if (!std.meta.eql(build_id, expected_build_id)) return error.TestFailed;51 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
51 _ = b.addModule("dummy", .{54 _ = b.addModule("dummy", .{
52 .root_source_file = b.path("build.zig"),55 .root_source_file = b.path("build.zig"),