std.Build: add addLibrary function (#22554)

Acts as a replacement for `addSharedLibrary` and `addStaticLibrary`, but
linking mode can be changed more easily in build.zig, for example:

In library:
```zig
const linkage = b.option(std.builtin.LinkMode, "linkage", "Link mode for a foo_bar library") orelse .static; // or other default

const lib = b.addLibrary(.{
    .linkage = linkage,
    .name = "foo_bar",
    .root_module = mod,
});
```

In consumer:
```zig
const dep_foo_bar = b.dependency("foo_bar", .{
    .target = target,
    .optimize = optimize,
    .linkage = .static // or dynamic
});

mod.linkLibrary(dep_foor_bar.artifact("foo_bar"));
```

It also matches nicely with `linkLibrary` name.

Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>
This commit is contained in:
BratishkaErik
2025-01-22 07:29:21 +05:00
committed by GitHub
parent 28a259d4a3
commit 941677e083
19 changed files with 86 additions and 26 deletions

View File

@@ -47,7 +47,8 @@ pub fn build(b: *std.Build) void {
// Now, we will create a static library based on the module we created above.
// This creates a `std.Build.Step.Compile`, which is the build step responsible
// for actually invoking the compiler.
const lib = b.addStaticLibrary(.{
const lib = b.addLibrary(.{
.linkage = .static,
.name = "$",
.root_module = lib_mod,
});