authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2025-03-24 00:01:28+01:00
committergravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2025-07-20 18:28:36+02:00
logfd5eba9358ebf2f498b7c35bae34267f06d070d2
treedf1ce9f5b186477d06e80c16cc890d0fa61fa398
parent1a9fae2a70371fdbd77446fd5173162bfa065624

Coerce slice-like arguments passed to `b.dependency()`

You can now pass string literals as options.

2 files changed, 76 insertions(+), 11 deletions(-)

lib/std/Build.zig+34
...@@ -511,6 +511,40 @@ fn addUserInputOptionFromArg(...@@ -511,6 +511,40 @@ fn addUserInputOptionFromArg(
511 .used = false,511 .used = false,
512 }) catch @panic("OOM");512 }) catch @panic("OOM");
513 },513 },
514 .pointer => |ptr_info| switch (ptr_info.size) {
515 .one => switch (@typeInfo(ptr_info.child)) {
516 .array => |array_info| {
517 comptime var slice_info = ptr_info;
518 slice_info.size = .slice;
519 slice_info.is_const = true;
520 slice_info.child = array_info.child;
521 slice_info.sentinel_ptr = null;
522 addUserInputOptionFromArg(
523 arena,
524 map,
525 field,
526 @Type(.{ .pointer = slice_info }),
527 maybe_value orelse null,
528 );
529 return;
530 },
531 else => {},
532 },
533 .slice => {
534 comptime var slice_info = ptr_info;
535 slice_info.is_const = true;
536 slice_info.sentinel_ptr = null;
537 addUserInputOptionFromArg(
538 arena,
539 map,
540 field,
541 @Type(.{ .pointer = slice_info }),
542 maybe_value orelse null,
543 );
544 return;
545 },
546 else => {},
547 },
514 .null => unreachable,548 .null => unreachable,
515 .optional => |info| switch (@typeInfo(info.child)) {549 .optional => |info| switch (@typeInfo(info.child)) {
516 .optional => {},550 .optional => {},
test/standalone/dependency_options/build.zig+42-11
...@@ -81,25 +81,56 @@ pub fn build(b: *std.Build) !void {...@@ -81,25 +81,56 @@ pub fn build(b: *std.Build) !void {
8181
82 if (all_specified_optional != all_specified) return error.TestFailed;82 if (all_specified_optional != all_specified) return error.TestFailed;
8383
84 const all_specified_literal = b.dependency("other", .{
85 .target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),
86 .optimize = .ReleaseSafe,
87 .bool = true,
88 .int = 123,
89 .float = 0.5,
90 .string = "abc",
91 .string_list = &[_][]const u8{ "a", "b", "c" },
92 .lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),
93 .lazy_path_list = &[_]std.Build.LazyPath{
94 .{ .cwd_relative = "a.txt" },
95 .{ .cwd_relative = "b.txt" },
96 .{ .cwd_relative = "c.txt" },
97 },
98 .@"enum" = .alfa,
99 //.enum_list = &[_]Enum{ .alfa, .bravo, .charlie },
100 //.build_id = @as(std.zig.BuildId, .uuid),
101 });
102
103 if (all_specified_literal != all_specified) return error.TestFailed;
104
105 var mut_string_buf = "abc".*;
106 const mut_string: []u8 = &mut_string_buf;
107 var mut_string_list_buf = [_][]const u8{ "a", "b", "c" };
108 const mut_string_list: [][]const u8 = &mut_string_list_buf;
109 var mut_lazy_path_list_buf = [_]std.Build.LazyPath{
110 .{ .cwd_relative = "a.txt" },
111 .{ .cwd_relative = "b.txt" },
112 .{ .cwd_relative = "c.txt" },
113 };
114 const mut_lazy_path_list: []std.Build.LazyPath = &mut_lazy_path_list_buf;
115 var mut_enum_list_buf = [_]Enum{ .alfa, .bravo, .charlie };
116 const mut_enum_list: []Enum = &mut_enum_list_buf;
117 _ = mut_enum_list;
118
84 // Most supported option types are serialized to a string representation,119 // Most supported option types are serialized to a string representation,
85 // so alternative representations of the same option value should resolve120 // so alternative representations of the same option value should resolve
86 // to the same cached dependency instance.121 // to the same cached dependency instance.
87 const all_specified_alt = b.dependency("other", .{122 const all_specified_alt = b.dependency("other", .{
88 .target = @as(std.Target.Query, .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),123 .target = @as(std.Target.Query, .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),
89 .optimize = @as([]const u8, "ReleaseSafe"),124 .optimize = "ReleaseSafe",
90 .bool = .true,125 .bool = .true,
91 .int = @as([]const u8, "123"),126 .int = "123",
92 .float = @as(f16, 0.5),127 .float = @as(f16, 0.5),
93 .string = .abc,128 .string = mut_string,
94 .string_list = @as([]const []const u8, &.{ "a", "b", "c" }),129 .string_list = mut_string_list,
95 .lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),130 .lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),
96 .lazy_path_list = @as([]const std.Build.LazyPath, &.{131 .lazy_path_list = mut_lazy_path_list,
97 .{ .cwd_relative = "a.txt" },132 .@"enum" = "alfa",
98 .{ .cwd_relative = "b.txt" },133 //.enum_list = mut_enum_list,
99 .{ .cwd_relative = "c.txt" },
100 }),
101 .@"enum" = @as([]const u8, "alfa"),
102 //.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),
103 //.build_id = @as(std.zig.BuildId, .uuid),134 //.build_id = @as(std.zig.BuildId, .uuid),
104 });135 });
105136