1pub fn build(b: *Build) void {
2 const test_step = b.step("test", "Test the new ELF linker");
3 b.default_step = test_step;
4
5 if (b.graph.host.result.cpu.arch == .x86_64 and b.graph.host.result.os.tag == .linux) {
6 addOne(b, test_step, b.graph.host, false, .static, false, "elf2-hello-native-selfhosted-static");
7 addOne(b, test_step, b.graph.host, false, .dynamic, false, "elf2-hello-native-selfhosted-dynamic");
8 addOne(b, test_step, b.graph.host, false, .static, true, "elf2-hello-native-selfhosted-static-pie");
9 addOne(b, test_step, b.graph.host, false, .dynamic, true, "elf2-hello-native-selfhosted-dynamic-pie");
10 addOne(b, test_step, b.graph.host, true, .static, false, "elf2-hello-native-llvm-static");
11 addOne(b, test_step, b.graph.host, true, .dynamic, false, "elf2-hello-native-llvm-dynamic");
12 }
13
14 const x86_64_linux_target: Build.ResolvedTarget = b.resolveTargetQuery(.{
15 .cpu_arch = .x86_64,
16 .os_tag = .linux,
17 });
18 addOne(b, test_step, x86_64_linux_target, false, .static, false, "elf2-hello-selfhosted-static");
19 addOne(b, test_step, x86_64_linux_target, false, .static, true, "elf2-hello-selfhosted-static-pie");
20 addOne(b, test_step, x86_64_linux_target, true, .static, false, "elf2-hello-llvm-static");
21}
22
23fn addOne(
24 b: *Build,
25 test_step: *Build.Step,
26 target: Build.ResolvedTarget,
27 use_llvm: bool,
28 link_mode: std.lang.LinkMode,
29 pie: bool,
30 name: []const u8,
31) void {
32 const mod = b.createModule(.{
33 .root_source_file = b.path("hello.zig"),
34 .target = target,
35 .optimize = .debug,
36 .link_libc = link_mode == .dynamic,
37 });
38 const exe = b.addExecutable(.{
39 .name = name,
40 .root_module = mod,
41 .linkage = link_mode,
42 });
43 exe.use_new_linker = true;
44 exe.use_llvm = use_llvm;
45 if (pie) exe.pie = true;
46
47 const run = b.addRunArtifact(exe);
48 run.expectExitCode(0);
49 run.expectStdOutEqual("Hello, World!\n");
50 run.skip_foreign_checks = true;
51
52 test_step.dependOn(&run.step);
53}
54
55const std = @import("std");
56const Build = std.Build;