@@ -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
2425const usage =
@@ -41,6 +42,8 @@ const usage =
4142
4243const VariableMap = std .StringArrayHashMapUnmanaged ([]const u8 );
4344
45+ var global_deps_file : ? std.fs.File = null ;
46+
4447pub 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+
149176fn 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