authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-10 13:50:33+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-11-10 13:50:33+01:00
log17177727c03d09303093c39c741a850abcc724f3
tree14e6bda126d10a2e6d5bfa26631536e579796c99
parent7566a8fbd81a71d4b61022fd2b6f5f80a4679fb0

test/link: refactor common bits between ELF and MachO tests


2 files changed, 19 insertions(+), 100 deletions(-)

test/link/elf.zig+15-99
......@@ -3480,109 +3480,25 @@ fn testZText(b: *Build, opts: Options) *Step {
34803480 return test_step;
34813481}
34823482
3483const Options = struct {
3484 target: CrossTarget = .{ .cpu_arch = .x86_64, .os_tag = .linux },
3485 optimize: std.builtin.OptimizeMode = .Debug,
3486 use_llvm: bool = true,
3487 use_lld: bool = false,
3488};
3489
34903483fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
3491 const target = opts.target.zigTriple(b.allocator) catch @panic("OOM");
3492 const optimize = @tagName(opts.optimize);
3493 const use_llvm = if (opts.use_llvm) "llvm" else "no-llvm";
3494 const name = std.fmt.allocPrint(b.allocator, "test-elf-" ++ prefix ++ "-{s}-{s}-{s}", .{
3495 target,
3496 optimize,
3497 use_llvm,
3498 }) catch @panic("OOM");
3499 return b.step(name, "");
3500}
3501
3502fn addExecutable(b: *Build, name: []const u8, opts: Options) *Compile {
3503 return b.addExecutable(.{
3504 .name = name,
3505 .target = opts.target,
3506 .optimize = opts.optimize,
3507 .use_llvm = opts.use_llvm,
3508 .use_lld = opts.use_lld,
3509 });
3510}
3511
3512fn addObject(b: *Build, name: []const u8, opts: Options) *Compile {
3513 return b.addObject(.{
3514 .name = name,
3515 .target = opts.target,
3516 .optimize = opts.optimize,
3517 .use_llvm = opts.use_llvm,
3518 .use_lld = opts.use_lld,
3519 });
3520}
3521
3522fn addStaticLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
3523 return b.addStaticLibrary(.{
3524 .name = name,
3525 .target = opts.target,
3526 .optimize = opts.optimize,
3527 .use_llvm = opts.use_llvm,
3528 .use_lld = opts.use_lld,
3529 });
3530}
3531
3532fn addSharedLibrary(b: *Build, name: []const u8, opts: Options) *Compile {
3533 return b.addSharedLibrary(.{
3534 .name = name,
3535 .target = opts.target,
3536 .optimize = opts.optimize,
3537 .use_llvm = opts.use_llvm,
3538 .use_lld = opts.use_lld,
3539 });
3540}
3541
3542fn addRunArtifact(comp: *Compile) *Run {
3543 const b = comp.step.owner;
3544 const run = b.addRunArtifact(comp);
3545 run.skip_foreign_checks = true;
3546 return run;
3547}
3548
3549fn addZigSourceBytes(comp: *Compile, bytes: []const u8) void {
3550 const b = comp.step.owner;
3551 const file = WriteFile.create(b).add("a.zig", bytes);
3552 file.addStepDependencies(&comp.step);
3553 comp.root_src = file;
3554}
3555
3556fn addCSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
3557 const b = comp.step.owner;
3558 const file = WriteFile.create(b).add("a.c", bytes);
3559 comp.addCSourceFile(.{ .file = file, .flags = flags });
3560}
3561
3562fn addCppSourceBytes(comp: *Compile, bytes: []const u8, flags: []const []const u8) void {
3563 const b = comp.step.owner;
3564 const file = WriteFile.create(b).add("a.cpp", bytes);
3565 comp.addCSourceFile(.{ .file = file, .flags = flags });
3566}
3567
3568fn addAsmSourceBytes(comp: *Compile, bytes: []const u8) void {
3569 const b = comp.step.owner;
3570 const actual_bytes = std.fmt.allocPrint(b.allocator, "{s}\n", .{bytes}) catch @panic("OOM");
3571 const file = WriteFile.create(b).add("a.s", actual_bytes);
3572 comp.addAssemblyFile(file);
3573}
3574
3575fn expectLinkErrors(comp: *Compile, test_step: *Step, expected_errors: Compile.ExpectedCompileErrors) void {
3576 comp.expect_errors = expected_errors;
3577 const bin_file = comp.getEmittedBin();
3578 bin_file.addStepDependencies(test_step);
3579}
3580
3484 return link.addTestStep(b, "elf-" ++ prefix, opts);
3485}
3486
3487const addAsmSourceBytes = link.addAsmSourceBytes;
3488const addCSourceBytes = link.addCSourceBytes;
3489const addCppSourceBytes = link.addCppSourceBytes;
3490const addExecutable = link.addExecutable;
3491const addObject = link.addObject;
3492const addRunArtifact = link.addRunArtifact;
3493const addSharedLibrary = link.addSharedLibrary;
3494const addStaticLibrary = link.addStaticLibrary;
3495const addZigSourceBytes = link.addZigSourceBytes;
3496const expectLinkErrors = link.expectLinkErrors;
3497const link = @import("link.zig");
35813498const std = @import("std");
35823499
35833500const Build = std.Build;
3584const Compile = Step.Compile;
35853501const CrossTarget = std.zig.CrossTarget;
3586const Run = Step.Run;
3502const Options = link.Options;
35873503const Step = Build.Step;
35883504const WriteFile = Step.WriteFile;
test/link/macho.zig+4-1
......@@ -77,11 +77,14 @@ fn testResolvingBoundarySymbols(b: *Build, opts: Options) *Step {
7777 return test_step;
7878}
7979
80fn addTestStep(b: *Build, comptime prefix: []const u8, opts: Options) *Step {
81 return link.addTestStep(b, "macho-" ++ prefix, opts);
82}
83
8084const addCppSourceBytes = link.addCppSourceBytes;
8185const addExecutable = link.addExecutable;
8286const addObject = link.addObject;
8387const addRunArtifact = link.addRunArtifact;
84const addTestStep = link.addTestStep;
8588const addZigSourceBytes = link.addZigSourceBytes;
8689const expectLinkErrors = link.expectLinkErrors;
8790const link = @import("link.zig");