authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-26 12:42:07-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-26 12:42:07-07:00
log26f2f9bf1c774caf246317b3fc032449e982a150
tree43bc4d4b19cea9a245d87f105841d075ab75f5ab
parent9b83112a1f6af758a1a995ea8838a2319d2fbc1e

stage2: implement -fno-emit-bin

we are now passing the cli tests

12 files changed, 137 insertions(+), 102 deletions(-)

BRANCH_TODO-1
......@@ -1,4 +1,3 @@
1 * support -fno-emit-bin for godbolt
21 * restore the legacy -femit-h feature using the stage1 backend
32 * figure out why test-translate-c is failing
43 * tests passing with -Dskip-non-native
src/Compilation.zig+73-56
......@@ -500,8 +500,6 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
500500 break :pic explicit;
501501 } else must_pic;
502502
503 const emit_bin = options.emit_bin orelse fatal("-fno-emit-bin not supported yet", .{}); // TODO
504
505503 // Make a decision on whether to use Clang for translate-c and compiling C files.
506504 const use_clang = if (options.use_clang) |explicit| explicit else blk: {
507505 if (build_options.have_llvm) {
......@@ -667,13 +665,29 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
667665 } else null;
668666 errdefer if (module) |zm| zm.deinit();
669667
668 const error_return_tracing = !options.strip and switch (options.optimize_mode) {
669 .Debug, .ReleaseSafe => true,
670 .ReleaseFast, .ReleaseSmall => false,
671 };
672
670673 // For resource management purposes.
671674 var owned_link_dir: ?std.fs.Dir = null;
672675 errdefer if (owned_link_dir) |*dir| dir.close();
673676
674 const bin_directory = emit_bin.directory orelse blk: {
675 if (module) |zm| break :blk zm.zig_cache_artifact_directory;
676
677 const bin_file_emit: ?link.Emit = blk: {
678 const emit_bin = options.emit_bin orelse break :blk null;
679 if (emit_bin.directory) |directory| {
680 break :blk link.Emit{
681 .directory = directory,
682 .sub_path = emit_bin.basename,
683 };
684 }
685 if (module) |zm| {
686 break :blk link.Emit{
687 .directory = zm.zig_cache_artifact_directory,
688 .sub_path = emit_bin.basename,
689 };
690 }
677691 // We could use the cache hash as is no problem, however, we increase
678692 // the likelihood of cache hits by adding the first C source file
679693 // path name (not contents) to the hash. This way if the user is compiling
......@@ -694,17 +708,14 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
694708 .handle = artifact_dir,
695709 .path = try options.local_cache_directory.join(arena, &[_][]const u8{artifact_sub_dir}),
696710 };
697 break :blk link_artifact_directory;
698 };
699
700 const error_return_tracing = !options.strip and switch (options.optimize_mode) {
701 .Debug, .ReleaseSafe => true,
702 .ReleaseFast, .ReleaseSmall => false,
711 break :blk link.Emit{
712 .directory = link_artifact_directory,
713 .sub_path = emit_bin.basename,
714 };
703715 };
704716
705717 const bin_file = try link.File.openPath(gpa, .{
706 .directory = bin_directory,
707 .sub_path = emit_bin.basename,
718 .emit = bin_file_emit,
708719 .root_name = root_name,
709720 .module = module,
710721 .target = options.target,
......@@ -815,43 +826,46 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
815826 comp.c_object_table.putAssumeCapacityNoClobber(c_object, {});
816827 }
817828
818 // If we need to build glibc for the target, add work items for it.
819 // We go through the work queue so that building can be done in parallel.
820 if (comp.wantBuildGLibCFromSource()) {
821 try comp.addBuildingGLibCJobs();
822 }
823 if (comp.wantBuildMuslFromSource()) {
824 try comp.work_queue.write(&[_]Job{
825 .{ .musl_crt_file = .crti_o },
826 .{ .musl_crt_file = .crtn_o },
827 .{ .musl_crt_file = .crt1_o },
828 .{ .musl_crt_file = .scrt1_o },
829 .{ .musl_crt_file = .libc_a },
830 });
831 }
832 if (comp.wantBuildMinGWW64FromSource()) {
833 @panic("TODO");
834 }
835 if (comp.wantBuildLibUnwindFromSource()) {
836 try comp.work_queue.writeItem(.{ .libunwind = {} });
837 }
838 if (build_options.have_llvm and comp.bin_file.options.output_mode != .Obj and
839 comp.bin_file.options.link_libcpp)
840 {
841 try comp.work_queue.writeItem(.libcxx);
842 try comp.work_queue.writeItem(.libcxxabi);
829 if (comp.bin_file.options.emit != null) {
830 // If we need to build glibc for the target, add work items for it.
831 // We go through the work queue so that building can be done in parallel.
832 if (comp.wantBuildGLibCFromSource()) {
833 try comp.addBuildingGLibCJobs();
834 }
835 if (comp.wantBuildMuslFromSource()) {
836 try comp.work_queue.write(&[_]Job{
837 .{ .musl_crt_file = .crti_o },
838 .{ .musl_crt_file = .crtn_o },
839 .{ .musl_crt_file = .crt1_o },
840 .{ .musl_crt_file = .scrt1_o },
841 .{ .musl_crt_file = .libc_a },
842 });
843 }
844 if (comp.wantBuildMinGWW64FromSource()) {
845 @panic("TODO");
846 }
847 if (comp.wantBuildLibUnwindFromSource()) {
848 try comp.work_queue.writeItem(.{ .libunwind = {} });
849 }
850 if (build_options.have_llvm and comp.bin_file.options.output_mode != .Obj and
851 comp.bin_file.options.link_libcpp)
852 {
853 try comp.work_queue.writeItem(.libcxx);
854 try comp.work_queue.writeItem(.libcxxabi);
855 }
856 if (is_exe_or_dyn_lib and !comp.bin_file.options.is_compiler_rt_or_libc and
857 build_options.is_stage1)
858 {
859 try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });
860 if (!comp.bin_file.options.link_libc) {
861 try comp.work_queue.writeItem(.{ .zig_libc = {} });
862 }
863 }
843864 }
865
844866 if (build_options.is_stage1 and comp.bin_file.options.use_llvm) {
845867 try comp.work_queue.writeItem(.{ .stage1_module = {} });
846868 }
847 if (is_exe_or_dyn_lib and !comp.bin_file.options.is_compiler_rt_or_libc and
848 build_options.is_stage1)
849 {
850 try comp.work_queue.writeItem(.{ .libcompiler_rt = {} });
851 if (!comp.bin_file.options.link_libc) {
852 try comp.work_queue.writeItem(.{ .zig_libc = {} });
853 }
854 }
855869
856870 return comp;
857871}
......@@ -2408,8 +2422,8 @@ fn buildStaticLibFromZig(comp: *Compilation, src_basename: []const u8, out: *?CR
24082422
24092423 assert(out.* == null);
24102424 out.* = Compilation.CRTFile{
2411 .full_object_path = try sub_compilation.bin_file.options.directory.join(comp.gpa, &[_][]const u8{
2412 sub_compilation.bin_file.options.sub_path,
2425 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{
2426 sub_compilation.bin_file.options.emit.?.sub_path,
24132427 }),
24142428 .lock = sub_compilation.bin_file.toOwnedLock(),
24152429 };
......@@ -2452,6 +2466,7 @@ fn updateStage1Module(comp: *Compilation) !void {
24522466 man.hash.add(comp.bin_file.options.dll_export_fns);
24532467 man.hash.add(comp.bin_file.options.function_sections);
24542468 man.hash.add(comp.is_test);
2469 man.hash.add(comp.bin_file.options.emit != null);
24552470 man.hash.add(comp.emit_h != null);
24562471 man.hash.add(comp.emit_asm != null);
24572472 man.hash.add(comp.emit_llvm_ir != null);
......@@ -2517,12 +2532,14 @@ fn updateStage1Module(comp: *Compilation) !void {
25172532 comp.is_test,
25182533 ) orelse return error.OutOfMemory;
25192534
2520 const bin_basename = try std.zig.binNameAlloc(arena, .{
2521 .root_name = comp.bin_file.options.root_name,
2522 .target = target,
2523 .output_mode = .Obj,
2524 });
2525 const emit_bin_path = try directory.join(arena, &[_][]const u8{bin_basename});
2535 const emit_bin_path = if (comp.bin_file.options.emit != null) blk: {
2536 const bin_basename = try std.zig.binNameAlloc(arena, .{
2537 .root_name = comp.bin_file.options.root_name,
2538 .target = target,
2539 .output_mode = .Obj,
2540 });
2541 break :blk try directory.join(arena, &[_][]const u8{bin_basename});
2542 } else "";
25262543 const emit_h_path = try stage1LocPath(arena, comp.emit_h, directory);
25272544 const emit_asm_path = try stage1LocPath(arena, comp.emit_asm, directory);
25282545 const emit_llvm_ir_path = try stage1LocPath(arena, comp.emit_llvm_ir, directory);
......@@ -2697,8 +2714,8 @@ pub fn build_crt_file(
26972714 try comp.crt_files.ensureCapacity(comp.gpa, comp.crt_files.count() + 1);
26982715
26992716 comp.crt_files.putAssumeCapacityNoClobber(basename, .{
2700 .full_object_path = try sub_compilation.bin_file.options.directory.join(comp.gpa, &[_][]const u8{
2701 sub_compilation.bin_file.options.sub_path,
2717 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(comp.gpa, &[_][]const u8{
2718 sub_compilation.bin_file.options.emit.?.sub_path,
27022719 }),
27032720 .lock = sub_compilation.bin_file.toOwnedLock(),
27042721 });
src/libcxx.zig+8-2
......@@ -189,7 +189,10 @@ pub fn buildLibCXX(comp: *Compilation) !void {
189189
190190 assert(comp.libcxx_static_lib == null);
191191 comp.libcxx_static_lib = Compilation.CRTFile{
192 .full_object_path = try sub_compilation.bin_file.options.directory.join(comp.gpa, &[_][]const u8{basename}),
192 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(
193 comp.gpa,
194 &[_][]const u8{basename},
195 ),
193196 .lock = sub_compilation.bin_file.toOwnedLock(),
194197 };
195198}
......@@ -303,7 +306,10 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
303306
304307 assert(comp.libcxxabi_static_lib == null);
305308 comp.libcxxabi_static_lib = Compilation.CRTFile{
306 .full_object_path = try sub_compilation.bin_file.options.directory.join(comp.gpa, &[_][]const u8{basename}),
309 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(
310 comp.gpa,
311 &[_][]const u8{basename},
312 ),
307313 .lock = sub_compilation.bin_file.toOwnedLock(),
308314 };
309315}
src/libunwind.zig+4-1
......@@ -126,7 +126,10 @@ pub fn buildStaticLib(comp: *Compilation) !void {
126126
127127 assert(comp.libunwind_static_lib == null);
128128 comp.libunwind_static_lib = Compilation.CRTFile{
129 .full_object_path = try sub_compilation.bin_file.options.directory.join(comp.gpa, &[_][]const u8{basename}),
129 .full_object_path = try sub_compilation.bin_file.options.emit.?.directory.join(
130 comp.gpa,
131 &[_][]const u8{basename},
132 ),
130133 .lock = sub_compilation.bin_file.toOwnedLock(),
131134 };
132135}
src/link.zig+18-13
......@@ -16,11 +16,17 @@ const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
1616
1717pub const producer_string = if (std.builtin.is_test) "zig test" else "zig " ++ build_options.version;
1818
19pub const Options = struct {
19pub const Emit = struct {
2020 /// Where the output will go.
2121 directory: Compilation.Directory,
2222 /// Path to the output file, relative to `directory`.
2323 sub_path: []const u8,
24};
25
26pub const Options = struct {
27 /// This is `null` when -fno-emit-bin is used. When `openPath` or `flush` is called,
28 /// it will have already been null-checked.
29 emit: ?Emit,
2430 target: std.Target,
2531 output_mode: std.builtin.OutputMode,
2632 link_mode: std.builtin.LinkMode,
......@@ -141,7 +147,7 @@ pub const File = struct {
141147 /// and does not cause Illegal Behavior. This operation is not atomic.
142148 pub fn openPath(allocator: *Allocator, options: Options) !*File {
143149 const use_stage1 = build_options.is_stage1 and options.use_llvm;
144 if (use_stage1) {
150 if (use_stage1 or options.emit == null) {
145151 return switch (options.object_format) {
146152 .coff, .pe => &(try Coff.createEmpty(allocator, options)).base,
147153 .elf => &(try Elf.createEmpty(allocator, options)).base,
......@@ -152,6 +158,7 @@ pub const File = struct {
152158 .raw => return error.RawObjectFormatUnimplemented,
153159 };
154160 }
161 const emit = options.emit.?;
155162 const use_lld = build_options.have_llvm and options.use_lld; // comptime known false when !have_llvm
156163 const sub_path = if (use_lld) blk: {
157164 if (options.module == null) {
......@@ -167,8 +174,8 @@ pub const File = struct {
167174 };
168175 }
169176 // Open a temporary object file, not the final output file because we want to link with LLD.
170 break :blk try std.fmt.allocPrint(allocator, "{}{}", .{ options.sub_path, options.target.oFileExt() });
171 } else options.sub_path;
177 break :blk try std.fmt.allocPrint(allocator, "{}{}", .{ emit.sub_path, options.target.oFileExt() });
178 } else emit.sub_path;
172179 errdefer if (use_lld) allocator.free(sub_path);
173180
174181 const file: *File = switch (options.object_format) {
......@@ -199,7 +206,8 @@ pub const File = struct {
199206 switch (base.tag) {
200207 .coff, .elf, .macho => {
201208 if (base.file != null) return;
202 base.file = try base.options.directory.handle.createFile(base.options.sub_path, .{
209 const emit = base.options.emit orelse return;
210 base.file = try emit.directory.handle.createFile(emit.sub_path, .{
203211 .truncate = false,
204212 .read = true,
205213 .mode = determineMode(base.options),
......@@ -305,14 +313,14 @@ pub const File = struct {
305313 /// Commit pending changes and write headers. Takes into account final output mode
306314 /// and `use_lld`, not only `effectiveOutputMode`.
307315 pub fn flush(base: *File, comp: *Compilation) !void {
316 const emit = base.options.emit orelse return; // -fno-emit-bin
317
308318 if (comp.clang_preprocessor_mode == .yes) {
309319 // TODO: avoid extra link step when it's just 1 object file (the `zig cc -c` case)
310320 // Until then, we do `lld -r -o output.o input.o` even though the output is the same
311321 // as the input. For the preprocessing case (`zig cc -E -o foo`) we copy the file
312322 // to the final location.
313 const full_out_path = try base.options.directory.join(comp.gpa, &[_][]const u8{
314 base.options.sub_path,
315 });
323 const full_out_path = try emit.directory.join(comp.gpa, &[_][]const u8{emit.sub_path});
316324 defer comp.gpa.free(full_out_path);
317325 assert(comp.c_object_table.count() == 1);
318326 const the_entry = comp.c_object_table.items()[0];
......@@ -402,7 +410,7 @@ pub const File = struct {
402410 defer arena_allocator.deinit();
403411 const arena = &arena_allocator.allocator;
404412
405 const directory = base.options.directory; // Just an alias to make it shorter to type.
413 const directory = base.options.emit.?.directory; // Just an alias to make it shorter to type.
406414
407415 // If there is no Zig code to compile, then we should skip flushing the output file because it
408416 // will not be part of the linker line anyway.
......@@ -471,10 +479,7 @@ pub const File = struct {
471479 object_files.appendAssumeCapacity(try arena.dupeZ(u8, p));
472480 }
473481
474 const full_out_path = if (directory.path) |dir_path|
475 try std.fs.path.join(arena, &[_][]const u8{ dir_path, base.options.sub_path })
476 else
477 base.options.sub_path;
482 const full_out_path = try directory.join(arena, &[_][]const u8{base.options.emit.?.sub_path});
478483 const full_out_path_z = try arena.dupeZ(u8, full_out_path);
479484
480485 if (base.options.verbose_link) {
src/link/C.zig+1-1
......@@ -30,7 +30,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
3030 if (options.use_llvm) return error.LLVMHasNoCBackend;
3131 if (options.use_lld) return error.LLDHasNoCBackend;
3232
33 const file = try options.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true, .mode = link.determineMode(options) });
33 const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true, .mode = link.determineMode(options) });
3434 errdefer file.close();
3535
3636 var c_file = try allocator.create(C);
src/link/Coff.zig+1-1
......@@ -120,7 +120,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
120120 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForCoff; // TODO
121121 if (options.use_lld) return error.LLD_LinkingIsTODO_ForCoff; // TODO
122122
123 const file = try options.directory.handle.createFile(sub_path, .{
123 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
124124 .truncate = false,
125125 .read = true,
126126 .mode = link.determineMode(options),
src/link/Elf.zig+3-3
......@@ -229,7 +229,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
229229
230230 if (options.use_llvm) return error.LLVMBackendUnimplementedForELF; // TODO
231231
232 const file = try options.directory.handle.createFile(sub_path, .{
232 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
233233 .truncate = false,
234234 .read = true,
235235 .mode = link.determineMode(options),
......@@ -1218,7 +1218,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
12181218 defer arena_allocator.deinit();
12191219 const arena = &arena_allocator.allocator;
12201220
1221 const directory = self.base.options.directory; // Just an alias to make it shorter to type.
1221 const directory = self.base.options.emit.?.directory; // Just an alias to make it shorter to type.
12221222
12231223 // If there is no Zig code to compile, then we should skip flushing the output file because it
12241224 // will not be part of the linker line anyway.
......@@ -1401,7 +1401,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation) !void {
14011401 try argv.append("-pie");
14021402 }
14031403
1404 const full_out_path = try directory.join(arena, &[_][]const u8{self.base.options.sub_path});
1404 const full_out_path = try directory.join(arena, &[_][]const u8{self.base.options.emit.?.sub_path});
14051405 try argv.append("-o");
14061406 try argv.append(full_out_path);
14071407
src/link/MachO.zig+1-1
......@@ -142,7 +142,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
142142 if (options.use_llvm) return error.LLVM_BackendIsTODO_ForMachO; // TODO
143143 if (options.use_lld) return error.LLD_LinkingIsTODO_ForMachO; // TODO
144144
145 const file = try options.directory.handle.createFile(sub_path, .{
145 const file = try options.emit.?.directory.handle.createFile(sub_path, .{
146146 .truncate = false,
147147 .read = true,
148148 .mode = link.determineMode(options),
src/link/Wasm.zig+1-1
......@@ -59,7 +59,7 @@ pub fn openPath(allocator: *Allocator, sub_path: []const u8, options: link.Optio
5959 if (options.use_lld) return error.LLD_LinkingIsTODO_ForWasm; // TODO
6060
6161 // TODO: read the file and keep vaild parts instead of truncating
62 const file = try options.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true });
62 const file = try options.emit.?.directory.handle.createFile(sub_path, .{ .truncate = true, .read = true });
6363 errdefer file.close();
6464
6565 const wasm = try createEmpty(allocator, options);
src/main.zig+11-6
......@@ -1307,7 +1307,9 @@ fn buildOutputType(
13071307 };
13081308 }
13091309 if (fs.path.dirname(full_path)) |dirname| {
1310 const handle = try fs.cwd().openDir(dirname, .{});
1310 const handle = fs.cwd().openDir(dirname, .{}) catch |err| {
1311 fatal("unable to open output directory '{}': {}", .{ dirname, @errorName(err) });
1312 };
13111313 cleanup_emit_bin_dir = handle;
13121314 break :b Compilation.EmitLoc{
13131315 .basename = basename,
......@@ -1545,7 +1547,7 @@ fn buildOutputType(
15451547 switch (emit_bin) {
15461548 .no => break :blk .none,
15471549 .yes_default_path => break :blk .{
1548 .print = comp.bin_file.options.directory.path orelse ".",
1550 .print = comp.bin_file.options.emit.?.directory.path orelse ".",
15491551 },
15501552 .yes => |full_path| break :blk .{ .update = full_path },
15511553 }
......@@ -1560,7 +1562,7 @@ fn buildOutputType(
15601562 switch (arg_mode) {
15611563 .run, .zig_test => run: {
15621564 const exe_loc = emit_bin_loc orelse break :run;
1563 const exe_directory = exe_loc.directory orelse comp.bin_file.options.directory;
1565 const exe_directory = exe_loc.directory orelse comp.bin_file.options.emit.?.directory;
15641566 const exe_path = try fs.path.join(arena, &[_][]const u8{
15651567 exe_directory.path orelse ".", exe_loc.basename,
15661568 });
......@@ -1676,8 +1678,8 @@ fn updateModule(gpa: *Allocator, comp: *Compilation, zir_out_path: ?[]const u8,
16761678 } else switch (hook) {
16771679 .none => {},
16781680 .print => |bin_path| try io.getStdOut().writer().print("{s}\n", .{bin_path}),
1679 .update => |full_path| _ = try comp.bin_file.options.directory.handle.updateFile(
1680 comp.bin_file.options.sub_path,
1681 .update => |full_path| _ = try comp.bin_file.options.emit.?.directory.handle.updateFile(
1682 comp.bin_file.options.emit.?.sub_path,
16811683 fs.cwd(),
16821684 full_path,
16831685 .{},
......@@ -2106,7 +2108,10 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
21062108
21072109 try updateModule(gpa, comp, null, .none);
21082110
2109 child_argv.items[argv_index_exe] = try comp.bin_file.options.directory.join(arena, &[_][]const u8{exe_basename});
2111 child_argv.items[argv_index_exe] = try comp.bin_file.options.emit.?.directory.join(
2112 arena,
2113 &[_][]const u8{exe_basename},
2114 );
21102115
21112116 break :lock_and_argv .{
21122117 .child_argv = child_argv.items,
test/cli.zig+16-16
......@@ -58,7 +58,7 @@ fn printCmd(cwd: []const u8, argv: []const []const u8) void {
5858 std.debug.warn("\n", .{});
5959}
6060
61fn exec(cwd: []const u8, argv: []const []const u8) !ChildProcess.ExecResult {
61fn exec(cwd: []const u8, expect_0: bool, argv: []const []const u8) !ChildProcess.ExecResult {
6262 const max_output_size = 100 * 1024;
6363 const result = ChildProcess.exec(.{
6464 .allocator = a,
......@@ -72,7 +72,7 @@ fn exec(cwd: []const u8, argv: []const []const u8) !ChildProcess.ExecResult {
7272 };
7373 switch (result.term) {
7474 .Exited => |code| {
75 if (code != 0) {
75 if ((code != 0) == expect_0) {
7676 std.debug.warn("The following command exited with error code {}:\n", .{code});
7777 printCmd(cwd, argv);
7878 std.debug.warn("stderr:\n{}\n", .{result.stderr});
......@@ -90,14 +90,14 @@ fn exec(cwd: []const u8, argv: []const []const u8) !ChildProcess.ExecResult {
9090}
9191
9292fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void {
93 _ = try exec(dir_path, &[_][]const u8{ zig_exe, "init-lib" });
94 const test_result = try exec(dir_path, &[_][]const u8{ zig_exe, "build", "test" });
93 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-lib" });
94 const test_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "test" });
9595 testing.expect(std.mem.endsWith(u8, test_result.stderr, "All 1 tests passed.\n"));
9696}
9797
9898fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void {
99 _ = try exec(dir_path, &[_][]const u8{ zig_exe, "init-exe" });
100 const run_result = try exec(dir_path, &[_][]const u8{ zig_exe, "build", "run" });
99 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
100 const run_result = try exec(dir_path, true, &[_][]const u8{ zig_exe, "build", "run" });
101101 testing.expect(std.mem.eql(u8, run_result.stderr, "info: All your codebase are belong to us.\n"));
102102}
103103
......@@ -131,7 +131,7 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
131131 const emit_asm_arg = try std.fmt.allocPrint(a, "-femit-asm={s}", .{example_s_path});
132132 try args.append(emit_asm_arg);
133133
134 _ = try exec(dir_path, args.items);
134 _ = try exec(dir_path, true, args.items);
135135
136136 const out_asm = try std.fs.cwd().readFileAlloc(a, example_s_path, std.math.maxInt(usize));
137137 testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null);
......@@ -140,23 +140,23 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void {
140140}
141141
142142fn testMissingOutputPath(zig_exe: []const u8, dir_path: []const u8) !void {
143 _ = try exec(dir_path, &[_][]const u8{ zig_exe, "init-exe" });
144 const output_path = try fs.path.join(a, &[_][]const u8{ "does", "not", "exist" });
143 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
144 const output_path = try fs.path.join(a, &[_][]const u8{ "does", "not", "exist", "foo.exe" });
145 const output_arg = try std.fmt.allocPrint(a, "-femit-bin={s}", .{output_path});
145146 const source_path = try fs.path.join(a, &[_][]const u8{ "src", "main.zig" });
146 _ = try exec(dir_path, &[_][]const u8{
147 zig_exe, "build-exe", source_path, "--output-dir", output_path,
148 });
147 const result = try exec(dir_path, false, &[_][]const u8{ zig_exe, "build-exe", source_path, output_arg });
148 testing.expect(std.mem.eql(u8, result.stderr, "error: unable to open output directory 'does/not/exist': FileNotFound\n"));
149149}
150150
151151fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
152 _ = try exec(dir_path, &[_][]const u8{ zig_exe, "init-exe" });
152 _ = try exec(dir_path, true, &[_][]const u8{ zig_exe, "init-exe" });
153153
154154 const unformatted_code = " // no reason for indent";
155155
156156 const fmt1_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt1.zig" });
157157 try fs.cwd().writeFile(fmt1_zig_path, unformatted_code);
158158
159 const run_result1 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
159 const run_result1 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", fmt1_zig_path });
160160 // stderr should be file path + \n
161161 testing.expect(std.mem.startsWith(u8, run_result1.stderr, fmt1_zig_path));
162162 testing.expect(run_result1.stderr.len == fmt1_zig_path.len + 1 and run_result1.stderr[run_result1.stderr.len - 1] == '\n');
......@@ -164,12 +164,12 @@ fn testZigFmt(zig_exe: []const u8, dir_path: []const u8) !void {
164164 const fmt2_zig_path = try fs.path.join(a, &[_][]const u8{ dir_path, "fmt2.zig" });
165165 try fs.cwd().writeFile(fmt2_zig_path, unformatted_code);
166166
167 const run_result2 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", dir_path });
167 const run_result2 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
168168 // running it on the dir, only the new file should be changed
169169 testing.expect(std.mem.startsWith(u8, run_result2.stderr, fmt2_zig_path));
170170 testing.expect(run_result2.stderr.len == fmt2_zig_path.len + 1 and run_result2.stderr[run_result2.stderr.len - 1] == '\n');
171171
172 const run_result3 = try exec(dir_path, &[_][]const u8{ zig_exe, "fmt", dir_path });
172 const run_result3 = try exec(dir_path, true, &[_][]const u8{ zig_exe, "fmt", dir_path });
173173 // both files have been formatted, nothing should change now
174174 testing.expect(run_result3.stderr.len == 0);
175175}