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

@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
}
fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
const lib_a = b.addStaticLibrary(.{
const lib_a = b.addLibrary(.{
.linkage = .static,
.name = "a",
.root_module = b.createModule(.{
.root_source_file = null,

View File

@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
}
fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
const lib_a = b.addStaticLibrary(.{
const lib_a = b.addLibrary(.{
.linkage = .static,
.name = "a",
.root_module = b.createModule(.{
.root_source_file = null,

View File

@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
}
fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
const lib_a = b.addStaticLibrary(.{
const lib_a = b.addLibrary(.{
.linkage = .static,
.name = "a",
.root_module = b.createModule(.{
.root_source_file = null,
@@ -22,7 +23,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
lib_a.root_module.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });
lib_a.root_module.addIncludePath(b.path("."));
const lib_b = b.addStaticLibrary(.{
const lib_b = b.addLibrary(.{
.linkage = .static,
.name = "b",
.root_module = b.createModule(.{
.root_source_file = null,

View File

@@ -65,7 +65,8 @@ pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
}
pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
return b.addStaticLibrary(.{
return b.addLibrary(.{
.linkage = .static,
.name = overlay.name,
.root_module = createModule(b, base, overlay),
.use_llvm = base.use_llvm,
@@ -74,7 +75,8 @@ pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Comp
}
pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
return b.addSharedLibrary(.{
return b.addLibrary(.{
.linkage = .dynamic,
.name = overlay.name,
.root_module = createModule(b, base, overlay),
.use_llvm = base.use_llvm,

View File

@@ -85,11 +85,13 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
mod.addCSourceFile(.{ .file = file, .flags = &flags });
}
const lib_a = b.addStaticLibrary(.{
const lib_a = b.addLibrary(.{
.linkage = .static,
.name = "test2_a",
.root_module = mod_a,
});
const lib_b = b.addStaticLibrary(.{
const lib_b = b.addLibrary(.{
.linkage = .static,
.name = "test2_b",
.root_module = mod_b,
});
@@ -130,11 +132,13 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
lib_mod.addObject(obj);
}
const lib_a = b.addStaticLibrary(.{
const lib_a = b.addLibrary(.{
.linkage = .static,
.name = "test3_a",
.root_module = mod_a,
});
const lib_b = b.addStaticLibrary(.{
const lib_b = b.addLibrary(.{
.linkage = .static,
.name = "test3_b",
.root_module = mod_b,
});