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 };...@@ -14,13 +14,30 @@ const zig_version = std.builtin.Version{ .major = 0, .minor = 11, .patch = 0 };
14const stack_size = 32 * 1024 * 1024;14const stack_size = 32 * 1024 * 1024;
1515
16pub fn build(b: *Builder) !void {16pub fn build(b: *Builder) !void {
17 b.setPreferredReleaseMode(.ReleaseFast);17 const wasi_bootstrap = b.option(bool, "wasi-bootstrap", "Produce a WASI build for bootstrapping the compiler") orelse false;
18 const test_step = b.step("test", "Run all the tests");18 const release = b.option(bool, "release", "Build in release mode") orelse wasi_bootstrap;
19 const mode = b.standardReleaseOptions();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 var target = b.standardTargetOptions(.{});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
21 const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");36 const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");
22 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;37 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
24 const docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");41 const docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
25 docgen_exe.single_threaded = single_threaded;42 docgen_exe.single_threaded = single_threaded;
2643
...@@ -48,8 +65,6 @@ pub fn build(b: *Builder) !void {...@@ -48,8 +65,6 @@ pub fn build(b: *Builder) !void {
4865
49 const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});66 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
53 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;68 const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
54 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;69 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
55 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;70 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 {...@@ -65,7 +80,7 @@ pub fn build(b: *Builder) !void {
65 if (deprecated_skip_install_lib_files) {80 if (deprecated_skip_install_lib_files) {
66 std.log.warn("-Dskip-install-lib-files is deprecated in favor of -Dno-lib", .{});81 std.log.warn("-Dskip-install-lib-files is deprecated in favor of -Dno-lib", .{});
67 }82 }
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
70 const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;85 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 {...@@ -129,21 +144,17 @@ pub fn build(b: *Builder) !void {
129 const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);144 const tracy_callstack = b.option(bool, "tracy-callstack", "Include callstack information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
130 const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);145 const tracy_allocation = b.option(bool, "tracy-allocation", "Include allocation information with Tracy data. Does nothing if -Dtracy is not provided") orelse (tracy != null);
131 const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false;146 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));
133 const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;148 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");
135 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;150 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
137 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: {152 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);
139 if (mode != .Debug) break :blk 0;154 if (mode != .Debug) break :blk 0;
140 break :blk 4;155 break :blk 4;
141 };156 };
142157
143 if (only_c) {
144 target.ofmt = .c;
145 }
146
147 const main_file: ?[]const u8 = "src/main.zig";158 const main_file: ?[]const u8 = "src/main.zig";
148159
149 const exe = b.addExecutable("zig", main_file);160 const exe = b.addExecutable("zig", main_file);
src/Compilation.zig+7-5
...@@ -2421,11 +2421,13 @@ pub fn update(comp: *Compilation) !void {...@@ -2421,11 +2421,13 @@ pub fn update(comp: *Compilation) !void {
2421 return;2421 return;
2422 }2422 }
24232423
2424 if (comp.emit_docs) |doc_location| {2424 if (!build_options.only_c) {
2425 if (comp.bin_file.options.module) |module| {2425 if (comp.emit_docs) |doc_location| {
2426 var autodoc = Autodoc.init(module, doc_location);2426 if (comp.bin_file.options.module) |module| {
2427 defer autodoc.deinit();2427 var autodoc = Autodoc.init(module, doc_location);
2428 try autodoc.generateZirData();2428 defer autodoc.deinit();
2429 try autodoc.generateZirData();
2430 }
2429 }2431 }
2430 }2432 }
24312433
src/main.zig+7-1
...@@ -168,7 +168,13 @@ pub fn main() anyerror!void {...@@ -168,7 +168,13 @@ pub fn main() anyerror!void {
168 // This sets our CWD to "/preopens/cwd"168 // This sets our CWD to "/preopens/cwd"
169 // Dot-prefixed preopens like `--dir=.` are "mounted" at "/preopens/cwd"169 // Dot-prefixed preopens like `--dir=.` are "mounted" at "/preopens/cwd"
170 // Other preopens like `--dir=lib` are "mounted" at "/"170 // 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 });
172 }178 }
173179
174 return mainArgs(gpa, arena, args);180 return mainArgs(gpa, arena, args);