|
| 1 | +//! DirectoryFileInput adds files within a directory to the dependencies of the given Step.Run command |
| 2 | +//! This is required so that generated directories will work. |
| 3 | + |
| 4 | +const std = @import("std"); |
| 5 | +const androidbuild = @import("androidbuild.zig"); |
| 6 | +const builtin = @import("builtin"); |
| 7 | +const Build = std.Build; |
| 8 | +const Step = Build.Step; |
| 9 | +const Run = Build.Step.Run; |
| 10 | +const LazyPath = Build.LazyPath; |
| 11 | +const fs = std.fs; |
| 12 | +const mem = std.mem; |
| 13 | +const assert = std.debug.assert; |
| 14 | + |
| 15 | +step: Step, |
| 16 | + |
| 17 | +/// Runner to update |
| 18 | +run: *Build.Step.Run, |
| 19 | + |
| 20 | +/// The directory that will contain the files to glob |
| 21 | +dir: LazyPath, |
| 22 | + |
| 23 | +pub fn create(owner: *std.Build, run: *Run, dir: LazyPath) void { |
| 24 | + const self = owner.allocator.create(DirectoryFileInput) catch @panic("OOM"); |
| 25 | + self.* = .{ |
| 26 | + .step = Step.init(.{ |
| 27 | + .id = .custom, |
| 28 | + .name = androidbuild.runNameContext("directory-file-input"), |
| 29 | + .owner = owner, |
| 30 | + .makeFn = make, |
| 31 | + }), |
| 32 | + .run = run, |
| 33 | + .dir = dir, |
| 34 | + }; |
| 35 | + // Run step relies on DirectoryFileInput finishing |
| 36 | + run.step.dependOn(&self.step); |
| 37 | + // If dir is generated then this will wait for that dir to generate |
| 38 | + dir.addStepDependencies(&self.step); |
| 39 | +} |
| 40 | + |
| 41 | +fn make(step: *Step, _: Build.Step.MakeOptions) !void { |
| 42 | + const b = step.owner; |
| 43 | + const arena = b.allocator; |
| 44 | + const self: *DirectoryFileInput = @fieldParentPtr("step", step); |
| 45 | + |
| 46 | + const run = self.run; |
| 47 | + const dir_path = self.dir.getPath3(b, step); |
| 48 | + |
| 49 | + // NOTE(jae): 2025-07-23 |
| 50 | + // As of Zig 0.15.0-dev.1092+d772c0627, package_name_path.openDir("") is not possible as it assumes you're appending a sub-path |
| 51 | + var dir = if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15) |
| 52 | + try dir_path.root_dir.handle.openDir(dir_path.sub_path, .{ .iterate = true }) |
| 53 | + else |
| 54 | + try dir_path.root_dir.handle.openDir(b.graph.io, dir_path.sub_path, .{ .iterate = true }); |
| 55 | + defer if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15) |
| 56 | + dir.close() |
| 57 | + else |
| 58 | + dir.close(b.graph.io); |
| 59 | + |
| 60 | + var walker = try dir.walk(arena); |
| 61 | + defer walker.deinit(); |
| 62 | + while (if (builtin.zig_version.major == 0 and builtin.zig_version.minor <= 15) |
| 63 | + try walker.next() |
| 64 | + else |
| 65 | + try walker.next(b.graph.io)) |entry| |
| 66 | + { |
| 67 | + if (entry.kind != .file) continue; |
| 68 | + |
| 69 | + // Add file as dependency to run command |
| 70 | + run.addFileInput(LazyPath{ |
| 71 | + .cwd_relative = try dir_path.root_dir.join(b.allocator, &.{ dir_path.sub_path, entry.path }), |
| 72 | + }); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +const DirectoryFileInput = @This(); |
0 commit comments