Skip to content

Commit 01b8687

Browse files
committed
Add dynamic lib + single threaded option
1 parent 6c72830 commit 01b8687

File tree

1 file changed

+43
-9
lines changed

1 file changed

+43
-9
lines changed

build.zig

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4-
const upstream = b.dependency("zlib", .{});
5-
const lib = b.addStaticLibrary(.{
6-
.name = "z",
7-
.target = b.standardTargetOptions(.{}),
8-
.optimize = b.standardOptimizeOption(.{}),
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");
7+
8+
const upstream = b.dependency("zlib", .{
9+
.target = target,
10+
.optimize = optimize,
11+
});
12+
13+
const lib_mod = b.createModule(.{
14+
.target = target,
15+
.optimize = optimize,
16+
.link_libc = true,
17+
.single_threaded = single_threaded,
918
});
10-
lib.linkLibC();
11-
lib.addCSourceFiles(.{
12-
.root = upstream.path(""),
13-
.files = &.{
19+
lib_mod.addCSourceFiles(.{
20+
.root = upstream.path("."),
21+
.files = &[_][]const u8{
1422
"adler32.c",
1523
"crc32.c",
1624
"deflate.c",
@@ -34,11 +42,37 @@ pub fn build(b: *std.Build) void {
3442
"-DZ_HAVE_UNISTD_H",
3543
},
3644
});
45+
46+
const lib = b.addLibrary(.{
47+
.linkage = .static,
48+
.name = "z",
49+
.root_module = lib_mod,
50+
});
3751
lib.installHeadersDirectory(upstream.path(""), "", .{
3852
.include_extensions = &.{
3953
"zconf.h",
4054
"zlib.h",
4155
},
4256
});
4357
b.installArtifact(lib);
58+
59+
const dynamic_lib = b.addLibrary(.{
60+
.linkage = .dynamic,
61+
.name = "z",
62+
.root_module = lib_mod,
63+
});
64+
// Install with no version extension
65+
b.installArtifact(dynamic_lib);
66+
// Install with version extension
67+
const output_name = b.fmt("libz{s}.{s}", .{
68+
dynamic_lib.root_module.resolved_target.?.result.dynamicLibSuffix(),
69+
"1.3.1",
70+
});
71+
const install_step = b.addInstallArtifact(dynamic_lib, .{
72+
.dest_dir = .{
73+
.override = .lib,
74+
},
75+
.dest_sub_path = output_name,
76+
});
77+
b.getInstallStep().dependOn(&install_step.step);
4478
}

0 commit comments

Comments
 (0)