diff --git a/README.md b/README.md index 97ec53e..901a9a9 100644 --- a/README.md +++ b/README.md @@ -4,21 +4,25 @@ Provides a package to be used by the zig package manager for C++ programs. ## Status -| Refname | Catch2 version | Zig `0.12.x` | Zig `0.13.x` | Zig `0.14.0-dev` | -|:--------|:---------------|:------------:|:------------:|:----------------:| -| `3.7.1` | `v3.7.1` | ✅ | ✅ | ✅ | +| Refname | Catch2 version | Zig `0.12.x` | Zig `0.13.x` | Zig `0.14.0-dev` | +|:----------|:---------------|:------------:|:------------:|:----------------:| +| `3.7.1+1` | `v3.7.1` | ✅ | ✅ | ✅ | ## Use Add the dependency in your `build.zig.zon` by running the following command: ```bash -zig fetch --save git+https://github.com/allyourcodebase/catch2#3.7.1 +zig fetch --save git+https://github.com/allyourcodebase/catch2#3.7.1+1 ``` Then, in your `build.zig`: ```zig const catch2_dep = b.dependency("catch2", { .target = target, .optimize = optimize }); -const catch2 = catch2_dep.artifact("Catch2"); // or Catch2WithMain +const catch2_lib = catch2_dep.artifact("Catch2"); +const catch2_main = catch2_dep.artifact("Catch2WithMain"); // wherever needed: -exe.linkLibrary(catch2); +exe.linkLibrary(catch2_lib); +exe.linkLibrary(catch2_main); ``` + +A complete usage demonstration is provided in the [example](example) directory diff --git a/build.zig b/build.zig index b228a24..266a6f7 100644 --- a/build.zig +++ b/build.zig @@ -29,7 +29,7 @@ pub fn build(b: *std.Build) !void { }, ); - const catch2 = b.addStaticLibrary(.{ .name = "catch2", .target = target, .optimize = optimize }); + const catch2 = b.addStaticLibrary(.{ .name = "Catch2", .target = target, .optimize = optimize }); catch2.addCSourceFiles(.{ .root = upstream.path("src/catch2"), .files = &source_files, diff --git a/example/build.zig b/example/build.zig new file mode 100644 index 0000000..d25af66 --- /dev/null +++ b/example/build.zig @@ -0,0 +1,46 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const include = b.path("include"); + + const lib = b.addStaticLibrary(.{ + .name = "demo", + .target = target, + .optimize = optimize, + }); + lib.addIncludePath(include); + lib.addCSourceFiles(.{ + .root = b.path("src"), + .files = &.{"source.cpp"}, + .flags = &CXXFLAGS, + }); + lib.linkLibCpp(); + lib.installHeadersDirectory(include, "demo", .{ .include_extensions = &.{".hpp"} }); + b.installArtifact(lib); + + { // Test + const test_step = b.step("test", "Run tests"); + const test_exe = b.addExecutable(.{ .name = "test_demo", .target = target, .optimize = optimize }); + const run_test = b.addRunArtifact(test_exe); + + const catch2_dep = b.dependency("catch2", .{ .target = target, .optimize = optimize }); + const catch2_lib = catch2_dep.artifact("Catch2"); + const catch2_main = catch2_dep.artifact("Catch2WithMain"); + + test_exe.addCSourceFiles(.{ .root = b.path("test"), .files = &.{"test.cpp"}, .flags = &CXXFLAGS }); + test_exe.linkLibrary(lib); + test_exe.linkLibrary(catch2_lib); + test_exe.linkLibrary(catch2_main); + test_step.dependOn(&run_test.step); + } +} + +const CXXFLAGS = .{ + "--std=c++23", + "-Wall", + "-Wextra", + "-Werror", +}; diff --git a/example/build.zig.zon b/example/build.zig.zon new file mode 100644 index 0000000..4fe9028 --- /dev/null +++ b/example/build.zig.zon @@ -0,0 +1,18 @@ +.{ + .name = "cath2UsageDemo", + .version = "1.0.0", + .dependencies = .{ + .catch2 = .{ + .url = "..", + .hash = "1220aef10cc88ccd919517ece637e54cfd7381c91ab9ee79a2d0d8791165ccd6bfff", + }, + }, + .minimum_zig_version = "0.12.0", + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + "include", + "test", + }, +} diff --git a/example/include/header.hpp b/example/include/header.hpp new file mode 100644 index 0000000..52f0983 --- /dev/null +++ b/example/include/header.hpp @@ -0,0 +1,3 @@ +#include // uintmax_t uint8_t + +uintmax_t fibonacci(uint8_t n, uintmax_t f0 = 0, uintmax_t f1 = 1) __attribute__((pure)); diff --git a/example/src/source.cpp b/example/src/source.cpp new file mode 100644 index 0000000..b1efedc --- /dev/null +++ b/example/src/source.cpp @@ -0,0 +1,10 @@ +#include "header.hpp" + +auto fibonacci(uint8_t n, uintmax_t f0, uintmax_t f1) -> uintmax_t +{ + switch (n) { + case 0: return f0; + case 1: return f1; + } + return fibonacci(n-1, f1, f0 + f1); +} diff --git a/example/test/test.cpp b/example/test/test.cpp new file mode 100644 index 0000000..5da7392 --- /dev/null +++ b/example/test/test.cpp @@ -0,0 +1,12 @@ +#include + +#include + +TEST_CASE("Fibonacci") +{ + CHECK(fibonacci(7) == 13); + CHECK(fibonacci(6, 1, 1) == 13); + CHECK(fibonacci(5, 1, 2) == 13); + + CHECK(fibonacci(4, 5, 10) == 40); +}