| ... | @@ -1,4 +1,5 @@ | ... | @@ -1,4 +1,5 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| | 2 | const builtin = @import("builtin"); |
| 2 | const os = std.os; | 3 | const os = std.os; |
| 3 | const assertOrPanic = std.debug.assertOrPanic; | 4 | const assertOrPanic = std.debug.assertOrPanic; |
| 4 | | 5 | |
| ... | @@ -28,8 +29,18 @@ pub fn main() !void { | ... | @@ -28,8 +29,18 @@ pub fn main() !void { |
| 28 | }); | 29 | }); |
| 29 | const zig_exe = try os.path.resolve(a, zig_exe_rel); | 30 | const zig_exe = try os.path.resolve(a, zig_exe_rel); |
| 30 | | 31 | |
| 31 | try testZigInitLib(zig_exe, cache_root); | 32 | const dir_path = try os.path.join(a, cache_root, "clitest"); |
| 32 | try testZigInitExe(zig_exe, cache_root); | 33 | const TestFn = fn ([]const u8, []const u8) error!void; |
| | 34 | const test_fns = []TestFn{ |
| | 35 | testZigInitLib, |
| | 36 | testZigInitExe, |
| | 37 | testGodboltApi, |
| | 38 | }; |
| | 39 | for (test_fns) |testFn| { |
| | 40 | try os.deleteTree(a, dir_path); |
| | 41 | try os.makeDir(dir_path); |
| | 42 | try testFn(zig_exe, dir_path); |
| | 43 | } |
| 33 | } | 44 | } |
| 34 | | 45 | |
| 35 | fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 { | 46 | fn unwrapArg(arg: UnwrapArgError![]u8) UnwrapArgError![]u8 { |
| ... | @@ -73,20 +84,44 @@ fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult { | ... | @@ -73,20 +84,44 @@ fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult { |
| 73 | return result; | 84 | return result; |
| 74 | } | 85 | } |
| 75 | | 86 | |
| 76 | fn testZigInitLib(zig_exe: []const u8, cache_root: []const u8) !void { | 87 | fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void { |
| 77 | const dir_path = try os.path.join(a, cache_root, "clitest"); | | |
| 78 | try os.deleteTree(a, dir_path); | | |
| 79 | try os.makeDir(dir_path); | | |
| 80 | _ = try exec(dir_path, [][]const u8{ zig_exe, "init-lib" }); | 88 | _ = try exec(dir_path, [][]const u8{ zig_exe, "init-lib" }); |
| 81 | const test_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "test" }); | 89 | const test_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "test" }); |
| 82 | assertOrPanic(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n")); | 90 | assertOrPanic(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n")); |
| 83 | } | 91 | } |
| 84 | | 92 | |
| 85 | fn testZigInitExe(zig_exe: []const u8, cache_root: []const u8) !void { | 93 | fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void { |
| 86 | const dir_path = try os.path.join(a, cache_root, "clitest"); | | |
| 87 | try os.deleteTree(a, dir_path); | | |
| 88 | try os.makeDir(dir_path); | | |
| 89 | _ = try exec(dir_path, [][]const u8{ zig_exe, "init-exe" }); | 94 | _ = try exec(dir_path, [][]const u8{ zig_exe, "init-exe" }); |
| 90 | const run_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "run" }); | 95 | const run_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "run" }); |
| 91 | assertOrPanic(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n")); | 96 | assertOrPanic(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n")); |
| 92 | } | 97 | } |
| | 98 | |
| | 99 | fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) !void { |
| | 100 | if (builtin.os != builtin.Os.linux or builtin.arch != builtin.Arch.x86_64) return; |
| | 101 | |
| | 102 | const example_zig_path = try os.path.join(a, dir_path, "example.zig"); |
| | 103 | const example_s_path = try os.path.join(a, dir_path, "example.s"); |
| | 104 | |
| | 105 | try std.io.writeFile(example_zig_path, |
| | 106 | \\// Type your code here, or load an example. |
| | 107 | \\export fn square(num: i32) i32 { |
| | 108 | \\ return num * num; |
| | 109 | \\} |
| | 110 | ); |
| | 111 | |
| | 112 | const args = [][]const u8{ |
| | 113 | zig_exe, "build-obj", |
| | 114 | "--cache-dir", dir_path, |
| | 115 | "--output", example_s_path, |
| | 116 | "--output-h", "/dev/null", |
| | 117 | "--emit", "asm", |
| | 118 | "-mllvm", "--x86-asm-syntax=intel", |
| | 119 | "--strip", "--release-fast", |
| | 120 | example_zig_path, |
| | 121 | }; |
| | 122 | _ = try exec(dir_path, args); |
| | 123 | |
| | 124 | const out_asm = try std.io.readFileAlloc(a, example_s_path); |
| | 125 | assertOrPanic(std.mem.indexOf(u8, out_asm, "square:") != null); |
| | 126 | assertOrPanic(std.mem.indexOf(u8, out_asm, "imul\tedi, edi") != null); |
| | 127 | } |