Skip to content

Commit

Permalink
Add an example to demonstrate usage, and fix artifact name
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Nov 10, 2024
1 parent fb9b321 commit 5edd462
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 7 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
46 changes: 46 additions & 0 deletions example/build.zig
Original file line number Diff line number Diff line change
@@ -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",
};
18 changes: 18 additions & 0 deletions example/build.zig.zon
Original file line number Diff line number Diff line change
@@ -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",
},
}
3 changes: 3 additions & 0 deletions example/include/header.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <cstdint> // uintmax_t uint8_t

uintmax_t fibonacci(uint8_t n, uintmax_t f0 = 0, uintmax_t f1 = 1) __attribute__((pure));
10 changes: 10 additions & 0 deletions example/src/source.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
12 changes: 12 additions & 0 deletions example/test/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <demo/header.hpp>

#include <catch2/catch_test_macros.hpp>

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);
}

0 comments on commit 5edd462

Please sign in to comment.