diff --git a/build.zig b/build.zig index 2fa7265..840a578 100644 --- a/build.zig +++ b/build.zig @@ -3,8 +3,16 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const optimize = b.standardOptimizeOption(.{}); const target = b.standardTargetOptions(.{}); + const add_paths = b.option(bool, "add-paths", "add macos SDK paths from dependency") orelse false; - _ = b.addModule("objc", .{ .root_source_file = .{ .path = "src/main.zig" } }); + const objc = b.addModule("objc", .{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + if (add_paths) @import("macos_sdk").addPathsModule(objc); + objc.linkSystemLibrary("objc", .{}); + objc.linkFramework("Foundation", .{}); const tests = b.addTest(.{ .name = "objc-test", diff --git a/build.zig.zon b/build.zig.zon index d6b5f86..9cb862c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -10,8 +10,8 @@ }, .dependencies = .{ .macos_sdk = .{ - .url = "https://github.com/mitchellh/zig-build-macos-sdk/archive/4186e9fd445d12041651abe59ea5f396499b0844.tar.gz", - .hash = "1220bc2612b57b0cfaaecbcac38e3144e5a9362ff668d71eb8334e895047bdbb7148", + .url = "https://github.com/mitchellh/zig-build-macos-sdk/archive/ee70f27c08680307fa35ada92e6b2c36e0ff84c6.tar.gz", + .hash = "1220b415f529f1c04ed876c2b481e9f8119d353d4e3d4d7c8607ee302d2142e13eca", }, }, } diff --git a/flake.lock b/flake.lock index 3b57092..923bacf 100644 --- a/flake.lock +++ b/flake.lock @@ -109,11 +109,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1711585351, - "narHash": "sha256-rVJ/lRPQNCjU/98SZmQUhksoZOrKajwGS1UmEMuXzss=", + "lastModified": 1712923708, + "narHash": "sha256-gx0nOU67FBJ6S4S2VtnahhSHW9CW8zsYzE616/NV4eo=", "owner": "mitchellh", "repo": "zig-overlay", - "rev": "6ac62029cf9a5fd2cec831a2dfc541f3cae63297", + "rev": "c4412eedbfed081eefd5120c85e7910a953ae818", "type": "github" }, "original": { diff --git a/src/block.zig b/src/block.zig index 46b3ca9..ccdb4fe 100644 --- a/src/block.zig +++ b/src/block.zig @@ -41,7 +41,7 @@ pub fn Block( .size = @sizeOf(Context), .copy_helper = &descCopyHelper, .dispose_helper = &descDisposeHelper, - .signature = objc.comptimeEncode(InvokeFn).ptr, + .signature = &objc.comptimeEncode(InvokeFn), }; /// This is the function type that is called back. diff --git a/src/class.zig b/src/class.zig index f63264a..bb0fff6 100644 --- a/src/class.zig +++ b/src/class.zig @@ -76,7 +76,7 @@ pub const Class = struct { self.value, objc.sel(name).value, @ptrCast(&imp), - encoding.ptr, + &encoding, )); } diff --git a/src/encoding.zig b/src/encoding.zig index edd247f..cbc6245 100644 --- a/src/encoding.zig +++ b/src/encoding.zig @@ -4,22 +4,30 @@ const c = @import("c.zig"); const assert = std.debug.assert; const testing = std.testing; -/// Encode a type into a comptime string. -pub fn comptimeEncode(comptime T: type) [:0]const u8 { +/// how much space do we need to encode this type? +fn comptimeN(comptime T: type) usize { comptime { const encoding = objc.Encoding.init(T); // Figure out how much space we need var counting = std.io.countingWriter(std.io.null_writer); try std.fmt.format(counting.writer(), "{}", .{encoding}); + return counting.bytes_written; + } +} + +/// Encode a type into a comptime string. +pub fn comptimeEncode(comptime T: type) [comptimeN(T):0]u8 { + comptime { + const encoding = objc.Encoding.init(T); // Build our final signature - var buf: [counting.bytes_written + 1]u8 = undefined; - var fbs = std.io.fixedBufferStream(buf[0..counting.bytes_written]); + var buf: [comptimeN(T) + 1]u8 = undefined; + var fbs = std.io.fixedBufferStream(buf[0 .. buf.len - 1]); try std.fmt.format(fbs.writer(), "{}", .{encoding}); - buf[counting.bytes_written] = 0; + buf[buf.len - 1] = 0; - return buf[0..counting.bytes_written :0]; + return buf[0 .. buf.len - 1 :0].*; } }