Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: 0.15.2
version: 0.16.0

- name: Basic Build
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.zig-cache/
zig-out/
zig-pkg/
.vscode/
.dim-out/
3 changes: 2 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ pub const BuildInterface = @import("src/BuildInterface.zig");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSafe });
b.release_mode = .safe;
const optimize = b.standardOptimizeOption(.{});

const test_step = b.step("test", "Runs the test suite.");

Expand Down
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
.fingerprint = 0x9947018c924eecb2,
.dependencies = .{
.zfat = .{
.url = "git+https://github.com/ZigEmbeddedGroup/zfat.git#0571b0d8c8cc4fcb037a1d5e7ea5666cb2f83ddf",
.hash = "zfat-0.15.0-SNNK9RRqcgCLQ5mjXghbB6mokzcHORsGnY7GtbfOt2k3",
.url = "git+https://github.com/khitiara/zfat?ref=0.15#4bd56e3905b537b06877a301c6f4f0c462a2cb8a",
.hash = "zfat-0.15.0-SNNK9e-YcgBoU_BG3WuNXvo3Mkaz79C-m9_igwmJ224v",
},
.args = .{
.url = "git+https://github.com/ikskuh/zig-args.git#8ae26b44a884ff20dca98ee84c098e8f8e94902f",
.hash = "args-0.0.0-CiLiqojRAACGzDRO7A9dw7kWSchNk29caJZkXuMCb0Cn",
.url = "git+https://github.com/ikskuh/zig-args?ref=master#d47ae21768f9ec47c2a4154ab48b87166965b820",
.hash = "args-0.0.0-CiLiqmvgAADyJmrzcQTP9IOYNvTzR_KGrg3ZNNsH2Qv0",
},
},
.paths = .{
Expand Down
57 changes: 33 additions & 24 deletions src/BuildInterface.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ pub fn createDisk(dimmer: Interface, size: u64, content: Content) std.Build.Lazy

const write_files = b.addWriteFiles();

const script_source, const variables = renderContent(write_files, b.allocator, content);
const script_source, const variables = renderContent(write_files, b.allocator, content, dimmer.builder.graph.io);

const script_file = write_files.add("image.dis", script_source);

const compile_script = b.addRunArtifact(dimmer.dimmer_exe);

compile_script.setCwd(script_file.dirname());

_ = compile_script.addPrefixedDepFileOutputArg("--deps-file=", "image.d");

compile_script.addArg(b.fmt("--size={d}", .{size}));

compile_script.addPrefixedFileArg("--script=", script_file);
compile_script.addPrefixedDirectoryArg("--script-root=", .{ .cwd_relative = "." });
// compile_script.addPrefixedDirectoryArg("--script-root=", .{ .cwd_relative = "." });

const result_file = compile_script.addPrefixedOutputFileArg("--output=", "disk.img");

Expand Down Expand Up @@ -67,19 +69,20 @@ fn renderContent(
wfs: *std.Build.Step.WriteFile,
allocator: std.mem.Allocator,
content: Content,
io: std.Io,
) struct { []const u8, ContentWriter.VariableMap } {
var code: std.Io.Writer.Allocating = .init(allocator);
defer code.deinit();

var variables: ContentWriter.VariableMap = .init(allocator);
var variables: ContentWriter.VariableMap = .empty;

var cw: ContentWriter = .{
.code = &code.writer,
.wfs = wfs,
.vars = &variables,
};

cw.render(content) catch @panic("out of memory");
cw.render(content, io, allocator) catch @panic("out of memory");

const source = std.mem.trim(
u8,
Expand All @@ -101,13 +104,13 @@ fn renderContent(
}

const ContentWriter = struct {
pub const VariableMap = std.StringArrayHashMap(struct { std.Build.LazyPath, ContentWriter.UsageHint });
pub const VariableMap = std.array_hash_map.String(struct { std.Build.LazyPath, ContentWriter.UsageHint });

wfs: *std.Build.Step.WriteFile,
code: *std.Io.Writer,
vars: *VariableMap,

fn render(cw: ContentWriter, content: Content) !void {
fn render(cw: ContentWriter, content: Content, io: std.Io, gpa: std.mem.Allocator) !void {
// Always insert some padding before and after:
try cw.code.writeAll(" ");
errdefer cw.code.writeAll(" ") catch {};
Expand All @@ -122,15 +125,15 @@ const ContentWriter = struct {
},

.paste_file => |data| {
try cw.code.print("paste-file {f}", .{cw.fmtLazyPath(data, .file)});
try cw.code.print("paste-file {f}", .{cw.fmtLazyPath(data, .file, io, gpa)});
},

.mbr_part_table => |data| {
try cw.code.writeAll("mbr-part\n");

if (data.bootloader) |loader| {
try cw.code.writeAll(" bootloader ");
try cw.render(loader.*);
try cw.render(loader.*, io, gpa);
try cw.code.writeAll("\n");
}

Expand All @@ -151,7 +154,7 @@ const ContentWriter = struct {
try cw.code.print(" size {d}\n", .{size});
}
try cw.code.writeAll(" contains");
try cw.render(part.data);
try cw.render(part.data, io, gpa);
try cw.code.writeAll("\n");
try cw.code.writeAll(" endpart\n");
} else {
Expand Down Expand Up @@ -183,14 +186,17 @@ const ContentWriter = struct {
if (part.name) |name| {
try cw.code.print(" name \"{f}\"\n", .{std.zig.fmtString(name)});
}
if (part.part_guid) |pg| {
try cw.code.print(" guid \"{s}\"", .{&pg});
}
if (part.offset) |offset| {
try cw.code.print(" offset {d}\n", .{offset});
}
if (part.size) |size| {
try cw.code.print(" size {d}\n", .{size});
}
try cw.code.writeAll(" contains");
try cw.render(part.data);
try cw.render(part.data, io, gpa);
try cw.code.writeAll("\n");
try cw.code.writeAll(" endpart\n");
}
Expand All @@ -208,14 +214,14 @@ const ContentWriter = struct {
});
}

try cw.renderFileSystemTree(data.tree);
try cw.renderFileSystemTree(data.tree, io, gpa);

try cw.code.writeAll("endfat\n");
},
}
}

fn renderFileSystemTree(cw: ContentWriter, fs: FileSystem) !void {
fn renderFileSystemTree(cw: ContentWriter, fs: FileSystem, io: std.Io, gpa: std.mem.Allocator) !void {
for (fs.items) |item| {
switch (item) {
.empty_dir => |dir| try cw.code.print("mkdir {f}\n", .{
Expand All @@ -224,16 +230,16 @@ const ContentWriter = struct {

.copy_dir => |copy| try cw.code.print("copy-dir {f} {f}\n", .{
fmtPath(copy.destination),
cw.fmtLazyPath(copy.source, .directory),
cw.fmtLazyPath(copy.source, .directory, io, gpa),
}),

.copy_file => |copy| try cw.code.print("copy-file {f} {f}\n", .{
fmtPath(copy.destination),
cw.fmtLazyPath(copy.source, .file),
cw.fmtLazyPath(copy.source, .file, io, gpa),
}),

.include_script => |script| try cw.code.print("!include {f}\n", .{
cw.fmtLazyPath(script, .file),
cw.fmtLazyPath(script, .file, io, gpa),
}),
}
}
Expand Down Expand Up @@ -280,7 +286,7 @@ const ContentWriter = struct {
}
};
const LazyPathFormatter = std.fmt.Alt(
struct { ContentWriter, std.Build.LazyPath, UsageHint },
struct { ContentWriter, std.Build.LazyPath, UsageHint, std.Io, std.mem.Allocator },
formatLazyPath,
);
const UsageHint = enum { file, directory };
Expand All @@ -289,19 +295,21 @@ const ContentWriter = struct {
cw: ContentWriter,
path: std.Build.LazyPath,
hint: UsageHint,
io: std.Io,
gpa: std.mem.Allocator,
) LazyPathFormatter {
return .{ .data = .{ cw, path, hint } };
return .{ .data = .{ cw, path, hint, io, gpa } };
}

fn fmtPath(path: []const u8) PathFormatter {
return .{ .path = path };
}

fn formatLazyPath(
data: struct { ContentWriter, std.Build.LazyPath, UsageHint },
data: struct { ContentWriter, std.Build.LazyPath, UsageHint, std.Io, std.mem.Allocator },
writer: *std.Io.Writer,
) std.Io.Writer.Error!void {
const cw, const path, const hint = data;
const cw, const path, const hint, const io, const gpa = data;

switch (path) {
.cwd_relative,
Expand All @@ -314,12 +322,12 @@ const ContentWriter = struct {
const rel_path = path.getPath2(cw.wfs.step.owner, &cw.wfs.step);

const full_path = if (!std.fs.path.isAbsolute(rel_path))
std.fs.cwd().realpathAlloc(cw.wfs.step.owner.allocator, rel_path) catch @panic("oom")
std.Io.Dir.cwd().realPathFileAlloc(io, rel_path, cw.wfs.step.owner.allocator) catch @panic("oom")
else
rel_path;

if (!std.fs.path.isAbsolute(full_path)) {
const cwd = std.fs.cwd().realpathAlloc(cw.wfs.step.owner.allocator, ".") catch @panic("oom");
const cwd = std.Io.Dir.cwd().realPathFileAlloc(io, ".", cw.wfs.step.owner.allocator) catch @panic("oom");
std.debug.print("non-absolute path detected for {t}: cwd=\"{f}\" path=\"{f}\"\n", .{
path,
std.zig.fmtString(cwd),
Expand All @@ -339,7 +347,7 @@ const ContentWriter = struct {
const var_id = cw.vars.count() + 1;
const var_name = cw.wfs.step.owner.fmt("PATH{}", .{var_id});

cw.vars.put(var_name, .{ path, hint }) catch return error.WriteFailed;
cw.vars.put(gpa, var_name, .{ path, hint }) catch return error.WriteFailed;

try writer.print("${s}", .{var_name});
},
Expand Down Expand Up @@ -414,6 +422,7 @@ pub const GptPartTable = struct {
guid: [36]u8,
},
name: ?[]const u8 = null,
part_guid: ?[36]u8 = null,
size: ?u64 = null,
offset: ?u64 = null,
data: Content,
Expand All @@ -439,12 +448,12 @@ pub const FatFs = struct {

pub const FileSystemBuilder = struct {
b: *std.Build,
list: std.ArrayListUnmanaged(FileSystem.Item),
list: std.ArrayList(FileSystem.Item),

pub fn init(b: *std.Build) FileSystemBuilder {
return FileSystemBuilder{
.b = b,
.list = .{},
.list = .empty,
};
}

Expand Down
Loading
Loading