|
1 | 1 | const std = @import("std");
|
2 | 2 | const builtin = @import("builtin");
|
3 | 3 |
|
| 4 | +fn root() []const u8 { |
| 5 | + return comptime (std.fs.path.dirname(@src().file) orelse "."); |
| 6 | +} |
| 7 | +const build_root = root(); |
| 8 | + |
4 | 9 | pub const KiB = 1024;
|
5 | 10 | pub const MiB = 1024 * KiB;
|
6 | 11 | pub const GiB = 1024 * MiB;
|
@@ -125,7 +130,7 @@ fn resolveFilesystemMaker(b: *std.Build, fs: FileSystem.Format) std.Build.LazyPa
|
125 | 130 |
|
126 | 131 | const mkfs_fat = b.addExecutable(.{
|
127 | 132 | .name = "mkfs.fat",
|
128 |
| - .root_source_file = .{ .path = "src/mkfs.fat.zig" }, |
| 133 | + .root_source_file = .{ .cwd_relative = build_root ++ "/src/mkfs.fat.zig" }, |
129 | 134 | });
|
130 | 135 | mkfs_fat.addModule("fat", fatfs_module);
|
131 | 136 | mkfs_fat.linkLibC();
|
@@ -190,6 +195,27 @@ pub const InitializeDiskStep = struct {
|
190 | 195 | return .{ .generated = &ids.disk_file };
|
191 | 196 | }
|
192 | 197 |
|
| 198 | + fn addDirectoryToCache(b: *std.Build, manifest: *std.Build.Cache.Manifest, parent: std.fs.Dir, path: []const u8) !void { |
| 199 | + var dir = try parent.openIterableDir(path, .{}); |
| 200 | + defer dir.close(); |
| 201 | + |
| 202 | + var walker = try dir.walk(b.allocator); |
| 203 | + defer walker.deinit(); |
| 204 | + |
| 205 | + while (try walker.next()) |entry| { |
| 206 | + switch (entry.kind) { |
| 207 | + .file => { |
| 208 | + const abs_path = try entry.dir.realpathAlloc(b.allocator, entry.basename); |
| 209 | + defer b.allocator.free(abs_path); |
| 210 | + _ = try manifest.addFile(abs_path, null); |
| 211 | + }, |
| 212 | + .directory => try addDirectoryToCache(b, manifest, entry.dir, entry.basename), |
| 213 | + |
| 214 | + else => return error.Unsupported, |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
193 | 219 | fn addToCacheManifest(b: *std.Build, asking: *std.Build.Step, manifest: *std.Build.Cache.Manifest, content: Content) !void {
|
194 | 220 | manifest.hash.addBytes(@tagName(content));
|
195 | 221 | switch (content) {
|
@@ -241,7 +267,7 @@ pub const InitializeDiskStep = struct {
|
241 | 267 | },
|
242 | 268 | .copy_dir => |dir| {
|
243 | 269 | manifest.hash.addBytes(dir.destination);
|
244 |
| - manifest.hash.addBytes(dir.source.getPath2(b, asking)); |
| 270 | + try addDirectoryToCache(b, manifest, std.fs.cwd(), dir.source.getPath2(b, asking)); |
245 | 271 | },
|
246 | 272 | .copy_file => |file| {
|
247 | 273 | manifest.hash.addBytes(file.destination);
|
@@ -915,3 +941,50 @@ pub const FileSystem = struct {
|
915 | 941 | // private:
|
916 | 942 | executable: ?std.Build.LazyPath = null,
|
917 | 943 | };
|
| 944 | + |
| 945 | +pub const FileSystemBuilder = struct { |
| 946 | + b: *std.Build, |
| 947 | + list: std.ArrayListUnmanaged(FileSystem.Item), |
| 948 | + |
| 949 | + pub fn init(b: *std.Build) FileSystemBuilder { |
| 950 | + return FileSystemBuilder{ |
| 951 | + .b = b, |
| 952 | + .list = .{}, |
| 953 | + }; |
| 954 | + } |
| 955 | + |
| 956 | + pub fn finalize(fsb: *FileSystemBuilder, options: struct { |
| 957 | + format: FileSystem.Format, |
| 958 | + label: []const u8, |
| 959 | + }) FileSystem { |
| 960 | + return FileSystem{ |
| 961 | + .format = options.format, |
| 962 | + .label = fsb.b.dupe(options.label), |
| 963 | + .items = fsb.list.toOwnedSlice(fsb.b.allocator) catch @panic("out of memory"), |
| 964 | + }; |
| 965 | + } |
| 966 | + |
| 967 | + pub fn addFile(fsb: *FileSystemBuilder, source: std.Build.LazyPath, destination: []const u8) void { |
| 968 | + fsb.list.append(fsb.b.allocator, .{ |
| 969 | + .copy_file = .{ |
| 970 | + .source = source.dupe(fsb.b), |
| 971 | + .destination = fsb.b.dupe(destination), |
| 972 | + }, |
| 973 | + }) catch @panic("out of memory"); |
| 974 | + } |
| 975 | + |
| 976 | + pub fn addDirectory(fsb: *FileSystemBuilder, source: std.Build.LazyPath, destination: []const u8) void { |
| 977 | + fsb.list.append(fsb.b.allocator, .{ |
| 978 | + .copy_dir = .{ |
| 979 | + .source = source.dupe(fsb.b), |
| 980 | + .destination = fsb.b.dupe(destination), |
| 981 | + }, |
| 982 | + }) catch @panic("out of memory"); |
| 983 | + } |
| 984 | + |
| 985 | + pub fn mkdir(fsb: *FileSystemBuilder, destination: []const u8) void { |
| 986 | + fsb.list.append(fsb.b.allocator, .{ |
| 987 | + .empty_dir = fsb.b.dupe(destination), |
| 988 | + }) catch @panic("out of memory"); |
| 989 | + } |
| 990 | +}; |
0 commit comments