Skip to content

Commit 9f4fb8a

Browse files
author
Felix "xq" Queißner
committed
Update to 0.13.0, adds nix flake and github ci
1 parent 6baf155 commit 9f4fb8a

File tree

10 files changed

+328
-74
lines changed

10 files changed

+328
-74
lines changed

.envrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
use flake
2+

.github/FUNDING.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: MasterQ32
1+
github: ikskuh

.github/workflows/validate.yml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Build
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
push:
7+
branches: [main]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: Setup Zig
17+
uses: mlugg/setup-zig@v1
18+
with:
19+
version: 0.13.0
20+
21+
- name: Basic Build
22+
run: |
23+
zig build
24+
25+
- name: Exercise Build options
26+
run: |
27+
zig build -Dstatic-rtc=1980-01-01
28+
zig build -Dstatic-rtc=2107-01-01
29+
zig build -Dsector-size=512
30+
zig build -Dsector-size=1024
31+
zig build -Dsector-size=2048
32+
zig build -Dsector-size=4096
33+
zig build -Dsector-size=512:4096
34+
zig build -Dsector-size=1024:4096
35+
zig build -Dsector-size=2048:4096
36+
zig build -Dvolume-count=1
37+
zig build -Dvolume-count=2
38+
zig build -Dvolume-count=10
39+
zig build -Dvolume-names=C
40+
zig build -Dvolume-names=C,D,E,F

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
zig-cache/
1+
.zig-cache/
22
zig-out/

build.zig

+78-62
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ pub const KiB = 1024;
1010
pub const MiB = 1024 * KiB;
1111
pub const GiB = 1024 * MiB;
1212

