authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-11-04 18:35:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-06 12:15:04-07:00
logdb023b98a4deef527d4c70d146009f754e9b168d
treece17bf192695b873c1e6ecb221a22a0d3a30048b
parent28514476ef8c824c3d189d98f23d0f8d23e496ea

build: introduce -Dwasi-bootstrap option

Also, make -Donly-c prevent Autodoc from being included in the binary. This produces a 2.6 MiB zig.wasm file. 781 KB if piped through zstd.

3 files changed, 39 insertions(+), 20 deletions(-)

build.zig+25-14
......@@ -14,13 +14,30 @@ const zig_version = std.builtin.Version{ .major = 0, .minor = 11, .patch = 0 };
1414const stack_size = 32 * 1024 * 1024;
1515
1616pub fn build(b: *Builder) !void {
17 b.setPreferredReleaseMode(.ReleaseFast);
18 const test_step = b.step("test", "Run all the tests");
19 const mode = b.standardReleaseOptions();
20 var target = b.standardTargetOptions(.{});
17 const wasi_bootstrap = b.option(bool, "wasi-bootstrap", "Produce a WASI build for bootstrapping the compiler") orelse false;
18 const release = b.option(bool, "release", "Build in release mode") orelse wasi_bootstrap;
19 const only_c = b.option(bool, "only-c", "Translate the Zig compiler to C code, with only the C backend enabled") orelse wasi_bootstrap;
20 const target = t: {
21 var default_target: std.zig.CrossTarget = .{};
22 if (wasi_bootstrap) {
23 default_target.cpu_arch = .wasm32;
24 default_target.os_tag = .wasi;
25 break :t default_target;
26 } else if (only_c) {
27 default_target.ofmt = .c;
28 }
29 break :t b.standardTargetOptions(.{ .default_target = default_target });
30 };
31 const mode: std.builtin.Mode = if (release) switch (target.getCpuArch()) {
32 .wasm32 => .ReleaseSmall,
33 else => .ReleaseFast,
34 } else .Debug;
35
2136 const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");
2237 const use_zig_libcxx = b.option(bool, "use-zig-libcxx", "If libc++ is needed, use zig's bundled version, don't try to integrate with the system") orelse false;
2338
39 const test_step = b.step("test", "Run all the tests");
40
2441 const docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
2542 docgen_exe.single_threaded = single_threaded;
2643
......@@ -48,8 +65,6 @@ pub fn build(b: *Builder) !void {
4865
4966 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
5067
51 const only_c = b.option(bool, "only-c", "Translate the Zig compiler to C code, with only the C backend enabled") orelse false;
52
5368 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
5469 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
5570 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;
......@@ -65,7 +80,7 @@ pub fn build(b: *Builder) !void {
6580 if (deprecated_skip_install_lib_files) {
6681 std.log.warn("-Dskip-install-lib-files is deprecated in favor of -Dno-lib", .{});
6782 }
68 const skip_install_lib_files = b.option(bool, "no-lib", "skip copying of lib/ files to installation prefix. Useful for development") orelse deprecated_skip_install_lib_files;
83 const skip_install_lib_files = b.option(bool, "no-lib", "skip copying of lib/ files to installation prefix. Useful for development") orelse (deprecated_skip_install_lib_files or wasi_bootstrap);
6984
7085 const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;
7186
......@@ -129,21 +144,17 @@ pub fn build(b: *Builder) !void {
129144 const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
130145 const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
131146 const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false;
132 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);
147 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or (only_c and !wasi_bootstrap));
133148 const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;
134 const strip = b.option(bool, "strip", "Omit debug information") orelse false;
149 const strip = b.option(bool, "strip", "Omit debug information");
135150 const value_tracing = b.option(bool, "value-tracing", "Enable extra state tracking to help troubleshoot bugs in the compiler (using the std.debug.Trace API)") orelse false;
136151
137152 const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: {
138 if (strip) break :blk @as(u32, 0);
153 if (strip == true) break :blk @as(u32, 0);
139154 if (mode != .Debug) break :blk 0;
140155 break :blk 4;
141156 };
142157
143 if (only_c) {
144 target.ofmt = .c;
145 }
146
147158 const main_file: ?[]const u8 = "src/main.zig";
148159
149160 const exe = b.addExecutable("zig", main_file);
src/Compilation.zig+7-5
......@@ -2421,11 +2421,13 @@ pub fn update(comp: *Compilation) !void {
24212421 return;
24222422 }
24232423
2424 if (comp.emit_docs) |doc_location| {
2425 if (comp.bin_file.options.module) |module| {
2426 var autodoc = Autodoc.init(module, doc_location);
2427 defer autodoc.deinit();
2428 try autodoc.generateZirData();
2424 if (!build_options.only_c) {
2425 if (comp.emit_docs) |doc_location| {
2426 if (comp.bin_file.options.module) |module| {
2427 var autodoc = Autodoc.init(module, doc_location);
2428 defer autodoc.deinit();
2429 try autodoc.generateZirData();
2430 }
24292431 }
24302432 }
24312433
src/main.zig+7-1
......@@ -168,7 +168,13 @@ pub fn main() anyerror!void {
168168 // This sets our CWD to "/preopens/cwd"
169169 // Dot-prefixed preopens like `--dir=.` are "mounted" at "/preopens/cwd"
170170 // Other preopens like `--dir=lib` are "mounted" at "/"
171 try std.os.initPreopensWasi(std.heap.page_allocator, "/preopens/cwd");
171 try std.os.initPreopensWasi(arena, "/preopens/cwd");
172 }
173
174 // Short circuit some of the other logic for bootstrapping.
175 if (build_options.only_c) {
176 assert(mem.eql(u8, args[1], "build-obj"));
177 return buildOutputType(gpa, arena, args, .{ .build = .Obj });
172178 }
173179
174180 return mainArgs(gpa, arena, args);