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 {...@@ -44,6 +44,7 @@ pub const Builder = struct {
44 /// The purpose of executing the command is for a human to read compile errors from the terminal44 /// The purpose of executing the command is for a human to read compile errors from the terminal
45 prominent_compile_errors: bool,45 prominent_compile_errors: bool,
46 color: enum { auto, on, off } = .auto,46 color: enum { auto, on, off } = .auto,
47 use_stage1: ?bool = null,
47 invalid_user_input: bool,48 invalid_user_input: bool,
48 zig_exe: []const u8,49 zig_exe: []const u8,
49 default_step: *Step,50 default_step: *Step,
...@@ -1591,6 +1592,7 @@ pub const LibExeObjStep = struct {...@@ -1591,6 +1592,7 @@ pub const LibExeObjStep = struct {
1591 stack_size: ?u64 = null,1592 stack_size: ?u64 = null,
15921593
1593 want_lto: ?bool = null,1594 want_lto: ?bool = null,
1595 use_stage1: ?bool = null,
15941596
1595 output_path_source: GeneratedFile,1597 output_path_source: GeneratedFile,
1596 output_lib_path_source: GeneratedFile,1598 output_lib_path_source: GeneratedFile,
...@@ -2338,6 +2340,20 @@ pub const LibExeObjStep = struct {...@@ -2338,6 +2340,20 @@ pub const LibExeObjStep = struct {
2338 try zig_args.append(@tagName(builder.color));2340 try zig_args.append(@tagName(builder.color));
2339 }2341 }
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
2341 if (self.entry_symbol_name) |entry| {2357 if (self.entry_symbol_name) |entry| {
2342 try zig_args.append("--entry");2358 try zig_args.append("--entry");
2343 try zig_args.append(entry);2359 try zig_args.append(entry);
lib/std/special/build_runner.zig+12-5
...@@ -24,19 +24,19 @@ pub fn main() !void {...@@ -24,19 +24,19 @@ pub fn main() !void {
24 var arg_idx: usize = 1;24 var arg_idx: usize = 1;
2525
26 const zig_exe = nextArg(args, &arg_idx) orelse {26 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", .{});
28 return error.InvalidArgs;28 return error.InvalidArgs;
29 };29 };
30 const build_root = nextArg(args, &arg_idx) orelse {30 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", .{});
32 return error.InvalidArgs;32 return error.InvalidArgs;
33 };33 };
34 const cache_root = nextArg(args, &arg_idx) orelse {34 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", .{});
36 return error.InvalidArgs;36 return error.InvalidArgs;
37 };37 };
38 const global_cache_root = nextArg(args, &arg_idx) orelse {38 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", .{});
40 return error.InvalidArgs;40 return error.InvalidArgs;
41 };41 };
4242
...@@ -181,6 +181,10 @@ pub fn main() !void {...@@ -181,6 +181,10 @@ pub fn main() !void {
181 builder.enable_darling = true;181 builder.enable_darling = true;
182 } else if (mem.eql(u8, arg, "-fno-darling")) {182 } else if (mem.eql(u8, arg, "-fno-darling")) {
183 builder.enable_darling = false;183 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;
184 } else if (mem.eql(u8, arg, "--")) {188 } else if (mem.eql(u8, arg, "--")) {
185 builder.args = argsRest(args, arg_idx);189 builder.args = argsRest(args, arg_idx);
186 break;190 break;
...@@ -302,8 +306,11 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void...@@ -302,8 +306,11 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void
302 try out_stream.writeAll(306 try out_stream.writeAll(
303 \\307 \\
304 \\Advanced Options:308 \\Advanced Options:
309 \\ -fstage1 Force using bootstrap compiler as the codegen backend
310 \\ -fno-stage1 Prevent using bootstrap compiler as the codegen backend
305 \\ --build-file [file] Override path to build.zig311 \\ --build-file [file] Override path to build.zig
306 \\ --cache-dir [path] Override path to zig cache directory312 \\ --cache-dir [path] Override path to local Zig cache directory
313 \\ --global-cache-dir [path] Override path to global Zig cache directory
307 \\ --zig-lib-dir [arg] Override path to Zig lib directory314 \\ --zig-lib-dir [arg] Override path to Zig lib directory
308 \\ --debug-log [scope] Enable debugging the compiler315 \\ --debug-log [scope] Enable debugging the compiler
309 \\ --verbose-link Enable compiler debug output for linking316 \\ --verbose-link Enable compiler debug output for linking
src/main.zig+16-2
...@@ -3464,13 +3464,20 @@ pub const usage_build =...@@ -3464,13 +3464,20 @@ pub const usage_build =
3464 \\ Build a project from build.zig.3464 \\ Build a project from build.zig.
3465 \\3465 \\
3466 \\Options:3466 \\Options:
3467 \\ -h, --help Print this help and exit3467 \\ -fstage1 Force using bootstrap compiler as the codegen backend
3468 \\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
3469 \\3475 \\
3470;3476;
34713477
3472pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {3478pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
3473 var prominent_compile_errors: bool = false;3479 var prominent_compile_errors: bool = false;
3480 var use_stage1: ?bool = null;
34743481
3475 // We want to release all the locks before executing the child process, so we make a nice3482 // We want to release all the locks before executing the child process, so we make a nice
3476 // big block here to ensure the cleanup gets run when we extract out our argv.3483 // 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...@@ -3525,6 +3532,12 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
3525 continue;3532 continue;
3526 } else if (mem.eql(u8, arg, "--prominent-compile-errors")) {3533 } else if (mem.eql(u8, arg, "--prominent-compile-errors")) {
3527 prominent_compile_errors = true;3534 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);
3528 }3541 }
3529 }3542 }
3530 try child_argv.append(arg);3543 try child_argv.append(arg);
...@@ -3665,6 +3678,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -3665,6 +3678,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
3665 .optimize_mode = .Debug,3678 .optimize_mode = .Debug,
3666 .self_exe_path = self_exe_path,3679 .self_exe_path = self_exe_path,
3667 .thread_pool = &thread_pool,3680 .thread_pool = &thread_pool,
3681 .use_stage1 = use_stage1,
3668 }) catch |err| {3682 }) catch |err| {
3669 fatal("unable to create compilation: {s}", .{@errorName(err)});3683 fatal("unable to create compilation: {s}", .{@errorName(err)});
3670 };3684 };