Commit 0c5ad335d2

Andrew Kelley <andrew@ziglang.org>
2022-04-19 23:40:27
build system: add -fstage1/-fno-stage1 to `zig build`
So that people can start experimenting with compiling their projects with the self-hosted compiler. I expect this commit to be reverted after #89 is closed.
1 parent 9c2cbe3
Changed files (3)
lib/std/special/build_runner.zig
@@ -24,19 +24,19 @@ pub fn main() !void {
     var arg_idx: usize = 1;
 
     const zig_exe = nextArg(args, &arg_idx) orelse {
-        std.debug.print("Expected first argument to be path to zig compiler\n", .{});
+        std.debug.print("Expected path to zig compiler\n", .{});
         return error.InvalidArgs;
     };
     const build_root = nextArg(args, &arg_idx) orelse {
-        std.debug.print("Expected second argument to be build root directory path\n", .{});
+        std.debug.print("Expected build root directory path\n", .{});
         return error.InvalidArgs;
     };
     const cache_root = nextArg(args, &arg_idx) orelse {
-        std.debug.print("Expected third argument to be cache root directory path\n", .{});
+        std.debug.print("Expected cache root directory path\n", .{});
         return error.InvalidArgs;
     };
     const global_cache_root = nextArg(args, &arg_idx) orelse {
-        std.debug.print("Expected third argument to be global cache root directory path\n", .{});
+        std.debug.print("Expected global cache root directory path\n", .{});
         return error.InvalidArgs;
     };
 
@@ -181,6 +181,10 @@ pub fn main() !void {
                 builder.enable_darling = true;
             } else if (mem.eql(u8, arg, "-fno-darling")) {
                 builder.enable_darling = false;
+            } else if (mem.eql(u8, arg, "-fstage1")) {
+                builder.use_stage1 = true;
+            } else if (mem.eql(u8, arg, "-fno-stage1")) {
+                builder.use_stage1 = false;
             } else if (mem.eql(u8, arg, "--")) {
                 builder.args = argsRest(args, arg_idx);
                 break;
@@ -302,8 +306,11 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
     try out_stream.writeAll(
         \\
         \\Advanced Options:
+        \\  -fstage1                     Force using bootstrap compiler as the codegen backend
+        \\  -fno-stage1                  Prevent using bootstrap compiler as the codegen backend
         \\  --build-file [file]          Override path to build.zig
-        \\  --cache-dir [path]           Override path to zig cache directory
+        \\  --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
         \\  --debug-log [scope]          Enable debugging the compiler
         \\  --verbose-link               Enable compiler debug output for linking
lib/std/build.zig
@@ -44,6 +44,7 @@ pub const Builder = struct {
     /// The purpose of executing the command is for a human to read compile errors from the terminal
     prominent_compile_errors: bool,
     color: enum { auto, on, off } = .auto,
+    use_stage1: ?bool = null,
     invalid_user_input: bool,
     zig_exe: []const u8,
     default_step: *Step,
@@ -1591,6 +1592,7 @@ pub const LibExeObjStep = struct {
     stack_size: ?u64 = null,
 
     want_lto: ?bool = null,
+    use_stage1: ?bool = null,
 
     output_path_source: GeneratedFile,
     output_lib_path_source: GeneratedFile,
@@ -2338,6 +2340,20 @@ pub const LibExeObjStep = struct {
             try zig_args.append(@tagName(builder.color));
         }
 
+        if (self.use_stage1) |stage1| {
+            if (stage1) {
+                try zig_args.append("-fstage1");
+            } else {
+                try zig_args.append("-fno-stage1");
+            }
+        } else if (builder.use_stage1) |stage1| {
+            if (stage1) {
+                try zig_args.append("-fstage1");
+            } else {
+                try zig_args.append("-fno-stage1");
+            }
+        }
+
         if (self.entry_symbol_name) |entry| {
             try zig_args.append("--entry");
             try zig_args.append(entry);
src/main.zig
@@ -3464,13 +3464,20 @@ pub const usage_build =
     \\   Build a project from build.zig.
     \\
     \\Options:
-    \\   -h, --help             Print this help and exit
-    \\
+    \\   -fstage1                      Force using bootstrap compiler as the codegen backend
+    \\   -fno-stage1                   Prevent using bootstrap compiler as the codegen backend
+    \\   --build-file [file]           Override path to build.zig
+    \\   --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
+    \\   --prominent-compile-errors    Output compile errors formatted for a human to read
+    \\   -h, --help                    Print this help and exit
     \\
 ;
 
 pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
     var prominent_compile_errors: bool = false;
+    var use_stage1: ?bool = null;
 
     // We want to release all the locks before executing the child process, so we make a nice
     // big block here to ensure the cleanup gets run when we extract out our argv.
@@ -3525,6 +3532,12 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
                         continue;
                     } else if (mem.eql(u8, arg, "--prominent-compile-errors")) {
                         prominent_compile_errors = true;
+                    } else if (mem.eql(u8, arg, "-fstage1")) {
+                        use_stage1 = true;
+                        try child_argv.append(arg);
+                    } else if (mem.eql(u8, arg, "-fno-stage1")) {
+                        use_stage1 = false;
+                        try child_argv.append(arg);
                     }
                 }
                 try child_argv.append(arg);
@@ -3665,6 +3678,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
             .optimize_mode = .Debug,
             .self_exe_path = self_exe_path,
             .thread_pool = &thread_pool,
+            .use_stage1 = use_stage1,
         }) catch |err| {
             fatal("unable to create compilation: {s}", .{@errorName(err)});
         };