authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-19 14:40:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-19 14:40:27-07:00
log0c5ad335d293be638b8d34e671cf62f84b0babce
treefb06d4122b52921129470cb07e3c1a883d74188e
parent9c2cbe39c2caa9137e1123fcdf2f326282cad1b5

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.

3 files changed, 44 insertions(+), 7 deletions(-)

lib/std/build.zig+16
......@@ -44,6 +44,7 @@ pub const Builder = struct {
4444 /// The purpose of executing the command is for a human to read compile errors from the terminal
4545 prominent_compile_errors: bool,
4646 color: enum { auto, on, off } = .auto,
47 use_stage1: ?bool = null,
4748 invalid_user_input: bool,
4849 zig_exe: []const u8,
4950 default_step: *Step,
......@@ -1591,6 +1592,7 @@ pub const LibExeObjStep = struct {
15911592 stack_size: ?u64 = null,
15921593
15931594 want_lto: ?bool = null,
1595 use_stage1: ?bool = null,
15941596
15951597 output_path_source: GeneratedFile,
15961598 output_lib_path_source: GeneratedFile,
......@@ -2338,6 +2340,20 @@ pub const LibExeObjStep = struct {
23382340 try zig_args.append(@tagName(builder.color));
23392341 }
23402342
2343 if (self.use_stage1) |stage1| {
2344 if (stage1) {
2345 try zig_args.append("-fstage1");
2346 } else {
2347 try zig_args.append("-fno-stage1");
2348 }
2349 } else if (builder.use_stage1) |stage1| {
2350 if (stage1) {
2351 try zig_args.append("-fstage1");
2352 } else {
2353 try zig_args.append("-fno-stage1");
2354 }
2355 }
2356
23412357 if (self.entry_symbol_name) |entry| {
23422358 try zig_args.append("--entry");
23432359 try zig_args.append(entry);
lib/std/special/build_runner.zig+12-5
......@@ -24,19 +24,19 @@ pub fn main() !void {
2424 var arg_idx: usize = 1;
2525
2626 const zig_exe = nextArg(args, &arg_idx) orelse {
27 std.debug.print("Expected first argument to be path to zig compiler\n", .{});
27 std.debug.print("Expected path to zig compiler\n", .{});
2828 return error.InvalidArgs;
2929 };
3030 const build_root = nextArg(args, &arg_idx) orelse {
31 std.debug.print("Expected second argument to be build root directory path\n", .{});
31 std.debug.print("Expected build root directory path\n", .{});
3232 return error.InvalidArgs;
3333 };
3434 const cache_root = nextArg(args, &arg_idx) orelse {
35 std.debug.print("Expected third argument to be cache root directory path\n", .{});
35 std.debug.print("Expected cache root directory path\n", .{});
3636 return error.InvalidArgs;
3737 };
3838 const global_cache_root = nextArg(args, &arg_idx) orelse {
39 std.debug.print("Expected third argument to be global cache root directory path\n", .{});
39 std.debug.print("Expected global cache root directory path\n", .{});
4040 return error.InvalidArgs;
4141 };
4242
......@@ -181,6 +181,10 @@ pub fn main() !void {
181181 builder.enable_darling = true;
182182 } else if (mem.eql(u8, arg, "-fno-darling")) {
183183 builder.enable_darling = false;
184 } else if (mem.eql(u8, arg, "-fstage1")) {
185 builder.use_stage1 = true;
186 } else if (mem.eql(u8, arg, "-fno-stage1")) {
187 builder.use_stage1 = false;
184188 } else if (mem.eql(u8, arg, "--")) {
185189 builder.args = argsRest(args, arg_idx);
186190 break;
......@@ -302,8 +306,11 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
302306 try out_stream.writeAll(
303307 \\
304308 \\Advanced Options:
309 \\ -fstage1 Force using bootstrap compiler as the codegen backend
310 \\ -fno-stage1 Prevent using bootstrap compiler as the codegen backend
305311 \\ --build-file [file] Override path to build.zig
306 \\ --cache-dir [path] Override path to zig cache directory
312 \\ --cache-dir [path] Override path to local Zig cache directory
313 \\ --global-cache-dir [path] Override path to global Zig cache directory
307314 \\ --zig-lib-dir [arg] Override path to Zig lib directory
308315 \\ --debug-log [scope] Enable debugging the compiler
309316 \\ --verbose-link Enable compiler debug output for linking
src/main.zig+16-2
......@@ -3464,13 +3464,20 @@ pub const usage_build =
34643464 \\ Build a project from build.zig.
34653465 \\
34663466 \\Options:
3467 \\ -h, --help Print this help and exit
3468 \\
3467 \\ -fstage1 Force using bootstrap compiler as the codegen backend
3468 \\ -fno-stage1 Prevent using bootstrap compiler as the codegen backend
3469 \\ --build-file [file] Override path to build.zig
3470 \\ --cache-dir [path] Override path to local Zig cache directory
3471 \\ --global-cache-dir [path] Override path to global Zig cache directory
3472 \\ --zig-lib-dir [arg] Override path to Zig lib directory
3473 \\ --prominent-compile-errors Output compile errors formatted for a human to read
3474 \\ -h, --help Print this help and exit
34693475 \\
34703476;
34713477
34723478pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
34733479 var prominent_compile_errors: bool = false;
3480 var use_stage1: ?bool = null;
34743481
34753482 // We want to release all the locks before executing the child process, so we make a nice
34763483 // 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
35253532 continue;
35263533 } else if (mem.eql(u8, arg, "--prominent-compile-errors")) {
35273534 prominent_compile_errors = true;
3535 } else if (mem.eql(u8, arg, "-fstage1")) {
3536 use_stage1 = true;
3537 try child_argv.append(arg);
3538 } else if (mem.eql(u8, arg, "-fno-stage1")) {
3539 use_stage1 = false;
3540 try child_argv.append(arg);
35283541 }
35293542 }
35303543 try child_argv.append(arg);
......@@ -3665,6 +3678,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
36653678 .optimize_mode = .Debug,
36663679 .self_exe_path = self_exe_path,
36673680 .thread_pool = &thread_pool,
3681 .use_stage1 = use_stage1,
36683682 }) catch |err| {
36693683 fatal("unable to create compilation: {s}", .{@errorName(err)});
36703684 };