diff --git a/build.zig b/build.zig index 395f22d..7481891 100644 --- a/build.zig +++ b/build.zig @@ -7,23 +7,26 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const module = b.addModule("notmuch", .{ - .root_source_file = b.path("src/notmuch.zig"), + const notmuch_c = b.addTranslateC(.{ + .root_source_file = b.path("src/notmuch.c"), + .link_libc = true, .target = target, .optimize = optimize, - .link_libc = true, }); + if (b.graph.environ_map.get("NOTMUCH_INCLUDE")) |path| notmuch_c.addIncludePath(.{ .cwd_relative = path }); - module.linkSystemLibrary("notmuch", .{}); - - const tests = b.addTest(.{ + const notmuch_zig = b.addModule("notmuch", .{ .root_source_file = b.path("src/notmuch.zig"), .target = target, .optimize = optimize, + .link_libc = true, }); + notmuch_zig.addImport("c", notmuch_c.createModule()); + notmuch_zig.linkSystemLibrary("notmuch", .{}); - tests.linkSystemLibrary2("notmuch", .{}); - tests.linkLibC(); + const tests = b.addTest(.{ + .root_module = notmuch_zig, + }); const run_tests = b.addRunArtifact(tests); diff --git a/build.zig.zon b/build.zig.zon index 5119cbe..124df48 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,10 +3,9 @@ .{ .name = .notmuch, - - .version = "0.1.0", + .version = "0.2.0", .fingerprint = 0x25da472dbcafbfc8, - .minimum_zig_version = "0.14.0", + .minimum_zig_version = "0.16.0-dev.2682+02142a54d", .dependencies = .{}, @@ -14,7 +13,7 @@ "build.zig", "build.zig.zon", "src", - "LICENSE", + "LICENSES", "README.md", }, } diff --git a/flake.nix b/flake.nix index 6dc893f..0acf735 100644 --- a/flake.nix +++ b/flake.nix @@ -43,6 +43,7 @@ buildInputs = [ pkgs.notmuch ]; + NOTMUCH_INCLUDE = "${lib.getInclude pkgs.notmuch}/include"; }; }); }; diff --git a/src/Database.zig b/src/Database.zig index 0ca632f..564ca19 100644 --- a/src/Database.zig +++ b/src/Database.zig @@ -6,7 +6,7 @@ const Database = @This(); const std = @import("std"); const log = std.log.scoped(.notmuch); -const c = @import("c.zig").c; +const c = @import("c"); const Error = @import("error.zig").Error; const wrap = @import("error.zig").wrap; diff --git a/src/Message.zig b/src/Message.zig index a2de87e..03ed6b7 100644 --- a/src/Message.zig +++ b/src/Message.zig @@ -6,7 +6,7 @@ const Message = @This(); const std = @import("std"); const log = std.log.scoped(.notmuch); -const c = @import("c.zig").c; +const c = @import("c"); const Error = @import("error.zig").Error; const wrap = @import("error.zig").wrap; diff --git a/src/MessagesIterator.zig b/src/MessagesIterator.zig index 4348cf3..8a88533 100644 --- a/src/MessagesIterator.zig +++ b/src/MessagesIterator.zig @@ -3,7 +3,7 @@ const MessagesIterator = @This(); -const c = @import("c.zig").c; +const c = @import("c"); const Message = @import("Message.zig"); diff --git a/src/Query.zig b/src/Query.zig index d779887..f0ebeae 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -5,7 +5,7 @@ const Query = @This(); const std = @import("std"); -const c = @import("c.zig").c; +const c = @import("c"); const Error = @import("error.zig").Error; const wrap = @import("error.zig").wrap; diff --git a/src/TagsIterator.zig b/src/TagsIterator.zig index 653b5cc..32b8a0d 100644 --- a/src/TagsIterator.zig +++ b/src/TagsIterator.zig @@ -5,7 +5,7 @@ const TagsIterator = @This(); const std = @import("std"); -const c = @import("c.zig").c; +const c = @import("c"); tags: ?*c.notmuch_tags_t, diff --git a/src/Thread.zig b/src/Thread.zig index d85af57..eb0e05c 100644 --- a/src/Thread.zig +++ b/src/Thread.zig @@ -5,7 +5,7 @@ const Thread = @This(); const std = @import("std"); -const c = @import("c.zig").c; +const c = @import("c"); const Error = @import("error.zig").Error; const wrap = @import("error.zig").wrap; diff --git a/src/ThreadsIterator.zig b/src/ThreadsIterator.zig index ab896db..1e45673 100644 --- a/src/ThreadsIterator.zig +++ b/src/ThreadsIterator.zig @@ -3,7 +3,7 @@ pub const ThreadsIterator = @This(); -const c = @import("c.zig").c; +const c = @import("c"); const Thread = @import("Thread.zig"); diff --git a/src/enums.zig b/src/enums.zig index 1a89608..1e1f28b 100644 --- a/src/enums.zig +++ b/src/enums.zig @@ -3,41 +3,36 @@ const std = @import("std"); -const c = @import("c.zig").c; +const c = @import("c"); fn generateEnum(comptime prefix: []const u8, skips: []const []const u8) type { @setEvalBranchQuota(24000); const info = @typeInfo(c); - var count: usize = 0; + var count = 0; + var max = 0; outer: for (info.@"struct".decls) |decl| { for (skips) |skip| if (std.mem.eql(u8, skip, decl.name)) continue :outer; if (std.mem.startsWith(u8, decl.name, prefix)) { + max = @max(max, @field(c, decl.name)); count += 1; } } - var fields: [count]std.builtin.Type.EnumField = undefined; - var index: usize = 0; - var max: c.notmuch_status_t = 0; + var field_names: [count]std.builtin.Type.EnumField = undefined; + var field_values: [count]std.math.IntFittingRange(0, max) = undefined; + var index = 0; outer: for (info.@"struct".decls) |decl| { for (skips) |skip| if (std.mem.eql(u8, skip, decl.name)) continue :outer; - if (std.mem.startsWith(u8, decl.name, prefix)) { - max = @max(max, @field(c, decl.name)); - fields[index] = .{ - .name = decl.name[prefix.len..], - .value = @field(c, decl.name), - }; + if (std.mem.cutPrefix(u8, decl.name, prefix)) |suffix| { + field_names[index] = suffix; + field_values = @field(c, decl.name); index += 1; } } - return @Type( - .{ - .@"enum" = .{ - .tag_type = std.math.IntFittingRange(0, max), - .fields = &fields, - .decls = &.{}, - .is_exhaustive = true, - }, - }, + return @Enum( + std.math.IntFittingRange(0, max), + .exhaustive, + &field_names, + &field_values, ); } diff --git a/src/error.zig b/src/error.zig index 78b379e..9c9dd16 100644 --- a/src/error.zig +++ b/src/error.zig @@ -5,7 +5,7 @@ const std = @import("std"); const log = std.log.scoped(.notmuch); -const c = @import("c.zig").c; +const c = @import("c"); const STATUS = @import("enums.zig").STATUS; diff --git a/src/c.zig b/src/notmuch.c similarity index 50% rename from src/c.zig rename to src/notmuch.c index 61f47e1..9219daf 100644 --- a/src/c.zig +++ b/src/notmuch.c @@ -1,7 +1,5 @@ // SPDX-FileCopyrightText: © 2024 Jeffrey C. Ollie // SPDX-License-Identifier: MIT -pub const c = @cImport({ - @cInclude("stdlib.h"); - @cInclude("notmuch.h"); -}); +#include +#include "notmuch.h" diff --git a/src/notmuch.zig b/src/notmuch.zig index 2248320..bb1c940 100644 --- a/src/notmuch.zig +++ b/src/notmuch.zig @@ -11,5 +11,5 @@ pub const Message = @import("Message.zig"); pub const Query = @import("Query.zig"); test { - std.testing.refAllDeclsRecursive(@This()); + std.testing.refAllDecls(@This()); }