Skip to content

Commit 81c523c

Browse files
author
Felix "xq" Queißner
committed
Fixes bugs.
1 parent 4551a62 commit 81c523c

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

build.zig

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const std = @import("std");
22
const builtin = @import("builtin");
33

4+
fn root() []const u8 {
5+
return comptime (std.fs.path.dirname(@src().file) orelse ".");
6+
}
7+
const build_root = root();
8+
49
pub const KiB = 1024;
510
pub const MiB = 1024 * KiB;
611
pub const GiB = 1024 * MiB;
@@ -125,7 +130,7 @@ fn resolveFilesystemMaker(b: *std.Build, fs: FileSystem.Format) std.Build.LazyPa
125130

126131
const mkfs_fat = b.addExecutable(.{
127132
.name = "mkfs.fat",
128-
.root_source_file = .{ .path = "src/mkfs.fat.zig" },
133+
.root_source_file = .{ .cwd_relative = build_root ++ "/src/mkfs.fat.zig" },
129134
});
130135
mkfs_fat.addModule("fat", fatfs_module);
131136
mkfs_fat.linkLibC();
@@ -190,6 +195,27 @@ pub const InitializeDiskStep = struct {
190195
return .{ .generated = &ids.disk_file };
191196
}
192197

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+
193219
fn addToCacheManifest(b: *std.Build, asking: *std.Build.Step, manifest: *std.Build.Cache.Manifest, content: Content) !void {
194220
manifest.hash.addBytes(@tagName(content));
195221
switch (content) {
@@ -241,7 +267,7 @@ pub const InitializeDiskStep = struct {
241267
},
242268
.copy_dir => |dir| {
243269
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));
245271
},
246272
.copy_file => |file| {
247273
manifest.hash.addBytes(file.destination);
@@ -915,3 +941,50 @@ pub const FileSystem = struct {
915941
// private:
916942
executable: ?std.Build.LazyPath = null,
917943
};
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+
};

src/mkfs.fat.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ var filesystem_format: fatfs.DiskFormat = undefined;
2424

2525
var filesystem: fatfs.FileSystem = undefined;
2626

27+
const format_mapping = std.ComptimeStringMap(fatfs.DiskFormat, &.{
28+
.{ "fat12", .fat },
29+
.{ "fat16", .fat },
30+
.{ "fat32", .fat32 },
31+
.{ "exfat", .exfat },
32+
});
33+
2734
pub fn init(file_system: []const u8) !void {
28-
filesystem_format = std.meta.stringToEnum(fatfs.DiskFormat, file_system) orelse return error.InvalidFilesystem;
35+
filesystem_format = format_mapping.get(file_system) orelse return error.InvalidFilesystem;
2936
fatfs.disks[0] = &fat_disk;
3037
}
3138

0 commit comments

Comments
 (0)