From 976d4f51ccae088044ac90a89c3840db94f4ceb2 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Wed, 4 Oct 2023 12:38:18 +0200 Subject: [PATCH] elf: add hello-world c++ link test --- test/link/elf.zig | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/test/link/elf.zig b/test/link/elf.zig index 1ac3e5e799..8b70249ad8 100644 --- a/test/link/elf.zig +++ b/test/link/elf.zig @@ -18,6 +18,7 @@ pub fn build(b: *Build) void { // Exercise linker with LLVM backend elf_step.dependOn(testEmptyObject(b, .{ .target = musl_target })); elf_step.dependOn(testLinkingC(b, .{ .target = musl_target })); + elf_step.dependOn(testLinkingCpp(b, .{ .target = musl_target })); elf_step.dependOn(testLinkingZig(b, .{ .target = musl_target })); elf_step.dependOn(testTlsStatic(b, .{ .target = musl_target })); } @@ -38,7 +39,7 @@ fn testEmptyObject(b: *Build, opts: Options) *Step { } fn testLinkingC(b: *Build, opts: Options) *Step { - const test_step = addTestStep(b, "linking-c-static", opts); + const test_step = addTestStep(b, "linking-c", opts); const exe = addExecutable(b, opts); addCSourceBytes(exe, @@ -66,6 +67,36 @@ fn testLinkingC(b: *Build, opts: Options) *Step { return test_step; } +fn testLinkingCpp(b: *Build, opts: Options) *Step { + const test_step = addTestStep(b, "linking-cpp", opts); + + const exe = addExecutable(b, opts); + addCppSourceBytes(exe, + \\#include + \\int main() { + \\ std::cout << "Hello World!" << std::endl; + \\ return 0; + \\} + ); + exe.is_linking_libc = true; + exe.is_linking_libcpp = true; + + const run = addRunArtifact(exe); + run.expectStdOutEqual("Hello World!\n"); + test_step.dependOn(&run.step); + + const check = exe.checkObject(); + check.checkStart(); + check.checkExact("header"); + check.checkExact("type EXEC"); + check.checkStart(); + check.checkExact("section headers"); + check.checkNotPresent("name .dynamic"); + test_step.dependOn(&check.step); + + return test_step; +} + fn testLinkingZig(b: *Build, opts: Options) *Step { const test_step = addTestStep(b, "linking-zig-static", opts); @@ -171,6 +202,12 @@ fn addCSourceBytes(comp: *Compile, comptime bytes: []const u8) void { comp.addCSourceFile(.{ .file = file, .flags = &.{} }); } +fn addCppSourceBytes(comp: *Compile, comptime bytes: []const u8) void { + const b = comp.step.owner; + const file = WriteFile.create(b).add("a.cpp", bytes); + comp.addCSourceFile(.{ .file = file, .flags = &.{} }); +} + fn addAsmSourceBytes(comp: *Compile, comptime bytes: []const u8) void { const b = comp.step.owner; const file = WriteFile.create(b).add("a.s", bytes ++ "\n");