Skip to content

Commit d25242b

Browse files
committed
initial commit
0 parents  commit d25242b

File tree

8 files changed

+305
-0
lines changed

8 files changed

+305
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.zig text eol=lf
2+
*.zon text eol=lf

.github/workflows/ci.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
zig-version: [master]
18+
os: [ubuntu-latest, macos-latest, windows-latest]
19+
include:
20+
- zig-version: "0.12.1"
21+
os: ubuntu-latest
22+
- zig-version: "0.13.0"
23+
os: ubuntu-latest
24+
runs-on: ${{ matrix.os }}
25+
steps:
26+
- name: Checkout
27+
uses: actions/checkout@v4
28+
29+
- name: Setup Zig
30+
uses: mlugg/setup-zig@v1
31+
with:
32+
version: ${{ matrix.zig-version }}
33+
34+
- name: Check Formatting
35+
run: zig fmt --ast-check --check .
36+
37+
- name: Build
38+
run: zig build --summary all

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.zig-cache
2+
zig-cache
3+
zig-out

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (Expat)
2+
3+
Copyright (c) contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

LICENSE-ZSTD

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
BSD License
2+
3+
For Zstandard software
4+
5+
Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
- Redistributions of source code must retain the above copyright notice, this
11+
list of conditions and the following disclaimer.
12+
13+
- Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
- Neither the name Facebook, nor Meta, nor the names of its contributors may
18+
be used to endorse or promote products derived from this software without
19+
specific prior written permission.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
25+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[![CI](https://github.com/allyourcodebase/zstd/actions/workflows/ci.yaml/badge.svg)](https://github.com/allyourcodebase/zstd/actions)
2+
3+
# zstd
4+
5+
This is [zstd](https://github.com/facebook/zstd), packaged for [Zig](https://ziglang.org/).
6+
7+
## Installation
8+
9+
First, update your `build.zig.zon`:
10+
11+
```
12+
# Initialize a `zig build` project if you haven't already
13+
zig init
14+
zig fetch --save git+https://github.com/allyourcodebase/zstd.git#1.5.6
15+
```
16+
17+
You can then import `zstd` in your `build.zig` with:
18+
19+
```zig
20+
const zstd_dependency = b.dependency("zstd", .{
21+
.target = target,
22+
.optimize = optimize,
23+
});
24+
your_exe.linkLibrary(zstd_dependency.artifact("zstd"));
25+
```

build.zig

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const upstream = b.dependency("zstd", .{});
5+
const target = b.standardTargetOptions(.{});
6+
const optimize = b.standardOptimizeOption(.{});
7+
8+
const strip = b.option(bool, "strip", "Omit debug information");
9+
const pic = b.option(bool, "pie", "Produce Position Independent Code");
10+
11+
const compression = b.option(bool, "compression", "build compression module") orelse true;
12+
const decompression = b.option(bool, "decompression", "build decompression module") orelse true;
13+
const dictbuilder = b.option(bool, "dictbuilder", "build dictbuilder module") orelse compression;
14+
const deprecated = b.option(bool, "deprecated", "build deprecated module") orelse false;
15+
16+
const minify = b.option(bool, "minify", "Configures a bunch of other options to space-optimized defaults") orelse false;
17+
const legacy_support = b.option(usize, "legacy-support", "makes it possible to decompress legacy zstd formats") orelse @as(usize, if (minify) 0 else 5);
18+
// For example, `-Dlegacy-support=0` means: no support for legacy formats
19+
// For example, `-Dlegacy-support=2` means: support legacy formats >= v0.2.0
20+
std.debug.assert(legacy_support < 8);
21+
22+
const disable_assembly = b.option(bool, "disable-assembly", "Assembly support") orelse false;
23+
const huf_force_decompress_x1 = b.option(bool, "huf-force-decompress-x1", "") orelse minify;
24+
const huf_force_decompress_x2 = b.option(bool, "huf-force-decompress-x2", "") orelse false;
25+
const force_decompress_sequences_short = b.option(bool, "force-decompress-sequences-short", "") orelse minify;
26+
const force_decompress_sequences_long = b.option(bool, "force-decompress-sequences-long", "") orelse false;
27+
const no_inline = b.option(bool, "no-inline", "Disable Inlining") orelse minify;
28+
const strip_error_strings = b.option(bool, "strip-error-strings", "removes the error messages that are otherwise returned by `ZSTD_getErrorName` (implied by `-Dminify`)") orelse minify;
29+
const exclude_compressors_dfast_and_up = b.option(bool, "exclude-compressors-dfast-and-up", "") orelse false;
30+
const exclude_compressors_greedy_and_up = b.option(bool, "exclude-compressors-greedy-and-up", "") orelse false;
31+
32+
const zstd = b.addStaticLibrary(.{
33+
.name = "zstd",
34+
.target = target,
35+
.optimize = optimize,
36+
.strip = strip,
37+
.pic = pic,
38+
.link_libc = true,
39+
});
40+
b.installArtifact(zstd);
41+
zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = common_sources });
42+
// zstd does not install into its own subdirectory. :(
43+
zstd.installHeader(upstream.path("lib/zstd.h"), "zstd.h");
44+
zstd.installHeader(upstream.path("lib/zdict.h"), "zdict.h");
45+
zstd.installHeader(upstream.path("lib/zstd_errors.h"), "zstd_errors.h");
46+
if (compression) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = compression_sources });
47+
if (decompression) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = decompress_sources });
48+
if (dictbuilder) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = dict_builder_sources });
49+
if (deprecated) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = deprecated_sources });
50+
if (legacy_support != 0) {
51+
for (legacy_support..8) |i| zstd.addCSourceFile(.{ .file = upstream.path(b.fmt("lib/legacy/zstd_v0{d}.c", .{i})) });
52+
}
53+
54+
if (target.result.cpu.arch == .x86_64) {
55+
if (decompression) {
56+
zstd.addAssemblyFile(upstream.path("lib/decompress/huf_decompress_amd64.S"));
57+
}
58+
} else {
59+
zstd.defineCMacro("ZSTD_DISABLE_ASM", null);
60+
}
61+
62+
zstd.defineCMacro("ZSTD_LEGACY_SUPPORT", b.fmt("{d}", .{legacy_support}));
63+
if (disable_assembly) zstd.defineCMacro("ZSTD_DISABLE_ASM", null);
64+
if (huf_force_decompress_x1) zstd.defineCMacro("HUF_FORCE_DECOMPRESS_X1", null);
65+
if (huf_force_decompress_x2) zstd.defineCMacro("HUF_FORCE_DECOMPRESS_X2", null);
66+
if (force_decompress_sequences_short) zstd.defineCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT", null);
67+
if (force_decompress_sequences_long) zstd.defineCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG", null);
68+
if (no_inline) zstd.defineCMacro("ZSTD_NO_INLINE", null);
69+
if (strip_error_strings) zstd.defineCMacro("ZSTD_STRIP_ERROR_STRINGS", null);
70+
if (exclude_compressors_dfast_and_up) {
71+
zstd.defineCMacro("ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR", null);
72+
zstd.defineCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", null);
73+
zstd.defineCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", null);
74+
zstd.defineCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", null);
75+
zstd.defineCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", null);
76+
zstd.defineCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", null);
77+
}
78+
if (exclude_compressors_greedy_and_up) {
79+
zstd.defineCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", null);
80+
zstd.defineCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", null);
81+
zstd.defineCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", null);
82+
zstd.defineCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", null);
83+
zstd.defineCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", null);
84+
}
85+
86+
{
87+
const examples: []const []const u8 = &.{
88+
"simple_compression",
89+
"simple_decompression",
90+
"multiple_simple_compression",
91+
"dictionary_compression",
92+
"dictionary_decompression",
93+
"streaming_compression",
94+
"streaming_decompression",
95+
"multiple_streaming_compression",
96+
"streaming_memory_usage",
97+
};
98+
99+
for (examples) |name| {
100+
const exe = b.addExecutable(.{
101+
.name = name,
102+
.target = target,
103+
.optimize = optimize,
104+
});
105+
exe.addCSourceFile(.{ .file = upstream.path(b.fmt("examples/{s}.c", .{name})) });
106+
exe.addIncludePath(upstream.path("examples/common.c"));
107+
exe.linkLibrary(zstd);
108+
b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "examples" } } }).step);
109+
}
110+
}
111+
}
112+
113+
const common_sources: []const []const u8 = &.{
114+
"common/debug.c",
115+
"common/zstd_common.c",
116+
"common/threading.c",
117+
"common/entropy_common.c",
118+
"common/fse_decompress.c",
119+
"common/xxhash.c",
120+
"common/error_private.c",
121+
"common/pool.c",
122+
};
123+
124+
const compression_sources: []const []const u8 = &.{
125+
"compress/fse_compress.c",
126+
"compress/huf_compress.c",
127+
"compress/zstd_double_fast.c",
128+
"compress/zstd_compress_literals.c",
129+
"compress/zstdmt_compress.c",
130+
"compress/zstd_compress_superblock.c",
131+
"compress/zstd_opt.c",
132+
"compress/zstd_compress.c",
133+
"compress/zstd_compress_sequences.c",
134+
"compress/hist.c",
135+
"compress/zstd_ldm.c",
136+
"compress/zstd_lazy.c",
137+
"compress/zstd_fast.c",
138+
};
139+
140+
const decompress_sources: []const []const u8 = &.{
141+
"decompress/zstd_decompress.c",
142+
"decompress/huf_decompress.c",
143+
"decompress/zstd_decompress_block.c",
144+
"decompress/zstd_ddict.c",
145+
};
146+
147+
const dict_builder_sources: []const []const u8 = &.{
148+
"dictBuilder/divsufsort.c",
149+
"dictBuilder/zdict.c",
150+
"dictBuilder/cover.c",
151+
"dictBuilder/fastcover.c",
152+
};
153+
154+
const deprecated_sources: []const []const u8 = &.{
155+
"deprecated/zbuff_decompress.c",
156+
"deprecated/zbuff_common.c",
157+
"deprecated/zbuff_compress.c",
158+
};
159+
160+
const legacy_sources: []const []const u8 = &.{
161+
"legacy/zstd_v01.c",
162+
"legacy/zstd_v02.c",
163+
"legacy/zstd_v03.c",
164+
"legacy/zstd_v04.c",
165+
"legacy/zstd_v05.c",
166+
"legacy/zstd_v06.c",
167+
"legacy/zstd_v07.c",
168+
};

build.zig.zon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.{
2+
.name = "zstd",
3+
.version = "1.5.6",
4+
.minimum_zig_version = "0.12.0",
5+
.dependencies = .{
6+
.zstd = .{
7+
.url = "git+https://github.com/facebook/zstd.git#v1.5.6",
8+
.hash = "12205df4790849e6ab600128051b962361901b706f75cbffe2eb8da09a81069f9011",
9+
},
10+
},
11+
.paths = .{
12+
"build.zig",
13+
"build.zig.zon",
14+
"LICENSE",
15+
"LICENSE-ZSTD",
16+
"README.md",
17+
},
18+
}

0 commit comments

Comments
 (0)