| 1 | const InstallDir = @This(); |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const Io = std.Io; |
| 5 | const log = std.log; |
| 6 | const Configuration = std.Build.Configuration; |
| 7 | const endsWith = std.mem.endsWith; |
| 8 | |
| 9 | const Step = @import("../Step.zig"); |
| 10 | const Maker = @import("../../Maker.zig"); |
| 11 | |
| 12 | pub fn make( |
| 13 | install_dir: *InstallDir, |
| 14 | step_index: Configuration.Step.Index, |
| 15 | maker: *Maker, |
| 16 | progress_node: std.Progress.Node, |
| 17 | ) Step.ExtendedMakeError!void { |
| 18 | _ = install_dir; |
| 19 | const graph = maker.graph; |
| 20 | const gpa = maker.gpa; |
| 21 | const arena = maker.graph.arena; // TODO don't leak into process arena |
| 22 | const io = graph.io; |
| 23 | const step = maker.stepByIndex(step_index); |
| 24 | const conf = &maker.scanned_config.configuration; |
| 25 | const conf_step = step_index.ptr(conf); |
| 26 | const conf_id = conf_step.extended.get(conf.extra).install_dir; |
| 27 | |
| 28 | step.clearWatchInputs(maker); |
| 29 | |
| 30 | const dest_parent_path = try maker.resolveInstallDir(arena, conf_id.dest_dir); |
| 31 | const dest_prefix = if (conf_id.dest_sub_path.value) |s| |
| 32 | try dest_parent_path.join(arena, s.slice(conf)) |
| 33 | else |
| 34 | dest_parent_path; |
| 35 | const src_dir_lazy_path = conf_id.source_dir.get(conf); |
| 36 | const src_dir_path = try maker.resolveLazyPath(arena, src_dir_lazy_path, step_index); |
| 37 | const need_derived_inputs = try step.addDirectoryWatchInput(maker, src_dir_lazy_path); |
| 38 | |
| 39 | var src_dir = src_dir_path.root_dir.handle.openDir( |
| 40 | io, |
| 41 | src_dir_path.subPathOrDot(), |
| 42 | .{ .iterate = true }, |
| 43 | ) catch |err| return step.fail(maker, "failed opening source directory {f}: {t}", .{ src_dir_path, err }); |
| 44 | defer src_dir.close(io); |
| 45 | |
| 46 | const exclude_extensions = conf_id.exclude_extensions.slice; |
| 47 | const include_extensions: ?[]const Configuration.String = if (conf_id.flags.include_extensions_active) |
| 48 | conf_id.include_extensions.slice |
| 49 | else |
| 50 | null; |
| 51 | const blank_extensions = conf_id.blank_extensions.slice; |
| 52 | |
| 53 | var all_cached = true; |
| 54 | var it = try src_dir.walk(gpa); |
| 55 | defer it.deinit(); |
| 56 | next_entry: while (it.next(io) catch |err| switch (err) { |
| 57 | error.Canceled, error.OutOfMemory => |e| return e, |
| 58 | else => |e| return step.fail(maker, "failed iterating dir {f}: {t}", .{ src_dir_path, e }), |
| 59 | }) |entry| { |
| 60 | for (exclude_extensions) |ext| { |
| 61 | if (endsWith(u8, entry.path, ext.slice(conf))) continue :next_entry; |
| 62 | } |
| 63 | if (include_extensions) |includes| { |
| 64 | for (includes) |inc| { |
| 65 | if (endsWith(u8, entry.path, inc.slice(conf))) break; |
| 66 | } else { |
| 67 | continue :next_entry; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | const dest_path = try dest_prefix.join(arena, entry.path); |
| 72 | switch (entry.kind) { |
| 73 | .directory => { |
| 74 | if (need_derived_inputs) { |
| 75 | const entry_path = try src_dir_path.join(arena, entry.path); |
| 76 | try step.addDirectoryWatchInputFromPath(maker, entry_path); |
| 77 | } |
| 78 | const p = try maker.installDir(arena, dest_path, step_index); |
| 79 | all_cached = all_cached and p == .existed; |
| 80 | }, |
| 81 | .file => { |
| 82 | for (blank_extensions) |ext| { |
| 83 | if (endsWith(u8, entry.path, ext.slice(conf))) { |
| 84 | try maker.truncatePath(arena, dest_path, step_index); |
| 85 | continue :next_entry; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | const entry_path = try src_dir_path.join(arena, entry.path); |
| 90 | const p = try maker.installPath(arena, entry_path, dest_path, step_index); |
| 91 | all_cached = all_cached and p == .fresh; |
| 92 | progress_node.completeOne(); |
| 93 | }, |
| 94 | else => continue, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | step.result_cached = all_cached; |
| 99 | } |