authorgravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2025-03-23 20:38:41+01:00
committergravatar for carl@astholm.seCarl Åstholm <carl@astholm.se> 2025-07-20 18:28:36+02:00
log00bc72b5ff01c7f8ceb4b58e82614e22a147ccc8
treea80034b19a078a6fcba88dd07132b2892b12355e
parentb92b55ab8e11614a587929bc66c023b9fe7cf7f3

Add standalone test case for passing options to dependencies


5 files changed, 140 insertions(+), 0 deletions(-)

test/standalone/build.zig.zon+3
...@@ -181,6 +181,9 @@...@@ -181,6 +181,9 @@
181 .install_headers = .{181 .install_headers = .{
182 .path = "install_headers",182 .path = "install_headers",
183 },183 },
184 .dependency_options = .{
185 .path = "dependency_options",
186 },
184 .dependencyFromBuildZig = .{187 .dependencyFromBuildZig = .{
185 .path = "dependencyFromBuildZig",188 .path = "dependencyFromBuildZig",
186 },189 },
test/standalone/dependency_options/build.zig created+63
...@@ -0,0 +1,63 @@
1const std = @import("std");
2
3pub const Enum = enum { alfa, bravo, charlie };
4
5pub fn build(b: *std.Build) !void {
6 const test_step = b.step("test", "Test passing options to a dependency");
7 b.default_step = test_step;
8
9 const none_specified = b.dependency("other", .{});
10
11 const none_specified_mod = none_specified.module("dummy");
12 if (!none_specified_mod.resolved_target.?.query.eql(b.graph.host.query)) return error.TestFailed;
13 if (none_specified_mod.optimize.? != .Debug) return error.TestFailed;
14
15 const all_specified = b.dependency("other", .{
16 .target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),
17 .optimize = @as(std.builtin.OptimizeMode, .ReleaseSafe),
18 .bool = @as(bool, true),
19 .int = @as(i64, 123),
20 .float = @as(f64, 0.5),
21 .string = @as([]const u8, "abc"),
22 .string_list = @as([]const []const u8, &.{ "a", "b", "c" }),
23 .lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),
24 .lazy_path_list = @as([]const std.Build.LazyPath, &.{
25 .{ .cwd_relative = "a.txt" },
26 .{ .cwd_relative = "b.txt" },
27 .{ .cwd_relative = "c.txt" },
28 }),
29 .@"enum" = @as(Enum, .alfa),
30 //.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),
31 //.build_id = @as(std.zig.BuildId, .uuid),
32 });
33
34 const all_specified_mod = all_specified.module("dummy");
35 if (all_specified_mod.resolved_target.?.result.cpu.arch != .x86_64) return error.TestFailed;
36 if (all_specified_mod.resolved_target.?.result.os.tag != .windows) return error.TestFailed;
37 if (all_specified_mod.resolved_target.?.result.abi != .gnu) return error.TestFailed;
38 if (all_specified_mod.optimize.? != .ReleaseSafe) return error.TestFailed;
39
40 // Most supported option types are serialized to a string representation,
41 // so alternative representations of the same option value should resolve
42 // to the same cached dependency instance.
43 const all_specified_alt = b.dependency("other", .{
44 .target = @as(std.Target.Query, .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }),
45 .optimize = @as([]const u8, "ReleaseSafe"),
46 .bool = .true,
47 .int = @as([]const u8, "123"),
48 .float = @as(f16, 0.5),
49 .string = .abc,
50 .string_list = @as([]const []const u8, &.{ "a", "b", "c" }),
51 .lazy_path = @as(std.Build.LazyPath, .{ .cwd_relative = "abc.txt" }),
52 .lazy_path_list = @as([]const std.Build.LazyPath, &.{
53 .{ .cwd_relative = "a.txt" },
54 .{ .cwd_relative = "b.txt" },
55 .{ .cwd_relative = "c.txt" },
56 }),
57 .@"enum" = @as([]const u8, "alfa"),
58 //.enum_list = @as([]const Enum, &.{ .alfa, .bravo, .charlie }),
59 //.build_id = @as(std.zig.BuildId, .uuid),
60 });
61
62 if (all_specified != all_specified_alt) return error.TestFailed;
63}
test/standalone/dependency_options/build.zig.zon created+11
...@@ -0,0 +1,11 @@
1.{
2 .name = .dependency_options,
3 .fingerprint = 0x3e3ce1c1f92ba47e,
4 .version = "0.0.0",
5 .dependencies = .{
6 .other = .{
7 .path = "other",
8 },
9 },
10 .paths = .{""},
11}
test/standalone/dependency_options/other/build.zig created+56
...@@ -0,0 +1,56 @@
1const std = @import("std");
2
3pub const Enum = enum { alfa, bravo, charlie };
4
5pub fn build(b: *std.Build) !void {
6 const target = b.standardTargetOptions(.{});
7 const optimize = b.standardOptimizeOption(.{});
8
9 const expected_bool: bool = true;
10 const expected_int: i64 = 123;
11 const expected_float: f64 = 0.5;
12 const expected_string: []const u8 = "abc";
13 const expected_string_list: []const []const u8 = &.{ "a", "b", "c" };
14 const expected_lazy_path: std.Build.LazyPath = .{ .cwd_relative = "abc.txt" };
15 const expected_lazy_path_list: []const std.Build.LazyPath = &.{
16 .{ .cwd_relative = "a.txt" },
17 .{ .cwd_relative = "b.txt" },
18 .{ .cwd_relative = "c.txt" },
19 };
20 const expected_enum: Enum = .alfa;
21 const expected_enum_list: []const Enum = &.{ .alfa, .bravo, .charlie };
22 const expected_build_id: std.zig.BuildId = .uuid;
23
24 const @"bool" = b.option(bool, "bool", "bool") orelse expected_bool;
25 const int = b.option(i64, "int", "int") orelse expected_int;
26 const float = b.option(f64, "float", "float") orelse expected_float;
27 const string = b.option([]const u8, "string", "string") orelse expected_string;
28 const string_list = b.option([]const []const u8, "string_list", "string_list") orelse expected_string_list;
29 const lazy_path = b.option(std.Build.LazyPath, "lazy_path", "lazy_path") orelse expected_lazy_path;
30 const lazy_path_list = b.option([]const std.Build.LazyPath, "lazy_path_list", "lazy_path_list") orelse expected_lazy_path_list;
31 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 build_id = b.option(std.zig.BuildId, "build_id", "build_id") orelse expected_build_id;
34
35 if (@"bool" != expected_bool) return error.TestFailed;
36 if (int != expected_int) return error.TestFailed;
37 if (float != expected_float) return error.TestFailed;
38 if (!std.mem.eql(u8, string, expected_string)) return error.TestFailed;
39 if (string_list.len != expected_string_list.len) return error.TestFailed;
40 for (string_list, expected_string_list) |x, y| {
41 if (!std.mem.eql(u8, x, y)) return error.TestFailed;
42 }
43 if (!std.mem.eql(u8, lazy_path.cwd_relative, expected_lazy_path.cwd_relative)) return error.TestFailed;
44 for (lazy_path_list, expected_lazy_path_list) |x, y| {
45 if (!std.mem.eql(u8, x.cwd_relative, y.cwd_relative)) return error.TestFailed;
46 }
47 if (@"enum" != expected_enum) return error.TestFailed;
48 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;
50
51 _ = b.addModule("dummy", .{
52 .root_source_file = b.path("build.zig"),
53 .target = target,
54 .optimize = optimize,
55 });
56}
test/standalone/dependency_options/other/build.zig.zon created+7
...@@ -0,0 +1,7 @@
1.{
2 .name = .other,
3 .fingerprint = 0xd95835207bc8b630,
4 .version = "0.0.0",
5 .dependencies = .{},
6 .paths = .{""},
7}