authorgravatar for greenblattryan@gmail.comRyan Greenblatt <greenblattryan@gmail.com> 2021-02-21 05:26:46-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-02-21 12:26:46+02:00
log36178caf3e34859fc715bb281f2692135a337154
tree2fed5f105b55170fe86f11ae9f7462b6e6403202
parent9712e892656ace8ee26c2b2decfde0f2d116ec54
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Added support for passing write file args as build options (#7909)

* Added support for passing write file args as build options * Fix missing fmtEscapes and unused format * Actually fixed now, must be formatted * remove addPathBuildOption

1 files changed, 45 insertions(+), 4 deletions(-)

lib/std/build.zig+45-4
......@@ -1308,6 +1308,12 @@ const BuildOptionArtifactArg = struct {
13081308 artifact: *LibExeObjStep,
13091309};
13101310
1311const BuildOptionWriteFileArg = struct {
1312 name: []const u8,
1313 write_file: *WriteFileStep,
1314 basename: []const u8,
1315};
1316
13111317pub const LibExeObjStep = struct {
13121318 step: Step,
13131319 builder: *Builder,
......@@ -1355,6 +1361,7 @@ pub const LibExeObjStep = struct {
13551361 packages: ArrayList(Pkg),
13561362 build_options_contents: std.ArrayList(u8),
13571363 build_options_artifact_args: std.ArrayList(BuildOptionArtifactArg),
1364 build_options_write_file_args: std.ArrayList(BuildOptionWriteFileArg),
13581365
13591366 object_src: []const u8,
13601367
......@@ -1515,6 +1522,7 @@ pub const LibExeObjStep = struct {
15151522 .object_src = undefined,
15161523 .build_options_contents = std.ArrayList(u8).init(builder.allocator),
15171524 .build_options_artifact_args = std.ArrayList(BuildOptionArtifactArg).init(builder.allocator),
1525 .build_options_write_file_args = std.ArrayList(BuildOptionWriteFileArg).init(builder.allocator),
15181526 .c_std = Builder.CStd.C99,
15191527 .override_lib_dir = null,
15201528 .main_pkg_path = null,
......@@ -2008,6 +2016,23 @@ pub const LibExeObjStep = struct {
20082016 self.step.dependOn(&artifact.step);
20092017 }
20102018
2019 /// The value is the path in the cache dir.
2020 /// Adds a dependency automatically.
2021 /// basename refers to the basename of the WriteFileStep
2022 pub fn addBuildOptionWriteFile(
2023 self: *LibExeObjStep,
2024 name: []const u8,
2025 write_file: *WriteFileStep,
2026 basename: []const u8,
2027 ) void {
2028 self.build_options_write_file_args.append(.{
2029 .name = name,
2030 .write_file = write_file,
2031 .basename = basename,
2032 }) catch unreachable;
2033 self.step.dependOn(&write_file.step);
2034 }
2035
20112036 pub fn addSystemIncludeDir(self: *LibExeObjStep, path: []const u8) void {
20122037 self.include_dirs.append(IncludeDir{ .RawPathSystem = self.builder.dupe(path) }) catch unreachable;
20132038 }
......@@ -2228,11 +2253,27 @@ pub const LibExeObjStep = struct {
22282253 }
22292254 }
22302255
2231 if (self.build_options_contents.items.len > 0 or self.build_options_artifact_args.items.len > 0) {
2232 // Render build artifact options at the last minute, now that the path is known.
2256 if (self.build_options_contents.items.len > 0 or
2257 self.build_options_artifact_args.items.len > 0 or
2258 self.build_options_write_file_args.items.len > 0)
2259 {
2260 // Render build artifact and write file options at the last minute, now that the path is known.
2261 //
2262 // Note that pathFromRoot uses resolve path, so this will have
2263 // correct behavior even if getOutputPath is already absolute.
22332264 for (self.build_options_artifact_args.items) |item| {
2234 const out = self.build_options_contents.writer();
2235 out.print("pub const {s}: []const u8 = \"{}\";\n", .{ item.name, std.zig.fmtEscapes(item.artifact.getOutputPath()) }) catch unreachable;
2265 self.addBuildOption(
2266 []const u8,
2267 item.name,
2268 self.builder.pathFromRoot(item.artifact.getOutputPath()),
2269 );
2270 }
2271 for (self.build_options_write_file_args.items) |item| {
2272 self.addBuildOption(
2273 []const u8,
2274 item.name,
2275 self.builder.pathFromRoot(item.write_file.getOutputPath(item.basename)),
2276 );
22362277 }
22372278
22382279 const build_options_file = try fs.path.join(