From 0c5ad335d293be638b8d34e671cf62f84b0babce Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 19 Apr 2022 14:40:27 -0700 Subject: [PATCH] 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. --- lib/std/build.zig | 16 ++++++++++++++++ lib/std/special/build_runner.zig | 17 ++++++++++++----- src/main.zig | 18 ++++++++++++++++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index ff0f36b4a8..5966002799 100644 --- a/lib/std/build.zig +++ b/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); diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index eb83ef8fcd..523723ddf2 100644 --- a/lib/std/special/build_runner.zig +++ b/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 diff --git a/src/main.zig b/src/main.zig index 51765edb51..84a69b98f1 100644 --- a/src/main.zig +++ b/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)}); };