Skip to content

Commit e37cf1f

Browse files
committed
test: check that zig cc passes -l/-L like Clang/GCC for ELF
This test follows the example from #19699.
1 parent 4c3367a commit e37cf1f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

test/link/elf.zig

Lines changed: 48 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,53 @@ 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", .soname = false });
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.addLibraryPath(.{ .generated = .{ .file = &scripts.generated_directory, .sub_path = "foo" } });
340+
exe.addRPath(path.dirname());
341+
342+
exe.linkSystemLibrary2("foo", .{ .needed = false });
343+
exe.linkLibC();
344+
345+
const run = addRunArtifact(exe);
346+
run.expectStdOutEqual("42\n");
347+
test_step.dependOn(&run.step);
348+
349+
const check = exe.checkObject();
350+
check.checkInDynamicSection();
351+
check.checkExact("NEEDED libfoo.so");
352+
check.checkNotPresent("NEEDED foo/libfoo.so");
353+
test_step.dependOn(&check.step);
354+
}
355+
356+
return test_step;
357+
}
358+
311359
fn testCanonicalPlt(b: *Build, opts: Options) *Step {
312360
const test_step = addTestStep(b, "canonical-plt", opts);
313361

0 commit comments

Comments
 (0)