@@ -19,6 +19,7 @@ const Options = struct {
19
19
size : DiskSize = DiskSize .empty ,
20
20
script : ? []const u8 = null ,
21
21
@"import-env" : bool = false ,
22
+ @"deps-file" : ? []const u8 = null ,
22
23
};
23
24
24
25
const usage =
@@ -41,6 +42,8 @@ const usage =
41
42
42
43
const VariableMap = std .StringArrayHashMapUnmanaged ([]const u8 );
43
44
45
+ var global_deps_file : ? std.fs.File = null ;
46
+
44
47
pub fn main () ! u8 {
45
48
var gpa_impl : std .heap .DebugAllocator (.{}) = .init ;
46
49
defer _ = gpa_impl .deinit ();
@@ -95,6 +98,19 @@ pub fn main() !u8 {
95
98
const script_source = try current_dir .readFileAlloc (gpa , script_path , max_script_size );
96
99
defer gpa .free (script_source );
97
100
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
+
98
114
var mem_arena : std.heap.ArenaAllocator = .init (gpa );
99
115
defer mem_arena .deinit ();
100
116
@@ -143,9 +159,20 @@ pub fn main() !u8 {
143
159
try root_content .render (& stream );
144
160
}
145
161
162
+ if (global_deps_file ) | deps_file | {
163
+ try deps_file .writeAll ("\n " );
164
+ }
165
+
146
166
return 0 ;
147
167
}
148
168
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
+
149
176
fn fatal (msg : []const u8 ) noreturn {
150
177
std .debug .print ("Error: {s}\n " , .{msg });
151
178
std .debug .print ("Usage: {s}" , .{usage });
@@ -325,8 +352,12 @@ const Environment = struct {
325
352
std .log .err ("PARSE ERROR: " ++ fmt , params );
326
353
}
327
354
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 {
329
356
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
+
330
361
return env .include_base .readFileAlloc (allocator , path , max_script_size ) catch | err | switch (err ) {
331
362
error .OutOfMemory = > return error .OutOfMemory ,
332
363
error .FileNotFound = > return error .FileNotFound ,
@@ -435,11 +466,14 @@ pub const FileName = struct {
435
466
error .FileBusy ,
436
467
= > return error .IoError ,
437
468
};
469
+
470
+ try name .declare_dependency ();
471
+
438
472
return .{ .file = file };
439
473
}
440
474
441
475
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 ) {
443
477
error .FileNotFound = > {
444
478
var buffer : [std .fs .max_path_bytes ]u8 = undefined ;
445
479
std .log .err ("failed to open \" {}/{}\" : not found" , .{
@@ -467,6 +501,20 @@ pub const FileName = struct {
467
501
error .NotDir ,
468
502
= > return error .IoError ,
469
503
};
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!" );
470
518
}
471
519
472
520
pub const GetSizeError = error { FileNotFound , InvalidPath , IoError };
0 commit comments