-
Notifications
You must be signed in to change notification settings - Fork 39
/
build_helpers.zig
65 lines (59 loc) · 1.95 KB
/
build_helpers.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const std = @import("std");
/// Represents a dependency on an external package.
pub const Dependency = struct {
name: []const u8,
module_name: []const u8,
};
/// Generate an array of Build.Module.Import from the external dependencies.
/// # Arguments
/// * `b` - The build object.
/// * `external_dependencies` - The external dependencies.
/// * `dependencies_opts` - The options to use when generating the dependency modules.
/// # Returns
/// A new array of Build.Module.Import.
pub fn generateModuleDependencies(
b: *std.Build,
external_dependencies: []const Dependency,
dependencies_opts: anytype,
) ![]std.Build.Module.Import {
var dependency_modules = std.ArrayList(*std.Build.Module).init(b.allocator);
defer _ = dependency_modules.deinit();
// Populate dependency modules.
for (external_dependencies) |dep| {
const module = b.dependency(
dep.name,
dependencies_opts,
).module(dep.module_name);
_ = dependency_modules.append(module) catch unreachable;
}
return try toModuleDependencyArray(
b.allocator,
dependency_modules.items,
external_dependencies,
);
}
/// Convert an array of Build.Module pointers to an array of Build.Module.Import.
/// # Arguments
/// * `allocator` - The allocator to use for the new array.
/// * `modules` - The array of Build.Module pointers to convert.
/// * `ext_deps` - The array of external dependencies.
/// # Returns
/// A new array of Build.Module.Import.
fn toModuleDependencyArray(
allocator: std.mem.Allocator,
modules: []const *std.Build.Module,
ext_deps: []const Dependency,
) ![]std.Build.Module.Import {
var deps = std.ArrayList(std.Build.Module.Import).init(allocator);
defer deps.deinit();
for (
modules,
0..,
) |module_ptr, i| {
try deps.append(.{
.name = ext_deps[i].name,
.module = module_ptr,
});
}
return deps.toOwnedSlice();
}