authorgravatar for bratishkaerik@landless-city.netEric Joldasov <bratishkaerik@landless-city.net> 2025-01-22 07:29:21+05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-22 02:29:21+00:00
log941677e08318c2baaabc9d0fc87892d1b63487ae
tree3a6682be6f5063e6d7a27bd90d50a9cf729d5937
parent28a259d4a34e544090a76863802e600a4e01fcc2
signaturebadge-check Signed by PGP key B5690EEEBB952194

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>

19 files changed, 86 insertions(+), 26 deletions(-)

lib/init/build.zig+2-1
...@@ -47,7 +47,8 @@ pub fn build(b: *std.Build) void {...@@ -47,7 +47,8 @@ pub fn build(b: *std.Build) void {
47 // Now, we will create a static library based on the module we created above.47 // Now, we will create a static library based on the module we created above.
48 // This creates a `std.Build.Step.Compile`, which is the build step responsible48 // This creates a `std.Build.Step.Compile`, which is the build step responsible
49 // for actually invoking the compiler.49 // for actually invoking the compiler.
50 const lib = b.addStaticLibrary(.{50 const lib = b.addLibrary(.{
51 .linkage = .static,
51 .name = "$",52 .name = "$",
52 .root_module = lib_mod,53 .root_module = lib_mod,
53 });54 });
lib/std/Build.zig+34
...@@ -873,6 +873,7 @@ pub const SharedLibraryOptions = struct {...@@ -873,6 +873,7 @@ pub const SharedLibraryOptions = struct {
873 error_tracing: ?bool = null,873 error_tracing: ?bool = null,
874};874};
875875
876/// Deprecated: use `b.addLibrary(.{ ..., .linkage = .dynamic })` instead.
876pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile {877pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile {
877 if (options.root_module != null and options.target != null) {878 if (options.root_module != null and options.target != null) {
878 @panic("`root_module` and `target` cannot both be populated");879 @panic("`root_module` and `target` cannot both be populated");
...@@ -943,6 +944,7 @@ pub const StaticLibraryOptions = struct {...@@ -943,6 +944,7 @@ pub const StaticLibraryOptions = struct {
943 error_tracing: ?bool = null,944 error_tracing: ?bool = null,
944};945};
945946
947/// Deprecated: use `b.addLibrary(.{ ..., .linkage = .static })` instead.
946pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile {948pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile {
947 if (options.root_module != null and options.target != null) {949 if (options.root_module != null and options.target != null) {
948 @panic("`root_module` and `target` cannot both be populated");950 @panic("`root_module` and `target` cannot both be populated");
...@@ -973,6 +975,38 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile...@@ -973,6 +975,38 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile
973 });975 });
974}976}
975977
978pub const LibraryOptions = struct {
979 linkage: std.builtin.LinkMode = .static,
980 name: []const u8,
981 root_module: *Module,
982 version: ?std.SemanticVersion = null,
983 max_rss: usize = 0,
984 use_llvm: ?bool = null,
985 use_lld: ?bool = null,
986 zig_lib_dir: ?LazyPath = null,
987 /// Embed a `.manifest` file in the compilation if the object format supports it.
988 /// https://learn.microsoft.com/en-us/windows/win32/sbscs/manifest-files-reference
989 /// Manifest files must have the extension `.manifest`.
990 /// Can be set regardless of target. The `.manifest` file will be ignored
991 /// if the target object format does not support embedded manifests.
992 win32_manifest: ?LazyPath = null,
993};
994
995pub fn addLibrary(b: *Build, options: LibraryOptions) *Step.Compile {
996 return .create(b, .{
997 .name = options.name,
998 .root_module = options.root_module,
999 .kind = .lib,
1000 .linkage = options.linkage,
1001 .version = options.version,
1002 .max_rss = options.max_rss,
1003 .use_llvm = options.use_llvm,
1004 .use_lld = options.use_lld,
1005 .zig_lib_dir = options.zig_lib_dir,
1006 .win32_manifest = options.win32_manifest,
1007 });
1008}
1009
976pub const TestOptions = struct {1010pub const TestOptions = struct {
977 name: []const u8 = "test",1011 name: []const u8 = "test",
978 max_rss: usize = 0,1012 max_rss: usize = 0,
test/behavior/x86_64/build.zig+2-1
...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {
6 "Skip tests that do not match any filter",6 "Skip tests that do not match any filter",
7 ) orelse &[0][]const u8{};7 ) orelse &[0][]const u8{};
88
9 const compiler_rt_lib = b.addStaticLibrary(.{9 const compiler_rt_lib = b.addLibrary(.{
10 .linkage = .static,
10 .name = "compiler_rt",11 .name = "compiler_rt",
11 .use_llvm = false,12 .use_llvm = false,
12 .use_lld = false,13 .use_lld = false,
test/link/common_symbols/build.zig+2-1
...@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {...@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
11}11}
1212
13fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {13fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
14 const lib_a = b.addStaticLibrary(.{14 const lib_a = b.addLibrary(.{
15 .linkage = .static,
15 .name = "a",16 .name = "a",
16 .root_module = b.createModule(.{17 .root_module = b.createModule(.{
17 .root_source_file = null,18 .root_source_file = null,
test/link/common_symbols_alignment/build.zig+2-1
...@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {...@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
11}11}
1212
13fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {13fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
14 const lib_a = b.addStaticLibrary(.{14 const lib_a = b.addLibrary(.{
15 .linkage = .static,
15 .name = "a",16 .name = "a",
16 .root_module = b.createModule(.{17 .root_module = b.createModule(.{
17 .root_source_file = null,18 .root_source_file = null,
test/link/interdependent_static_c_libs/build.zig+4-2
...@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {...@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
11}11}
1212
13fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {13fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.OptimizeMode) void {
14 const lib_a = b.addStaticLibrary(.{14 const lib_a = b.addLibrary(.{
15 .linkage = .static,
15 .name = "a",16 .name = "a",
16 .root_module = b.createModule(.{17 .root_module = b.createModule(.{
17 .root_source_file = null,18 .root_source_file = null,
...@@ -22,7 +23,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize...@@ -22,7 +23,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
22 lib_a.root_module.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });23 lib_a.root_module.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });
23 lib_a.root_module.addIncludePath(b.path("."));24 lib_a.root_module.addIncludePath(b.path("."));
2425
25 const lib_b = b.addStaticLibrary(.{26 const lib_b = b.addLibrary(.{
27 .linkage = .static,
26 .name = "b",28 .name = "b",
27 .root_module = b.createModule(.{29 .root_module = b.createModule(.{
28 .root_source_file = null,30 .root_source_file = null,
test/link/link.zig+4-2
...@@ -65,7 +65,8 @@ pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Compile {...@@ -65,7 +65,8 @@ pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
65}65}
6666
67pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {67pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
68 return b.addStaticLibrary(.{68 return b.addLibrary(.{
69 .linkage = .static,
69 .name = overlay.name,70 .name = overlay.name,
70 .root_module = createModule(b, base, overlay),71 .root_module = createModule(b, base, overlay),
71 .use_llvm = base.use_llvm,72 .use_llvm = base.use_llvm,
...@@ -74,7 +75,8 @@ pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Comp...@@ -74,7 +75,8 @@ pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Comp
74}75}
7576
76pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {77pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
77 return b.addSharedLibrary(.{78 return b.addLibrary(.{
79 .linkage = .dynamic,
78 .name = overlay.name,80 .name = overlay.name,
79 .root_module = createModule(b, base, overlay),81 .root_module = createModule(b, base, overlay),
80 .use_llvm = base.use_llvm,82 .use_llvm = base.use_llvm,
test/link/static_libs_from_object_files/build.zig+8-4
...@@ -85,11 +85,13 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built...@@ -85,11 +85,13 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
85 mod.addCSourceFile(.{ .file = file, .flags = &flags });85 mod.addCSourceFile(.{ .file = file, .flags = &flags });
86 }86 }
8787
88 const lib_a = b.addStaticLibrary(.{88 const lib_a = b.addLibrary(.{
89 .linkage = .static,
89 .name = "test2_a",90 .name = "test2_a",
90 .root_module = mod_a,91 .root_module = mod_a,
91 });92 });
92 const lib_b = b.addStaticLibrary(.{93 const lib_b = b.addLibrary(.{
94 .linkage = .static,
93 .name = "test2_b",95 .name = "test2_b",
94 .root_module = mod_b,96 .root_module = mod_b,
95 });97 });
...@@ -130,11 +132,13 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built...@@ -130,11 +132,13 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
130 lib_mod.addObject(obj);132 lib_mod.addObject(obj);
131 }133 }
132134
133 const lib_a = b.addStaticLibrary(.{135 const lib_a = b.addLibrary(.{
136 .linkage = .static,
134 .name = "test3_a",137 .name = "test3_a",
135 .root_module = mod_a,138 .root_module = mod_a,
136 });139 });
137 const lib_b = b.addStaticLibrary(.{140 const lib_b = b.addLibrary(.{
141 .linkage = .static,
138 .name = "test3_b",142 .name = "test3_b",
139 .root_module = mod_b,143 .root_module = mod_b,
140 });144 });
test/src/Cases.zig+2-1
...@@ -684,7 +684,8 @@ pub fn lowerToBuildSteps(...@@ -684,7 +684,8 @@ pub fn lowerToBuildSteps(
684 .name = case.name,684 .name = case.name,
685 .root_module = mod,685 .root_module = mod,
686 }),686 }),
687 .Lib => b.addStaticLibrary(.{687 .Lib => b.addLibrary(.{
688 .linkage = .static,
688 .name = case.name,689 .name = case.name,
689 .root_module = mod,690 .root_module = mod,
690 }),691 }),
test/standalone/coff_dwarf/build.zig+2-1
...@@ -21,7 +21,8 @@ pub fn build(b: *std.Build) void {...@@ -21,7 +21,8 @@ pub fn build(b: *std.Build) void {
21 }),21 }),
22 });22 });
2323
24 const lib = b.addSharedLibrary(.{24 const lib = b.addLibrary(.{
25 .linkage = .dynamic,
25 .name = "shared_lib",26 .name = "shared_lib",
26 .root_module = b.createModule(.{27 .root_module = b.createModule(.{
27 .root_source_file = null,28 .root_source_file = null,
test/standalone/dep_duplicate_module/build.zig+2-1
...@@ -23,7 +23,8 @@ pub fn build(b: *std.Build) void {...@@ -23,7 +23,8 @@ pub fn build(b: *std.Build) void {
23 lib_mod.addImport("mod", shared_mod);23 lib_mod.addImport("mod", shared_mod);
24 exe_mod.addImport("mod", shared_mod);24 exe_mod.addImport("mod", shared_mod);
2525
26 const lib = b.addStaticLibrary(.{26 const lib = b.addLibrary(.{
27 .linkage = .static,
27 .name = "lib",28 .name = "lib",
28 .root_module = lib_mod,29 .root_module = lib_mod,
29 });30 });
test/standalone/extern/build.zig+2-1
...@@ -14,7 +14,8 @@ pub fn build(b: *std.Build) void {...@@ -14,7 +14,8 @@ pub fn build(b: *std.Build) void {
14 .optimize = optimize,14 .optimize = optimize,
15 }),15 }),
16 });16 });
17 const shared = b.addSharedLibrary(.{17 const shared = b.addLibrary(.{
18 .linkage = .dynamic,
18 .name = "shared",19 .name = "shared",
19 .root_module = b.createModule(.{20 .root_module = b.createModule(.{
20 .root_source_file = null,21 .root_source_file = null,
test/standalone/global_linkage/build.zig+4-2
...@@ -7,7 +7,8 @@ pub fn build(b: *std.Build) void {...@@ -7,7 +7,8 @@ pub fn build(b: *std.Build) void {
7 const optimize: std.builtin.OptimizeMode = .Debug;7 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target = b.graph.host;8 const target = b.graph.host;
99
10 const obj1 = b.addStaticLibrary(.{10 const obj1 = b.addLibrary(.{
11 .linkage = .static,
11 .name = "obj1",12 .name = "obj1",
12 .root_module = b.createModule(.{13 .root_module = b.createModule(.{
13 .root_source_file = b.path("obj1.zig"),14 .root_source_file = b.path("obj1.zig"),
...@@ -16,7 +17,8 @@ pub fn build(b: *std.Build) void {...@@ -16,7 +17,8 @@ pub fn build(b: *std.Build) void {
16 }),17 }),
17 });18 });
1819
19 const obj2 = b.addStaticLibrary(.{20 const obj2 = b.addLibrary(.{
21 .linkage = .static,
20 .name = "obj2",22 .name = "obj2",
21 .root_module = b.createModule(.{23 .root_module = b.createModule(.{
22 .root_source_file = b.path("obj2.zig"),24 .root_source_file = b.path("obj2.zig"),
test/standalone/install_headers/build.zig+4-2
...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {
66
7 const empty_c = b.addWriteFiles().add("empty.c", "");7 const empty_c = b.addWriteFiles().add("empty.c", "");
88
9 const libfoo = b.addStaticLibrary(.{9 const libfoo = b.addLibrary(.{
10 .linkage = .static,
10 .name = "foo",11 .name = "foo",
11 .root_module = b.createModule(.{12 .root_module = b.createModule(.{
12 .root_source_file = null,13 .root_source_file = null,
...@@ -61,7 +62,8 @@ pub fn build(b: *std.Build) void {...@@ -61,7 +62,8 @@ pub fn build(b: *std.Build) void {
61 .FOO_CONFIG_2 = "2",62 .FOO_CONFIG_2 = "2",
62 }));63 }));
6364
64 const libbar = b.addStaticLibrary(.{65 const libbar = b.addLibrary(.{
66 .linkage = .static,
65 .name = "bar",67 .name = "bar",
66 .root_module = b.createModule(.{68 .root_module = b.createModule(.{
67 .root_source_file = null,69 .root_source_file = null,
test/standalone/load_dynamic_library/build.zig+2-1
...@@ -10,7 +10,8 @@ pub fn build(b: *std.Build) void {...@@ -10,7 +10,8 @@ pub fn build(b: *std.Build) void {
1010
11 if (builtin.os.tag == .wasi) return;11 if (builtin.os.tag == .wasi) return;
1212
13 const lib = b.addSharedLibrary(.{13 const lib = b.addLibrary(.{
14 .linkage = .dynamic,
14 .name = "add",15 .name = "add",
15 .version = .{ .major = 1, .minor = 0, .patch = 0 },16 .version = .{ .major = 1, .minor = 0, .patch = 0 },
16 .root_module = b.createModule(.{17 .root_module = b.createModule(.{
test/standalone/shared_library/build.zig+2-1
...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {
66
7 const optimize: std.builtin.OptimizeMode = .Debug;7 const optimize: std.builtin.OptimizeMode = .Debug;
8 const target = b.graph.host;8 const target = b.graph.host;
9 const lib = b.addSharedLibrary(.{9 const lib = b.addLibrary(.{
10 .linkage = .dynamic,
10 .name = "mathtest",11 .name = "mathtest",
11 .version = .{ .major = 1, .minor = 0, .patch = 0 },12 .version = .{ .major = 1, .minor = 0, .patch = 0 },
12 .root_module = b.createModule(.{13 .root_module = b.createModule(.{
test/standalone/stack_iterator/build.zig+2-1
...@@ -68,7 +68,8 @@ pub fn build(b: *std.Build) void {...@@ -68,7 +68,8 @@ pub fn build(b: *std.Build) void {
68 // - x86_64: STACK_IMMD, STACK_IND68 // - x86_64: STACK_IMMD, STACK_IND
69 // - aarch64: FRAMELESS, DWARF69 // - aarch64: FRAMELESS, DWARF
70 {70 {
71 const c_shared_lib = b.addSharedLibrary(.{71 const c_shared_lib = b.addLibrary(.{
72 .linkage = .dynamic,
72 .name = "c_shared_lib",73 .name = "c_shared_lib",
73 .root_module = b.createModule(.{74 .root_module = b.createModule(.{
74 .root_source_file = null,75 .root_source_file = null,
test/standalone/static_c_lib/build.zig+2-1
...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {...@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {
66
7 const optimize: std.builtin.OptimizeMode = .Debug;7 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const foo = b.addStaticLibrary(.{9 const foo = b.addLibrary(.{
10 .linkage = .static,
10 .name = "foo",11 .name = "foo",
11 .root_module = b.createModule(.{12 .root_module = b.createModule(.{
12 .root_source_file = null,13 .root_source_file = null,
test/standalone/windows_argv/build.zig+4-2
...@@ -9,7 +9,8 @@ pub fn build(b: *std.Build) !void {...@@ -9,7 +9,8 @@ pub fn build(b: *std.Build) !void {
99
10 const optimize: std.builtin.OptimizeMode = .Debug;10 const optimize: std.builtin.OptimizeMode = .Debug;
1111
12 const lib_gnu = b.addStaticLibrary(.{12 const lib_gnu = b.addLibrary(.{
13 .linkage = .static,
13 .name = "toargv-gnu",14 .name = "toargv-gnu",
14 .root_module = b.createModule(.{15 .root_module = b.createModule(.{
15 .root_source_file = b.path("lib.zig"),16 .root_source_file = b.path("lib.zig"),
...@@ -74,7 +75,8 @@ pub fn build(b: *std.Build) !void {...@@ -74,7 +75,8 @@ pub fn build(b: *std.Build) !void {
74 break :has_msvc true;75 break :has_msvc true;
75 };76 };
76 if (has_msvc) {77 if (has_msvc) {
77 const lib_msvc = b.addStaticLibrary(.{78 const lib_msvc = b.addLibrary(.{
79 .linkage = .static,
78 .name = "toargv-msvc",80 .name = "toargv-msvc",
79 .root_module = b.createModule(.{81 .root_module = b.createModule(.{
80 .root_source_file = b.path("lib.zig"),82 .root_source_file = b.path("lib.zig"),