@@ -26,6 +26,18 @@ const ArrayList = if (@hasDecl(std, "array_list")) std.ArrayList else std.ArrayL
2626const Step = std .Build .Step ;
2727const Allocator = std .mem .Allocator ;
2828
29+ const has_process_env = @import ("builtin" ).zig_version .order (.{
30+ .major = 0 ,
31+ .minor = 16 ,
32+ .patch = 0 ,
33+ .pre = "dev.1976" ,
34+ }) == .lt ;
35+ const process_stdio_ignore : std.process.SpawnOptions.StdIo =
36+ if (@hasField (std .process .SpawnOptions .StdIo , "Ignore" ))
37+ .Ignore
38+ else
39+ .ignore ;
40+
2941pub const dependencies = @import ("@dependencies" );
3042
3143pub const std_options : std.Options = .{
@@ -34,9 +46,31 @@ pub const std_options: std.Options = .{
3446 .crypto_fork_safety = false ,
3547};
3648
37- ///! This is a modified build runner to extract information out of build.zig
38- ///! Modified version of lib/build_runner.zig
39- pub fn main () ! void {
49+ pub const main = if (has_process_env ) @"main < dev.1976" else @"main >= dev.1976" ;
50+
51+ fn @"main >= dev.1976" (init : std.process.Init.Minimal ) ! void {
52+ // Here we use an ArenaAllocator backed by a DirectAllocator because a build is a short-lived,
53+ // one shot program. We don't need to waste time freeing memory and finding places to squish
54+ // bytes into. So we free everything all at once at the very end.
55+ var single_threaded_arena = std .heap .ArenaAllocator .init (std .heap .page_allocator );
56+ defer single_threaded_arena .deinit ();
57+
58+ var thread_safe_arena : std.heap.ThreadSafeAllocator = .{
59+ .child_allocator = single_threaded_arena .allocator (),
60+ };
61+ const arena = thread_safe_arena .allocator ();
62+
63+ const args = try init .args .toSlice (arena );
64+
65+ var threaded : std.Io.Threaded = .init (arena , .{
66+ .environ = init .environ ,
67+ });
68+ defer threaded .deinit ();
69+
70+ try doMain (arena , & threaded , args );
71+ }
72+
73+ fn @"main < dev.1976" () ! void {
4074 // Here we use an ArenaAllocator backed by a DirectAllocator because a build is a short-lived,
4175 // one shot program. We don't need to waste time freeing memory and finding places to squish
4276 // bytes into. So we free everything all at once at the very end.
@@ -52,6 +86,13 @@ pub fn main() !void {
5286
5387 var threaded : std.Io.Threaded = .init (arena , .{});
5488 defer threaded .deinit ();
89+
90+ try doMain (arena , & threaded , args );
91+ }
92+
93+ ///! This is a modified build runner to extract information out of build.zig
94+ ///! Modified version of lib/build_runner.zig
95+ fn doMain (arena : std.mem.Allocator , threaded : * std.Io.Threaded , args : []const [:0 ]const u8 ) ! void {
5596 const io = threaded .ioBasic ();
5697
5798 // skip my own exe name
@@ -64,6 +105,7 @@ pub fn main() !void {
64105 const global_cache_root = nextArg (args , & arg_idx ) orelse fatal ("missing global cache root directory path" , .{});
65106
66107 const cwd : std.Io.Dir = .cwd ();
108+ const cwd_path = try std .process .getCwdAlloc (arena );
67109
68110 const zig_lib_directory : std.Build.Cache.Directory = .{
69111 .path = zig_lib_dir ,
@@ -85,24 +127,33 @@ pub fn main() !void {
85127 .handle = try cwd .createDirPathOpen (io , global_cache_root , .{}),
86128 };
87129
88- var graph : std.Build.Graph = .{
89- .io = io ,
90- .arena = arena ,
91- .cache = .{
130+ var graph : std.Build.Graph = undefined ;
131+ graph .io = io ;
132+ graph .arena = arena ;
133+ graph .zig_exe = zig_exe ;
134+ graph .global_cache_root = global_cache_directory ;
135+ graph .zig_lib_directory = zig_lib_directory ;
136+ graph .host = .{
137+ .query = .{},
138+ .result = try std .zig .system .resolveTargetQuery (io , .{}),
139+ };
140+ graph .time_report = false ;
141+
142+ if (has_process_env ) {
143+ graph .env_map = try process .getEnvMap (arena );
144+ graph .cache = .{
92145 .io = io ,
93146 .gpa = arena ,
94147 .manifest_dir = try local_cache_directory .handle .createDirPathOpen (io , "h" , .{}),
95- },
96- .zig_exe = zig_exe ,
97- .env_map = try process .getEnvMap (arena ),
98- .global_cache_root = global_cache_directory ,
99- .zig_lib_directory = zig_lib_directory ,
100- .host = .{
101- .query = .{},
102- .result = try std .zig .system .resolveTargetQuery (io , .{}),
103- },
104- .time_report = false ,
105- };
148+ };
149+ } else {
150+ graph .cache = .{
151+ .io = io ,
152+ .gpa = arena ,
153+ .manifest_dir = try local_cache_directory .handle .createDirPathOpen (io , "h" , .{}),
154+ .cwd = cwd_path ,
155+ };
156+ }
106157
107158 graph .cache .addPrefix (.{ .path = null , .handle = cwd });
108159 graph .cache .addPrefix (build_root_directory );
@@ -388,7 +439,7 @@ pub fn main() !void {
388439 return ;
389440 }
390441
391- var w = try Watch .init (io );
442+ var w = try Watch .init (io , cwd_path );
392443
393444 const message_thread = try std .Thread .spawn (.{}, struct {
394445 fn do (ww : * Watch ) void {
@@ -475,10 +526,24 @@ const Watch = struct {
475526 manual_event : std.Io.Event ,
476527 steps : []const * Step ,
477528
478- fn init (io : std.Io ) ! Watch {
529+ fn init (io : std.Io , cwd_path : []const u8 ) ! Watch {
530+ const std_build_watch_status : enum {
531+ unimplemented ,
532+ v1 ,
533+ v2 ,
534+ } = if (@TypeOf (std .Build .Watch ) == void )
535+ .unimplemented
536+ else if (@typeInfo (@TypeOf (std .Build .Watch .init )).@"fn" .params .len == 0 )
537+ .v1
538+ else
539+ .v2 ;
479540 return .{
480541 .io = io ,
481- .fs_watch = if (@TypeOf (std .Build .Watch ) != void ) try std .Build .Watch .init () else {},
542+ .fs_watch = switch (std_build_watch_status ) {
543+ .unimplemented = > {},
544+ .v1 = > try std .Build .Watch .init (),
545+ .v2 = > try std .Build .Watch .init (cwd_path ),
546+ },
482547 .supports_fs_watch = @TypeOf (std .Build .Watch ) != void and shared .BuildOnSaveSupport .isSupportedRuntime (builtin .zig_version ) == .supported ,
483548 .manual_event = .unset ,
484549 .steps = &.{},
@@ -1395,7 +1460,7 @@ const copied_from_zig = struct {
13951460 pkg_name ,
13961461 "--cflags" ,
13971462 "--libs" ,
1398- }, & code , .Ignore )) | stdout | stdout else | err | switch (err ) {
1463+ }, & code , process_stdio_ignore )) | stdout | stdout else | err | switch (err ) {
13991464 error .ProcessTerminated = > return error .PkgConfigCrashed ,
14001465 error .ExecNotSupported = > return error .PkgConfigFailed ,
14011466 error .ExitCodeFailure = > return error .PkgConfigFailed ,
@@ -1437,7 +1502,7 @@ const copied_from_zig = struct {
14371502 }
14381503
14391504 fn execPkgConfigList (self : * std.Build , out_code : * u8 ) (std .Build .PkgConfigError || std .Build .RunError )! []const std.Build.PkgConfigPkg {
1440- const stdout = try self .runAllowFail (&.{ "pkg-config" , "--list-all" }, out_code , .Ignore );
1505+ const stdout = try self .runAllowFail (&.{ "pkg-config" , "--list-all" }, out_code , process_stdio_ignore );
14411506 var list = ArrayListManaged (std .Build .PkgConfigPkg ).init (self .allocator );
14421507 errdefer list .deinit ();
14431508 var line_it = mem .tokenizeAny (u8 , stdout , "\r \n " );
0 commit comments