diff --git a/.github/workflows/zig.yml b/.github/workflows/zig.yml new file mode 100644 index 0000000..3d5a0b6 --- /dev/null +++ b/.github/workflows/zig.yml @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index e901168..f67c10d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.zig-cache /zig-out +foo.gz diff --git a/README.md b/README.md index ed4b507..0d81658 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/build.zig b/build.zig index 5f30251..39df288 100644 --- a/build.zig +++ b/build.zig @@ -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", @@ -34,6 +42,12 @@ 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", @@ -41,4 +55,55 @@ pub fn build(b: *std.Build) void { }, }); 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; }