Skip to content

Commit fd5eba9

Browse files
committed
Coerce slice-like arguments passed to b.dependency()
You can now pass string literals as options.
1 parent 1a9fae2 commit fd5eba9

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

lib/std/Build.zig

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,40 @@ fn addUserInputOptionFromArg(
511511
.used = false,
512512
}) catch @panic("OOM");
513513
},
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+
},
514548
.null => unreachable,
515549
.optional => |info| switch (@typeInfo(info.child)) {
516550
.optional => {},

test/standalone/dependency_options/build.zig

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,56 @@ pub fn build(b: *std.Build) !void {
8181

8282
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+
84119
// Most supported option types are serialized to a string representation,
85120
// so alternative representations of the same option value should resolve
86121
// to the same cached dependency instance.
87122
const all_specified_alt = b.dependency("other", .{
88123
.target = @as(std.Target.Query, .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),
89-
.optimize = @as([]const u8, "ReleaseSafe"),
124+
.optimize = "ReleaseSafe",
90125
.bool = .true,
91-
.int = @as([]const u8, "123"),
126+
.int = "123",
92127
.float = @as(f16, 0.5),
93-
.string = .abc,
94-
.string_list = @as([]const []const u8, &.{ "a", "b", "c" }),
128+
.string = mut_string,
129+
.string_list = mut_string_list,
95130
.lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),
96-
.lazy_path_list = @as([]const std.Build.LazyPath, &.{
97-
.{ .cwd_relative = "a.txt" },
98-
.{ .cwd_relative = "b.txt" },
99-
.{ .cwd_relative = "c.txt" },
100-
}),
101-
.@"enum" = @as([]const u8, "alfa"),
102-
//.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),
131+
.lazy_path_list = mut_lazy_path_list,
132+
.@"enum" = "alfa",
133+
//.enum_list = mut_enum_list,
103134
//.build_id = @as(std.zig.BuildId, .uuid),
104135
});
105136

0 commit comments

Comments
 (0)