Skip to content

Commit e7d2834

Browse files
authored
Merge pull request #13560 from ziglang/wasi-bootstrap
Nuke the C++ implementation of Zig from orbit using WASI
2 parents 817cf6a + 20d86d9 commit e7d2834

File tree

579 files changed

+25576
-126675
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

579 files changed

+25576
-126675
lines changed

CMakeLists.txt

Lines changed: 152 additions & 382 deletions
Large diffs are not rendered by default.

build.zig

Lines changed: 83 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ const zig_version = std.builtin.Version{ .major = 0, .minor = 11, .patch = 0 };
1414
const stack_size = 32 * 1024 * 1024;
1515

1616
pub 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 release = b.option(bool, "release", "Build in release mode") orelse false;
18+
const only_c = b.option(bool, "only-c", "Translate the Zig compiler to C code, with only the C backend enabled") orelse false;
19+
const target = t: {
20+
var default_target: std.zig.CrossTarget = .{};
21+
if (only_c) {
22+
default_target.ofmt = .c;
23+
}
24+
break :t b.standardTargetOptions(.{ .default_target = default_target });
25+
};
26+
const mode: std.builtin.Mode = if (release) switch (target.getCpuArch()) {
27+
.wasm32 => .ReleaseSmall,
28+
else => .ReleaseFast,
29+
} else .Debug;
30+
2131
const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");
2232
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;
2333

34+
const test_step = b.step("test", "Run all the tests");
35+
2436
const docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
2537
docgen_exe.single_threaded = single_threaded;
2638

