Skip to content

Commit 5095f01

Browse files
authored
Refactors and improvements (#2)
* Make better use of the build system lazy paths. * General re-organization. * Get rid of pointless ConfigHeader. * Use addCMacro to enable `ZSTD_MULTITHREAD` - and also the static-linking-only API. * Make tests.zig import the bindings as a module
1 parent 5d1bd20 commit 5095f01

File tree

2 files changed

+22
-32
lines changed

2 files changed

+22
-32
lines changed

build.zig

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,27 @@
11
const std = @import("std");
22

3-
const package_name = "zstd";
4-
const package_path = "src/lib.zig";
5-
63
pub fn build(b: *std.Build) void {
74
const optimize = b.standardOptimizeOption(.{});
85
const target = b.standardTargetOptions(.{});
96

7+
const test_step = b.step("test", "Run library tests");
8+
109
const zstd_dep = b.dependency("zstd", .{});
1110

1211
const zstd_lib = b.addStaticLibrary(.{
13-
.name = package_name,
12+
.name = "zstd",
1413
.target = target,
1514
.optimize = optimize,
1615
});
16+
b.installArtifact(zstd_lib);
1717
zstd_lib.linkLibC();
1818
zstd_lib.addIncludePath(zstd_dep.path("lib"));
1919
zstd_lib.installHeader(zstd_dep.path("lib/zstd.h"), "zstd.h");
2020
zstd_lib.installHeader(zstd_dep.path("lib/zstd_errors.h"), "zstd_errors.h");
2121

22-
const config_header = b.addConfigHeader(
23-
.{ .style = .blank },
24-
.{
25-
.ZSTD_CONFIG_H = {},
26-
.ZSTD_MULTITHREAD_SUPPORT_DEFAULT = null,
27-
.ZSTD_LEGACY_SUPPORT = null,
28-
},
29-
);
30-
zstd_lib.addConfigHeader(config_header);
22+
zstd_lib.root_module.addCMacro("ZSTD_MULTITHREAD", "");
23+
zstd_lib.root_module.addCMacro("ZSTD_STATIC_LINKING_ONLY", "");
24+
3125
zstd_lib.addCSourceFiles(.{
3226
.root = zstd_dep.path("lib"),
3327
.files = &.{
@@ -61,24 +55,19 @@ pub fn build(b: *std.Build) void {
6155
},
6256
});
6357
zstd_lib.addAssemblyFile(zstd_dep.path("lib/decompress/huf_decompress_amd64.S"));
64-
b.installArtifact(zstd_lib);
6558

66-
const module = b.addModule(package_name, .{
67-
.root_source_file = b.path(package_path),
68-
.imports = &.{},
59+
const zstd_mod = b.addModule("zstd", .{
60+
.root_source_file = b.path("src/lib.zig"),
6961
});
70-
module.linkLibrary(zstd_lib);
62+
zstd_mod.linkLibrary(zstd_lib);
7163

72-
// tests
73-
const tests = b.addTest(.{
64+
const tests_exe = b.addTest(.{
7465
.target = target,
7566
.optimize = optimize,
7667
.root_source_file = b.path("src/tests.zig"),
7768
});
78-
tests.linkLibrary(zstd_lib);
69+
tests_exe.root_module.addImport("zstd", zstd_mod);
7970

80-
const run_tests = b.addRunArtifact(tests);
81-
const test_step = b.step("test", "Run library tests");
82-
test_step.dependOn(&zstd_lib.step);
83-
test_step.dependOn(&run_tests.step);
71+
const tests_exe_run = b.addRunArtifact(tests_exe);
72+
test_step.dependOn(&tests_exe_run.step);
8473
}

src/tests.zig

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const std = @import("std");
2-
const lib = @import("lib.zig");
2+
const zstd = @import("zstd");
33

4-
test {
5-
std.testing.log_level = std.log.Level.err;
6-
std.testing.refAllDecls(lib);
4+
comptime {
5+
std.testing.refAllDecls(zstd);
76
}
87

98
test "writing & reading" {
9+
std.testing.log_level = std.log.Level.err;
10+
1011
const test_data = blk: {
1112
var bytes: [32]u8 = undefined;
1213
@memset(&bytes, 0);
@@ -20,18 +21,18 @@ test "writing & reading" {
2021
var compressed_data = std.ArrayList(u8).init(std.testing.allocator);
2122
defer compressed_data.deinit();
2223

23-
const compressor = try lib.Compressor.init(.{});
24+
const compressor = try zstd.Compressor.init(.{});
2425
defer compressor.deinit();
2526
var buffer: [128]u8 = undefined;
26-
const writer_ctx = lib.writerCtx(compressed_data.writer(), &compressor, &buffer);
27+
const writer_ctx = zstd.writerCtx(compressed_data.writer(), &compressor, &buffer);
2728
const writer = writer_ctx.writer();
2829

2930
try writer.writeAll(&test_data);
3031
try writer_ctx.finish();
3132

3233
try std.testing.expect(compressed_data.items.len <= test_data.len);
3334

34-
var reader_state = try lib.Reader.init(compressed_data.items);
35+
var reader_state = try zstd.Reader.init(compressed_data.items);
3536
defer reader_state.deinit();
3637
const reader = reader_state.reader();
3738

0 commit comments

Comments
 (0)