| 1 | b: *std.Build, |
| 2 | options: Options, |
| 3 | root_step: *std.Build.Step, |
| 4 | |
| 5 | pub const Options = struct { |
| 6 | enable_llvm: bool, |
| 7 | test_filters: []const []const u8, |
| 8 | test_target_filters: []const []const u8, |
| 9 | }; |
| 10 | |
| 11 | const TestCase = struct { |
| 12 | name: []const u8, |
| 13 | source: []const u8, |
| 14 | check: union(enum) { |
| 15 | matches: []const []const u8, |
| 16 | exact: []const u8, |
| 17 | }, |
| 18 | params: Params, |
| 19 | |
| 20 | pub const Params = struct { |
| 21 | code_model: std.builtin.CodeModel = .default, |
| 22 | dll_export_fns: ?bool = null, |
| 23 | dwarf_format: ?std.dwarf.Format = null, |
| 24 | error_tracing: ?bool = null, |
| 25 | no_builtin: ?bool = null, |
| 26 | omit_frame_pointer: ?bool = null, |
| 27 | // For most cases, we want to test the LLVM IR that we output; we don't want to be in the |
| 28 | // business of testing LLVM's optimization passes. `Debug` gets us the closest to that as it |
| 29 | // disables the vast majority of passes in LLVM. |
| 30 | optimize: std.builtin.Optimize = .debug, |
| 31 | pic: ?bool = null, |
| 32 | pie: ?bool = null, |
| 33 | red_zone: ?bool = null, |
| 34 | sanitize_thread: ?bool = null, |
| 35 | single_threaded: ?bool = null, |
| 36 | stack_check: ?bool = null, |
| 37 | stack_protector: ?bool = null, |
| 38 | strip: ?bool = null, |
| 39 | target: std.Target.Query = .{}, |
| 40 | unwind_tables: ?std.builtin.UnwindTables = null, |
| 41 | valgrind: ?bool = null, |
| 42 | }; |
| 43 | }; |
| 44 | |
| 45 | pub fn addMatches( |
| 46 | self: *LlvmIr, |
| 47 | name: []const u8, |
| 48 | source: []const u8, |
| 49 | matches: []const []const u8, |
| 50 | params: TestCase.Params, |
| 51 | ) void { |
| 52 | self.addCase(.{ |
| 53 | .name = name, |
| 54 | .source = source, |
| 55 | .check = .{ .matches = matches }, |
| 56 | .params = params, |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | pub fn addExact( |
| 61 | self: *LlvmIr, |
| 62 | name: []const u8, |
| 63 | source: []const u8, |
| 64 | expected: []const []const u8, |
| 65 | params: TestCase.Params, |
| 66 | ) void { |
| 67 | self.addCase(.{ |
| 68 | .name = name, |
| 69 | .source = source, |
| 70 | .check = .{ .exact = expected }, |
| 71 | .params = params, |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | pub fn addCase(self: *LlvmIr, case: TestCase) void { |
| 76 | const target = self.b.resolveTargetQuery(case.params.target); |
| 77 | if (self.options.test_target_filters.len > 0) { |
| 78 | const triple_txt = target.query.zigTriple(self.b.allocator) catch @panic("OOM"); |
| 79 | for (self.options.test_target_filters) |filter| { |
| 80 | if (std.mem.find(u8, triple_txt, filter) != null) break; |
| 81 | } else return; |
| 82 | } |
| 83 | |
| 84 | const name = std.fmt.allocPrint(self.b.allocator, "check llvm-ir {s}", .{case.name}) catch @panic("OOM"); |
| 85 | if (self.options.test_filters.len > 0) { |
| 86 | for (self.options.test_filters) |filter| { |
| 87 | if (std.mem.find(u8, name, filter) != null) break; |
| 88 | } else return; |
| 89 | } |
| 90 | |
| 91 | const obj = self.b.addObject(.{ |
| 92 | .name = "test", |
| 93 | .root_module = self.b.createModule(.{ |
| 94 | .root_source_file = self.b.addWriteFiles().add("test.zig", case.source), |
| 95 | |
| 96 | .code_model = case.params.code_model, |
| 97 | .error_tracing = case.params.error_tracing, |
| 98 | .omit_frame_pointer = case.params.omit_frame_pointer, |
| 99 | .optimize = case.params.optimize, |
| 100 | .pic = case.params.pic, |
| 101 | .sanitize_thread = case.params.sanitize_thread, |
| 102 | .single_threaded = case.params.single_threaded, |
| 103 | .strip = case.params.strip, |
| 104 | .target = target, |
| 105 | .unwind_tables = case.params.unwind_tables, |
| 106 | }), |
| 107 | .use_llvm = true, |
| 108 | }); |
| 109 | |
| 110 | obj.dll_export_fns = case.params.dll_export_fns; |
| 111 | obj.pie = case.params.pie; |
| 112 | |
| 113 | obj.root_module.dwarf_format = case.params.dwarf_format; |
| 114 | obj.root_module.no_builtin = case.params.no_builtin; |
| 115 | obj.root_module.red_zone = case.params.red_zone; |
| 116 | obj.root_module.stack_check = case.params.stack_check; |
| 117 | obj.root_module.stack_protector = case.params.stack_protector; |
| 118 | obj.root_module.valgrind = case.params.valgrind; |
| 119 | |
| 120 | // This is not very sophisticated at the moment. Eventually, we should move towards something |
| 121 | // like LLVM's `FileCheck` utility (https://llvm.org/docs/CommandGuide/FileCheck.html), though |
| 122 | // likely a more simplified version as we probably don't want a full-blown regex engine in the |
| 123 | // standard library... |
| 124 | const check = self.b.addCheckFile(obj.getEmittedLlvmIr(), switch (case.check) { |
| 125 | .matches => |m| .{ .expected_matches = m }, |
| 126 | .exact => |e| .{ .expected_exact = e }, |
| 127 | }); |
| 128 | check.setName(name); |
| 129 | check.max_bytes = 64 * 1024 * 1024; // allow fairly big LLVM IR files |
| 130 | |
| 131 | self.root_step.dependOn(&check.step); |
| 132 | } |
| 133 | |
| 134 | const LlvmIr = @This(); |
| 135 | const std = @import("std"); |