pub fn create(owner: *std.Build) *WriteFilepub fn add(wf: *WriteFile, sub_path: []const u8, contents: []const u8) std.Build.LazyPathWrites contents to a file at sub_path relative to the output directory.
sub_path may be a basename, or it may include subdirectories, which are created as needed.
pub fn addCopyFile(wf: *WriteFile, src_file: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPathCopies the provided file to sub_path relative to the output directory.
sub_path may be a basename, or it may include subdirectories, which are created as needed.
pub fn addCopyDirectory(wf: *WriteFile, src_path: std.Build.LazyPath, sub_path: []const u8, options: CopyDirectoryOptions) std.Build.LazyPathCopy files matching the specified exclude/include patterns to the specified subdirectory relative to this step's generated directory.
The returned value is a lazy path to the generated subdirectory.
pub fn getDirectory(wf: *WriteFile) std.Build.LazyPathReturns a LazyPath representing the base directory that contains all the files from this WriteFile.
fn maybeUpdateName(wf: *WriteFile) void1
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185//! WriteFile is used to create a directory in an appropriate location inside
//! the local cache which has a set of files that have either been generated
//! during the build, or are copied from the source package.
const WriteFile = @This();
const std = @import("std");
const Step = std.Build.Step;
const Configuration = std.Build.Configuration;
step: Step,
embeds: std.ArrayList(Embed) = .empty,
copies: std.ArrayList(Copy) = .empty,
directories: std.ArrayList(Directory) = .empty,
generated_directory: Configuration.GeneratedFileIndex,
mode: Mode = .whole_cached,
pub const base_tag: Step.Tag = .write_file;
pub const Mode = union(enum) {
/// Default mode. Integrates with the cache system. The directory should be
/// read-only during the make phase. Any different inputs result in
/// different "o" subdirectory.
whole_cached,
/// In this mode, the directory will be placed inside "tmp" rather than
/// "o", and caching will be skipped. During the `make` phase, the step
/// will always do all the file system operations, and on successful build
/// completion, the dir will be deleted along with all other tmp
/// directories. The directory is therefore eligible to be used for
/// mutations by other steps.
tmp,
/// The operations will not be performed against a freshly created
/// directory, but instead act against a temporary directory.
mutate: std.Build.LazyPath,
};
pub const Embed = Configuration.Step.WriteFile.Embed;
pub const Copy = struct {
sub_path: Configuration.String,
src_file: std.Build.LazyPath,
};
pub const Directory = struct {
sub_path: Configuration.String,
src_path: std.Build.LazyPath,
exclude_extensions: Configuration.OptionalStringList,
include_extensions: Configuration.OptionalStringList,
};
pub fn create(owner: *std.Build) *WriteFile {
const graph = owner.graph;
const wf = graph.create(WriteFile);
wf.* = .{
.step = .init(.{
.tag = base_tag,
.name = "WriteFile",
.owner = owner,
}),
.generated_directory = graph.addGeneratedFile(&wf.step),
};
return wf;
}
/// Writes `contents` to a file at `sub_path` relative to the output
/// directory.
///
/// `sub_path` may be a basename, or it may include subdirectories, which are
/// created as needed.
pub fn add(wf: *WriteFile, sub_path: []const u8, contents: []const u8) std.Build.LazyPath {
const graph = wf.step.owner.graph;
const wc = &graph.wip_configuration;
const arena = graph.arena;
wf.embeds.append(arena, .{
.sub_path = wc.addString(sub_path) catch @panic("OOM"),
.contents = wc.addBytes(contents) catch @panic("OOM"),
}) catch @panic("OOM");
wf.maybeUpdateName();
return .{
.generated = .{
.index = wf.generated_directory,
.sub_path = graph.dupeString(sub_path),
},
};
}
/// Copies the provided file to `sub_path` relative to the output directory.
///
/// `sub_path` may be a basename, or it may include subdirectories, which are
/// created as needed.
pub fn addCopyFile(wf: *WriteFile, src_file: std.Build.LazyPath, sub_path: []const u8) std.Build.LazyPath {
const graph = wf.step.owner.graph;
const wc = &graph.wip_configuration;
const arena = graph.arena;
wf.copies.append(arena, .{
.sub_path = wc.addString(sub_path) catch @panic("OOM"),
.src_file = src_file.dupe(graph),
}) catch @panic("OOM");
wf.maybeUpdateName();
src_file.addStepDependencies(&wf.step);
return .{ .generated = .{
.index = wf.generated_directory,
.sub_path = graph.dupePath(sub_path),
} };
}
pub const CopyDirectoryOptions = struct {
/// File paths that end in any of these suffixes will be excluded from copying.
exclude_extensions: []const []const u8 = &.{},
/// Only file paths that end in any of these suffixes will be included in copying.
/// `null` means that all suffixes will be included.
/// `exclude_extensions` takes precedence over `include_extensions`.
include_extensions: ?[]const []const u8 = null,
};
/// Copy files matching the specified exclude/include patterns to the specified
/// subdirectory relative to this step's generated directory.
///
/// The returned value is a lazy path to the generated subdirectory.
pub fn addCopyDirectory(
wf: *WriteFile,
src_path: std.Build.LazyPath,
sub_path: []const u8,
options: CopyDirectoryOptions,
) std.Build.LazyPath {
const graph = wf.step.owner.graph;
const wc = &graph.wip_configuration;
const arena = graph.arena;
wf.directories.append(arena, .{
.sub_path = wc.addString(sub_path) catch @panic("OOM"),
.src_path = src_path.dupe(graph),
.exclude_extensions = if (options.exclude_extensions.len != 0)
.init(wc.addStringList(options.exclude_extensions) catch @panic("OOM"))
else
.none,
.include_extensions = if (options.include_extensions) |list|
.init(wc.addStringList(list) catch @panic("OOM"))
else
.none,
}) catch @panic("OOM");
wf.maybeUpdateName();
src_path.addStepDependencies(&wf.step);
return .{
.generated = .{
.index = wf.generated_directory,
.sub_path = graph.dupePath(sub_path),
},
};
}
/// Returns a `LazyPath` representing the base directory that contains all the
/// files from this `WriteFile`.
pub fn getDirectory(wf: *WriteFile) std.Build.LazyPath {
return .{ .generated = .{ .index = wf.generated_directory } };
}
fn maybeUpdateName(wf: *WriteFile) void {
const graph = wf.step.owner.graph;
const wc = &graph.wip_configuration;
const files_count = wf.embeds.items.len + wf.copies.items.len;
if (files_count == 1 and wf.directories.items.len == 0) {
// First time adding a file; update name.
const sub_path = if (wf.embeds.items.len == 1) wf.embeds.items[0].sub_path else wf.copies.items[0].sub_path;
if (std.mem.eql(u8, wf.step.name, "WriteFile")) {
wf.step.name = wf.step.owner.fmt("WriteFile {s}", .{wc.stringSlice(sub_path)});
}
} else if (wf.directories.items.len == 1 and files_count == 0) {
// First time adding a directory; update name.
const dir_name = wc.stringSlice(wf.directories.items[0].sub_path);
if (std.mem.eql(u8, wf.step.name, "WriteFile")) {
wf.step.name = wf.step.owner.fmt("WriteFile {s}", .{dir_name});
}
}
}
WriteFile is used to create a directory in an appropriate location inside the local cache which has a set of files that have either been generated during the build, or are copied from the source package.