aboutsummaryrefslogtreecommitdiff
path: root/test/link/macho/headerpad/build.zig
blob: 0730a01d442e87e6cb29cf7c60a214a62f34984d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const std = @import("std");
const builtin = @import("builtin");
const Builder = std.build.Builder;
const LibExeObjectStep = std.build.LibExeObjStep;

pub fn build(b: *Builder) void {
    const mode = b.standardReleaseOptions();

    const test_step = b.step("test", "Test");
    test_step.dependOn(b.getInstallStep());

    {
        // Test -headerpad_max_install_names
        const exe = simpleExe(b, mode);
        exe.headerpad_max_install_names = true;

        const check = exe.checkObject(.macho);
        check.checkStart("sectname __text");
        check.checkNext("offset {offset}");

        switch (builtin.cpu.arch) {
            .aarch64 => {
                check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x4000 } });
            },
            .x86_64 => {
                check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x1000 } });
            },
            else => unreachable,
        }

        test_step.dependOn(&check.step);

        const run = exe.run();
        test_step.dependOn(&run.step);
    }

    {
        // Test -headerpad
        const exe = simpleExe(b, mode);
        exe.headerpad_size = 0x10000;

        const check = exe.checkObject(.macho);
        check.checkStart("sectname __text");
        check.checkNext("offset {offset}");
        check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } });

        test_step.dependOn(&check.step);

        const run = exe.run();
        test_step.dependOn(&run.step);
    }

    {
        // Test both flags with -headerpad overriding -headerpad_max_install_names
        const exe = simpleExe(b, mode);
        exe.headerpad_max_install_names = true;
        exe.headerpad_size = 0x10000;

        const check = exe.checkObject(.macho);
        check.checkStart("sectname __text");
        check.checkNext("offset {offset}");
        check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x10000 } });

        test_step.dependOn(&check.step);

        const run = exe.run();
        test_step.dependOn(&run.step);
    }

    {
        // Test both flags with -headerpad_max_install_names overriding -headerpad
        const exe = simpleExe(b, mode);
        exe.headerpad_size = 0x1000;
        exe.headerpad_max_install_names = true;

        const check = exe.checkObject(.macho);
        check.checkStart("sectname __text");
        check.checkNext("offset {offset}");

        switch (builtin.cpu.arch) {
            .aarch64 => {
                check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x4000 } });
            },
            .x86_64 => {
                check.checkComputeCompare("offset", .{ .op = .gte, .value = .{ .literal = 0x1000 } });
            },
            else => unreachable,
        }

        test_step.dependOn(&check.step);

        const run = exe.run();
        test_step.dependOn(&run.step);
    }
}

fn simpleExe(b: *Builder, mode: std.builtin.Mode) *LibExeObjectStep {
    const exe = b.addExecutable("main", null);
    exe.setBuildMode(mode);
    exe.addCSourceFile("main.c", &.{});
    exe.linkLibC();
    exe.linkFramework("CoreFoundation");
    exe.linkFramework("Foundation");
    exe.linkFramework("Cocoa");
    exe.linkFramework("CoreGraphics");
    exe.linkFramework("CoreHaptics");
    exe.linkFramework("CoreAudio");
    exe.linkFramework("AVFoundation");
    exe.linkFramework("CoreImage");
    exe.linkFramework("CoreLocation");
    exe.linkFramework("CoreML");
    exe.linkFramework("CoreVideo");
    exe.linkFramework("CoreText");
    exe.linkFramework("CryptoKit");
    exe.linkFramework("GameKit");
    exe.linkFramework("SwiftUI");
    exe.linkFramework("StoreKit");
    exe.linkFramework("SpriteKit");
    return exe;
}