@@ -48,8 +60,6 @@ pub fn build(b: *Builder) !void {
4860

4961
const fmt_build_zig = b.addFmt(&[_][]const u8{"build.zig"});
5062

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-
5363
const skip_debug = b.option(bool, "skip-debug", "Main test suite skips debug builds") orelse false;
5464
const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
5565
const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;
@@ -69,9 +79,8 @@ pub fn build(b: *Builder) !void {
6979

7080
const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;
7181

72-
const have_stage1 = b.option(bool, "enable-stage1", "Include the stage1 compiler behind a feature flag") orelse false;
7382
const static_llvm = b.option(bool, "static-llvm", "Disable integration with system-installed LLVM, Clang, LLD, and libc++") orelse false;
74-
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (have_stage1 or static_llvm);
83+
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse static_llvm;
7584
const llvm_has_m68k = b.option(
7685
bool,
7786
"llvm-has-m68k",
@@ -132,38 +141,26 @@ pub fn build(b: *Builder) !void {
132141
const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false;
133142
const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse (enable_llvm or only_c);
134143
const sanitize_thread = b.option(bool, "sanitize-thread", "Enable thread-sanitization") orelse false;
135-
const strip = b.option(bool, "strip", "Omit debug information") orelse false;
136-
const use_zig0 = b.option(bool, "zig0", "Bootstrap using zig0") orelse false;
144+
const strip = b.option(bool, "strip", "Omit debug information");
137145
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;
138146

139147
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: {
140-
if (strip) break :blk @as(u32, 0);
148+
if (strip == true) break :blk @as(u32, 0);
141149
if (mode != .Debug) break :blk 0;
142150
break :blk 4;
143151
};
144152

145-
if (only_c) {
146-
target.ofmt = .c;
147-
}
148-
149-
const main_file: ?[]const u8 = mf: {
150-
if (!have_stage1) break :mf "src/main.zig";
151-
if (use_zig0) break :mf null;
152-
break :mf "src/stage1.zig";
153-
};
154-
155-
const exe = b.addExecutable("zig", main_file);
156-
157-
const compile_step = b.step("compile", "Build the self-hosted compiler");
158-
compile_step.dependOn(&exe.step);
159-
160-
exe.stack_size = stack_size;
153+
const exe = addCompilerStep(b);
161154
exe.strip = strip;
162155
exe.sanitize_thread = sanitize_thread;
163156
exe.build_id = b.option(bool, "build-id", "Include a build id note") orelse false;
164157
exe.install();
165158
exe.setBuildMode(mode);
166159
exe.setTarget(target);
160+
161+
const compile_step = b.step("compile", "Build the self-hosted compiler");
162+
compile_step.dependOn(&exe.step);
163+
167164
if (!skip_stage2_tests) {
168165
test_step.dependOn(&exe.step);
169166
}
@@ -199,7 +196,7 @@ pub fn build(b: *Builder) !void {
199196
const enable_link_snapshots = b.option(bool, "link-snapshot", "Whether to enable linker state snapshots") orelse false;
200197

201198
const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git.");
202-
const version = if (opt_version_string) |version| version else v: {
199+
const version_slice = if (opt_version_string) |version| version else v: {
203200
if (!std.process.can_spawn) {
204201
std.debug.print("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n", .{});
205202
std.process.exit(1);
@@ -251,7 +248,8 @@ pub fn build(b: *Builder) !void {
251248
},
252249
}
253250
};
254-
exe_options.addOption([:0]const u8, "version", try b.allocator.dupeZ(u8, version));
251+
const version = try b.allocator.dupeZ(u8, version_slice);
252+
exe_options.addOption([:0]const u8, "version", version);
255253

256254
if (enable_llvm) {
257255
const cmake_cfg = if (static_llvm) null else blk: {
@@ -264,92 +262,6 @@ pub fn build(b: *Builder) !void {
264262
}
265263
};
266264

267-
if (have_stage1) {
268-
const softfloat = b.addStaticLibrary("softfloat", null);
269-
softfloat.setBuildMode(.ReleaseFast);
270-
softfloat.setTarget(target);
271-
softfloat.addIncludePath("deps/SoftFloat-3e-prebuilt");
272-
softfloat.addIncludePath("deps/SoftFloat-3e/source/8086");
273-
softfloat.addIncludePath("deps/SoftFloat-3e/source/include");
274-
softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" });
275-
softfloat.single_threaded = single_threaded;
276-
277-
const zig0 = b.addExecutable("zig0", null);
278-
zig0.addCSourceFiles(&.{"src/stage1/zig0.cpp"}, &exe_cflags);
279-
zig0.addIncludePath("zig-cache/tmp"); // for config.h
280-
zig0.defineCMacro("ZIG_VERSION_MAJOR", b.fmt("{d}", .{zig_version.major}));
281-
zig0.defineCMacro("ZIG_VERSION_MINOR", b.fmt("{d}", .{zig_version.minor}));
282-
zig0.defineCMacro("ZIG_VERSION_PATCH", b.fmt("{d}", .{zig_version.patch}));
283-
zig0.defineCMacro("ZIG_VERSION_STRING", b.fmt("\"{s}\"", .{version}));
284-
285-
for ([_]*std.build.LibExeObjStep{ zig0, exe, test_cases }) |artifact| {
286-
artifact.addIncludePath("src");
287-
artifact.addIncludePath("deps/SoftFloat-3e/source/include");
288-
artifact.addIncludePath("deps/SoftFloat-3e-prebuilt");
289-
290-
artifact.defineCMacro("ZIG_LINK_MODE", "Static");
291-
292-
artifact.addCSourceFiles(&stage1_sources, &exe_cflags);
293-
artifact.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" });
294-
295-
artifact.linkLibrary(softfloat);
296-
artifact.linkLibCpp();
297-
}
298-
299-
try addStaticLlvmOptionsToExe(zig0);
300-
301-
const zig1_obj_ext = target.getObjectFormat().fileExt(target.getCpuArch());
302-
const zig1_obj_path = b.pathJoin(&.{ "zig-cache", "tmp", b.fmt("zig1{s}", .{zig1_obj_ext}) });
303-
const zig1_compiler_rt_path = b.pathJoin(&.{ b.pathFromRoot("lib"), "std", "special", "compiler_rt.zig" });
304-
305-
const zig1_obj = zig0.run();
306-
zig1_obj.addArgs(&.{
307-
"src/stage1.zig",
308-
"-target",
309-
try target.zigTriple(b.allocator),
310-
"-mcpu=baseline",
311-
"--name",
312-
"zig1",
313-
"--zig-lib-dir",
314-
b.pathFromRoot("lib"),
315-
b.fmt("-femit-bin={s}", .{b.pathFromRoot(zig1_obj_path)}),
316-
"-fcompiler-rt",
317-
"-lc",
318-
});
319-
{
320-
zig1_obj.addArgs(&.{ "--pkg-begin", "build_options" });
321-
zig1_obj.addFileSourceArg(exe_options.getSource());
322-
zig1_obj.addArgs(&.{ "--pkg-end", "--pkg-begin", "compiler_rt", zig1_compiler_rt_path, "--pkg-end" });
323-
}
324-
switch (mode) {
325-
.Debug => {},
326-
.ReleaseFast => {
327-
zig1_obj.addArg("-OReleaseFast");
328-
zig1_obj.addArg("-fstrip");
329-
},
330-
.ReleaseSafe => {
331-
zig1_obj.addArg("-OReleaseSafe");
332-
zig1_obj.addArg("-fstrip");
333-
},
334-
.ReleaseSmall => {
335-
zig1_obj.addArg("-OReleaseSmall");
336-
zig1_obj.addArg("-fstrip");
337-
},
338-
}
339-
if (single_threaded orelse false) {
340-
zig1_obj.addArg("-fsingle-threaded");
341-
}
342-
343-
if (use_zig0) {
344-
exe.step.dependOn(&zig1_obj.step);
345-
exe.addObjectFile(zig1_obj_path);
346-
}
347-
348-
// This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case
349-
// of being built by cmake. But when built by zig it's gonna get a compiler_rt so that
350-
// is pointless.
351-
exe.addPackagePath("compiler_rt", "src/empty.zig");
352-
}
353265
if (cmake_cfg) |cfg| {
354266
// Inside this code path, we have to coordinate with system packaged LLVM, Clang, and LLD.
355267
// That means we also have to rely on stage1 compiled c++ files. We parse config.h to find
@@ -379,7 +291,6 @@ pub fn build(b: *Builder) !void {
379291
exe_options.addOption(bool, "enable_tracy_callstack", tracy_callstack);
380292
exe_options.addOption(bool, "enable_tracy_allocation", tracy_allocation);
381293
exe_options.addOption(bool, "value_tracing", value_tracing);
382-
exe_options.addOption(bool, "have_stage1", have_stage1);
383294
if (tracy) |tracy_path| {
384295
const client_cpp = fs.path.join(
385296
b.allocator,
@@ -414,7 +325,6 @@ pub fn build(b: *Builder) !void {
414325
test_cases_options.addOption(bool, "enable_link_snapshots", enable_link_snapshots);
415326
test_cases_options.addOption(bool, "skip_non_native", skip_non_native);
416327
test_cases_options.addOption(bool, "skip_stage1", skip_stage1);
417-
test_cases_options.addOption(bool, "have_stage1", have_stage1);
418328
test_cases_options.addOption(bool, "have_llvm", enable_llvm);
419329
test_cases_options.addOption(bool, "llvm_has_m68k", llvm_has_m68k);
420330
test_cases_options.addOption(bool, "llvm_has_csky", llvm_has_csky);
@@ -429,7 +339,7 @@ pub fn build(b: *Builder) !void {
429339
test_cases_options.addOption(u32, "mem_leak_frames", mem_leak_frames * 2);
430340
test_cases_options.addOption(bool, "value_tracing", value_tracing);
431341
test_cases_options.addOption(?[]const u8, "glibc_runtimes_dir", b.glibc_runtimes_dir);
432-
test_cases_options.addOption([:0]const u8, "version", try b.allocator.dupeZ(u8, version));
342+
test_cases_options.addOption([:0]const u8, "version", version);
433343
test_cases_options.addOption(std.SemanticVersion, "semver", semver);
434344
test_cases_options.addOption(?[]const u8, "test_filter", test_filter);
435345

@@ -547,6 +457,61 @@ pub fn build(b: *Builder) !void {
547457
skip_stage1,
548458
true, // TODO get these all passing
549459
));
460+
461+
try addWasiUpdateStep(b, version);
462+
}
463+
464+
fn addWasiUpdateStep(b: *Builder, version: [:0]const u8) !void {
465+
const semver = try std.SemanticVersion.parse(version);
466+
467+
var target: std.zig.CrossTarget = .{
468+
.cpu_arch = .wasm32,
469+
.os_tag = .wasi,
470+
};
471+
target.cpu_features_add.addFeature(@enumToInt(std.Target.wasm.Feature.bulk_memory));
472+
473+
const exe = addCompilerStep(b);
474+
exe.setBuildMode(.ReleaseSmall);
475+
exe.setTarget(target);
476+
477+
const exe_options = b.addOptions();
478+
exe.addOptions("build_options", exe_options);
479+
480+
exe_options.addOption(u32, "mem_leak_frames", 0);
481+
exe_options.addOption(bool, "have_llvm", false);
482+
exe_options.addOption(bool, "force_gpa", false);
483+
exe_options.addOption(bool, "only_c", true);
484+
exe_options.addOption([:0]const u8, "version", version);
485+
exe_options.addOption(std.SemanticVersion, "semver", semver);
486+
exe_options.addOption(bool, "enable_logging", false);
487+
exe_options.addOption(bool, "enable_link_snapshots", false);
488+
exe_options.addOption(bool, "enable_tracy", false);
489+
exe_options.addOption(bool, "enable_tracy_callstack", false);
490+
exe_options.addOption(bool, "enable_tracy_allocation", false);
491+
exe_options.addOption(bool, "value_tracing", false);
492+
493+
const run_opt = b.addSystemCommand(&.{ "wasm-opt", "-Oz", "--enable-bulk-memory" });
494+
run_opt.addArtifactArg(exe);
495+
run_opt.addArg("-o");
496+
run_opt.addFileSourceArg(.{ .path = "stage1/zig1.wasm" });
497+
498+
const run_zstd = b.addSystemCommand(&.{ "zstd", "-19", "-f" });
499+
run_zstd.step.dependOn(&run_opt.step);
500+
run_zstd.addFileSourceArg(.{ .path = "stage1/zig1.wasm" });
501+
run_zstd.addArg("-o");
502+
run_zstd.addFileSourceArg(.{ .path = "stage1/zig1.wasm.zst" });
503+
504+
const cleanup = b.addRemoveDirTree("stage1/zig1.wasm");
505+
cleanup.step.dependOn(&run_zstd.step);
506+
507+
const update_zig1_step = b.step("update-zig1", "Update stage1/zig1.wasm.zst");
508+
update_zig1_step.dependOn(&cleanup.step);
509+
}
510+
511+
fn addCompilerStep(b: *Builder) *std.build.LibExeObjStep {
512+
const exe = b.addExecutable("zig", "src/main.zig");
513+
exe.stack_size = stack_size;
514+
return exe;
550515
}
551516

552517
const exe_cflags = [_][]const u8{
@@ -1010,31 +975,6 @@ const softfloat_sources = [_][]const u8{
1010975
"deps/SoftFloat-3e/source/ui64_to_extF80M.c",
1011976
};
1012977

1013-
const stage1_sources = [_][]const u8{
1014-
"src/stage1/analyze.cpp",
1015-
"src/stage1/astgen.cpp",
1016-
"src/stage1/bigfloat.cpp",
1017-
"src/stage1/bigint.cpp",
1018-
"src/stage1/buffer.cpp",
1019-
"src/stage1/codegen.cpp",
1020-
"src/stage1/errmsg.cpp",
1021-
"src/stage1/error.cpp",
1022-
"src/stage1/heap.cpp",
1023-
"src/stage1/ir.cpp",
1024-
"src/stage1/ir_print.cpp",
1025-
"src/stage1/mem.cpp",
1026-
"src/stage1/os.cpp",
1027-
"src/stage1/parser.cpp",
1028-
"src/stage1/range_set.cpp",
1029-
"src/stage1/stage1.cpp",
1030-
"src/stage1/target.cpp",
1031-
"src/stage1/tokenizer.cpp",
1032-
"src/stage1/util.cpp",
1033-
"src/stage1/softfloat_ext.cpp",
1034-
};
1035-
const optimized_c_sources = [_][]const u8{
1036-
"src/stage1/parse_f128.c",
1037-
};
1038978
const zig_cpp_sources = [_][]const u8{
1039979
// These are planned to stay even when we are self-hosted.
1040980
"src/zig_llvm.cpp",

ci/aarch64-linux-debug.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -e
88
ARCH="$(uname -m)"
99
TARGET="$ARCH-linux-musl"
1010
MCPU="baseline"
11-
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.448+e6e459e9e"
11+
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.534+b0b1cc356"
1212
PREFIX="$HOME/deps/$CACHE_BASENAME"
1313
ZIG="$PREFIX/bin/zig"
1414

ci/aarch64-linux-release.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -e
88
ARCH="$(uname -m)"
99
TARGET="$ARCH-linux-musl"
1010
MCPU="baseline"
11-
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.448+e6e459e9e"
11+
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.534+b0b1cc356"
1212
PREFIX="$HOME/deps/$CACHE_BASENAME"
1313
ZIG="$PREFIX/bin/zig"
1414

ci/aarch64-macos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set -e
99
ZIGDIR="$(pwd)"
1010
TARGET="$ARCH-macos-none"
1111
MCPU="baseline"
12-
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.448+e6e459e9e"
12+
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.534+b0b1cc356-1"
1313
PREFIX="$HOME/$CACHE_BASENAME"
1414
ZIG="$PREFIX/bin/zig"
1515

ci/x86_64-linux-release.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ stage3-release/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-l
7575
stage3-release/bin/zig build \
7676
--prefix stage4-release \
7777
-Denable-llvm \
78-
-Denable-stage1 \
7978
-Dno-lib \
8079
-Drelease \
8180
-Dstrip \

ci/x86_64-macos.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set -e
99
ZIGDIR="$(pwd)"
1010
TARGET="$ARCH-macos-none"
1111
MCPU="baseline"
12-
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.448+e6e459e9e"
12+
CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.11.0-dev.534+b0b1cc356"
1313
PREFIX="$HOME/$CACHE_BASENAME"
1414
JOBS="-j3"
1515

ci/x86_64-windows.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ Invoke-WebRequest -Uri "$ZIG_LLVM_CLANG_LLD_URL" -OutFile "$ZIG_LLVM_CLANG_LLD_N
88

99
Write-Output "Extracting..."
1010

11-
Add-Type -AssemblyName System.IO.Compression.FileSystem ;
11+
Add-Type -AssemblyName System.IO.Compression.FileSystem ;
1212
[System.IO.Compression.ZipFile]::ExtractToDirectory("$PWD/$ZIG_LLVM_CLANG_LLD_NAME.zip", "$PWD")
1313

1414
Set-Variable -Name ZIGLIBDIR -Value "$(Get-Location)\lib"
1515
Set-Variable -Name ZIGINSTALLDIR -Value "$(Get-Location)\stage3-release"
1616
Set-Variable -Name ZIGPREFIXPATH -Value "$(Get-Location)\$ZIG_LLVM_CLANG_LLD_NAME"
17-
17+
1818
function CheckLastExitCode {
1919
if (!$?) {
2020
exit 1
@@ -37,7 +37,6 @@ Write-Output "Building Zig..."
3737
--prefix "$ZIGINSTALLDIR" `
3838
--search-prefix "$ZIGPREFIXPATH" `
3939
--zig-lib-dir "$ZIGLIBDIR" `
40-
-Denable-stage1 `
4140
-Dstatic-llvm `
4241
-Drelease `
4342
-Duse-zig-libcxx `

0 commit comments

Comments
 (0)