Skip to content

Commit 2f11e37

Browse files
committed
Port zstd library to zig build system
1 parent bc04db8 commit 2f11e37

File tree

2 files changed

+153
-5
lines changed

2 files changed

+153
-5
lines changed

build.zig

Lines changed: 147 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,124 @@
11
const std = @import("std");
22

3+
const public_path = "lib/";
4+
const public_headers = [_][]const u8{
5+
"zdict.h",
6+
"zstd.h",
7+
"zstd_errors.h",
8+
};
9+
10+
const common_headers = [_][]const u8{
11+
"lib/common/allocations.h",
12+
"lib/common/bits.h",
13+
"lib/common/bitstream.h",
14+
"lib/common/compiler.h",
15+
"lib/common/cpu.h",
16+
"lib/common/debug.h",
17+
"lib/common/error_private.h",
18+
"lib/common/fse.h",
19+
"lib/common/huf.h",
20+
"lib/common/mem.h",
21+
"lib/common/pool.h",
22+
"lib/common/portability_macros.h",
23+
"lib/common/threading.h",
24+
"lib/common/xxhash.h",
25+
"lib/common/zstd_deps.h",
26+
"lib/common/zstd_internal.h",
27+
"lib/common/zstd_trace.h",
28+
};
29+
const common_sources = [_][]const u8{
30+
"lib/common/debug.c",
31+
"lib/common/entropy_common.c",
32+
"lib/common/error_private.c",
33+
"lib/common/fse_decompress.c",
34+
"lib/common/pool.c",
35+
"lib/common/threading.c",
36+
"lib/common/xxhash.c",
37+
"lib/common/zstd_common.c",
38+
};
39+
40+
const compress_headers = [_][]const u8{
41+
"lib/compress/clevels.h",
42+
"lib/compress/hist.h",
43+
"lib/compress/zstd_compress_internal.h",
44+
"lib/compress/zstd_compress_literals.h",
45+
"lib/compress/zstd_compress_sequences.h",
46+
"lib/compress/zstd_compress_superblock.h",
47+
"lib/compress/zstd_cwksp.h",
48+
"lib/compress/zstd_double_fast.h",
49+
"lib/compress/zstd_fast.h",
50+
"lib/compress/zstd_lazy.h",
51+
"lib/compress/zstd_ldm.h",
52+
"lib/compress/zstd_ldm_geartab.h",
53+
"lib/compress/zstd_opt.h",
54+
"lib/compress/zstd_preSplit.h",
55+
"lib/compress/zstdmt_compress.h",
56+
};
57+
const compress_sources = [_][]const u8{
58+
"lib/compress/fse_compress.c",
59+
"lib/compress/hist.c",
60+
"lib/compress/huf_compress.c",
61+
"lib/compress/zstd_compress.c",
62+
"lib/compress/zstd_compress_literals.c",
63+
"lib/compress/zstd_compress_sequences.c",
64+
"lib/compress/zstd_compress_superblock.c",
65+
"lib/compress/zstd_double_fast.c",
66+
"lib/compress/zstd_fast.c",
67+
"lib/compress/zstd_lazy.c",
68+
"lib/compress/zstd_ldm.c",
69+
"lib/compress/zstd_opt.c",
70+
"lib/compress/zstd_preSplit.c",
71+
"lib/compress/zstdmt_compress.c",
72+
};
73+
74+
const decompress_headers = [_][]const u8{
75+
"lib/decompress/zstd_ddict.h",
76+
"lib/decompress/zstd_decompress_block.h",
77+
"lib/decompress/zstd_decompress_internal.h",
78+
};
79+
const decompress_sources = [_][]const u8{
80+
"lib/decompress/huf_decompress.c",
81+
"lib/decompress/zstd_ddict.c",
82+
"lib/decompress/zstd_decompress.c",
83+
"lib/decompress/zstd_decompress_block.c",
84+
};
85+
const decompress_asm = [_][]const u8{
86+
"lib/decompress/huf_decompress_amd64.S",
87+
};
88+
89+
const dictbuilder_headers = [_][]const u8{
90+
"lib/dictBuilder/cover.h",
91+
"lib/dictBuilder/divsufsort.h",
92+
};
93+
const dictbuilder_sources = [_][]const u8{
94+
"lib/dictBuilder/cover.c",
95+
"lib/dictBuilder/divsufsort.c",
96+
"lib/dictBuilder/fastcover.c",
97+
"lib/dictBuilder/zdict.c",
98+
};
99+
100+
const legacy_headers = [_][]const u8{
101+
"lib/legacy/zstd_legacy.h",
102+
"lib/legacy/zstd_v01.h",
103+
"lib/legacy/zstd_v02.h",
104+
"lib/legacy/zstd_v03.h",
105+
"lib/legacy/zstd_v04.h",
106+
"lib/legacy/zstd_v05.h",
107+
"lib/legacy/zstd_v06.h",
108+
"lib/legacy/zstd_v07.h",
109+
};
110+
const legacy_sources = [_][]const u8{
111+
"lib/legacy/zstd_v01.c",
112+
"lib/legacy/zstd_v02.c",
113+
"lib/legacy/zstd_v03.c",
114+
"lib/legacy/zstd_v04.c",
115+
"lib/legacy/zstd_v05.c",
116+
"lib/legacy/zstd_v06.c",
117+
"lib/legacy/zstd_v07.c",
118+
};
119+
120+
const flags = [_][]const u8{};
121+
3122
pub fn build(b: *std.Build) void {
4123
const target = b.standardTargetOptions(.{});
5124
const optimize = b.standardOptimizeOption(.{});
@@ -12,16 +131,41 @@ pub fn build(b: *std.Build) void {
12131
.optimize = optimize,
13132
});
14133

