Skip to content

Commit 9107b5a

Browse files
committed
Move the -l/-L ELF flag forwarding test
1 parent c37eec2 commit 9107b5a

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

test/link/elf.zig

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub fn testAll(b: *Build, build_opts: BuildOptions) *Step {
8282

8383
// glibc tests
8484
elf_step.dependOn(testAsNeeded(b, .{ .target = gnu_target }));
85+
elf_step.dependOn(testLibraryPathsCompatibility(b, .{ .target = gnu_target, .use_lld = true }));
8586
// https://github.com/ziglang/zig/issues/17430
8687
// elf_step.dependOn(testCanonicalPlt(b, .{ .target = gnu_target }));
8788
elf_step.dependOn(testCommentString(b, .{ .target = gnu_target }));
@@ -308,6 +309,54 @@ fn testAsNeeded(b: *Build, opts: Options) *Step {
308309
return test_step;
309310
}
310311

312+
fn testLibraryPathsCompatibility(b: *Build, opts: Options) *Step {
313+
const test_step = addTestStep(b, "library-paths-compatibility", opts);
314+
315+
const main_o = addObject(b, opts, .{
316+
.name = "main",
317+
.c_source_bytes =
318+
\\#include <stdio.h>
319+
\\int foo();
320+
\\int main() {
321+
\\ printf("%d\n", foo());
322+
\\ return 0;
323+
\\}
324+
\\
325+
,
326+
});
327+
main_o.linkLibC();
328+
329+
const libfoo = addSharedLibrary(b, opts, .{ .name = "foo", .omit_soname = true });
330+
addCSourceBytes(libfoo, "int foo() { return 42; }", &.{});
331+
332+
{
333+
const scripts = WriteFile.create(b);
334+
const path = scripts.addCopyFile(libfoo.getEmittedBin(), "foo/libfoo.so");
335+
336+
const exe = addExecutable(b, opts, .{ .name = "test" });
337+
exe.addObject(main_o);
338+
339+
exe.setCwd(scripts.getDirectory());
340+
exe.addLibraryPathSpecial("foo");
341+
exe.addRPath(path.dirname());
342+
343+
exe.linkSystemLibrary2("foo", .{ .needed = false });
344+
exe.linkLibC();
345+
346+
const run = addRunArtifact(exe);
347+
run.expectStdOutEqual("42\n");
348+
test_step.dependOn(&run.step);
349+
350+
const check = exe.checkObject();
351+
check.checkInDynamicSection();
352+
check.checkExact("NEEDED libfoo.so");
353+
check.checkNotPresent("NEEDED foo/libfoo.so");
354+
test_step.dependOn(&check.step);
355+
}
356+
357+
return test_step;
358+
}
359+
311360
fn testCanonicalPlt(b: *Build, opts: Options) *Step {
312361
const test_step = addTestStep(b, "canonical-plt", opts);
313362

test/tests.zig

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -779,54 +779,6 @@ pub fn addCliTests(b: *std.Build) *Step {
779779
step.dependOn(&cleanup.step);
780780
}
781781

782-
{
783-
// Test `zig cc` `-l`/`-L` linker flag forwarding for ELF.
784-
const tmp_path = b.makeTempPath();
785-
var dir = std.fs.cwd().openDir(tmp_path, .{}) catch @panic("unhandled");
786-
defer dir.close();
787-
dir.writeFile("main.c",
788-
\\#include "foo/foo.h"
789-
\\int main() { f(); }
790-
) catch @panic("unhandled");
791-
dir.makeDir("foo") catch @panic("unhandled");
792-
var subdir = dir.openDir("foo", .{}) catch @panic("unhandled");
793-
defer subdir.close();
794-
subdir.writeFile("foo.h", "void f();") catch @panic("unhandled");
795-
subdir.writeFile("foo.c", "void f() {}") catch @panic("unhandled");
796-
797-
const cc_shared = b.addSystemCommand(&.{
798-
b.graph.zig_exe, "cc",
799-
"-shared", "foo/foo.c",
800-
"-target", "x86_64-linux-gnu",
801-
"-o", "foo/libfoo.so",
802-
});
803-
cc_shared.setCwd(.{ .cwd_relative = tmp_path });
804-
cc_shared.setName("build the shared library");
805-
806-
const cc_link = b.addSystemCommand(&.{
807-
b.graph.zig_exe, "cc",
808-
"main.c", "-Lfoo",
809-
"-lfoo", "-target",
810-
"x86_64-linux-gnu", "-o",
811-
"main",
812-
});
813-
const main_path = std.fs.path.join(b.allocator, &.{ tmp_path, "main" }) catch @panic("OOM");
814-
cc_link.setCwd(.{ .cwd_relative = tmp_path });
815-
cc_link.setName("link the shared library");
816-
cc_link.step.dependOn(&cc_shared.step);
817-
818-
const check = Step.CheckObject.create(step.owner, .{ .cwd_relative = main_path }, .elf);
819-
check.checkInDynamicSection();
820-
check.checkExact("NEEDED libfoo.so");
821-
check.checkNotPresent("NEEDED foo/libfoo.so");
822-
check.step.dependOn(&cc_link.step);
823-
824-
const cleanup = b.addRemoveDirTree(tmp_path);
825-
cleanup.step.dependOn(&check.step);
826-
827-
step.dependOn(&cleanup.step);
828-
}
829-
830782
// Test Godbolt API
831783
if (builtin.os.tag == .linux and builtin.cpu.arch == .x86_64) {
832784
const tmp_path = b.makeTempPath();

0 commit comments

Comments
 (0)