| author | |
| committer | |
| log | 941677e08318c2baaabc9d0fc87892d1b63487ae |
| tree | 3a6682be6f5063e6d7a27bd90d50a9cf729d5937 |
| parent | 28a259d4a34e544090a76863802e600a4e01fcc2 |
| signature |
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 | 47 | // Now, we will create a static library based on the module we created above. |
| 48 | 48 | // This creates a `std.Build.Step.Compile`, which is the build step responsible |
| 49 | 49 | // for actually invoking the compiler. |
| 50 | const lib = b.addStaticLibrary(.{ | |
| 50 | const lib = b.addLibrary(.{ | |
| 51 | .linkage = .static, | |
| 51 | 52 | .name = "$", |
| 52 | 53 | .root_module = lib_mod, |
| 53 | 54 | }); |
lib/std/Build.zig+34| ... | ... | @@ -873,6 +873,7 @@ pub const SharedLibraryOptions = struct { |
| 873 | 873 | error_tracing: ?bool = null, |
| 874 | 874 | }; |
| 875 | 875 | |
| 876 | /// Deprecated: use `b.addLibrary(.{ ..., .linkage = .dynamic })` instead. | |
| 876 | 877 | pub fn addSharedLibrary(b: *Build, options: SharedLibraryOptions) *Step.Compile { |
| 877 | 878 | if (options.root_module != null and options.target != null) { |
| 878 | 879 | @panic("`root_module` and `target` cannot both be populated"); |
| ... | ... | @@ -943,6 +944,7 @@ pub const StaticLibraryOptions = struct { |
| 943 | 944 | error_tracing: ?bool = null, |
| 944 | 945 | }; |
| 945 | 946 | |
| 947 | /// Deprecated: use `b.addLibrary(.{ ..., .linkage = .static })` instead. | |
| 946 | 948 | pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile { |
| 947 | 949 | if (options.root_module != null and options.target != null) { |
| 948 | 950 | @panic("`root_module` and `target` cannot both be populated"); |
| ... | ... | @@ -973,6 +975,38 @@ pub fn addStaticLibrary(b: *Build, options: StaticLibraryOptions) *Step.Compile |
| 973 | 975 | }); |
| 974 | 976 | } |
| 975 | 977 | |
| 978 | pub 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 | ||
| 995 | pub 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 | ||
| 976 | 1010 | pub const TestOptions = struct { |
| 977 | 1011 | name: []const u8 = "test", |
| 978 | 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 | 6 | "Skip tests that do not match any filter", |
| 7 | 7 | ) orelse &[0][]const u8{}; |
| 8 | 8 | |
| 9 | const compiler_rt_lib = b.addStaticLibrary(.{ | |
| 9 | const compiler_rt_lib = b.addLibrary(.{ | |
| 10 | .linkage = .static, | |
| 10 | 11 | .name = "compiler_rt", |
| 11 | 12 | .use_llvm = false, |
| 12 | 13 | .use_lld = false, |
test/link/common_symbols/build.zig+2-1| ... | ... | @@ -11,7 +11,8 @@ pub fn build(b: *std.Build) void { |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | fn 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 | 16 | .name = "a", |
| 16 | 17 | .root_module = b.createModule(.{ |
| 17 | 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 | 11 | } |
| 12 | 12 | |
| 13 | 13 | fn 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 | 16 | .name = "a", |
| 16 | 17 | .root_module = b.createModule(.{ |
| 17 | 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 | 11 | } |
| 12 | 12 | |
| 13 | 13 | fn 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 | 16 | .name = "a", |
| 16 | 17 | .root_module = b.createModule(.{ |
| 17 | 18 | .root_source_file = null, |
| ... | ... | @@ -22,7 +23,8 @@ fn add(b: *std.Build, test_step: *std.Build.Step, optimize: std.builtin.Optimize |
| 22 | 23 | lib_a.root_module.addCSourceFile(.{ .file = b.path("a.c"), .flags = &[_][]const u8{} }); |
| 23 | 24 | lib_a.root_module.addIncludePath(b.path(".")); |
| 24 | 25 | |
| 25 | const lib_b = b.addStaticLibrary(.{ | |
| 26 | const lib_b = b.addLibrary(.{ | |
| 27 | .linkage = .static, | |
| 26 | 28 | .name = "b", |
| 27 | 29 | .root_module = b.createModule(.{ |
| 28 | 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 | 65 | } |
| 66 | 66 | |
| 67 | 67 | pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile { |
| 68 | return b.addStaticLibrary(.{ | |
| 68 | return b.addLibrary(.{ | |
| 69 | .linkage = .static, | |
| 69 | 70 | .name = overlay.name, |
| 70 | 71 | .root_module = createModule(b, base, overlay), |
| 71 | 72 | .use_llvm = base.use_llvm, |
| ... | ... | @@ -74,7 +75,8 @@ pub fn addStaticLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Comp |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | pub fn addSharedLibrary(b: *Build, base: Options, overlay: OverlayOptions) *Compile { |
| 77 | return b.addSharedLibrary(.{ | |
| 78 | return b.addLibrary(.{ | |
| 79 | .linkage = .dynamic, | |
| 78 | 80 | .name = overlay.name, |
| 79 | 81 | .root_module = createModule(b, base, overlay), |
| 80 | 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 | 85 | mod.addCSourceFile(.{ .file = file, .flags = &flags }); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | const lib_a = b.addStaticLibrary(.{ | |
| 88 | const lib_a = b.addLibrary(.{ | |
| 89 | .linkage = .static, | |
| 89 | 90 | .name = "test2_a", |
| 90 | 91 | .root_module = mod_a, |
| 91 | 92 | }); |
| 92 | const lib_b = b.addStaticLibrary(.{ | |
| 93 | const lib_b = b.addLibrary(.{ | |
| 94 | .linkage = .static, | |
| 93 | 95 | .name = "test2_b", |
| 94 | 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 | 132 | lib_mod.addObject(obj); |
| 131 | 133 | } |
| 132 | 134 | |
| 133 | const lib_a = b.addStaticLibrary(.{ | |
| 135 | const lib_a = b.addLibrary(.{ | |
| 136 | .linkage = .static, | |
| 134 | 137 | .name = "test3_a", |
| 135 | 138 | .root_module = mod_a, |
| 136 | 139 | }); |
| 137 | const lib_b = b.addStaticLibrary(.{ | |
| 140 | const lib_b = b.addLibrary(.{ | |
| 141 | .linkage = .static, | |
| 138 | 142 | .name = "test3_b", |
| 139 | 143 | .root_module = mod_b, |
| 140 | 144 | }); |
test/src/Cases.zig+2-1| ... | ... | @@ -684,7 +684,8 @@ pub fn lowerToBuildSteps( |
| 684 | 684 | .name = case.name, |
| 685 | 685 | .root_module = mod, |
| 686 | 686 | }), |
| 687 | .Lib => b.addStaticLibrary(.{ | |
| 687 | .Lib => b.addLibrary(.{ | |
| 688 | .linkage = .static, | |
| 688 | 689 | .name = case.name, |
| 689 | 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 | 21 | }), |
| 22 | 22 | }); |
| 23 | 23 | |
| 24 | const lib = b.addSharedLibrary(.{ | |
| 24 | const lib = b.addLibrary(.{ | |
| 25 | .linkage = .dynamic, | |
| 25 | 26 | .name = "shared_lib", |
| 26 | 27 | .root_module = b.createModule(.{ |
| 27 | 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 | 23 | lib_mod.addImport("mod", shared_mod); |
| 24 | 24 | exe_mod.addImport("mod", shared_mod); |
| 25 | 25 | |
| 26 | const lib = b.addStaticLibrary(.{ | |
| 26 | const lib = b.addLibrary(.{ | |
| 27 | .linkage = .static, | |
| 27 | 28 | .name = "lib", |
| 28 | 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 | 14 | .optimize = optimize, |
| 15 | 15 | }), |
| 16 | 16 | }); |
| 17 | const shared = b.addSharedLibrary(.{ | |
| 17 | const shared = b.addLibrary(.{ | |
| 18 | .linkage = .dynamic, | |
| 18 | 19 | .name = "shared", |
| 19 | 20 | .root_module = b.createModule(.{ |
| 20 | 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 | const optimize: std.builtin.OptimizeMode = .Debug; |
| 8 | 8 | const target = b.graph.host; |
| 9 | 9 | |
| 10 | const obj1 = b.addStaticLibrary(.{ | |
| 10 | const obj1 = b.addLibrary(.{ | |
| 11 | .linkage = .static, | |
| 11 | 12 | .name = "obj1", |
| 12 | 13 | .root_module = b.createModule(.{ |
| 13 | 14 | .root_source_file = b.path("obj1.zig"), |
| ... | ... | @@ -16,7 +17,8 @@ pub fn build(b: *std.Build) void { |
| 16 | 17 | }), |
| 17 | 18 | }); |
| 18 | 19 | |
| 19 | const obj2 = b.addStaticLibrary(.{ | |
| 20 | const obj2 = b.addLibrary(.{ | |
| 21 | .linkage = .static, | |
| 20 | 22 | .name = "obj2", |
| 21 | 23 | .root_module = b.createModule(.{ |
| 22 | 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 | 6 | |
| 7 | 7 | const empty_c = b.addWriteFiles().add("empty.c", ""); |
| 8 | 8 | |
| 9 | const libfoo = b.addStaticLibrary(.{ | |
| 9 | const libfoo = b.addLibrary(.{ | |
| 10 | .linkage = .static, | |
| 10 | 11 | .name = "foo", |
| 11 | 12 | .root_module = b.createModule(.{ |
| 12 | 13 | .root_source_file = null, |
| ... | ... | @@ -61,7 +62,8 @@ pub fn build(b: *std.Build) void { |
| 61 | 62 | .FOO_CONFIG_2 = "2", |
| 62 | 63 | })); |
| 63 | 64 | |
| 64 | const libbar = b.addStaticLibrary(.{ | |
| 65 | const libbar = b.addLibrary(.{ | |
| 66 | .linkage = .static, | |
| 65 | 67 | .name = "bar", |
| 66 | 68 | .root_module = b.createModule(.{ |
| 67 | 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 | 10 | |
| 11 | 11 | if (builtin.os.tag == .wasi) return; |
| 12 | 12 | |
| 13 | const lib = b.addSharedLibrary(.{ | |
| 13 | const lib = b.addLibrary(.{ | |
| 14 | .linkage = .dynamic, | |
| 14 | 15 | .name = "add", |
| 15 | 16 | .version = .{ .major = 1, .minor = 0, .patch = 0 }, |
| 16 | 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 | 6 | |
| 7 | 7 | const optimize: std.builtin.OptimizeMode = .Debug; |
| 8 | 8 | const target = b.graph.host; |
| 9 | const lib = b.addSharedLibrary(.{ | |
| 9 | const lib = b.addLibrary(.{ | |
| 10 | .linkage = .dynamic, | |
| 10 | 11 | .name = "mathtest", |
| 11 | 12 | .version = .{ .major = 1, .minor = 0, .patch = 0 }, |
| 12 | 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 | 68 | // - x86_64: STACK_IMMD, STACK_IND |
| 69 | 69 | // - aarch64: FRAMELESS, DWARF |
| 70 | 70 | { |
| 71 | const c_shared_lib = b.addSharedLibrary(.{ | |
| 71 | const c_shared_lib = b.addLibrary(.{ | |
| 72 | .linkage = .dynamic, | |
| 72 | 73 | .name = "c_shared_lib", |
| 73 | 74 | .root_module = b.createModule(.{ |
| 74 | 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 | 6 | |
| 7 | 7 | const optimize: std.builtin.OptimizeMode = .Debug; |
| 8 | 8 | |
| 9 | const foo = b.addStaticLibrary(.{ | |
| 9 | const foo = b.addLibrary(.{ | |
| 10 | .linkage = .static, | |
| 10 | 11 | .name = "foo", |
| 11 | 12 | .root_module = b.createModule(.{ |
| 12 | 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 | 9 | |
| 10 | 10 | const optimize: std.builtin.OptimizeMode = .Debug; |
| 11 | 11 | |
| 12 | const lib_gnu = b.addStaticLibrary(.{ | |
| 12 | const lib_gnu = b.addLibrary(.{ | |
| 13 | .linkage = .static, | |
| 13 | 14 | .name = "toargv-gnu", |
| 14 | 15 | .root_module = b.createModule(.{ |
| 15 | 16 | .root_source_file = b.path("lib.zig"), |
| ... | ... | @@ -74,7 +75,8 @@ pub fn build(b: *std.Build) !void { |
| 74 | 75 | break :has_msvc true; |
| 75 | 76 | }; |
| 76 | 77 | if (has_msvc) { |
| 77 | const lib_msvc = b.addStaticLibrary(.{ | |
| 78 | const lib_msvc = b.addLibrary(.{ | |
| 79 | .linkage = .static, | |
| 78 | 80 | .name = "toargv-msvc", |
| 79 | 81 | .root_module = b.createModule(.{ |
| 80 | 82 | .root_source_file = b.path("lib.zig"), |