@@ -14,13 +14,25 @@ const zig_version = std.builtin.Version{ .major = 0, .minor = 11, .patch = 0 };
14
14
const stack_size = 32 * 1024 * 1024 ;
15
15
16
16
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
+
21
31
const single_threaded = b .option (bool , "single-threaded" , "Build artifacts that run in single threaded mode" );
22
32
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 ;
23
33
34
+ const test_step = b .step ("test" , "Run all the tests" );
35
+
24
36
const docgen_exe = b .addExecutable ("docgen" , "doc/docgen.zig" );
25
37
docgen_exe .single_threaded = single_threaded ;
26
38
@@ -48,8 +60,6 @@ pub fn build(b: *Builder) !void {
48
60
49
61
const fmt_build_zig = b .addFmt (&[_ ][]const u8 {"build.zig" });
50
62
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
63
const skip_debug = b .option (bool , "skip-debug" , "Main test suite skips debug builds" ) orelse false ;
54
64
const skip_release = b .option (bool , "skip-release" , "Main test suite skips release builds" ) orelse false ;
55
65
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 {
69
79
70
80
const only_install_lib_files = b .option (bool , "lib-files-only" , "Only install library files" ) orelse false ;
71
81
72
- const have_stage1 = b .option (bool , "enable-stage1" , "Include the stage1 compiler behind a feature flag" ) orelse false ;
73
82
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 ;
75
84
const llvm_has_m68k = b .option (
76
85
bool ,
77
86
"llvm-has-m68k" ,
@@ -132,38 +141,26 @@ pub fn build(b: *Builder) !void {
132
141
const force_gpa = b .option (bool , "force-gpa" , "Force the compiler to use GeneralPurposeAllocator" ) orelse false ;
133
142
const link_libc = b .option (bool , "force-link-libc" , "Force self-hosted compiler to link libc" ) orelse (enable_llvm or only_c );
134
143
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" );
137
145
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 ;
138
146
139
147
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 );
141
149
if (mode != .Debug ) break :blk 0 ;
142
150
break :blk 4 ;
143
151
};
144
152
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 );
161
154
exe .strip = strip ;
162
155
exe .sanitize_thread = sanitize_thread ;
163
156
exe .build_id = b .option (bool , "build-id" , "Include a build id note" ) orelse false ;
164
157
exe .install ();
165
158
exe .setBuildMode (mode );
166
159
exe .setTarget (target );
160
+
161
+ const compile_step = b .step ("compile" , "Build the self-hosted compiler" );
162
+ compile_step .dependOn (& exe .step );
163
+
167
164
if (! skip_stage2_tests ) {
168
165
test_step .dependOn (& exe .step );
169
166
}
@@ -199,7 +196,7 @@ pub fn build(b: *Builder) !void {
199
196
const enable_link_snapshots = b .option (bool , "link-snapshot" , "Whether to enable linker state snapshots" ) orelse false ;
200
197
201
198
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 : {
203
200
if (! std .process .can_spawn ) {
204
201
std .debug .print ("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n " , .{});
205
202
std .process .exit (1 );
@@ -251,7 +248,8 @@ pub fn build(b: *Builder) !void {
251
248
},
252
249
}
253
250
};
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 );
255
253
256
254
if (enable_llvm ) {
257
255
const cmake_cfg = if (static_llvm ) null else blk : {
@@ -264,92 +262,6 @@ pub fn build(b: *Builder) !void {
264
262
}
265
263
};
266
264
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
- }
353
265
if (cmake_cfg ) | cfg | {
354
266
// Inside this code path, we have to coordinate with system packaged LLVM, Clang, and LLD.
355
267
// 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 {
379
291
exe_options .addOption (bool , "enable_tracy_callstack" , tracy_callstack );
380
292
exe_options .addOption (bool , "enable_tracy_allocation" , tracy_allocation );
381
293
exe_options .addOption (bool , "value_tracing" , value_tracing );
382
- exe_options .addOption (bool , "have_stage1" , have_stage1 );
383
294
if (tracy ) | tracy_path | {
384
295
const client_cpp = fs .path .join (
385
296
b .allocator ,
@@ -414,7 +325,6 @@ pub fn build(b: *Builder) !void {
414
325
test_cases_options .addOption (bool , "enable_link_snapshots" , enable_link_snapshots );
415
326
test_cases_options .addOption (bool , "skip_non_native" , skip_non_native );
416
327
test_cases_options .addOption (bool , "skip_stage1" , skip_stage1 );
417
- test_cases_options .addOption (bool , "have_stage1" , have_stage1 );
418
328
test_cases_options .addOption (bool , "have_llvm" , enable_llvm );
419
329
test_cases_options .addOption (bool , "llvm_has_m68k" , llvm_has_m68k );
420
330
test_cases_options .addOption (bool , "llvm_has_csky" , llvm_has_csky );
@@ -429,7 +339,7 @@ pub fn build(b: *Builder) !void {
429
339
test_cases_options .addOption (u32 , "mem_leak_frames" , mem_leak_frames * 2 );
430
340
test_cases_options .addOption (bool , "value_tracing" , value_tracing );
431
341
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 );
433
343
test_cases_options .addOption (std .SemanticVersion , "semver" , semver );
434
344
test_cases_options .addOption (? []const u8 , "test_filter" , test_filter );
435
345
@@ -547,6 +457,61 @@ pub fn build(b: *Builder) !void {
547
457
skip_stage1 ,
548
458
true , // TODO get these all passing
549
459
));
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 ;
550
515
}
551
516
552
517
const exe_cflags = [_ ][]const u8 {
@@ -1010,31 +975,6 @@ const softfloat_sources = [_][]const u8{
1010
975
"deps/SoftFloat-3e/source/ui64_to_extF80M.c" ,
1011
976
};
1012
977
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
- };
1038
978
const zig_cpp_sources = [_ ][]const u8 {
1039
979
// These are planned to stay even when we are self-hosted.
1040
980
"src/zig_llvm.cpp" ,
0 commit comments