Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ jobs:
zig-version: [master]
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- zig-version: "0.14.0"
- zig-version: "0.14.1"
os: ubuntu-latest
- zig-version: "0.15.2"
os: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
uses: actions/checkout@v6

- name: Setup Zig
uses: mlugg/setup-zig@v1
uses: mlugg/setup-zig@v2
with:
version: ${{ matrix.zig-version }}

Expand Down
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

This is [zstd](https://github.com/facebook/zstd), packaged for [Zig](https://ziglang.org/).

Compatible with zig `0.14`-`0.16`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A statement like "Compatible with zig 0.16" is misleading because there is no 0.16.0 release (yet). You would have to mention the exact Zig version this has been tested with or say 0.16.0-dev. This can easily get outdated over time so I'd suggest to remove this statement entirely and let the user test whether it works with their Zig version instead.

Also, projects in allyourcodebase only need to guarantee support for the latest tagged release. Anything beyond that may change over time.

## Installation

First, update your `build.zig.zon`:

```
# Initialize a `zig build` project if you haven't already
zig init
zig fetch --save git+https://github.com/allyourcodebase/zstd.git#1.5.7
zig fetch --save git+https://github.com/allyourcodebase/zstd.git#master
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will tag a new release of this PR has been merged. This should be reverted.

Suggested change
zig fetch --save git+https://github.com/allyourcodebase/zstd.git#master
zig fetch --save git+https://github.com/allyourcodebase/zstd.git#1.5.7

```

You can then import `zstd` in your `build.zig` with:
Expand All @@ -23,3 +25,30 @@ const zstd_dependency = b.dependency("zstd", .{
});
your_exe.linkLibrary(zstd_dependency.artifact("zstd"));
```

## Options

```
-Dlinkage=[enum] Link mode. Defaults to static
Supported Values:
static
dynamic
-Dstrip=[bool] Omit debug information
-Dpie=[bool] Produce Position Independent Code
-Dcompression=[bool] build compression module
-Ddecompression=[bool] build decompression module
-Ddictbuilder=[bool] build dictbuilder module
-Ddeprecated=[bool] build deprecated module
-Dminify=[bool] Configures a bunch of other options to space-optimized defaults
-Dlegacy-support=[int] makes it possible to decompress legacy zstd formats
-Dmulti-thread=[bool] Enable multi-threading
-Ddisable-assembly=[bool] Assembly support
-Dhuf-force-decompress-x1=[bool]
-Dhuf-force-decompress-x2=[bool]
-Dforce-decompress-sequences-short=[bool]
-Dforce-decompress-sequences-long=[bool]
-Dno-inline=[bool] Disable Inlining
-Dstrip-error-strings=[bool] removes the error messages that are otherwise returned by `ZSTD_getErrorName` (implied by `-Dminify`)
-Dexclude-compressors-dfast-and-up=[bool]
-Dexclude-compressors-greedy-and-up=[bool]
```
Comment on lines +28 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can run zig build -h to get this output so there is no need to provide a copy in the readme that may eventually get outdated. Please remove it.

89 changes: 46 additions & 43 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const linkage = b.option(std.builtin.LinkMode, "linkage", "Link mode") orelse .static;
const linkage = b.option(std.builtin.LinkMode, "linkage", "Link mode. Defaults to static") orelse .static;
const strip = b.option(bool, "strip", "Omit debug information");
const pic = b.option(bool, "pie", "Produce Position Independent Code");

Expand All @@ -31,62 +31,64 @@ pub fn build(b: *std.Build) void {
const exclude_compressors_dfast_and_up = b.option(bool, "exclude-compressors-dfast-and-up", "") orelse false;
const exclude_compressors_greedy_and_up = b.option(bool, "exclude-compressors-greedy-and-up", "") orelse false;

const module = b.createModule(.{
.target = target,
.optimize = optimize,
.strip = strip,
.pic = pic,
.link_libc = true,
});
const zstd = b.addLibrary(.{
.linkage = linkage,
.name = "zstd",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.strip = strip,
.pic = pic,
.link_libc = true,
}),
.root_module = module,
Comment on lines -37 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the diff unnecessarily complicated. Can you keep the b.createModule call inside the b.addLibrary call and then just replace zstd.addSomething with zstd.root_module.addSomething wherever needed.

});
b.installArtifact(zstd);
zstd.root_module.addCSourceFiles(.{ .root = upstream.path("lib"), .files = common_sources });
module.addCSourceFiles(.{ .root = upstream.path("lib"), .files = common_sources });
// zstd does not install into its own subdirectory. :(
zstd.installHeader(upstream.path("lib/zstd.h"), "zstd.h");
zstd.installHeader(upstream.path("lib/zdict.h"), "zdict.h");
zstd.installHeader(upstream.path("lib/zstd_errors.h"), "zstd_errors.h");
if (compression) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = compression_sources });
if (decompression) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = decompress_sources });
if (dictbuilder) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = dict_builder_sources });
if (deprecated) zstd.addCSourceFiles(.{ .root = upstream.path("lib"), .files = deprecated_sources });
if (compression) module.addCSourceFiles(.{ .root = upstream.path("lib"), .files = compression_sources });
if (decompression) module.addCSourceFiles(.{ .root = upstream.path("lib"), .files = decompress_sources });
if (dictbuilder) module.addCSourceFiles(.{ .root = upstream.path("lib"), .files = dict_builder_sources });
if (deprecated) module.addCSourceFiles(.{ .root = upstream.path("lib"), .files = deprecated_sources });
if (legacy_support != 0) {
for (legacy_support..8) |i| zstd.addCSourceFile(.{ .file = upstream.path(b.fmt("lib/legacy/zstd_v0{d}.c", .{i})) });
for (legacy_support..8) |i|
module.addCSourceFile(.{ .file = upstream.path(b.fmt("lib/legacy/zstd_v0{d}.c", .{i})) });
}

if (target.result.cpu.arch == .x86_64) {
if (decompression) {
zstd.root_module.addAssemblyFile(upstream.path("lib/decompress/huf_decompress_amd64.S"));
module.addAssemblyFile(upstream.path("lib/decompress/huf_decompress_amd64.S"));
}
} else {
zstd.root_module.addCMacro("ZSTD_DISABLE_ASM", "");
module.addCMacro("ZSTD_DISABLE_ASM", "");
}

zstd.root_module.addCMacro("ZSTD_LEGACY_SUPPORT", b.fmt("{d}", .{legacy_support}));
if (multi_thread) zstd.root_module.addCMacro("ZSTD_MULTITHREAD", "1");
if (disable_assembly) zstd.root_module.addCMacro("ZSTD_DISABLE_ASM", "");
if (huf_force_decompress_x1) zstd.root_module.addCMacro("HUF_FORCE_DECOMPRESS_X1", "");
if (huf_force_decompress_x2) zstd.root_module.addCMacro("HUF_FORCE_DECOMPRESS_X2", "");
if (force_decompress_sequences_short) zstd.root_module.addCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT", "");
if (force_decompress_sequences_long) zstd.root_module.addCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG", "");
if (no_inline) zstd.root_module.addCMacro("ZSTD_NO_INLINE", "");
if (strip_error_strings) zstd.root_module.addCMacro("ZSTD_STRIP_ERROR_STRINGS", "");
module.addCMacro("ZSTD_LEGACY_SUPPORT", b.fmt("{d}", .{legacy_support}));
if (multi_thread) module.addCMacro("ZSTD_MULTITHREAD", "1");
if (disable_assembly) module.addCMacro("ZSTD_DISABLE_ASM", "");
if (huf_force_decompress_x1) module.addCMacro("HUF_FORCE_DECOMPRESS_X1", "");
if (huf_force_decompress_x2) module.addCMacro("HUF_FORCE_DECOMPRESS_X2", "");
if (force_decompress_sequences_short) module.addCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_SHORT", "");
if (force_decompress_sequences_long) module.addCMacro("ZSTD_FORCE_DECOMPRESS_SEQUENCES_LONG", "");
if (no_inline) module.addCMacro("ZSTD_NO_INLINE", "");
if (strip_error_strings) module.addCMacro("ZSTD_STRIP_ERROR_STRINGS", "");
if (exclude_compressors_dfast_and_up) {
zstd.root_module.addCMacro("ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_DFAST_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", "");
}
if (exclude_compressors_greedy_and_up) {
zstd.root_module.addCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", "");
zstd.root_module.addCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_GREEDY_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_LAZY2_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_BTLAZY2_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_BTOPT_BLOCK_COMPRESSOR", "");
module.addCMacro("ZSTD_EXCLUDE_BTULTRA_BLOCK_COMPRESSOR", "");
}

{
Expand All @@ -103,16 +105,17 @@ pub fn build(b: *std.Build) void {
};

for (examples) |name| {
const mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}),
.root_module = mod,
Comment on lines -108 to +114
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the diff unnecessarily complicated. Can you keep the b.createModule call inside the b.addExecutable call and then just replace exe.addSomething with exe.root_module.addSomething.

});
exe.addCSourceFile(.{ .file = upstream.path(b.fmt("examples/{s}.c", .{name})) });
exe.addIncludePath(upstream.path("examples/common.c"));
exe.linkLibrary(zstd);
mod.addCSourceFile(.{ .file = upstream.path(b.fmt("examples/{s}.c", .{name})) });
mod.addIncludePath(upstream.path("examples/common.c"));
mod.linkLibrary(zstd);
b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "examples" } } }).step);
}
}
Expand Down