test/link/link: pass build options to elf and macho tests

This commit is contained in:
Jakub Konka 2024-01-13 18:49:27 +01:00
parent 041f7d69f0
commit d93a0763d4
7 changed files with 76 additions and 63 deletions

View File

@ -135,10 +135,6 @@ pub const cases = [_]Case{
.build_root = "test/link/macho/linksection",
.import = @import("link/macho/linksection/build.zig"),
},
.{
.build_root = "test/link/macho/needed_framework",
.import = @import("link/macho/needed_framework/build.zig"),
},
.{
.build_root = "test/link/macho/needed_library",
.import = @import("link/macho/needed_library/build.zig"),

View File

@ -2,7 +2,8 @@
//! Currently, we support linking x86_64 Linux, but in the future we
//! will progressively relax those to exercise more combinations.
pub fn testAll(b: *Build) *Step {
pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
_ = build_opts;
const elf_step = b.step("test-elf", "Run ELF tests");
const default_target = b.resolveTargetQuery(.{
@ -3901,6 +3902,7 @@ const link = @import("link.zig");
const std = @import("std");
const Build = std.Build;
const BuildOptions = link.BuildOptions;
const Options = link.Options;
const Step = Build.Step;
const WriteFile = Step.WriteFile;

View File

@ -2,10 +2,26 @@ pub fn build(b: *Build) void {
const test_step = b.step("test-link", "Run link tests");
b.default_step = test_step;
test_step.dependOn(@import("elf.zig").testAll(b));
test_step.dependOn(@import("macho.zig").testAll(b));
const has_macos_sdk = b.option(bool, "has_macos_sdk", "whether the host provides a macOS SDK in system path");
const has_ios_sdk = b.option(bool, "has_ios_sdk", "whether the host provides a iOS SDK in system path");
const has_symlinks_windows = b.option(bool, "has_symlinks_windows", "whether the host is windows and has symlinks enabled");
const build_opts: BuildOptions = .{
.has_macos_sdk = has_macos_sdk orelse false,
.has_ios_sdk = has_ios_sdk orelse false,
.has_symlinks_windows = has_symlinks_windows orelse false,
};
test_step.dependOn(@import("elf.zig").testAll(b, build_opts));
test_step.dependOn(@import("macho.zig").testAll(b, build_opts));
}
pub const BuildOptions = struct {
has_macos_sdk: bool,
has_ios_sdk: bool,
has_symlinks_windows: bool,
};
pub const Options = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode = .Debug,

View File

@ -1,7 +1,7 @@
//! Here we test our MachO linker for correctness and functionality.
//! TODO migrate standalone tests from test/link/macho/* to here.
pub fn testAll(b: *std.Build) *Step {
pub fn testAll(b: *std.Build, build_opts: BuildOptions) *Step {
const macho_step = b.step("test-macho", "Run MachO tests");
const default_target = b.resolveTargetQuery(.{
@ -13,6 +13,11 @@ pub fn testAll(b: *std.Build) *Step {
macho_step.dependOn(testSectionBoundarySymbols(b, .{ .target = default_target }));
macho_step.dependOn(testSegmentBoundarySymbols(b, .{ .target = default_target }));
// Tests requiring presence of macOS SDK in system path
if (build_opts.has_macos_sdk and build_opts.has_symlinks_windows) {
macho_step.dependOn(testNeededFramework(b, .{ .target = b.host }));
}
return macho_step;
}
@ -151,6 +156,26 @@ fn testEntryPointDylib(b: *std.Build, opts: Options) *Step {
return test_step;
}
fn testNeededFramework(b: *std.Build, opts: Options) *Step {
const test_step = addTestStep(b, "macho-needed-framework", opts);
const exe = addExecutable(b, opts, .{ .name = "main", .c_source_bytes = "int main() { return 0; }" });
exe.linkFrameworkNeeded("Cocoa");
exe.dead_strip_dylibs = true;
const check = exe.checkObject();
check.checkInHeaders();
check.checkExact("cmd LOAD_DYLIB");
check.checkContains("Cocoa");
test_step.dependOn(&check.step);
const run = addRunArtifact(exe);
run.expectExitCode(0);
test_step.dependOn(&run.step);
return test_step;
}
fn testSectionBoundarySymbols(b: *std.Build, opts: Options) *Step {
const test_step = addTestStep(b, "macho-section-boundary-symbols", opts);
@ -311,5 +336,6 @@ const addSharedLibrary = link.addSharedLibrary;
const expectLinkErrors = link.expectLinkErrors;
const link = @import("link.zig");
const std = @import("std");
const BuildOptions = link.BuildOptions;
const Options = link.Options;
const Step = std.Build.Step;

View File

@ -1,37 +0,0 @@
const std = @import("std");
pub const requires_symlinks = true;
pub const requires_macos_sdk = true;
pub fn build(b: *std.Build) void {
const test_step = b.step("test", "Test it");
b.default_step = test_step;
add(b, test_step, .Debug);
add(b, test_step, .ReleaseFast);
add(b, test_step, .ReleaseSmall);
add(b, test_step, .ReleaseSafe);
}
fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
// -dead_strip_dylibs
// -needed_framework Cocoa
const exe = b.addExecutable(.{
.name = "test",
.optimize = optimize,
.target = b.host,
});
exe.addCSourceFile(.{ .file = .{ .path = "main.c" }, .flags = &[0][]const u8{} });
exe.linkLibC();
exe.linkFrameworkNeeded("Cocoa");
exe.dead_strip_dylibs = true;
const check = exe.checkObject();
check.checkInHeaders();
check.checkExact("cmd LOAD_DYLIB");
check.checkContains("Cocoa");
test_step.dependOn(&check.step);
const run_cmd = b.addRunArtifact(exe);
test_step.dependOn(&run_cmd.step);
}

View File

@ -1,3 +0,0 @@
int main(int argc, char* argv[]) {
return 0;
}

View File

@ -750,26 +750,39 @@ pub fn addLinkTests(
const omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows;
inline for (link.cases) |case| {
const requires_stage2 = @hasDecl(case.import, "requires_stage2") and
case.import.requires_stage2;
const requires_symlinks = @hasDecl(case.import, "requires_symlinks") and
case.import.requires_symlinks;
const requires_macos_sdk = @hasDecl(case.import, "requires_macos_sdk") and
case.import.requires_macos_sdk;
const requires_ios_sdk = @hasDecl(case.import, "requires_ios_sdk") and
case.import.requires_ios_sdk;
const bad =
(requires_stage2 and omit_stage2) or
(requires_symlinks and omit_symlinks) or
(requires_macos_sdk and !enable_macos_sdk) or
(requires_ios_sdk and !enable_ios_sdk);
if (!bad) {
const dep = b.anonymousDependency(case.build_root, case.import, .{});
if (mem.eql(u8, @typeName(case.import), "test.link.link")) {
const dep = b.anonymousDependency(case.build_root, case.import, .{
.has_macos_sdk = enable_macos_sdk,
.has_ios_sdk = enable_ios_sdk,
.has_symlinks_windows = !omit_symlinks,
});
const dep_step = dep.builder.default_step;
assert(mem.startsWith(u8, dep.builder.dep_prefix, "test."));
const dep_prefix_adjusted = dep.builder.dep_prefix["test.".len..];
dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
step.dependOn(dep_step);
} else {
const requires_stage2 = @hasDecl(case.import, "requires_stage2") and
case.import.requires_stage2;
const requires_symlinks = @hasDecl(case.import, "requires_symlinks") and
case.import.requires_symlinks;
const requires_macos_sdk = @hasDecl(case.import, "requires_macos_sdk") and
case.import.requires_macos_sdk;
const requires_ios_sdk = @hasDecl(case.import, "requires_ios_sdk") and
case.import.requires_ios_sdk;
const bad =
(requires_stage2 and omit_stage2) or
(requires_symlinks and omit_symlinks) or
(requires_macos_sdk and !enable_macos_sdk) or
(requires_ios_sdk and !enable_ios_sdk);
if (!bad) {
const dep = b.anonymousDependency(case.build_root, case.import, .{});
const dep_step = dep.builder.default_step;
assert(mem.startsWith(u8, dep.builder.dep_prefix, "test."));
const dep_prefix_adjusted = dep.builder.dep_prefix["test.".len..];
dep_step.name = b.fmt("{s}{s}", .{ dep_prefix_adjusted, dep_step.name });
step.dependOn(dep_step);
}
}
}