| ... | @@ -16,6 +16,7 @@ pub fn build(b: *Build) void { | ... | @@ -16,6 +16,7 @@ pub fn build(b: *Build) void { |
| 16 | // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false })); | 16 | // elf_step.dependOn(testLinkingZig(b, .{ .use_llvm = false })); |
| 17 | | 17 | |
| 18 | // Exercise linker with LLVM backend | 18 | // Exercise linker with LLVM backend |
| | 19 | elf_step.dependOn(testCommonSymbols(b, .{ .target = musl_target })); |
| 19 | elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target })); | 20 | elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target })); |
| 20 | elf_step.dependOn(testGcSections(b, .{ .target = musl_target })); | 21 | elf_step.dependOn(testGcSections(b, .{ .target = musl_target })); |
| 21 | elf_step.dependOn(testLinkingC(b, .{ .target = musl_target })); | 22 | elf_step.dependOn(testLinkingC(b, .{ .target = musl_target })); |
| ... | @@ -24,12 +25,39 @@ pub fn build(b: *Build) void { | ... | @@ -24,12 +25,39 @@ pub fn build(b: *Build) void { |
| 24 | elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target })); | 25 | elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target })); |
| 25 | } | 26 | } |
| 26 | | 27 | |
| | 28 | fn testCommonSymbols(b: *Build, opts: Options) *Step { |
| | 29 | const test_step = addTestStep(b, "common-symbols", opts); |
| | 30 | |
| | 31 | const exe = addExecutable(b, opts); |
| | 32 | addCSourceBytes(exe, |
| | 33 | \\int foo; |
| | 34 | \\int bar; |
| | 35 | \\int baz = 42; |
| | 36 | , &.{"-fcommon"}); |
| | 37 | addCSourceBytes(exe, |
| | 38 | \\#include<stdio.h> |
| | 39 | \\int foo; |
| | 40 | \\int bar = 5; |
| | 41 | \\int baz; |
| | 42 | \\int main() { |
| | 43 | \\ printf("%d %d %d\n", foo, bar, baz); |
| | 44 | \\} |
| | 45 | , &.{"-fcommon"}); |
| | 46 | exe.is_linking_libc = true; |
| | 47 | |
| | 48 | const run = addRunArtifact(exe); |
| | 49 | run.expectStdOutEqual("0 5 42\n"); |
| | 50 | test_step.dependOn(&run.step); |
| | 51 | |
| | 52 | return test_step; |
| | 53 | } |
| | 54 | |
| 27 | fn testEmptyObject(b: *Build, opts: Options) *Step { | 55 | fn testEmptyObject(b: *Build, opts: Options) *Step { |
| 28 | const test_step = addTestStep(b, "empty-object", opts); | 56 | const test_step = addTestStep(b, "empty-object", opts); |
| 29 | | 57 | |
| 30 | const exe = addExecutable(b, opts); | 58 | const exe = addExecutable(b, opts); |
| 31 | addCSourceBytes(exe, "int main() { return 0; }"); | 59 | addCSourceBytes(exe, "int main() { return 0; }", &.{}); |
| 32 | addCSourceBytes(exe, ""); | 60 | addCSourceBytes(exe, "", &.{}); |
| 33 | exe.is_linking_libc = true; | 61 | exe.is_linking_libc = true; |
| 34 | | 62 | |
| 35 | const run = addRunArtifact(exe); | 63 | const run = addRunArtifact(exe); |
| ... | @@ -58,7 +86,7 @@ fn testGcSections(b: *Build, opts: Options) *Step { | ... | @@ -58,7 +86,7 @@ fn testGcSections(b: *Build, opts: Options) *Step { |
| 58 | \\ printf("%d %d\n", live_var1, live_var2); | 86 | \\ printf("%d %d\n", live_var1, live_var2); |
| 59 | \\ live_fn2(); | 87 | \\ live_fn2(); |
| 60 | \\} | 88 | \\} |
| 61 | ); | 89 | , &.{}); |
| 62 | obj.link_function_sections = true; | 90 | obj.link_function_sections = true; |
| 63 | obj.link_data_sections = true; | 91 | obj.link_data_sections = true; |
| 64 | obj.is_linking_libc = true; | 92 | obj.is_linking_libc = true; |
| ... | @@ -139,7 +167,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step { | ... | @@ -139,7 +167,7 @@ fn testLinkingC(b: *Build, opts: Options) *Step { |
| 139 | \\ printf("Hello World!\n"); | 167 | \\ printf("Hello World!\n"); |
| 140 | \\ return 0; | 168 | \\ return 0; |
| 141 | \\} | 169 | \\} |
| 142 | ); | 170 | , &.{}); |
| 143 | exe.is_linking_libc = true; | 171 | exe.is_linking_libc = true; |
| 144 | | 172 | |
| 145 | const run = addRunArtifact(exe); | 173 | const run = addRunArtifact(exe); |
| ... | @@ -168,7 +196,7 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step { | ... | @@ -168,7 +196,7 @@ fn testLinkingCpp(b: *Build, opts: Options) *Step { |
| 168 | \\ std::cout << "Hello World!" << std::endl; | 196 | \\ std::cout << "Hello World!" << std::endl; |
| 169 | \\ return 0; | 197 | \\ return 0; |
| 170 | \\} | 198 | \\} |
| 171 | ); | 199 | , &.{}); |
| 172 | exe.is_linking_libc = true; | 200 | exe.is_linking_libc = true; |
| 173 | exe.is_linking_libcpp = true; | 201 | exe.is_linking_libcpp = true; |
| 174 | | 202 | |
| ... | @@ -231,7 +259,7 @@ fn testTlsStatic(b: *Build, opts: Options) *Step { | ... | @@ -231,7 +259,7 @@ fn testTlsStatic(b: *Build, opts: Options) *Step { |
| 231 | \\ printf("%d %d %c\n", a, b, c); | 259 | \\ printf("%d %d %c\n", a, b, c); |
| 232 | \\ return 0; | 260 | \\ return 0; |
| 233 | \\} | 261 | \\} |
| 234 | ); | 262 | , &.{}); |
| 235 | exe.is_linking_libc = true; | 263 | exe.is_linking_libc = true; |
| 236 | | 264 | |
| 237 | const run = addRunArtifact(exe); | 265 | const run = addRunArtifact(exe); |
| ... | @@ -297,16 +325,16 @@ fn addZigSourceBytes(comp: *Compile, comptime bytes: []const u8) void { | ... | @@ -297,16 +325,16 @@ fn addZigSourceBytes(comp: *Compile, comptime bytes: []const u8) void { |
| 297 | comp.root_src = file; | 325 | comp.root_src = file; |
| 298 | } | 326 | } |
| 299 | | 327 | |
| 300 | fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8) void { | 328 | fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void { |
| 301 | const b = comp.step.owner; | 329 | const b = comp.step.owner; |
| 302 | const file = WriteFile.create(b).add("a.c", bytes); | 330 | const file = WriteFile.create(b).add("a.c", bytes); |
| 303 | comp.addCSourceFile(.{ .file = file, .flags = &.{} }); | 331 | comp.addCSourceFile(.{ .file = file, .flags = flags }); |
| 304 | } | 332 | } |
| 305 | | 333 | |
| 306 | fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8) void { | 334 | fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8, flags: []const []const u8) void { |
| 307 | const b = comp.step.owner; | 335 | const b = comp.step.owner; |
| 308 | const file = WriteFile.create(b).add("a.cpp", bytes); | 336 | const file = WriteFile.create(b).add("a.cpp", bytes); |
| 309 | comp.addCSourceFile(.{ .file = file, .flags = &.{} }); | 337 | comp.addCSourceFile(.{ .file = file, .flags = flags }); |
| 310 | } | 338 | } |
| 311 | | 339 | |
| 312 | fn addAsmSourceBytes(comp: *Compile, comptime bytes: []const u8) void { | 340 | fn addAsmSourceBytes(comp: *Compile, comptime bytes: []const u8) void { |