diff --git a/build.zig b/build.zig index 1dd58d7..840a578 100644 --- a/build.zig +++ b/build.zig @@ -5,7 +5,11 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const add_paths = b.option(bool, "add-paths", "add macos SDK paths from dependency") orelse false; - const objc = 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", .{}); 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/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].*; } }