13-
fn usageDemo(b: *std.Build, debug_step: *std.Build.Step) void {
14-
installDebugDisk(debug_step, "uninitialized.img", 50 * MiB, .uninitialized);
13+
fn usageDemo(
14+
b: *std.Build,
15+
dependency: *std.Build.Dependency,
16+
debug_step: *std.Build.Step,
17+
) void {
18+
installDebugDisk(dependency, debug_step, "uninitialized.img", 50 * MiB, .uninitialized);
1519

16-
installDebugDisk(debug_step, "empty-mbr.img", 50 * MiB, .{
20+
installDebugDisk(dependency, debug_step, "empty-mbr.img", 50 * MiB, .{
1721
.mbr = .{
1822
.partitions = .{
1923
null,
@@ -24,7 +28,7 @@ fn usageDemo(b: *std.Build, debug_step: *std.Build.Step) void {
2428
},
2529
});
2630

27-
installDebugDisk(debug_step, "manual-offset-mbr.img", 50 * MiB, .{
31+
installDebugDisk(dependency, debug_step, "manual-offset-mbr.img", 50 * MiB, .{
2832
.mbr = .{
2933
.partitions = .{
3034
&.{ .offset = 2048 + 0 * 10 * MiB, .size = 10 * MiB, .bootable = true, .type = .fat32_lba, .data = .uninitialized },
@@ -35,7 +39,7 @@ fn usageDemo(b: *std.Build, debug_step: *std.Build.Step) void {
3539
},
3640
});
3741

38-
installDebugDisk(debug_step, "auto-offset-mbr.img", 50 * MiB, .{
42+
installDebugDisk(dependency, debug_step, "auto-offset-mbr.img", 50 * MiB, .{
3943
.mbr = .{
4044
.partitions = .{
4145
&.{ .size = 7 * MiB, .bootable = true, .type = .fat32_lba, .data = .uninitialized },
@@ -46,29 +50,29 @@ fn usageDemo(b: *std.Build, debug_step: *std.Build.Step) void {
4650
},
4751
});
4852

49-
installDebugDisk(debug_step, "empty-fat32.img", 50 * MiB, .{
53+
installDebugDisk(dependency, debug_step, "empty-fat32.img", 50 * MiB, .{
5054
.fs = .{
5155
.format = .fat32,
5256
.label = "EMPTY",
5357
.items = &.{},
5458
},
5559
});
5660

57-
installDebugDisk(debug_step, "initialized-fat32.img", 50 * MiB, .{
61+
installDebugDisk(dependency, debug_step, "initialized-fat32.img", 50 * MiB, .{
5862
.fs = .{
5963
.format = .fat32,
6064
.label = "ROOTFS",
6165
.items = &.{
6266
.{ .empty_dir = "boot/EFI/refind/icons" },
6367
.{ .empty_dir = "/boot/EFI/nixos/.extra-files/" },
6468
.{ .empty_dir = "Users/xq/" },
65-
.{ .copy_dir = .{ .source = relpath(b, "dummy/Windows"), .destination = "Windows" } },
66-
.{ .copy_file = .{ .source = relpath(b, "dummy/README.md"), .destination = "Users/xq/README.md" } },
69+
.{ .copy_dir = .{ .source = b.path("dummy/Windows"), .destination = "Windows" } },
70+
.{ .copy_file = .{ .source = b.path("dummy/README.md"), .destination = "Users/xq/README.md" } },
6771
},
6872
},
6973
});
7074

71-
installDebugDisk(debug_step, "initialized-fat32-in-mbr-partitions.img", 100 * MiB, .{
75+
installDebugDisk(dependency, debug_step, "initialized-fat32-in-mbr-partitions.img", 100 * MiB, .{
7276
.mbr = .{
7377
.partitions = .{
7478
&.{
@@ -83,8 +87,8 @@ fn usageDemo(b: *std.Build, debug_step: *std.Build.Step) void {
8387
.{ .empty_dir = "boot/EFI/refind/icons" },
8488
.{ .empty_dir = "/boot/EFI/nixos/.extra-files/" },
8589
.{ .empty_dir = "Users/xq/" },
86-
.{ .copy_dir = .{ .source = relpath(b, "dummy/Windows"), .destination = "Windows" } },
87-
.{ .copy_file = .{ .source = relpath(b, "dummy/README.md"), .destination = "Users/xq/README.md" } },
90+
.{ .copy_dir = .{ .source = b.path("dummy/Windows"), .destination = "Windows" } },
91+
.{ .copy_file = .{ .source = b.path("dummy/README.md"), .destination = "Users/xq/README.md" } },
8892
},
8993
},
9094
},
@@ -104,45 +108,49 @@ fn usageDemo(b: *std.Build, debug_step: *std.Build.Step) void {
104108
// });
105109
}
106110

107-
const MyBuild = @This();
111+
pub fn build(b: *std.Build) void {
112+
// Steps:
108113

109-
const FatFS = @import("zfat");
114+
const debug_step = b.step("debug", "Builds a basic exemplary disk image.");
110115

111-
const fatfs_config = FatFS.Config{
112-
// .max_long_name_len = 121,
113-
.code_page = .us,
114-
.volumes = .{ .count = 1 },
115-
.rtc = .dynamic,
116-
.mkfs = true,
117-
.exfat = true,
118-
};
116+
// Dependency Setup:
119117

120-
pub fn build(b: *std.Build) void {
121-
const debug_step = b.step("debug", "Builds a basic exemplary disk image.");
118+
const zfat_dep = b.dependency("zfat", .{
119+
// .max_long_name_len = 121,
120+
.code_page = .us,
121+
.@"volume-count" = @as(u32, 1),
122+
// .rtc = .dynamic,
123+
.mkfs = true,
124+
.exfat = true,
125+
});
122126

123-
usageDemo(b, debug_step);
124-
}
127+
const zfat_mod = zfat_dep.module("zfat");
125128

126-
fn resolveFilesystemMaker(b: *std.Build, fs: FileSystem.Format) std.Build.LazyPath {
127-
switch (fs) {
128-
.fat12, .fat16, .fat32, .exfat => {
129-
const fatfs_module = FatFS.createModule(b, fatfs_config);
129+
const mkfs_fat = b.addExecutable(.{
130+
.name = "mkfs.fat",
131+
.target = b.host,
132+
.optimize = .ReleaseSafe,
133+
.root_source_file = .{ .cwd_relative = build_root ++ "/src/mkfs.fat.zig" },
134+
});
135+
mkfs_fat.root_module.addImport("fat", zfat_mod);
136+
mkfs_fat.linkLibC();
137+
b.installArtifact(mkfs_fat);
130138

131-
const mkfs_fat = b.addExecutable(.{
132-
.name = "mkfs.fat",
133-
.root_source_file = .{ .cwd_relative = build_root ++ "/src/mkfs.fat.zig" },
134-
});
135-
mkfs_fat.addModule("fat", fatfs_module);
136-
mkfs_fat.linkLibC();
137-
FatFS.link(mkfs_fat, fatfs_config);
139+
// Usage:
140+
var self_dep = std.Build.Dependency{
141+
.builder = b,
142+
};
143+
usageDemo(b, &self_dep, debug_step);
144+
}
138145

139-
return mkfs_fat.getEmittedBin();
140-
},
146+
fn resolveFilesystemMaker(dependency: *std.Build.Dependency, fs: FileSystem.Format) std.Build.LazyPath {
147+
return switch (fs) {
148+
.fat12, .fat16, .fat32, .exfat => dependency.artifact("mkfs.fat").getEmittedBin(),
141149

142-
.custom => |path| return path,
150+
.custom => |path| path,
143151

144152
else => std.debug.panic("Unsupported builtin file system: {s}", .{@tagName(fs)}),
145-
}
153+
};
146154
}
147155

148156
fn relpath(b: *std.Build, path: []const u8) std.Build.LazyPath {
@@ -151,30 +159,36 @@ fn relpath(b: *std.Build, path: []const u8) std.Build.LazyPath {
151159
};
152160
}
153161

154-
fn installDebugDisk(install_step: *std.Build.Step, name: []const u8, size: u64, content: Content) void {
155-
const initialize_disk = initializeDisk(install_step.owner, size, content);
162+
fn installDebugDisk(
163+
dependency: *std.Build.Dependency,
164+
install_step: *std.Build.Step,
165+
name: []const u8,
166+
size: u64,
167+
content: Content,
168+
) void {
169+
const initialize_disk = initializeDisk(dependency, size, content);
156170
const install_disk = install_step.owner.addInstallFile(initialize_disk.getImageFile(), name);
157171
install_step.dependOn(&install_disk.step);
158172
}
159173

160-
pub fn initializeDisk(b: *std.Build, size: u64, content: Content) *InitializeDiskStep {
161-
const ids = b.allocator.create(InitializeDiskStep) catch @panic("out of memory");
174+
pub fn initializeDisk(dependency: *std.Build.Dependency, size: u64, content: Content) *InitializeDiskStep {
175+
const ids = dependency.builder.allocator.create(InitializeDiskStep) catch @panic("out of memory");
162176

163177
ids.* = InitializeDiskStep{
164178
.step = std.Build.Step.init(.{
165-
.owner = b,
179+
.owner = dependency.builder, // TODO: Is this correct?
166180
.id = .custom,
167181
.name = "initialize disk",
168182
.makeFn = InitializeDiskStep.make,
169183
.first_ret_addr = @returnAddress(),
170184
.max_rss = 0,
171185
}),
172186
.disk_file = .{ .step = &ids.step },
173-
.content = content.dupe(b) catch @panic("out of memory"),
187+
.content = content.dupe(dependency.builder) catch @panic("out of memory"),
174188
.size = size,
175189
};
176190

177-
ids.content.resolveFileSystems(b);
191+
ids.content.resolveFileSystems(dependency);
178192

179193
ids.content.pushDependenciesTo(&ids.step);
180194

@@ -192,11 +206,13 @@ pub const InitializeDiskStep = struct {
192206
disk_file: std.Build.GeneratedFile,
193207

194208
pub fn getImageFile(ids: *InitializeDiskStep) std.Build.LazyPath {
195-
return .{ .generated = &ids.disk_file };
209+
return .{ .generated = .{
210+
.file = &ids.disk_file,
211+
} };
196212
}
197213

198214
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, .{});
215+
var dir = try parent.openDir(path, .{ .iterate = true });
200216
defer dir.close();
201217

202218
var walker = try dir.walk(b.allocator);
@@ -307,8 +323,8 @@ pub const InitializeDiskStep = struct {
307323

308324
@memcpy(boot_sector[0..table.bootloader.len], &table.bootloader);
309325

310-
std.mem.writeIntLittle(u32, boot_sector[0x1B8..0x1BC], if (table.disk_id) |disk_id| disk_id else 0x0000_0000);
311-
std.mem.writeIntLittle(u16, boot_sector[0x1BC..0x1BE], 0x0000);
326+
std.mem.writeInt(u32, boot_sector[0x1B8..0x1BC], if (table.disk_id) |disk_id| disk_id else 0x0000_0000, .little);
327+
std.mem.writeInt(u16, boot_sector[0x1BC..0x1BE], 0x0000, .little);
312328

313329
var all_auto = true;
314330
var all_manual = true;
@@ -369,8 +385,8 @@ pub const InitializeDiskStep = struct {
369385
desc[1..4].* = mbr.encodeMbrChsEntry(lba); // chs_start
370386
desc[4] = @intFromEnum(part.type);
371387
desc[5..8].* = mbr.encodeMbrChsEntry(lba + size - 1); // chs_end
372-
std.mem.writeIntLittle(u32, desc[8..12], lba); // lba_start
373-
std.mem.writeIntLittle(u32, desc[12..16], size); // block_count
388+
std.mem.writeInt(u32, desc[8..12], lba, .little); // lba_start
389+
std.mem.writeInt(u32, desc[12..16], size, .little); // block_count
374390

375391
auto_offset += part.size;
376392
} else {
@@ -455,7 +471,7 @@ pub const InitializeDiskStep = struct {
455471
}
456472

457473
// use shared access to the file:
458-
const stdout = b.exec(argv.items);
474+
const stdout = b.run(argv.items);
459475

460476
try disk.sync();
461477

@@ -502,13 +518,13 @@ pub const InitializeDiskStep = struct {
502518
}
503519
}
504520

505-
fn make(step: *std.Build.Step, progress: *std.Progress.Node) !void {
521+
fn make(step: *std.Build.Step, progress: std.Progress.Node) !void {
506522
const b = step.owner;
507523
_ = progress;
508524

509525
const ids: *InitializeDiskStep = @fieldParentPtr("step", step);
510526

511-
var man = b.cache.obtain();
527+
var man = b.graph.cache.obtain();
512528
defer man.deinit();
513529

514530
man.hash.addBytes(&.{ 232, 8, 75, 249, 2, 210, 51, 118, 171, 12 }); // Change when impl changes
@@ -561,7 +577,7 @@ pub const Content = union(enum) {
561577

562578
data: std.Build.LazyPath,
563579

564-
binary: *std.Build.CompileStep,
580+
binary: *std.Build.Step.Compile,
565581

566582
pub fn dupe(content: Content, b: *std.Build) !Content {
567583
const allocator = b.allocator;
@@ -660,23 +676,23 @@ pub const Content = union(enum) {
660676
}
661677
}
662678

663-
pub fn resolveFileSystems(content: *Content, b: *std.Build) void {
679+
pub fn resolveFileSystems(content: *Content, dependency: *std.Build.Dependency) void {
664680
switch (content.*) {
665681
.uninitialized => {},
666682
.mbr => |*table| {
667683
for (&table.partitions) |*part| {
668684
if (part.*) |p| {
669-
@constCast(&p.data).resolveFileSystems(b);
685+
@constCast(&p.data).resolveFileSystems(dependency);
670686
}
671687
}
672688
},
673689
.gpt => |*table| {
674690
for (table.partitions) |*part| {
675-
@constCast(&part.data).resolveFileSystems(b);
691+
@constCast(&part.data).resolveFileSystems(dependency);
676692
}
677693
},
678694
.fs => |*fs| {
679-
fs.executable = resolveFilesystemMaker(b, fs.format);
695+
fs.executable = resolveFilesystemMaker(dependency, fs.format);
680696
},
681697
.data, .binary => {},
682698
}

build.zig.zon

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
.version = "0.1.0",
44
.dependencies = .{
55
.zfat = .{
6-
.url = "https://github.com/ZigEmbeddedGroup/zfat/archive/c97bcca86b4e878023df070c9e0264a21c056622.tar.gz",
7-
.hash = "1220a1f050f3a67785cbe68283b252f02f72885eea80d6a9e1856b02cd66deaf1492",
6+
.url = "https://github.com/ZigEmbeddedGroup/zfat/archive/34f1c70afe1b04d9b378c373922fe01de342ddcf.tar.gz",
7+
.hash = "1220321b9050e25e4e6d94cb8564009e5f913cf39f26f2c0b358f971175bb1c299b2",
88
},
99
},
1010
.paths = .{

0 commit comments

Comments
 (0)