Skip to content

Commit

Permalink
stage2: implement zig run and zig test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Sep 22, 2020
1 parent 528832b commit ead50ea
Show file tree
Hide file tree
Showing 3 changed files with 664 additions and 525 deletions.
13 changes: 7 additions & 6 deletions BRANCH_TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
* build & link against libcxx and libcxxabi
* `zig test`
* `zig build`
* repair @cImport
* make sure zig cc works
- using it as a preprocessor (-E)
- try building some software
* `-ftime-report`
* -fstack-report print stack size diagnostics\n"
* -fdump-analysis write analysis.json file with type information\n"
Expand All @@ -11,17 +14,14 @@
* -femit-llvm-ir produce a .ll file with LLVM IR\n"
* -fno-emit-llvm-ir (default) do not produce a .ll file with LLVM IR\n"
* --cache-dir [path] override the local cache directory\n"
* make sure zig cc works
- using it as a preprocessor (-E)
- try building some software
* implement proper parsing of LLD stderr/stdout and exposing compile errors
* implement proper parsing of clang stderr/stdout and exposing compile errors
* support rpaths in ELF linker code
* repair @cImport
* add CLI support for a way to pass extra flags to c source files
* musl
* mingw-w64
* use global zig-cache dir for crt files
* use global zig-cache dir for `zig run` executables but not `zig test`
* MachO LLD linking
* COFF LLD linking
* WASM LLD linking
Expand All @@ -30,9 +30,9 @@
* audit the CLI options for stage2
* `zig init-lib`
* `zig init-exe`
* `zig run`
* restore error messages for stage2_add_link_lib
* audit the base cache hash
* On operating systems that support it, do an execve for `zig test` and `zig run` rather than child process.

* implement proper compile errors for failing to build glibc crt files and shared libs
* implement -fno-emit-bin
Expand Down Expand Up @@ -66,3 +66,4 @@
* some kind of "zig identifier escape" function rather than unconditionally using @"" syntax
in builtin.zig
* rename std.builtin.Mode to std.builtin.OptimizeMode
* implement `zig run` and `zig test` when combined with `--watch`
45 changes: 39 additions & 6 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ owned_link_dir: ?std.fs.Dir,
/// Don't use this for anything other than stage1 compatibility.
color: @import("main.zig").Color = .Auto,

test_filter: ?[]const u8,
test_name_prefix: ?[]const u8,
test_evented_io: bool,

pub const InnerError = Module.InnerError;

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

pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
Expand Down Expand Up @@ -554,6 +561,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
hash.add(single_threaded);
hash.add(options.target.os.getVersionRange());
hash.add(dll_export_fns);
hash.add(options.is_test);

const digest = hash.final();
const artifact_sub_dir = try std.fs.path.join(arena, &[_][]const u8{ "o", &digest });
Expand Down Expand Up @@ -728,6 +736,9 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
.is_test = options.is_test,
.color = options.color,
.time_report = options.time_report,
.test_filter = options.test_filter,
.test_name_prefix = options.test_name_prefix,
.test_evented_io = options.test_evented_io,
};
break :comp comp;
};
Expand Down Expand Up @@ -1996,6 +2007,25 @@ pub fn generateBuiltinZigSource(comp: *Compilation, allocator: *Allocator) ![]u8
comp.bin_file.options.strip,
@tagName(comp.bin_file.options.machine_code_model),
});

if (comp.is_test) {
try buffer.appendSlice(
\\pub var test_functions: []TestFn = undefined; // overwritten later
\\
);
if (comp.test_evented_io) {
try buffer.appendSlice(
\\pub const test_io_mode = .evented;
\\
);
} else {
try buffer.appendSlice(
\\pub const test_io_mode = .blocking;
\\
);
}
}

return buffer.toOwnedSlice();
}

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

if (try ch.hit()) {
const digest = ch.final();
Expand All @@ -2155,7 +2186,7 @@ fn updateStage1Module(comp: *Compilation) !void {
.llvm_cpu_features = comp.bin_file.options.llvm_cpu_features.?,
};
var progress: std.Progress = .{};
var main_progress_node = try progress.start("", 100);
var main_progress_node = try progress.start("", null);
defer main_progress_node.end();
if (comp.color == .Off) progress.terminal = null;

Expand Down Expand Up @@ -2184,17 +2215,19 @@ fn updateStage1Module(comp: *Compilation) !void {
.parent = null,
};
const output_dir = comp.bin_file.options.directory.path orelse ".";
const test_filter = comp.test_filter orelse ""[0..0];
const test_name_prefix = comp.test_name_prefix orelse ""[0..0];
stage1_module.* = .{
.root_name_ptr = comp.bin_file.options.root_name.ptr,
.root_name_len = comp.bin_file.options.root_name.len,
.output_dir_ptr = output_dir.ptr,
.output_dir_len = output_dir.len,
.builtin_zig_path_ptr = builtin_zig_path.ptr,
.builtin_zig_path_len = builtin_zig_path.len,
.test_filter_ptr = "",
.test_filter_len = 0,
.test_name_prefix_ptr = "",
.test_name_prefix_len = 0,
.test_filter_ptr = test_filter.ptr,
.test_filter_len = test_filter.len,
.test_name_prefix_ptr = test_name_prefix.ptr,
.test_name_prefix_len = test_name_prefix.len,
.userdata = @ptrToInt(comp),
.root_pkg = stage1_pkg,
.code_model = @enumToInt(comp.bin_file.options.machine_code_model),
Expand All @@ -2217,7 +2250,7 @@ fn updateStage1Module(comp: *Compilation) !void {
.emit_bin = true,
.emit_asm = false,
.emit_llvm_ir = false,
.test_is_evented = false,
.test_is_evented = comp.test_evented_io,
.verbose_tokenize = comp.verbose_tokenize,
.verbose_ast = comp.verbose_ast,
.verbose_ir = comp.verbose_ir,
Expand Down
Loading

0 comments on commit ead50ea

Please sign in to comment.