Skip to content

Commit 00bc72b

Browse files
committed
Add standalone test case for passing options to dependencies
1 parent b92b55a commit 00bc72b

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

test/standalone/build.zig.zon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@
181181
.install_headers = .{
182182
.path = "install_headers",
183183
},
184+
.dependency_options = .{
185+
.path = "dependency_options",
186+
},
184187
.dependencyFromBuildZig = .{
185188
.path = "dependencyFromBuildZig",
186189
},
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const std = @import("std");
2+
3+
pub const Enum = enum { alfa, bravo, charlie };
4+
5+
pub 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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const std = @import("std");
2+
3+
pub const Enum = enum { alfa, bravo, charlie };
4+
5+
pub 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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.{
2+
.name = .other,
3+
.fingerprint = 0xd95835207bc8b630,
4+
.version = "0.0.0",
5+
.dependencies = .{},
6+
.paths = .{""},
7+
}

0 commit comments

Comments
 (0)