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 {
4747 // Now, we will create a static library based on the module we created above.
4848 // This creates a `std.Build.Step.Compile`, which is the build step responsible
4949 // for actually invoking the compiler.
50 const lib = b.addStaticLibrary(.{
50 const lib = b.addLibrary(.{
51 .linkage = .static,
5152 .name = "$",
5253 .root_module = lib_mod,
5354 });
lib/std/Build.zig+34
......@@ -873,6 +873,7 @@ pub const SharedLibraryOptions = struct {
873873 error_tracing: ?bool = null,
874874};
875875
876/// Deprecated: use `b.addLibrary(.{ ..., .linkage = .dynamic })` instead.
876877pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile {
877878 if (options.root_module != null and options.target != null) {
878879 @panic("`root_module` and `target` cannot both be populated");
......@@ -943,6 +944,7 @@ pub const StaticLibraryOptions = struct {
943944 error_tracing: ?bool = null,
944945};
945946
947/// Deprecated: use `b.addLibrary(.{ ..., .linkage = .static })` instead.
946948pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile {
947949 if (options.root_module != null and options.target != null) {
948950 @panic("`root_module` and `target` cannot both be populated");
......@@ -973,6 +975,38 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile
973975 });
974976}
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
9761010pub const TestOptions = struct {
9771011 name: []const u8 = "test",
9781012 max_rss: usize = 0,
test/behavior/x86_64/build.zig+2-1
......@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {
66 "Skip tests that do not match any filter",
77 ) orelse &[0][]const u8{};
88
9 const compiler_rt_lib = b.addStaticLibrary(.{
9 const compiler_rt_lib = b.addLibrary(.{
10 .linkage = .static,
1011 .name = "compiler_rt",
1112 .use_llvm = false,
1213 .use_lld = false,
test/link/common_symbols/build.zig+2-1
......@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
1111}
1212
1313fn 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,
1516 .name = "a",
1617 .root_module = b.createModule(.{
1718 .root_source_file = null,
test/link/common_symbols_alignment/build.zig+2-1
......@@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void {
1111}
1212
1313fn 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,
1516 .name = "a",
1617 .root_module = b.createModule(.{
1718 .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 {
1111}
1212
1313fn 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,
1516 .name = "a",
1617 .root_module = b.createModule(.{
1718 .root_source_file = null,
......@@ -22,7 +23,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize
2223 lib_a.root_module.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} });
2324 lib_a.root_module.addIncludePath(b.path("."));
2425
25 const lib_b = b.addStaticLibrary(.{
26 const lib_b = b.addLibrary(.{
27 .linkage = .static,
2628 .name = "b",
2729 .root_module = b.createModule(.{
2830 .root_source_file = null,
test/link/link.zig+4-2
......@@ -65,7 +65,8 @@ pub fn addObject(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
6565}
6666
6767pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
68 return b.addStaticLibrary(.{
68 return b.addLibrary(.{
69 .linkage = .static,
6970 .name = overlay.name,
7071 .root_module = createModule(b, base, overlay),
7172 .use_llvm = base.use_llvm,
......@@ -74,7 +75,8 @@ pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Comp
7475}
7576
7677pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile {
77 return b.addSharedLibrary(.{
78 return b.addLibrary(.{
79 .linkage = .dynamic,
7880 .name = overlay.name,
7981 .root_module = createModule(b, base, overlay),
8082 .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
8585 mod.addCSourceFile(.{ .file = file, .flags = &flags });
8686 }
8787
88 const lib_a = b.addStaticLibrary(.{
88 const lib_a = b.addLibrary(.{
89 .linkage = .static,
8990 .name = "test2_a",
9091 .root_module = mod_a,
9192 });
92 const lib_b = b.addStaticLibrary(.{
93 const lib_b = b.addLibrary(.{
94 .linkage = .static,
9395 .name = "test2_b",
9496 .root_module = mod_b,
9597 });
......@@ -130,11 +132,13 @@ fn add(b: *Build, test_step: *Step, files: []const LazyPath, optimize: std.built
130132 lib_mod.addObject(obj);
131133 }
132134
133 const lib_a = b.addStaticLibrary(.{
135 const lib_a = b.addLibrary(.{
136 .linkage = .static,
134137 .name = "test3_a",
135138 .root_module = mod_a,
136139 });
137 const lib_b = b.addStaticLibrary(.{
140 const lib_b = b.addLibrary(.{
141 .linkage = .static,
138142 .name = "test3_b",
139143 .root_module = mod_b,
140144 });
test/src/Cases.zig+2-1
......@@ -684,7 +684,8 @@ pub fn lowerToBuildSteps(
684684 .name = case.name,
685685 .root_module = mod,
686686 }),
687 .Lib => b.addStaticLibrary(.{
687 .Lib => b.addLibrary(.{
688 .linkage = .static,
688689 .name = case.name,
689690 .root_module = mod,
690691 }),
test/standalone/coff_dwarf/build.zig+2-1
......@@ -21,7 +21,8 @@ pub fn build(b: *std.Build) void {
2121 }),
2222 });
2323
24 const lib = b.addSharedLibrary(.{
24 const lib = b.addLibrary(.{
25 .linkage = .dynamic,
2526 .name = "shared_lib",
2627 .root_module = b.createModule(.{
2728 .root_source_file = null,
test/standalone/dep_duplicate_module/build.zig+2-1
......@@ -23,7 +23,8 @@ pub fn build(b: *std.Build) void {
2323 lib_mod.addImport("mod", shared_mod);
2424 exe_mod.addImport("mod", shared_mod);
2525
26 const lib = b.addStaticLibrary(.{
26 const lib = b.addLibrary(.{
27 .linkage = .static,
2728 .name = "lib",
2829 .root_module = lib_mod,
2930 });
test/standalone/extern/build.zig+2-1
......@@ -14,7 +14,8 @@ pub fn build(b: *std.Build) void {
1414 .optimize = optimize,
1515 }),
1616 });
17 const shared = b.addSharedLibrary(.{
17 const shared = b.addLibrary(.{
18 .linkage = .dynamic,
1819 .name = "shared",
1920 .root_module = b.createModule(.{
2021 .root_source_file = null,
test/standalone/global_linkage/build.zig+4-2
......@@ -7,7 +7,8 @@ pub fn build(b: *std.Build) void {
77 const optimize: std.builtin.OptimizeMode = .Debug;
88 const target = b.graph.host;
99
10 const obj1 = b.addStaticLibrary(.{
10 const obj1 = b.addLibrary(.{
11 .linkage = .static,
1112 .name = "obj1",
1213 .root_module = b.createModule(.{
1314 .root_source_file = b.path("obj1.zig"),
......@@ -16,7 +17,8 @@ pub fn build(b: *std.Build) void {
1617 }),
1718 });
1819
19 const obj2 = b.addStaticLibrary(.{
20 const obj2 = b.addLibrary(.{
21 .linkage = .static,
2022 .name = "obj2",
2123 .root_module = b.createModule(.{
2224 .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 {
66
77 const empty_c = b.addWriteFiles().add("empty.c", "");
88
9 const libfoo = b.addStaticLibrary(.{
9 const libfoo = b.addLibrary(.{
10 .linkage = .static,
1011 .name = "foo",
1112 .root_module = b.createModule(.{
1213 .root_source_file = null,
......@@ -61,7 +62,8 @@ pub fn build(b: *std.Build) void {
6162 .FOO_CONFIG_2 = "2",
6263 }));
6364
64 const libbar = b.addStaticLibrary(.{
65 const libbar = b.addLibrary(.{
66 .linkage = .static,
6567 .name = "bar",
6668 .root_module = b.createModule(.{
6769 .root_source_file = null,
test/standalone/load_dynamic_library/build.zig+2-1
......@@ -10,7 +10,8 @@ pub fn build(b: *std.Build) void {
1010
1111 if (builtin.os.tag == .wasi) return;
1212
13 const lib = b.addSharedLibrary(.{
13 const lib = b.addLibrary(.{
14 .linkage = .dynamic,
1415 .name = "add",
1516 .version = .{ .major = 1, .minor = 0, .patch = 0 },
1617 .root_module = b.createModule(.{
test/standalone/shared_library/build.zig+2-1
......@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88 const target = b.graph.host;
9 const lib = b.addSharedLibrary(.{
9 const lib = b.addLibrary(.{
10 .linkage = .dynamic,
1011 .name = "mathtest",
1112 .version = .{ .major = 1, .minor = 0, .patch = 0 },
1213 .root_module = b.createModule(.{
test/standalone/stack_iterator/build.zig+2-1
......@@ -68,7 +68,8 @@ pub fn build(b: *std.Build) void {
6868 // - x86_64: STACK_IMMD, STACK_IND
6969 // - aarch64: FRAMELESS, DWARF
7070 {
71 const c_shared_lib = b.addSharedLibrary(.{
71 const c_shared_lib = b.addLibrary(.{
72 .linkage = .dynamic,
7273 .name = "c_shared_lib",
7374 .root_module = b.createModule(.{
7475 .root_source_file = null,
test/standalone/static_c_lib/build.zig+2-1
......@@ -6,7 +6,8 @@ pub fn build(b: *std.Build) void {
66
77 const optimize: std.builtin.OptimizeMode = .Debug;
88
9 const foo = b.addStaticLibrary(.{
9 const foo = b.addLibrary(.{
10 .linkage = .static,
1011 .name = "foo",
1112 .root_module = b.createModule(.{
1213 .root_source_file = null,
test/standalone/windows_argv/build.zig+4-2
......@@ -9,7 +9,8 @@ pub fn build(b: *std.Build) !void {
99
1010 const optimize: std.builtin.OptimizeMode = .Debug;
1111
12 const lib_gnu = b.addStaticLibrary(.{
12 const lib_gnu = b.addLibrary(.{
13 .linkage = .static,
1314 .name = "toargv-gnu",
1415 .root_module = b.createModule(.{
1516 .root_source_file = b.path("lib.zig"),
......@@ -74,7 +75,8 @@ pub fn build(b: *std.Build) !void {
7475 break :has_msvc true;
7576 };
7677 if (has_msvc) {
77 const lib_msvc = b.addStaticLibrary(.{
78 const lib_msvc = b.addLibrary(.{
79 .linkage = .static,
7880 .name = "toargv-msvc",
7981 .root_module = b.createModule(.{
8082 .root_source_file = b.path("lib.zig"),