Skip to content

Commit 1a8669e

Browse files
thejoshwolfeandrewrk
authored andcommitted
build.zig: addBuildOptionArtifact
1 parent bd89bd6 commit 1a8669e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/std/build.zig

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,11 @@ pub const FileSource = union(enum) {
11651165
}
11661166
};
11671167

1168+
const BuildOptionArtifactArg = struct {
1169+
name: []const u8,
1170+
artifact: *LibExeObjStep,
1171+
};
1172+
11681173
pub const LibExeObjStep = struct {
11691174
step: Step,
11701175
builder: *Builder,
@@ -1210,6 +1215,7 @@ pub const LibExeObjStep = struct {
12101215
out_pdb_filename: []const u8,
12111216
packages: ArrayList(Pkg),
12121217
build_options_contents: std.ArrayList(u8),
1218+
build_options_artifact_args: std.ArrayList(BuildOptionArtifactArg),
12131219
system_linker_hack: bool = false,
12141220

12151221
object_src: []const u8,
@@ -1355,6 +1361,7 @@ pub const LibExeObjStep = struct {
13551361
.framework_dirs = ArrayList([]const u8).init(builder.allocator),
13561362
.object_src = undefined,
13571363
.build_options_contents = std.ArrayList(u8).init(builder.allocator),
1364+
.build_options_artifact_args = std.ArrayList(BuildOptionArtifactArg).init(builder.allocator),
13581365
.c_std = Builder.CStd.C99,
13591366
.override_lib_dir = null,
13601367
.main_pkg_path = null,
@@ -1812,6 +1819,13 @@ pub const LibExeObjStep = struct {
18121819
out.print("pub const {} = {};\n", .{ name, value }) catch unreachable;
18131820
}
18141821

1822+
/// The value is the path in the cache dir.
1823+
/// Adds a dependency automatically.
1824+
pub fn addBuildOptionArtifact(self: *LibExeObjStep, name: []const u8, artifact: *LibExeObjStep) void {
1825+
self.build_options_artifact_args.append(.{ .name = name, .artifact = artifact }) catch unreachable;
1826+
self.step.dependOn(&artifact.step);
1827+
}
1828+
18151829
pub fn addSystemIncludeDir(self: *LibExeObjStep, path: []const u8) void {
18161830
self.include_dirs.append(IncludeDir{ .RawPathSystem = self.builder.dupe(path) }) catch unreachable;
18171831
}
@@ -1995,7 +2009,15 @@ pub const LibExeObjStep = struct {
19952009
}
19962010
}
19972011

1998-
if (self.build_options_contents.items.len > 0) {
2012+
if (self.build_options_contents.items.len > 0 or self.build_options_artifact_args.items.len > 0) {
2013+
// Render build artifact options at the last minute, now that the path is known.
2014+
for (self.build_options_artifact_args.items) |item| {
2015+
const out = self.build_options_contents.writer();
2016+
out.print("pub const {}: []const u8 = ", .{item.name}) catch unreachable;
2017+
std.zig.renderStringLiteral(item.artifact.getOutputPath(), out) catch unreachable;
2018+
out.writeAll(";\n") catch unreachable;
2019+
}
2020+
19992021
const build_options_file = try fs.path.join(
20002022
builder.allocator,
20012023
&[_][]const u8{ builder.cache_root, builder.fmt("{}_build_options.zig", .{self.name}) },

0 commit comments

Comments
 (0)