Skip to content

Commit

Permalink
Refactors and improvements (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
InKryption authored Oct 24, 2024
1 parent 5d1bd20 commit 5095f01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
39 changes: 14 additions & 25 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
const std = @import("std");

const package_name = "zstd";
const package_path = "src/lib.zig";

pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

const test_step = b.step("test", "Run library tests");

const zstd_dep = b.dependency("zstd", .{});

const zstd_lib = b.addStaticLibrary(.{
.name = package_name,
.name = "zstd",
.target = target,
.optimize = optimize,
});
b.installArtifact(zstd_lib);
zstd_lib.linkLibC();
zstd_lib.addIncludePath(zstd_dep.path("lib"));
zstd_lib.installHeader(zstd_dep.path("lib/zstd.h"), "zstd.h");
zstd_lib.installHeader(zstd_dep.path("lib/zstd_errors.h"), "zstd_errors.h");

const config_header = b.addConfigHeader(
.{ .style = .blank },
.{
.ZSTD_CONFIG_H = {},
.ZSTD_MULTITHREAD_SUPPORT_DEFAULT = null,
.ZSTD_LEGACY_SUPPORT = null,
},
);
zstd_lib.addConfigHeader(config_header);
zstd_lib.root_module.addCMacro("ZSTD_MULTITHREAD", "");
zstd_lib.root_module.addCMacro("ZSTD_STATIC_LINKING_ONLY", "");

zstd_lib.addCSourceFiles(.{
.root = zstd_dep.path("lib"),
.files = &.{
Expand Down Expand Up @@ -61,24 +55,19 @@ pub fn build(b: *std.Build) void {
},
});
zstd_lib.addAssemblyFile(zstd_dep.path("lib/decompress/huf_decompress_amd64.S"));
b.installArtifact(zstd_lib);

const module = b.addModule(package_name, .{
.root_source_file = b.path(package_path),
.imports = &.{},
const zstd_mod = b.addModule("zstd", .{
.root_source_file = b.path("src/lib.zig"),
});
module.linkLibrary(zstd_lib);
zstd_mod.linkLibrary(zstd_lib);

// tests
const tests = b.addTest(.{
const tests_exe = b.addTest(.{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/tests.zig"),
});
tests.linkLibrary(zstd_lib);
tests_exe.root_module.addImport("zstd", zstd_mod);

const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&zstd_lib.step);
test_step.dependOn(&run_tests.step);
const tests_exe_run = b.addRunArtifact(tests_exe);
test_step.dependOn(&tests_exe_run.step);
}
15 changes: 8 additions & 7 deletions src/tests.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const std = @import("std");
const lib = @import("lib.zig");
const zstd = @import("zstd");

test {
std.testing.log_level = std.log.Level.err;
std.testing.refAllDecls(lib);
comptime {
std.testing.refAllDecls(zstd);
}

test "writing & reading" {
std.testing.log_level = std.log.Level.err;

const test_data = blk: {
var bytes: [32]u8 = undefined;
@memset(&bytes, 0);
Expand All @@ -20,18 +21,18 @@ test "writing & reading" {
var compressed_data = std.ArrayList(u8).init(std.testing.allocator);
defer compressed_data.deinit();

const compressor = try lib.Compressor.init(.{});
const compressor = try zstd.Compressor.init(.{});
defer compressor.deinit();
var buffer: [128]u8 = undefined;
const writer_ctx = lib.writerCtx(compressed_data.writer(), &compressor, &buffer);
const writer_ctx = zstd.writerCtx(compressed_data.writer(), &compressor, &buffer);
const writer = writer_ctx.writer();

try writer.writeAll(&test_data);
try writer_ctx.finish();

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

var reader_state = try lib.Reader.init(compressed_data.items);
var reader_state = try zstd.Reader.init(compressed_data.items);
defer reader_state.deinit();
const reader = reader_state.reader();

Expand Down

0 comments on commit 5095f01

Please sign in to comment.