Skip to content

Commit a9c9782

Browse files
committed
make self-hosted the default compiler
stage1 is available behind the -fstage1 flag. closes #89
1 parent f0a464e commit a9c9782

25 files changed

+176
-209
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if(NOT CMAKE_BUILD_TYPE)
1212
endif()
1313

1414
if(NOT CMAKE_INSTALL_PREFIX)
15-
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/stage1" CACHE STRING
15+
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/stage2" CACHE STRING
1616
"Directory to install zig to" FORCE)
1717
endif()
1818

build.zig

+6-10
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ pub fn build(b: *Builder) !void {
6464

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

67-
const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false;
67+
const have_stage1 = b.option(bool, "enable-stage1", "Include the stage1 compiler behind a feature flag") orelse false;
6868
const static_llvm = b.option(bool, "static-llvm", "Disable integration with system-installed LLVM, Clang, LLD, and libc++") orelse false;
69-
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (is_stage1 or static_llvm);
69+
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (have_stage1 or static_llvm);
7070
const llvm_has_m68k = b.option(
7171
bool,
7272
"llvm-has-m68k",
@@ -136,7 +136,7 @@ pub fn build(b: *Builder) !void {
136136
};
137137

138138
const main_file: ?[]const u8 = mf: {
139-
if (!is_stage1) break :mf "src/main.zig";
139+
if (!have_stage1) break :mf "src/main.zig";
140140
if (use_zig0) break :mf null;
141141
break :mf "src/stage1.zig";
142142
};
@@ -247,7 +247,7 @@ pub fn build(b: *Builder) !void {
247247
}
248248
};
249249

250-
if (is_stage1) {
250+
if (have_stage1) {
251251
const softfloat = b.addStaticLibrary("softfloat", null);
252252
softfloat.setBuildMode(.ReleaseFast);
253253
softfloat.setTarget(target);
@@ -359,7 +359,7 @@ pub fn build(b: *Builder) !void {
359359
exe_options.addOption(bool, "enable_tracy_callstack", tracy_callstack);
360360
exe_options.addOption(bool, "enable_tracy_allocation", tracy_allocation);
361361
exe_options.addOption(bool, "value_tracing", value_tracing);
362-
exe_options.addOption(bool, "is_stage1", is_stage1);
362+
exe_options.addOption(bool, "have_stage1", have_stage1);
363363
if (tracy) |tracy_path| {
364364
const client_cpp = fs.path.join(
365365
b.allocator,
@@ -394,7 +394,7 @@ pub fn build(b: *Builder) !void {
394394
test_cases_options.addOption(bool, "enable_link_snapshots", enable_link_snapshots);
395395
test_cases_options.addOption(bool, "skip_non_native", skip_non_native);
396396
test_cases_options.addOption(bool, "skip_stage1", skip_stage1);
397-
test_cases_options.addOption(bool, "is_stage1", is_stage1);
397+
test_cases_options.addOption(bool, "have_stage1", have_stage1);
398398
test_cases_options.addOption(bool, "have_llvm", enable_llvm);
399399
test_cases_options.addOption(bool, "llvm_has_m68k", llvm_has_m68k);
400400
test_cases_options.addOption(bool, "llvm_has_csky", llvm_has_csky);
@@ -455,7 +455,6 @@ pub fn build(b: *Builder) !void {
455455
skip_libc,
456456
skip_stage1,
457457
false,
458-
is_stage1,
459458
));
460459

461460
toolchain_step.dependOn(tests.addPkgTests(
@@ -470,7 +469,6 @@ pub fn build(b: *Builder) !void {
470469
true, // skip_libc
471470
skip_stage1,
472471
true, // TODO get these all passing
473-
is_stage1,
474472
));
475473

476474
toolchain_step.dependOn(tests.addPkgTests(
@@ -485,7 +483,6 @@ pub fn build(b: *Builder) !void {
485483
true, // skip_libc
486484
skip_stage1,
487485
true, // TODO get these all passing
488-
is_stage1,
489486
));
490487

491488
toolchain_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
@@ -525,7 +522,6 @@ pub fn build(b: *Builder) !void {
525522
skip_libc,
526523
skip_stage1,
527524
true, // TODO get these all passing
528-
is_stage1,
529525
);
530526

531527
const test_step = b.step("test", "Run all the tests");

ci/azure/build.zig

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ pub fn build(b: *Builder) !void {
3737
const docs_step = b.step("docs", "Build documentation");
3838
docs_step.dependOn(&docgen_cmd.step);
3939

40-
const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false;
40+
const have_stage1 = b.option(bool, "enable-stage1", "Include the stage1 compiler behind a feature flag") orelse false;
4141
const static_llvm = b.option(bool, "static-llvm", "Disable integration with system-installed LLVM, Clang, LLD, and libc++") orelse false;
42-
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (is_stage1 or static_llvm);
42+
const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (have_stage1 or static_llvm);
4343
const llvm_has_m68k = b.option(
4444
bool,
4545
"llvm-has-m68k",
@@ -101,7 +101,7 @@ pub fn build(b: *Builder) !void {
101101
break :blk 4;
102102
};
103103

104-
const main_file: ?[]const u8 = if (is_stage1) null else "src/main.zig";
104+
const main_file: ?[]const u8 = if (have_stage1) null else "src/main.zig";
105105

106106
const exe = b.addExecutable("zig", main_file);
107107
exe.strip = strip;
@@ -190,7 +190,7 @@ pub fn build(b: *Builder) !void {
190190
if (enable_llvm) {
191191
const cmake_cfg = if (static_llvm) null else findAndParseConfigH(b, config_h_path_option);
192192

193-
if (is_stage1) {
193+
if (have_stage1) {
194194
const softfloat = b.addStaticLibrary("softfloat", null);
195195
softfloat.setBuildMode(.ReleaseFast);
196196
softfloat.setTarget(target);
@@ -298,7 +298,7 @@ pub fn build(b: *Builder) !void {
298298
exe_options.addOption(bool, "enable_tracy_callstack", tracy_callstack);
299299
exe_options.addOption(bool, "enable_tracy_allocation", tracy_allocation);
300300
exe_options.addOption(bool, "value_tracing", value_tracing);
301-
exe_options.addOption(bool, "is_stage1", is_stage1);
301+
exe_options.addOption(bool, "have_stage1", have_stage1);
302302
if (tracy) |tracy_path| {
303303
const client_cpp = fs.path.join(
304304
b.allocator,

ci/azure/macos_script

+6-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ git fetch --tags
3434
mkdir build
3535
cd build
3636
cmake .. \
37-
-DCMAKE_INSTALL_PREFIX="$(pwd)/release" \
37+
-DCMAKE_INSTALL_PREFIX="$(pwd)/stage2" \
3838
-DCMAKE_PREFIX_PATH="$PREFIX" \
3939
-DCMAKE_BUILD_TYPE=Release \
4040
-DZIG_TARGET_TRIPLE="$TARGET" \
@@ -52,31 +52,26 @@ make $JOBS install
5252
# Here we rebuild zig but this time using the Zig binary we just now produced to
5353
# build zig1.o rather than relying on the one built with stage0. See
5454
# https://github.com/ziglang/zig/issues/6830 for more details.
55-
cmake .. -DZIG_EXECUTABLE="$(pwd)/release/bin/zig"
55+
cmake .. -DZIG_EXECUTABLE="$(pwd)/stage2/bin/zig"
5656
make $JOBS install
5757

58-
# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt.
59-
release/bin/zig build -p stage2 -Denable-llvm
58+
stage2/bin/zig build -p release -Denable-llvm -Denable-stage1
6059

61-
stage2/bin/zig build test-behavior
62-
63-
# TODO: upgrade these to test stage2 instead of stage1
64-
# TODO: upgrade these to test stage3 instead of stage2
65-
release/bin/zig build test-behavior -Denable-macos-sdk -Domit-stage2
6660
release/bin/zig build test-compiler-rt -Denable-macos-sdk
61+
release/bin/zig build test-behavior -Denable-macos-sdk
6762
release/bin/zig build test-std -Denable-macos-sdk
6863
release/bin/zig build test-universal-libc -Denable-macos-sdk
6964
release/bin/zig build test-compare-output -Denable-macos-sdk
7065
release/bin/zig build test-standalone -Denable-macos-sdk
71-
release/bin/zig build test-stack-traces -Denable-macos-sdk
66+
release/bin/zig build test-stack-traces -Denable-macos-sdk -fstage1
7267
release/bin/zig build test-cli -Denable-macos-sdk
7368
release/bin/zig build test-asm-link -Denable-macos-sdk
7469
release/bin/zig build test-translate-c -Denable-macos-sdk
7570
release/bin/zig build test-run-translated-c -Denable-macos-sdk
7671
release/bin/zig build docs -Denable-macos-sdk
7772
release/bin/zig build test-fmt -Denable-macos-sdk
7873
release/bin/zig build test-cases -Denable-macos-sdk -Dsingle-threaded
79-
release/bin/zig build test-link -Denable-macos-sdk -Domit-stage2
74+
release/bin/zig build test-link -Denable-macos-sdk
8075

8176
if [ "${BUILD_REASON}" != "PullRequest" ]; then
8277
mv ../LICENSE release/

ci/azure/pipelines.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ jobs:
6161
& "${ZIGPREFIXPATH}/bin/zig.exe" build `
6262
--prefix "$ZIGINSTALLDIR" `
6363
--search-prefix "$ZIGPREFIXPATH" `
64-
-Dstage1 `
65-
<# stage2 is omitted until we resolve https://github.com/ziglang/zig/issues/6485 #> `
66-
-Domit-stage2 `
64+
-Denable-stage1 `
6765
-Dstatic-llvm `
6866
-Drelease `
6967
-Dstrip `

ci/srht/freebsd_script

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ cmake .. \
3838
-GNinja
3939
samu install
4040

41-
# TODO ld.lld: error: undefined symbol: main
42-
# >>> referenced by crt1_c.c:75 (/usr/src/lib/csu/amd64/crt1_c.c:75)
43-
# >>> /usr/lib/crt1.o:(_start)
44-
#release/bin/zig test ../test/behavior.zig -fno-stage1 -fLLVM -I ../test
41+
# Here we rebuild zig but this time using the Zig binary we just now produced to
42+
# build zig1.o rather than relying on the one built with stage0. This makes it
43+
# a stage3 build rather than a stage2 build.
44+
cmake .. -DZIG_EXECUTABLE="$PREFIX/bin/zig"
45+
samu install
4546

4647
# Here we skip some tests to save time.
4748
release/bin/zig build test -Dskip-stage1 -Dskip-non-native

ci/zinc/linux_test.sh

+23-25
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,39 @@ unset CXX
3333

3434
ninja install
3535

36-
STAGE1_ZIG="$DEBUG_STAGING/bin/zig"
37-
3836
# Here we rebuild zig but this time using the Zig binary we just now produced to
3937
# build zig1.o rather than relying on the one built with stage0. See
4038
# https://github.com/ziglang/zig/issues/6830 for more details.
41-
cmake .. -DZIG_EXECUTABLE="$STAGE1_ZIG"
39+
cmake .. -DZIG_EXECUTABLE="$DEBUG_STAGING/bin/zig"
4240
ninja install
4341

4442
cd $WORKSPACE
4543

44+
"$DEBUG_STAGING/bin/zig" build -p stage3 -Denable-stage1 -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
45+
46+
# simultaneously test building self-hosted without LLVM and with 32-bit arm
47+
stage3/bin/zig build -Dtarget=arm-linux-musleabihf
48+
4649
echo "Looking for non-conforming code formatting..."
4750
echo "Formatting errors can be fixed by running 'zig fmt' on the files printed here."
48-
$STAGE1_ZIG fmt --check . --exclude test/cases/
49-
50-
$STAGE1_ZIG build -p stage2 -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
51-
stage2/bin/zig build -p stage3 -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
52-
stage3/bin/zig build # test building self-hosted without LLVM
53-
stage3/bin/zig build -Dtarget=arm-linux-musleabihf # test building self-hosted for 32-bit arm
54-
55-
stage3/bin/zig build test-compiler-rt -fqemu -fwasmtime -Denable-llvm
56-
stage3/bin/zig build test-behavior -fqemu -fwasmtime -Denable-llvm
57-
stage3/bin/zig build test-std -fqemu -fwasmtime -Denable-llvm
58-
stage3/bin/zig build test-universal-libc -fqemu -fwasmtime -Denable-llvm
59-
stage3/bin/zig build test-compare-output -fqemu -fwasmtime -Denable-llvm
60-
stage3/bin/zig build test-asm-link -fqemu -fwasmtime -Denable-llvm
61-
stage3/bin/zig build test-fmt -fqemu -fwasmtime -Denable-llvm
62-
stage3/bin/zig build test-translate-c -fqemu -fwasmtime -Denable-llvm
63-
stage3/bin/zig build test-run-translated-c -fqemu -fwasmtime -Denable-llvm
64-
stage3/bin/zig build test-standalone -fqemu -fwasmtime -Denable-llvm
65-
stage3/bin/zig build test-cli -fqemu -fwasmtime -Denable-llvm
51+
stage3/bin/zig fmt --check . --exclude test/cases/
52+
53+
stage3/bin/zig build test-compiler-rt -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
54+
stage3/bin/zig build test-behavior -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
55+
stage3/bin/zig build test-std -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
56+
stage3/bin/zig build test-universal-libc -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
57+
stage3/bin/zig build test-compare-output -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
58+
stage3/bin/zig build test-asm-link -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
59+
stage3/bin/zig build test-fmt -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
60+
stage3/bin/zig build test-translate-c -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
61+
stage3/bin/zig build test-run-translated-c -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
62+
stage3/bin/zig build test-standalone -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
63+
stage3/bin/zig build test-cli -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
6664
stage3/bin/zig build test-cases -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
67-
stage3/bin/zig build test-link -fqemu -fwasmtime -Denable-llvm
65+
stage3/bin/zig build test-link -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
66+
stage3/bin/zig build docs -fqemu -fwasmtime -Dstatic-llvm -Dtarget=native-native-musl --search-prefix "$DEPS_LOCAL"
6867

69-
$STAGE1_ZIG build test-stack-traces -fqemu -fwasmtime
70-
$STAGE1_ZIG build docs -fqemu -fwasmtime
68+
stage3/bin/zig build test-stack-traces -fqemu -fwasmtime -fstage1
7169

7270
# Produce the experimental std lib documentation.
7371
mkdir -p "$RELEASE_STAGING/docs/std"
@@ -87,7 +85,7 @@ stage3/bin/zig build \
8785
-Drelease \
8886
-Dstrip \
8987
-Dtarget="$TARGET" \
90-
-Dstage1
88+
-Denable-stage1
9189

9290
# Explicit exit helps show last command duration.
9391
exit

doc/docgen.zig

+31
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ const Code = struct {
285285
link_objects: []const []const u8,
286286
target_str: ?[]const u8,
287287
link_libc: bool,
288+
backend_stage1: bool,
288289
link_mode: ?std.builtin.LinkMode,
289290
disable_cache: bool,
290291
verbose_cimport: bool,
@@ -554,6 +555,7 @@ fn genToc(allocator: Allocator, tokenizer: *Tokenizer) !Toc {
554555
var link_mode: ?std.builtin.LinkMode = null;
555556
var disable_cache = false;
556557
var verbose_cimport = false;
558+
var backend_stage1 = false;
557559

558560
const source_token = while (true) {
559561
const content_tok = try eatToken(tokenizer, Token.Id.Content);
@@ -586,6 +588,8 @@ fn genToc(allocator: Allocator, tokenizer: *Tokenizer) !Toc {
586588
link_libc = true;
587589
} else if (mem.eql(u8, end_tag_name, "link_mode_dynamic")) {
588590
link_mode = .Dynamic;
591+
} else if (mem.eql(u8, end_tag_name, "backend_stage1")) {
592+
backend_stage1 = true;
589593
} else if (mem.eql(u8, end_tag_name, "code_end")) {
590594
_ = try eatToken(tokenizer, Token.Id.BracketClose);
591595
break content_tok;
@@ -609,6 +613,7 @@ fn genToc(allocator: Allocator, tokenizer: *Tokenizer) !Toc {
609613
.link_objects = link_objects.toOwnedSlice(),
610614
.target_str = target_str,
611615
.link_libc = link_libc,
616+
.backend_stage1 = backend_stage1,
612617
.link_mode = link_mode,
613618
.disable_cache = disable_cache,
614619
.verbose_cimport = verbose_cimport,
@@ -1187,6 +1192,9 @@ fn printShell(out: anytype, shell_content: []const u8) !void {
11871192
try out.writeAll("</samp></pre></figure>");
11881193
}
11891194

1195+
// Override this to skip to later tests
1196+
const debug_start_line = 0;
1197+
11901198
fn genHtml(
11911199
allocator: Allocator,
11921200
tokenizer: *Tokenizer,
@@ -1266,6 +1274,13 @@ fn genHtml(
12661274
continue;
12671275
}
12681276

1277+
if (debug_start_line > 0) {
1278+
const loc = tokenizer.getTokenLocation(code.source_token);
1279+
if (debug_start_line > loc.line) {
1280+
continue;
1281+
}
1282+
}
1283+
12691284
const raw_source = tokenizer.buffer[code.source_token.start..code.source_token.end];
12701285
const trimmed_raw_source = mem.trim(u8, raw_source, " \n");
12711286
const tmp_source_file_name = try fs.path.join(
@@ -1311,6 +1326,10 @@ fn genHtml(
13111326
try build_args.append("-lc");
13121327
try shell_out.print("-lc ", .{});
13131328
}
1329+
if (code.backend_stage1) {
1330+
try build_args.append("-fstage1");
1331+
try shell_out.print("-fstage1", .{});
1332+
}
13141333
const target = try std.zig.CrossTarget.parse(.{
13151334
.arch_os_abi = code.target_str orelse "native",
13161335
});
@@ -1443,6 +1462,10 @@ fn genHtml(
14431462
try test_args.append("-lc");
14441463
try shell_out.print("-lc ", .{});
14451464
}
1465+
if (code.backend_stage1) {
1466+
try test_args.append("-fstage1");
1467+
try shell_out.print("-fstage1", .{});
1468+
}
14461469
if (code.target_str) |triple| {
14471470
try test_args.appendSlice(&[_][]const u8{ "-target", triple });
14481471
try shell_out.print("-target {s} ", .{triple});
@@ -1490,6 +1513,14 @@ fn genHtml(
14901513
try shell_out.print("-O {s} ", .{@tagName(code.mode)});
14911514
},
14921515
}
1516+
if (code.link_libc) {
1517+
try test_args.append("-lc");
1518+
try shell_out.print("-lc ", .{});
1519+
}
1520+
if (code.backend_stage1) {
1521+
try test_args.append("-fstage1");
1522+
try shell_out.print("-fstage1", .{});
1523+
}
14931524
const result = try ChildProcess.exec(.{
14941525
.allocator = allocator,
14951526
.argv = test_args.items,

0 commit comments

Comments
 (0)