authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-25 17:05:16+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-26 21:07:47+02:00
log8abfb3559ab6e1be7852b61455fe1197bf1cbd69
treeb6b1f01a232864cf9fa91c53a6d9b4ab6e9ef63d
parentaac04b4a5a70fc45f88d7b005e20c2cde271ff89

elf: test statically linking libc


1 files changed, 42 insertions(+), 4 deletions(-)

test/link/elf.zig+42-4
...@@ -11,12 +11,14 @@ pub fn build(b: *Build) void {...@@ -11,12 +11,14 @@ pub fn build(b: *Build) void {
11 .os_tag = .linux,11 .os_tag = .linux,
12 };12 };
1313
14 elf_step.dependOn(testHelloStatic(b, .{ .target = target, .use_llvm = true }));14 elf_step.dependOn(testLinkingZigStatic(b, .{ .target = target, .use_llvm = true }));
15 elf_step.dependOn(testHelloStatic(b, .{ .target = target, .use_llvm = false }));15 elf_step.dependOn(testLinkingZigStatic(b, .{ .target = target, .use_llvm = false }));
16 elf_step.dependOn(testLinkingCStatic(b, .{ .target = target, .use_llvm = true }));
17 // elf_step.dependOn(testLinkingCStatic(b, .{ .target = target, .use_llvm = false })); // TODO arocc
16}18}
1719
18fn testHelloStatic(b: *Build, opts: Options) *Step {20fn testLinkingZigStatic(b: *Build, opts: Options) *Step {
19 const test_step = addTestStep(b, "hello-static", opts);21 const test_step = addTestStep(b, "linking-zig-static", opts);
2022
21 const exe = addExecutable(b, opts);23 const exe = addExecutable(b, opts);
22 addZigSourceBytes(exe,24 addZigSourceBytes(exe,
...@@ -42,6 +44,36 @@ fn testHelloStatic(b: *Build, opts: Options) *Step {...@@ -42,6 +44,36 @@ fn testHelloStatic(b: *Build, opts: Options) *Step {
42 return test_step;44 return test_step;
43}45}
4446
47fn testLinkingCStatic(b: *Build, opts: Options) *Step {
48 const test_step = addTestStep(b, "linking-c-static", opts);
49
50 const exe = addExecutable(b, opts);
51 addCSourceBytes(exe,
52 \\#include <stdio.h>
53 \\int main() {
54 \\ printf("Hello World!\n");
55 \\ return 0;
56 \\}
57 );
58 exe.is_linking_libc = true;
59 exe.linkage = .static;
60
61 const run = b.addRunArtifact(exe);
62 run.expectStdOutEqual("Hello World!\n");
63 test_step.dependOn(&run.step);
64
65 const check = exe.checkObject();
66 check.checkStart();
67 check.checkExact("header");
68 check.checkExact("type EXEC");
69 check.checkStart();
70 check.checkExact("section headers");
71 check.checkNotPresent("name .dynamic");
72 test_step.dependOn(&check.step);
73
74 return test_step;
75}
76
45const Options = struct {77const Options = struct {
46 target: CrossTarget = .{ .os_tag = .linux },78 target: CrossTarget = .{ .os_tag = .linux },
47 optimize: std.builtin.OptimizeMode = .Debug,79 optimize: std.builtin.OptimizeMode = .Debug,
...@@ -78,6 +110,12 @@ fn addZigSourceBytes(comp: *Compile, bytes: []const u8) void {...@@ -78,6 +110,12 @@ fn addZigSourceBytes(comp: *Compile, bytes: []const u8) void {
78 comp.root_src = file;110 comp.root_src = file;
79}111}
80112
113fn addCSourceBytes(comp: *Compile, bytes: []const u8) void {
114 const b = comp.step.owner;
115 const file = WriteFile.create(b).add("a.c", bytes);
116 comp.addCSourceFile(.{ .file = file, .flags = &.{} });
117}
118
81const std = @import("std");119const std = @import("std");
82120
83const Build = std.Build;121const Build = std.Build;