From c610cde1eb460ddf80910f34e19c8a68d1bc86a9 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sun, 23 Jul 2023 23:45:31 -0400 Subject: [PATCH] test: test for issues starting codegen on many targets Specifically this is to make sure llvm data layout generation doesn't regress. The no emit bin is to allow testing targets that can't currently be linked. The commented out targets are ones that fail in the linker anyway when no emit bin is passed. --- lib/std/Build/Step.zig | 4 +- lib/std/Build/Step/Compile.zig | 7 +- lib/std/Build/Step/TranslateC.zig | 4 +- src/Compilation.zig | 1 + test/cases.zig | 1 + test/llvm_targets.zig | 117 ++++++++++++++++++++++++++++++ test/src/Cases.zig | 16 ++++ 7 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 test/llvm_targets.zig diff --git a/lib/std/Build/Step.zig b/lib/std/Build/Step.zig index f21ef8bc8f7b98a2d141a35ae39c6afcdb7054c1..991283dbc526cdfae354afa53b7b9ae9e2c3647d 100644 --- a/lib/std/Build/Step.zig +++ b/lib/std/Build/Step.zig @@ -294,7 +294,7 @@ pub fn evalZigProcess( s: *Step, argv: []const []const u8, prog_node: *std.Progress.Node, -) ![]const u8 { +) !?[]const u8 { assert(argv.len != 0); const b = s.owner; const arena = b.allocator; @@ -423,6 +423,8 @@ pub fn evalZigProcess( }); } + if (s.cast(Compile)) |compile| if (compile.emit_bin == .no_emit) return result; + return result orelse return s.fail( "the following command failed to communicate the compilation result:\n{s}", .{try allocPrintCmd(arena, null, argv)}, diff --git a/lib/std/Build/Step/Compile.zig b/lib/std/Build/Step/Compile.zig index 7ff82e4b7b10e15623e469f26f42e961b6da1f2e..6dd0bd2d11dea3e9613d32093972949713019f2c 100644 --- a/lib/std/Build/Step/Compile.zig +++ b/lib/std/Build/Step/Compile.zig @@ -1997,7 +1997,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { try zig_args.append(resolved_args_file); } - const output_bin_path = step.evalZigProcess(zig_args.items, prog_node) catch |err| switch (err) { + const maybe_output_bin_path = step.evalZigProcess(zig_args.items, prog_node) catch |err| switch (err) { error.NeedCompileErrorCheck => { assert(self.expect_errors.len != 0); try checkCompileErrors(self); @@ -2005,10 +2005,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { }, else => |e| return e, }; - const output_dir = fs.path.dirname(output_bin_path).?; // Update generated files - { + if (maybe_output_bin_path) |output_bin_path| { + const output_dir = fs.path.dirname(output_bin_path).?; + self.output_dirname_source.path = output_dir; self.output_path_source.path = b.pathJoin( diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig index 60e35e940b113d17c0faae8d8646c3fa614b7622..cfbabc2fa95836764a50923d66f4d0c288306914 100644 --- a/lib/std/Build/Step/TranslateC.zig +++ b/lib/std/Build/Step/TranslateC.zig @@ -148,8 +148,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void { const output_path = try step.evalZigProcess(argv_list.items, prog_node); - self.out_basename = fs.path.basename(output_path); - const output_dir = fs.path.dirname(output_path).?; + self.out_basename = fs.path.basename(output_path.?); + const output_dir = fs.path.dirname(output_path.?).?; self.output_file.path = try fs.path.join( b.allocator, diff --git a/src/Compilation.zig b/src/Compilation.zig index dc3101d598d8454c1ea2e25778214a4bda87d748..ba0e643fda322d4b341b86a43b48521b4418f679 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1053,6 +1053,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { buf.appendSliceAssumeCapacity(","); } } + if (buf.items.len == 0) break :blk ""; assert(mem.endsWith(u8, buf.items, ",")); buf.items[buf.items.len - 1] = 0; buf.shrinkAndFree(buf.items.len); diff --git a/test/cases.zig b/test/cases.zig index ffe046c70ede267e97ae2b00a6dae446dbb997da..6c4b1c994f3aa1319e8ce74cb1d7c17f938231d4 100644 --- a/test/cases.zig +++ b/test/cases.zig @@ -4,5 +4,6 @@ const Cases = @import("src/Cases.zig"); pub fn addCases(cases: *Cases) !void { try @import("compile_errors.zig").addCases(cases); try @import("cbe.zig").addCases(cases); + try @import("llvm_targets.zig").addCases(cases); try @import("nvptx.zig").addCases(cases); } diff --git a/test/llvm_targets.zig b/test/llvm_targets.zig new file mode 100644 index 0000000000000000000000000000000000000000..d042ce500cfbb3c03864b10d56d647d88ad212f4 --- /dev/null +++ b/test/llvm_targets.zig @@ -0,0 +1,117 @@ +const std = @import("std"); +const Cases = @import("src/Cases.zig"); + +const targets = [_]std.zig.CrossTarget{ + .{ .cpu_arch = .aarch64, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .aarch64, .os_tag = .macos, .abi = .none }, + .{ .cpu_arch = .aarch64, .os_tag = .uefi, .abi = .none }, + .{ .cpu_arch = .aarch64, .os_tag = .windows, .abi = .gnu }, + .{ .cpu_arch = .aarch64, .os_tag = .windows, .abi = .msvc }, + .{ .cpu_arch = .aarch64_be, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .aarch64_be, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .aarch64_32, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .aarch64_32, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .amdgcn, .os_tag = .amdhsa, .abi = .none }, + .{ .cpu_arch = .amdgcn, .os_tag = .amdpal, .abi = .none }, + .{ .cpu_arch = .amdgcn, .os_tag = .linux, .abi = .none }, + //.{ .cpu_arch = .amdgcn, .os_tag = .mesa3d, .abi = .none }, + .{ .cpu_arch = .arm, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .arm, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .arm, .os_tag = .uefi, .abi = .none }, + .{ .cpu_arch = .armeb, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .armeb, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .avr, .os_tag = .freebsd, .abi = .none }, + .{ .cpu_arch = .avr, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .avr, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .bpfel, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .bpfel, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .bpfeb, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .bpfeb, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .hexagon, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .mips, .os_tag = .linux, .abi = .gnueabihf }, + .{ .cpu_arch = .mips, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .mips, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .mipsel, .os_tag = .linux, .abi = .gnueabihf }, + .{ .cpu_arch = .mipsel, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .mipsel, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .mips64, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .mips64el, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .msp430, .os_tag = .freebsd, .abi = .none }, + .{ .cpu_arch = .msp430, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .msp430, .os_tag = .linux, .abi = .none }, + //.{ .cpu_arch = .nvptx, .os_tag = .cuda, .abi = .none }, + //.{ .cpu_arch = .nvptx64, .os_tag = .cuda, .abi = .none }, + .{ .cpu_arch = .powerpc, .os_tag = .freebsd, .abi = .none }, + .{ .cpu_arch = .powerpc, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .powerpc, .os_tag = .linux, .abi = .gnueabihf }, + .{ .cpu_arch = .powerpc, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .powerpc, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .powerpcle, .os_tag = .freebsd, .abi = .none }, + .{ .cpu_arch = .powerpcle, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .powerpcle, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .powerpcle, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .powerpcle, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .powerpc64, .os_tag = .freebsd, .abi = .none }, + .{ .cpu_arch = .powerpc64, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .powerpc64, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .powerpc64, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .powerpc64, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .powerpc64le, .os_tag = .freebsd, .abi = .none }, + .{ .cpu_arch = .powerpc64le, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .powerpc64le, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .powerpc64le, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .powerpc64le, .os_tag = .linux, .abi = .none }, + //.{ .cpu_arch = .r600, .os_tag = .mesa3d, .abi = .none }, + .{ .cpu_arch = .riscv32, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .riscv32, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .riscv64, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .riscv64, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .riscv64, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .riscv64, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .s390x, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .s390x, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .sparc, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .sparcel, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .sparcel, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .sparc64, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .sparc64, .os_tag = .linux, .abi = .gnu }, + //.{ .cpu_arch = .spirv32, .os_tag = .opencl, .abi = .none }, + //.{ .cpu_arch = .spirv32, .os_tag = .glsl450, .abi = .none }, + //.{ .cpu_arch = .spirv32, .os_tag = .vulkan, .abi = .none }, + //.{ .cpu_arch = .spirv64, .os_tag = .opencl, .abi = .none }, + //.{ .cpu_arch = .spirv64, .os_tag = .glsl450, .abi = .none }, + //.{ .cpu_arch = .spirv64, .os_tag = .vulkan, .abi = .none }, + .{ .cpu_arch = .thumb, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .thumbeb, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .ve, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .wasm32, .os_tag = .emscripten, .abi = .none }, + .{ .cpu_arch = .wasm32, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .wasm32, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .wasm32, .os_tag = .wasi, .abi = .none }, + .{ .cpu_arch = .wasm64, .os_tag = .emscripten, .abi = .none }, + .{ .cpu_arch = .wasm64, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .wasm64, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .wasm64, .os_tag = .wasi, .abi = .none }, + .{ .cpu_arch = .x86, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .x86, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .x86, .os_tag = .uefi, .abi = .none }, + .{ .cpu_arch = .x86, .os_tag = .windows, .abi = .gnu }, + .{ .cpu_arch = .x86, .os_tag = .windows, .abi = .msvc }, + .{ .cpu_arch = .x86_64, .os_tag = .freebsd, .abi = .none }, + .{ .cpu_arch = .x86_64, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .x86_64, .os_tag = .macos, .abi = .none }, + .{ .cpu_arch = .x86_64, .os_tag = .uefi, .abi = .none }, + .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .gnu }, + .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .msvc }, +}; + +pub fn addCases(ctx: *Cases) !void { + for (targets) |target| { + var case = ctx.noEmitUsingLlvmBackend("llvm_targets", target); + case.addCompile(""); + } +} diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 92ec3cb35f966cb59fa29dca339b6771a6fafee8..ce080b5490f85fce2570f4e7e0a7b5301d315f5a 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -76,6 +76,7 @@ pub const Case = struct { output_mode: std.builtin.OutputMode, optimize_mode: std.builtin.Mode = .Debug, updates: std.ArrayList(Update), + emit_bin: bool = true, emit_h: bool = false, is_test: bool = false, expect_exact: bool = false, @@ -176,6 +177,19 @@ pub fn exeFromCompiledC(ctx: *Cases, name: []const u8, target: CrossTarget) *Cas return &ctx.cases.items[ctx.cases.items.len - 1]; } +pub fn noEmitUsingLlvmBackend(ctx: *Cases, name: []const u8, target: CrossTarget) *Case { + ctx.cases.append(Case{ + .name = name, + .target = target, + .updates = std.ArrayList(Update).init(ctx.cases.allocator), + .output_mode = .Obj, + .emit_bin = false, + .deps = std.ArrayList(DepModule).init(ctx.arena), + .backend = .llvm, + }) catch @panic("out of memory"); + return &ctx.cases.items[ctx.cases.items.len - 1]; +} + /// Adds a test case that uses the LLVM backend to emit an executable. /// Currently this implies linking libc, because only then we can generate a testable executable. pub fn exeUsingLlvmBackend(ctx: *Cases, name: []const u8, target: CrossTarget) *Case { @@ -537,6 +551,8 @@ pub fn lowerToBuildSteps( }), }; + artifact.emit_bin = if (case.emit_bin) .default else .no_emit; + if (case.link_libc) artifact.linkLibC(); switch (case.backend) { -- 2.54.0