Commit 25b83188d0

Auguste Rame <19855629+SuperAuguste@users.noreply.github.com>
2023-03-01 19:20:01
Add --build-runner `zig build` option (#14742)
1 parent a7a709a
Changed files (2)
lib/build_runner.zig
@@ -362,6 +362,7 @@ fn usage(builder: *std.Build, already_ran_build: bool, out_stream: anytype) !voi
         \\  --cache-dir [path]           Override path to local Zig cache directory
         \\  --global-cache-dir [path]    Override path to global Zig cache directory
         \\  --zig-lib-dir [arg]          Override path to Zig lib directory
+        \\  --build-runner [file]        Override path to build runner
         \\  --debug-log [scope]          Enable debugging the compiler
         \\  --verbose-link               Enable compiler debug output for linking
         \\  --verbose-air                Enable compiler debug output for Zig AIR
src/main.zig
@@ -4013,6 +4013,7 @@ pub const usage_build =
     \\   --cache-dir [path]            Override path to local Zig cache directory
     \\   --global-cache-dir [path]     Override path to global Zig cache directory
     \\   --zig-lib-dir [arg]           Override path to Zig lib directory
+    \\   --build-runner [file]         Override path to build runner
     \\   --prominent-compile-errors    Output compile errors formatted for a human to read
     \\   -h, --help                    Print this help and exit
     \\
@@ -4031,6 +4032,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
         var override_lib_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIB_DIR");
         var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR");
         var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR");
+        var override_build_runner: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_BUILD_RUNNER");
         var child_argv = std.ArrayList([]const u8).init(arena);
         var reference_trace: ?u32 = null;
         var debug_compile_errors = false;
@@ -4065,6 +4067,11 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
                         override_lib_dir = args[i];
                         try child_argv.appendSlice(&[_][]const u8{ arg, args[i] });
                         continue;
+                    } else if (mem.eql(u8, arg, "--build-runner")) {
+                        if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
+                        i += 1;
+                        override_build_runner = args[i];
+                        continue;
                     } else if (mem.eql(u8, arg, "--cache-dir")) {
                         if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
                         i += 1;
@@ -4197,10 +4204,29 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
         try thread_pool.init(gpa);
         defer thread_pool.deinit();
 
-        var main_pkg: Package = .{
-            .root_src_directory = zig_lib_directory,
-            .root_src_path = "build_runner.zig",
-        };
+        var cleanup_build_runner_dir: ?fs.Dir = null;
+        defer if (cleanup_build_runner_dir) |*dir| dir.close();
+
+        var main_pkg: Package = if (override_build_runner) |build_runner_path|
+            .{
+                .root_src_directory = blk: {
+                    if (std.fs.path.dirname(build_runner_path)) |dirname| {
+                        const dir = fs.cwd().openDir(dirname, .{}) catch |err| {
+                            fatal("unable to open directory to build runner from argument 'build-runner', '{s}': {s}", .{ dirname, @errorName(err) });
+                        };
+                        cleanup_build_runner_dir = dir;
+                        break :blk .{ .path = dirname, .handle = dir };
+                    }
+
+                    break :blk .{ .path = null, .handle = fs.cwd() };
+                },
+                .root_src_path = std.fs.path.basename(build_runner_path),
+            }
+        else
+            .{
+                .root_src_directory = zig_lib_directory,
+                .root_src_path = "build_runner.zig",
+            };
 
         if (!build_options.omit_pkg_fetching_code) {
             var http_client: std.http.Client = .{ .allocator = gpa };