Skip to content

Commit ead50ea

Browse files
committed
stage2: implement zig run and zig test
1 parent 528832b commit ead50ea

File tree

3 files changed

+664
-525
lines changed

3 files changed

+664
-525
lines changed

BRANCH_TODO

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
* build & link against libcxx and libcxxabi
2-
* `zig test`
32
* `zig build`
3+
* repair @cImport
4+
* make sure zig cc works
5+
- using it as a preprocessor (-E)
6+
- try building some software
47
* `-ftime-report`
58
* -fstack-report print stack size diagnostics\n"
69
* -fdump-analysis write analysis.json file with type information\n"
@@ -11,17 +14,14 @@
1114
* -femit-llvm-ir produce a .ll file with LLVM IR\n"
1215
* -fno-emit-llvm-ir (default) do not produce a .ll file with LLVM IR\n"
1316
* --cache-dir [path] override the local cache directory\n"
14-
* make sure zig cc works
15-
- using it as a preprocessor (-E)
16-
- try building some software
1717
* implement proper parsing of LLD stderr/stdout and exposing compile errors
1818
* implement proper parsing of clang stderr/stdout and exposing compile errors
1919
* support rpaths in ELF linker code
20-
* repair @cImport
2120
* add CLI support for a way to pass extra flags to c source files
2221
* musl
2322
* mingw-w64
2423
* use global zig-cache dir for crt files
24+
* use global zig-cache dir for `zig run` executables but not `zig test`
2525
* MachO LLD linking
2626
* COFF LLD linking
2727
* WASM LLD linking
@@ -30,9 +30,9 @@
3030
* audit the CLI options for stage2
3131
* `zig init-lib`
3232
* `zig init-exe`
33-
* `zig run`
3433
* restore error messages for stage2_add_link_lib
3534
* audit the base cache hash
35+
* On operating systems that support it, do an execve for `zig test` and `zig run` rather than child process.
3636

3737
* implement proper compile errors for failing to build glibc crt files and shared libs
3838
* implement -fno-emit-bin
@@ -66,3 +66,4 @@
6666
* some kind of "zig identifier escape" function rather than unconditionally using @"" syntax
6767
in builtin.zig
6868
* rename std.builtin.Mode to std.builtin.OptimizeMode
69+
* implement `zig run` and `zig test` when combined with `--watch`

src/Compilation.zig

+39-6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ owned_link_dir: ?std.fs.Dir,
9797
/// Don't use this for anything other than stage1 compatibility.
9898
color: @import("main.zig").Color = .Auto,
9999

100+
test_filter: ?[]const u8,
101+
test_name_prefix: ?[]const u8,
102+
test_evented_io: bool,
103+
100104
pub const InnerError = Module.InnerError;
101105

102106
pub const CRTFile = struct {
@@ -327,6 +331,9 @@ pub const InitOptions = struct {
327331
machine_code_model: std.builtin.CodeModel = .default,
328332
/// This is for stage1 and should be deleted upon completion of self-hosting.
329333
color: @import("main.zig").Color = .Auto,
334+
test_filter: ?[]const u8 = null,
335+
test_name_prefix: ?[]const u8 = null,
336+
test_evented_io: bool = false,
330337
};
331338

332339
pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
@@ -554,6 +561,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
554561
hash.add(single_threaded);
555562
hash.add(options.target.os.getVersionRange());
556563
hash.add(dll_export_fns);
564+
hash.add(options.is_test);
557565

558566
const digest = hash.final();
559567
const artifact_sub_dir = try std.fs.path.join(arena, &[_][]const u8{ "o", &digest });
@@ -728,6 +736,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
728736
.is_test = options.is_test,
729737
.color = options.color,
730738
.time_report = options.time_report,
739+
.test_filter = options.test_filter,
740+
.test_name_prefix = options.test_name_prefix,
741+
.test_evented_io = options.test_evented_io,
731742
};
732743
break :comp comp;
733744
};
@@ -1996,6 +2007,25 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
19962007
comp.bin_file.options.strip,
19972008
@tagName(comp.bin_file.options.machine_code_model),
19982009
});
2010+
2011+
if (comp.is_test) {
2012+
try buffer.appendSlice(
2013+
\\pub var test_functions: []TestFn = undefined; // overwritten later
2014+
\\
2015+
);
2016+
if (comp.test_evented_io) {
2017+
try buffer.appendSlice(
2018+
\\pub const test_io_mode = .evented;
2019+
\\
2020+
);
2021+
} else {
2022+
try buffer.appendSlice(
2023+
\\pub const test_io_mode = .blocking;
2024+
\\
2025+
);
2026+
}
2027+
}
2028+
19992029
return buffer.toOwnedSlice();
20002030
}
20012031

@@ -2129,6 +2159,7 @@ fn updateStage1Module(comp: *Compilation) !void {
21292159
ch.hash.add(target.os.getVersionRange());
21302160
ch.hash.add(comp.bin_file.options.dll_export_fns);
21312161
ch.hash.add(comp.bin_file.options.function_sections);
2162+
ch.hash.add(comp.is_test);
21322163

21332164
if (try ch.hit()) {
21342165
const digest = ch.final();
@@ -2155,7 +2186,7 @@ fn updateStage1Module(comp: *Compilation) !void {
21552186
.llvm_cpu_features = comp.bin_file.options.llvm_cpu_features.?,
21562187
};
21572188
var progress: std.Progress = .{};
2158-
var main_progress_node = try progress.start("", 100);
2189+
var main_progress_node = try progress.start("", null);
21592190
defer main_progress_node.end();
21602191
if (comp.color == .Off) progress.terminal = null;
21612192

@@ -2184,17 +2215,19 @@ fn updateStage1Module(comp: *Compilation) !void {
21842215
.parent = null,
21852216
};
21862217
const output_dir = comp.bin_file.options.directory.path orelse ".";
2218+
const test_filter = comp.test_filter orelse ""[0..0];
2219+
const test_name_prefix = comp.test_name_prefix orelse ""[0..0];
21872220
stage1_module.* = .{
21882221
.root_name_ptr = comp.bin_file.options.root_name.ptr,
21892222
.root_name_len = comp.bin_file.options.root_name.len,
21902223
.output_dir_ptr = output_dir.ptr,
21912224
.output_dir_len = output_dir.len,
21922225
.builtin_zig_path_ptr = builtin_zig_path.ptr,
21932226
.builtin_zig_path_len = builtin_zig_path.len,
2194-
.test_filter_ptr = "",
2195-
.test_filter_len = 0,
2196-
.test_name_prefix_ptr = "",
2197-
.test_name_prefix_len = 0,
2227+
.test_filter_ptr = test_filter.ptr,
2228+
.test_filter_len = test_filter.len,
2229+
.test_name_prefix_ptr = test_name_prefix.ptr,
2230+
.test_name_prefix_len = test_name_prefix.len,
21982231
.userdata = @ptrToInt(comp),
21992232
.root_pkg = stage1_pkg,
22002233
.code_model = @enumToInt(comp.bin_file.options.machine_code_model),
@@ -2217,7 +2250,7 @@ fn updateStage1Module(comp: *Compilation) !void {
22172250
.emit_bin = true,
22182251
.emit_asm = false,
22192252
.emit_llvm_ir = false,
2220-
.test_is_evented = false,
2253+
.test_is_evented = comp.test_evented_io,
22212254
.verbose_tokenize = comp.verbose_tokenize,
22222255
.verbose_ast = comp.verbose_ast,
22232256
.verbose_ir = comp.verbose_ir,

0 commit comments

Comments
 (0)