@@ -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 , try init .environ .createMap (arena ));
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,18 @@ 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 (
96+ arena : std.mem.Allocator ,
97+ threaded : * std.Io.Threaded ,
98+ args : []const [:0 ]const u8 ,
99+ environ_map : if (@hasDecl (std.process , "Environ" )) std.process.Environ.Map else void ,
100+ ) ! void {
55101 const io = threaded .ioBasic ();
56102
57103 // skip my own exe name
@@ -64,6 +110,7 @@ pub fn main() !void {
64110 const global_cache_root = nextArg (args , & arg_idx ) orelse fatal ("missing global cache root directory path" , .{});
65111
66112 const cwd : std.Io.Dir = .cwd ();
113+ const cwd_path = try std .process .getCwdAlloc (arena );
67114
68115 const zig_lib_directory : std.Build.Cache.Directory = .{
69116 .path = zig_lib_dir ,
@@ -85,7 +132,7 @@ pub fn main() !void {
85132 .handle = try cwd .createDirPathOpen (io , global_cache_root , .{}),
86133 };
87134
88- var graph : std.Build.Graph = .{
135+ var graph : std.Build.Graph = if ( has_process_env ) .{
89136 .io = io ,
90137 .arena = arena ,
91138 .cache = .{
@@ -102,6 +149,24 @@ pub fn main() !void {
102149 .result = try std .zig .system .resolveTargetQuery (io , .{}),
103150 },
104151 .time_report = false ,
152+ } else .{
153+ .io = io ,
154+ .arena = arena ,
155+ .cache = .{
156+ .io = io ,
157+ .gpa = arena ,
158+ .manifest_dir = try local_cache_directory .handle .createDirPathOpen (io , "h" , .{}),
159+ .cwd = cwd_path ,
160+ },
161+ .zig_exe = zig_exe ,
162+ .environ_map = environ_map ,
163+ .global_cache_root = global_cache_directory ,
164+ .zig_lib_directory = zig_lib_directory ,
165+ .host = .{
166+ .query = .{},
167+ .result = try std .zig .system .resolveTargetQuery (io , .{}),
168+ },
169+ .time_report = false ,
105170 };
106171
107172 graph .cache .addPrefix (.{ .path = null , .handle = cwd });
@@ -388,7 +453,7 @@ pub fn main() !void {
388453 return ;
389454 }
390455
391- var w = try Watch .init (io );
456+ var w = try Watch .init (io , cwd_path );
392457
393458 const message_thread = try std .Thread .spawn (.{}, struct {
394459 fn do (ww : * Watch ) void {
@@ -475,10 +540,24 @@ const Watch = struct {
475540 manual_event : std.Io.Event ,
476541 steps : []const * Step ,
477542
478- fn init (io : std.Io ) ! Watch {
543+ fn init (io : std.Io , cwd_path : []const u8 ) ! Watch {
544+ const std_build_watch_status : enum {
545+ unimplemented ,
546+ v1 ,
547+ v2 ,
548+ } = if (@TypeOf (std .Build .Watch ) == void )
549+ .unimplemented
550+ else if (@typeInfo (@TypeOf (std .Build .Watch .init )).@"fn" .params .len == 0 )
551+ .v1
552+ else
553+ .v2 ;
479554 return .{
480555 .io = io ,
481- .fs_watch = if (@TypeOf (std .Build .Watch ) != void ) try std .Build .Watch .init () else {},
556+ .fs_watch = switch (std_build_watch_status ) {
557+ .unimplemented = > {},
558+ .v1 = > try std .Build .Watch .init (),
559+ .v2 = > try std .Build .Watch .init (cwd_path ),
560+ },
482561 .supports_fs_watch = @TypeOf (std .Build .Watch ) != void and shared .BuildOnSaveSupport .isSupportedRuntime (builtin .zig_version ) == .supported ,
483562 .manual_event = .unset ,
484563 .steps = &.{},
@@ -1395,7 +1474,7 @@ const copied_from_zig = struct {
13951474 pkg_name ,
13961475 "--cflags" ,
13971476 "--libs" ,
1398- }, & code , .Ignore )) | stdout | stdout else | err | switch (err ) {
1477+ }, & code , process_stdio_ignore )) | stdout | stdout else | err | switch (err ) {
13991478 error .ProcessTerminated = > return error .PkgConfigCrashed ,
14001479 error .ExecNotSupported = > return error .PkgConfigFailed ,
14011480 error .ExitCodeFailure = > return error .PkgConfigFailed ,
@@ -1437,7 +1516,7 @@ const copied_from_zig = struct {
14371516 }
14381517
14391518 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 );
1519+ const stdout = try self .runAllowFail (&.{ "pkg-config" , "--list-all" }, out_code , process_stdio_ignore );
14411520 var list = ArrayListManaged (std .Build .PkgConfigPkg ).init (self .allocator );
14421521 errdefer list .deinit ();
14431522 var line_it = mem .tokenizeAny (u8 , stdout , "\r \n " );
0 commit comments