From fd5eba9358ebf2f498b7c35bae34267f06d070d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20=C3=85stholm?= Date: Mon, 24 Mar 2025 00:01:28 +0100 Subject: [PATCH] Coerce slice-like arguments passed to `b.dependency()` You can now pass string literals as options. --- lib/std/Build.zig | 34 +++++++++++++ test/standalone/dependency_options/build.zig | 53 ++++++++++++++++---- 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 21eb5196ed..efff88b469 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -511,6 +511,40 @@ fn addUserInputOptionFromArg( .used = false, }) catch @panic("OOM"); }, + .pointer => |ptr_info| switch (ptr_info.size) { + .one => switch (@typeInfo(ptr_info.child)) { + .array => |array_info| { + comptime var slice_info = ptr_info; + slice_info.size = .slice; + slice_info.is_const = true; + slice_info.child = array_info.child; + slice_info.sentinel_ptr = null; + addUserInputOptionFromArg( + arena, + map, + field, + @Type(.{ .pointer = slice_info }), + maybe_value orelse null, + ); + return; + }, + else => {}, + }, + .slice => { + comptime var slice_info = ptr_info; + slice_info.is_const = true; + slice_info.sentinel_ptr = null; + addUserInputOptionFromArg( + arena, + map, + field, + @Type(.{ .pointer = slice_info }), + maybe_value orelse null, + ); + return; + }, + else => {}, + }, .null => unreachable, .optional => |info| switch (@typeInfo(info.child)) { .optional => {}, diff --git a/test/standalone/dependency_options/build.zig b/test/standalone/dependency_options/build.zig index 27ce63834d..351a82ccdb 100644 --- a/test/standalone/dependency_options/build.zig +++ b/test/standalone/dependency_options/build.zig @@ -81,25 +81,56 @@ pub fn build(b: *std.Build) !void { if (all_specified_optional != all_specified) return error.TestFailed; + const all_specified_literal = b.dependency("other", .{ + .target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }), + .optimize = .ReleaseSafe, + .bool = true, + .int = 123, + .float = 0.5, + .string = "abc", + .string_list = &[_][]const u8{ "a", "b", "c" }, + .lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }), + .lazy_path_list = &[_]std.Build.LazyPath{ + .{ .cwd_relative = "a.txt" }, + .{ .cwd_relative = "b.txt" }, + .{ .cwd_relative = "c.txt" }, + }, + .@"enum" = .alfa, + //.enum_list = &[_]Enum{ .alfa, .bravo, .charlie }, + //.build_id = @as(std.zig.BuildId, .uuid), + }); + + if (all_specified_literal != all_specified) return error.TestFailed; + + var mut_string_buf = "abc".*; + const mut_string: []u8 = &mut_string_buf; + var mut_string_list_buf = [_][]const u8{ "a", "b", "c" }; + const mut_string_list: [][]const u8 = &mut_string_list_buf; + var mut_lazy_path_list_buf = [_]std.Build.LazyPath{ + .{ .cwd_relative = "a.txt" }, + .{ .cwd_relative = "b.txt" }, + .{ .cwd_relative = "c.txt" }, + }; + const mut_lazy_path_list: []std.Build.LazyPath = &mut_lazy_path_list_buf; + var mut_enum_list_buf = [_]Enum{ .alfa, .bravo, .charlie }; + const mut_enum_list: []Enum = &mut_enum_list_buf; + _ = mut_enum_list; + // Most supported option types are serialized to a string representation, // so alternative representations of the same option value should resolve // to the same cached dependency instance. const all_specified_alt = b.dependency("other", .{ .target = @as(std.Target.Query, .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }), - .optimize = @as([]const u8, "ReleaseSafe"), + .optimize = "ReleaseSafe", .bool = .true, - .int = @as([]const u8, "123"), + .int = "123", .float = @as(f16, 0.5), - .string = .abc, - .string_list = @as([]const []const u8, &.{ "a", "b", "c" }), + .string = mut_string, + .string_list = mut_string_list, .lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }), - .lazy_path_list = @as([]const std.Build.LazyPath, &.{ - .{ .cwd_relative = "a.txt" }, - .{ .cwd_relative = "b.txt" }, - .{ .cwd_relative = "c.txt" }, - }), - .@"enum" = @as([]const u8, "alfa"), - //.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }), + .lazy_path_list = mut_lazy_path_list, + .@"enum" = "alfa", + //.enum_list = mut_enum_list, //.build_id = @as(std.zig.BuildId, .uuid), });