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 {...@@ -1308,6 +1308,12 @@ const BuildOptionArtifactArg = struct {
1308 artifact: *LibExeObjStep,1308 artifact: *LibExeObjStep,
1309};1309};
13101310
1311const BuildOptionWriteFileArg = struct {
1312 name: []const u8,
1313 write_file: *WriteFileStep,
1314 basename: []const u8,
1315};
1316
1311pub const LibExeObjStep = struct {1317pub const LibExeObjStep = struct {
1312 step: Step,1318 step: Step,
1313 builder: *Builder,1319 builder: *Builder,
...@@ -1355,6 +1361,7 @@ pub const LibExeObjStep = struct {...@@ -1355,6 +1361,7 @@ pub const LibExeObjStep = struct {
1355 packages: ArrayList(Pkg),1361 packages: ArrayList(Pkg),
1356 build_options_contents: std.ArrayList(u8),1362 build_options_contents: std.ArrayList(u8),
1357 build_options_artifact_args: std.ArrayList(BuildOptionArtifactArg),1363 build_options_artifact_args: std.ArrayList(BuildOptionArtifactArg),
1364 build_options_write_file_args: std.ArrayList(BuildOptionWriteFileArg),
13581365
1359 object_src: []const u8,1366 object_src: []const u8,
13601367
...@@ -1515,6 +1522,7 @@ pub const LibExeObjStep = struct {...@@ -1515,6 +1522,7 @@ pub const LibExeObjStep = struct {
1515 .object_src = undefined,1522 .object_src = undefined,
1516 .build_options_contents = std.ArrayList(u8).init(builder.allocator),1523 .build_options_contents = std.ArrayList(u8).init(builder.allocator),
1517 .build_options_artifact_args = std.ArrayList(BuildOptionArtifactArg).init(builder.allocator),1524 .build_options_artifact_args = std.ArrayList(BuildOptionArtifactArg).init(builder.allocator),
1525 .build_options_write_file_args = std.ArrayList(BuildOptionWriteFileArg).init(builder.allocator),
1518 .c_std = Builder.CStd.C99,1526 .c_std = Builder.CStd.C99,
1519 .override_lib_dir = null,1527 .override_lib_dir = null,
1520 .main_pkg_path = null,1528 .main_pkg_path = null,
...@@ -2008,6 +2016,23 @@ pub const LibExeObjStep = struct {...@@ -2008,6 +2016,23 @@ pub const LibExeObjStep = struct {
2008 self.step.dependOn(&artifact.step);2016 self.step.dependOn(&artifact.step);
2009 }2017 }
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
2011 pub fn addSystemIncludeDir(self: *LibExeObjStep, path: []const u8) void {2036 pub fn addSystemIncludeDir(self: *LibExeObjStep, path: []const u8) void {
2012 self.include_dirs.append(IncludeDir{ .RawPathSystem = self.builder.dupe(path) }) catch unreachable;2037 self.include_dirs.append(IncludeDir{ .RawPathSystem = self.builder.dupe(path) }) catch unreachable;
2013 }2038 }
...@@ -2228,11 +2253,27 @@ pub const LibExeObjStep = struct {...@@ -2228,11 +2253,27 @@ pub const LibExeObjStep = struct {
2228 }2253 }
2229 }2254 }
22302255
2231 if (self.build_options_contents.items.len > 0 or self.build_options_artifact_args.items.len > 0) {2256 if (self.build_options_contents.items.len > 0 or
2232 // Render build artifact options at the last minute, now that the path is known.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.
2233 for (self.build_options_artifact_args.items) |item| {2264 for (self.build_options_artifact_args.items) |item| {
2234 const out = self.build_options_contents.writer();2265 self.addBuildOption(
2235 out.print("pub const {s}: []const u8 = \"{}\";\n", .{ item.name, std.zig.fmtEscapes(item.artifact.getOutputPath()) }) catch unreachable;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 );
2236 }2277 }
22372278
2238 const build_options_file = try fs.path.join(2279 const build_options_file = try fs.path.join(