15-
_ = mod; // stub
16-
_ = upstream; // stub
134+
const lib = b.addLibrary(.{
135+
.name = "zstd",
136+
.linkage = .static,
137+
.root_module = mod,
138+
});
139+
lib.installHeadersDirectory(upstream.path(public_path), "", .{
140+
.include_extensions = &public_headers,
141+
});
142+
lib.addCSourceFiles(.{ .root = upstream.path(""), .files = &common_sources });
143+
lib.addCSourceFiles(.{ .root = upstream.path(""), .files = &compress_sources });
144+
lib.addCSourceFiles(.{ .root = upstream.path(""), .files = &decompress_sources });
145+
lib.addCSourceFiles(.{ .root = upstream.path(""), .files = &dictbuilder_sources });
146+
lib.addCSourceFiles(.{ .root = upstream.path(""), .files = &legacy_sources });
147+
148+
lib.root_module.addCMacro("XXH_NAMESPACE", "ZSTD_");
149+
lib.root_module.addCMacro("ZSTD_MULTITHREAD", "");
150+
lib.root_module.addCMacro("ZSTD_BUILD_STATIC", "ON");
151+
lib.root_module.addCMacro("ZSTD_BUILD_SHARED", "OFF");
152+
lib.root_module.addCMacro("ZSTD_LEGACY_SUPPORT", "7");
153+
154+
if (target.result.cpu.arch == .x86_64) {
155+
lib.addCSourceFiles(.{ .root = upstream.path(""), .files = &decompress_asm });
156+
} else {
157+
lib.root_module.addCMacro("ZSTD_DISABLE_ASM", "");
158+
}
159+
160+
b.installArtifact(lib);
17161

18162
// Smoke unit test
19163
const test_mod = b.addModule("test", .{
20164
.root_source_file = b.path("tests.zig"),
21165
.target = target,
22166
.optimize = optimize,
23167
});
24-
// TODO: mod.linkLibrary(lib);
168+
test_mod.linkLibrary(lib);
25169

26170
const run_mod_tests = b.addRunArtifact(b.addTest(.{ .root_module = test_mod }));
27171

tests.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
const std = @import("std");
22

33
const zstd = @cImport({
4-
@cInclude("stdio.h");
4+
@cInclude("zstd.h");
55
});
66

77
// Just a smoke test to make sure the library is linked correctly.
8-
test {}
8+
test {
9+
const version = zstd.ZSTD_versionString();
10+
11+
try std.testing.expectEqualStrings("1.5.7", std.mem.span(version));
12+
}

0 commit comments

Comments
 (0)