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
26 changes: 26 additions & 0 deletions .github/workflows/zig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI - Zig

on:
create:
push:
branches: main
pull_request:
workflow_dispatch:

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: mlugg/setup-zig@v2
with:
# Because of https://github.com/ziglang/zig/issues/23120 we wan't use
# 0.14.0.
# Wait for 0.14.1 to be released.
version: master
- run: zig build
- run: zig build test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.zig-cache
/zig-out
foo.gz
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const zlib_dep = b.dependency("zlib", .{
.target = target,
.optimize = optimize,
});
your_compilation.linkLibrary(libz_dep.artifact("z"));
your_compilation.linkLibrary(zlib_dep.artifact("z"));
```

This will provide zlib as a static library to `your_compilation`.
83 changes: 74 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const upstream = b.dependency("zlib", .{});
const lib = b.addStaticLibrary(.{
.name = "z",
.target = b.standardTargetOptions(.{}),
.optimize = b.standardOptimizeOption(.{}),
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");

const upstream = b.dependency("zlib", .{
.target = target,
.optimize = optimize,
});
lib.linkLibC();
lib.addCSourceFiles(.{
.root = upstream.path(""),
.files = &.{

const lib_mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.single_threaded = single_threaded,
});
lib_mod.addCSourceFiles(.{
.root = upstream.path("."),
.files = &[_][]const u8{
"adler32.c",
"crc32.c",
"deflate.c",
Expand All @@ -34,11 +42,68 @@ pub fn build(b: *std.Build) void {
"-DZ_HAVE_UNISTD_H",
},
});

const lib = b.addLibrary(.{
.linkage = .static,
.name = "z",
.root_module = lib_mod,
});
lib.installHeadersDirectory(upstream.path(""), "", .{
.include_extensions = &.{
"zconf.h",
"zlib.h",
},
});
b.installArtifact(lib);

const dynamic_lib = b.addLibrary(.{
.linkage = .dynamic,
.name = "z",
.root_module = lib_mod,
});
// Install with no version extension
b.installArtifact(dynamic_lib);
// Install with version extension
const output_name = b.fmt("libz{s}.{s}", .{
dynamic_lib.root_module.resolved_target.?.result.dynamicLibSuffix(),
"1.3.1",
});
const install_step = b.addInstallArtifact(dynamic_lib, .{
.dest_dir = .{
.override = .lib,
},
.dest_sub_path = output_name,
});
b.getInstallStep().dependOn(&install_step.step);

// Testing is about running the test binaries and checking they return 0.
const test_step = b.step("test", "Run tests");
const example = addTestExecutable(b, target, optimize, upstream, lib, "example", "test/example.c");
test_step.dependOn(&example.step);
const examplesh = addTestExecutable(b, target, optimize, upstream, dynamic_lib, "examplesh", "test/example.c");
test_step.dependOn(&examplesh.step);
}

fn addTestExecutable(b: *std.Build, target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode, zlib: *std.Build.Dependency, lib: *std.Build.Step.Compile,
name: []const u8, filename: []const u8) *std.Build.Step.Run {
const test_exe_mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.single_threaded = true,
});
test_exe_mod.addCSourceFiles(.{
.root = zlib.path("."),
.files = &[_][]const u8{
filename,
},
});
test_exe_mod.linkLibrary(lib);
const test_exe = b.addExecutable(.{
.name = name,
.root_module = test_exe_mod,
});
const run_exe_tests = b.addRunArtifact(test_exe);
return run_exe_tests;
}