| 1 | const InstallDir = @This(); |
| 2 | |
| 3 | const std = @import("std"); |
| 4 | const mem = std.mem; |
| 5 | const fs = std.fs; |
| 6 | const Step = std.Build.Step; |
| 7 | const LazyPath = std.Build.LazyPath; |
| 8 | |
| 9 | step: Step, |
| 10 | options: Options, |
| 11 | |
| 12 | pub const base_tag: Step.Tag = .install_dir; |
| 13 | |
| 14 | pub const Options = struct { |
| 15 | source_dir: LazyPath, |
| 16 | install_dir: std.Build.InstallDir, |
| 17 | install_subdir: []const u8, |
| 18 | /// File paths which end in any of these suffixes will be excluded |
| 19 | /// from being installed. |
| 20 | exclude_extensions: []const []const u8 = &.{}, |
| 21 | /// Only file paths which end in any of these suffixes will be included |
| 22 | /// in installation. `null` means all suffixes are valid for this option. |
| 23 | /// `exclude_extensions` take precedence over `include_extensions` |
| 24 | include_extensions: ?[]const []const u8 = null, |
| 25 | /// File paths which end in any of these suffixes will result in |
| 26 | /// empty files being installed. This is mainly intended for large |
| 27 | /// test.zig files in order to prevent needless installation bloat. |
| 28 | /// However if the files were not present at all, then |
| 29 | /// `@import("test.zig")` would be a compile error. |
| 30 | blank_extensions: []const []const u8 = &.{}, |
| 31 | |
| 32 | fn dupe(opts: Options, graph: *const std.Build.Graph) Options { |
| 33 | return .{ |
| 34 | .source_dir = opts.source_dir.dupe(graph), |
| 35 | .install_dir = opts.install_dir.dupe(graph), |
| 36 | .install_subdir = graph.dupeString(opts.install_subdir), |
| 37 | .exclude_extensions = graph.dupeStrings(opts.exclude_extensions), |
| 38 | .include_extensions = if (opts.include_extensions) |incs| graph.dupeStrings(incs) else null, |
| 39 | .blank_extensions = graph.dupeStrings(opts.blank_extensions), |
| 40 | }; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | pub fn create(owner: *std.Build, options: Options) *InstallDir { |
| 45 | const install_dir = owner.allocator.create(InstallDir) catch @panic("OOM"); |
| 46 | const graph = owner.graph; |
| 47 | install_dir.* = .{ |
| 48 | .step = Step.init(.{ |
| 49 | .tag = base_tag, |
| 50 | .name = owner.fmt("install {f}/", .{options.source_dir}), |
| 51 | .owner = owner, |
| 52 | }), |
| 53 | .options = options.dupe(graph), |
| 54 | }; |
| 55 | options.source_dir.addStepDependencies(&install_dir.step); |
| 56 | return install_dir; |
| 57 | } |