Skip to content

mod.linkSystemLibrary does not link emulated wasi-libc libraries #24325

Open
@rpkak

Description

@rpkak

Zig Version

0.15.0-dev.889+b8ac740a1

Steps to Reproduce and Observed Behavior

test.c:

#include <stdio.h>
#include <unistd.h>

int main() {
    printf("pid: %d\n", getpid());
}

The following works as expected:

$ zig build-exe -target wasm32-wasi-musl test.c -lc
error: wasm-ld: .zig-cache/o/00041ae31f49933b1031cc26bfb1dc76/test.o: undefined symbol: getpid
$ zig build-exe -target wasm32-wasi-musl test.c -lc -lwasi-emulated-getpid
$ wasmtime test.wasm
pid: 42

But with build.zig:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const mod = b.createModule(.{
        .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi, .abi = .musl }),
        .link_libc = true,
    });

    mod.addCSourceFile(.{ .file = b.path("test.c") });
    mod.linkSystemLibrary("wasi-emulated-getpid", .{});

    const exe = b.addExecutable(.{
        .name = "test",
        .root_module = mod,
    });

    b.installArtifact(exe);
}

wasi-emulated-getpid doesn't get linked:

$ zig build
install
└─ install test
   └─ compile exe test Debug wasm32-wasi-musl 1 errors
error: wasm-ld: .zig-cache/o/bb27660191785df4236a033b73cfc83b/test.o: undefined symbol: getpid
error: the following command failed with 1 compilation errors:
/home/user/.local/share/zigup/0.15.0-dev.889+b8ac740a1/files/zig build-exe /tmp/demo/test.c -target wasm32-wasi-musl -mcpu baseline -Mroot -lc --cache-dir .zig-cache --global-cache-dir /home/user/.cache/zig --name test --zig-lib-dir /home/user/.local/share/zigup/0.15.0-dev.889+b8ac740a1/files/lib/ --listen=-
Build Summary: 0/3 steps succeeded; 1 failed
install transitive failure
└─ install test transitive failure
   └─ compile exe test Debug wasm32-wasi-musl 1 errors
error: the following build command failed with exit code 1:
.zig-cache/o/3b6bcaf35fa2017ba3a291876365ba20/build /home/user/.local/share/zigup/0.15.0-dev.889+b8ac740a1/files/zig /home/user/.local/share/zigup/0.15.0-dev.889+b8ac740a1/files/lib /tmp/demo .zig-cache /home/user/.cache/zig --seed 0x3762f2a6 -Z008e0638cf71a675

Note that -lwasi-emulated-getpid is missing from the zig build-exe command.

This seems to be caused by #22191 and

if (std.zig.target.isLibCLibName(target, name)) {
m.link_libc = true;
return;
}

Expected Behavior

mod.linkSystemLibrary("wasi-emulated-getpid", .{}); should include -lwasi-emulated-getpid into the zig build-exe command or there should be another way to link emulated wasi-libc libraries like mod.wasi_emulate_getpid = true.

Metadata

Metadata

Assignees

Labels

bugObserved behavior contradicts documented or intended behavioros-wasiregressionIt worked in a previous version of Zig, but stopped working.zig build systemstd.Build, the build runner, `zig build` subcommand, package management

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions