Skip to content

Commit ccaa61b

Browse files
author
Felix "xq" Queißner
committed
Adds support for --deps-file
1 parent 1f829ba commit ccaa61b

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

justfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ behaviour-tests: \
2929
behaviour-test script: install
3030
@mkdir -p {{ join(out, parent_directory(script)) }}
3131
./zig-out/bin/dim --output {{ join(out, without_extension(script) + ".img") }} --script "{{script}}" --size 33M
32+
./zig-out/bin/dim --output {{ join(out, without_extension(script) + ".img") }} --deps-file {{ join(out, without_extension(script) + ".d") }} --script "{{script}}" --size 33M
3233

3334
# TODO(fqu): sfdisk --json .dim-out/tests/part/mbr/basic-single-part-unsized.img
3435

src/Parser.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Parser = @This();
99

1010
pub const Error = Tokenizer.Error || error{
1111
FileNotFound,
12+
InvalidPath,
1213
UnknownVariable,
1314
IoError,
1415
BadDirective,
@@ -19,10 +20,10 @@ pub const Error = Tokenizer.Error || error{
1920
};
2021

2122
pub const IO = struct {
22-
fetch_file_fn: *const fn (io: *const IO, std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory }![]const u8,
23+
fetch_file_fn: *const fn (io: *const IO, std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath }![]const u8,
2324
resolve_variable_fn: *const fn (io: *const IO, name: []const u8) error{UnknownVariable}![]const u8,
2425

25-
pub fn fetch_file(io: *const IO, allocator: std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory }![]const u8 {
26+
pub fn fetch_file(io: *const IO, allocator: std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath }![]const u8 {
2627
return io.fetch_file_fn(io, allocator, path);
2728
}
2829

@@ -402,6 +403,7 @@ fn fuzz_parser(_: void, input: []const u8) !void {
402403
error.BadDirective,
403404
error.FileNotFound,
404405
error.ExpectedIncludePath,
406+
error.InvalidPath,
405407
=> continue,
406408

407409
error.MaxIncludeDepthReached,

src/dim.zig

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const Options = struct {
1919
size: DiskSize = DiskSize.empty,
2020
script: ?[]const u8 = null,
2121
@"import-env": bool = false,
22+
@"deps-file": ?[]const u8 = null,
2223
};
2324

2425
const usage =
@@ -41,6 +42,8 @@ const usage =
4142

4243
const VariableMap = std.StringArrayHashMapUnmanaged([]const u8);
4344

45+
var global_deps_file: ?std.fs.File = null;
46+
4447
pub fn main() !u8 {
4548
var gpa_impl: std.heap.DebugAllocator(.{}) = .init;
4649
defer _ = gpa_impl.deinit();
@@ -95,6 +98,19 @@ pub fn main() !u8 {
9598
const script_source = try current_dir.readFileAlloc(gpa, script_path, max_script_size);
9699
defer gpa.free(script_source);
97100

101+
if (options.@"deps-file") |deps_file_path| {
102+
global_deps_file = try std.fs.cwd().createFile(deps_file_path, .{});
103+
104+
try global_deps_file.?.writer().print(
105+
\\{s}: {s}
106+
, .{
107+
output_path,
108+
script_path,
109+
});
110+
}
111+
defer if (global_deps_file) |deps_file|
112+
deps_file.close();
113+
98114
var mem_arena: std.heap.ArenaAllocator = .init(gpa);
99115
defer mem_arena.deinit();
100116

@@ -143,9 +159,20 @@ pub fn main() !u8 {
143159
try root_content.render(&stream);
144160
}
145161

162+
if (global_deps_file) |deps_file| {
163+
try deps_file.writeAll("\n");
164+
}
165+
146166
return 0;
147167
}
148168

169+
pub fn declare_file_dependency(path: []const u8) !void {
170+
const deps_file = global_deps_file orelse return;
171+
172+
try deps_file.writeAll(" \\\n ");
173+
try deps_file.writeAll(path);
174+
}
175+
149176
fn fatal(msg: []const u8) noreturn {
150177
std.debug.print("Error: {s}\n", .{msg});
151178
std.debug.print("Usage: {s}", .{usage});
@@ -325,8 +352,12 @@ const Environment = struct {
325352
std.log.err("PARSE ERROR: " ++ fmt, params);
326353
}
327354

328-
fn fetch_file(io: *const Parser.IO, allocator: std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory }![]const u8 {
355+
fn fetch_file(io: *const Parser.IO, allocator: std.mem.Allocator, path: []const u8) error{ FileNotFound, IoError, OutOfMemory, InvalidPath }![]const u8 {
329356
const env: *const Environment = @fieldParentPtr("io", io);
357+
358+
const name: FileName = .{ .root_dir = env.include_base, .rel_path = path };
359+
try name.declare_dependency();
360+
330361
return env.include_base.readFileAlloc(allocator, path, max_script_size) catch |err| switch (err) {
331362
error.OutOfMemory => return error.OutOfMemory,
332363
error.FileNotFound => return error.FileNotFound,
@@ -435,11 +466,14 @@ pub const FileName = struct {
435466
error.FileBusy,
436467
=> return error.IoError,
437468
};
469+
470+
try name.declare_dependency();
471+
438472
return .{ .file = file };
439473
}
440474

441475
pub fn open_dir(name: FileName) OpenError!std.fs.Dir {
442-
return name.root_dir.openDir(name.rel_path, .{ .iterate = true }) catch |err| switch (err) {
476+
const dir = name.root_dir.openDir(name.rel_path, .{ .iterate = true }) catch |err| switch (err) {
443477
error.FileNotFound => {
444478
var buffer: [std.fs.max_path_bytes]u8 = undefined;
445479
std.log.err("failed to open \"{}/{}\": not found", .{
@@ -467,6 +501,20 @@ pub const FileName = struct {
467501
error.NotDir,
468502
=> return error.IoError,
469503
};
504+
505+
try name.declare_dependency();
506+
507+
return dir;
508+
}
509+
510+
pub fn declare_dependency(name: FileName) OpenError!void {
511+
var buffer: [std.fs.max_path_bytes]u8 = undefined;
512+
513+
const realpath = name.root_dir.realpath(
514+
name.rel_path,
515+
&buffer,
516+
) catch @panic("failed to determine real path for dependency file!");
517+
declare_file_dependency(realpath) catch @panic("Failed to write to deps file!");
470518
}
471519

472520
pub const GetSizeError = error{ FileNotFound, InvalidPath, IoError };

0 commit comments

Comments
 (0)