From 77bb670be5a5548c1fe345043d4fd97c680d27c0 Mon Sep 17 00:00:00 2001 From: Rylee Lyman Date: Fri, 12 Apr 2024 09:44:18 +0100 Subject: [PATCH 1/3] feat: add `add-paths` option; link needed frameworks --- build.zig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 2fa7265..1dd58d7 100644 --- a/build.zig +++ b/build.zig @@ -3,8 +3,12 @@ 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" } }); + if (add_paths) @import("macos_sdk").addPathsModule(objc); + objc.linkSystemLibrary("objc", .{}); + objc.linkFramework("Foundation", .{}); const tests = b.addTest(.{ .name = "objc-test", From 8113ef70e08f2caf3baaf3bce818189e7e400110 Mon Sep 17 00:00:00 2001 From: Rylee Lyman Date: Fri, 12 Apr 2024 10:01:04 +0100 Subject: [PATCH 2/3] fix!: avoid "comptime var" error by returning array instead of slice this changes the semantics of `comptimeEncode`; users will need to, e.g. use ```zig const encoding = comptimeEncode(T); passToObjc(&encoding) ``` to pass the encoding by pointer rather than by value. --- build.zig | 6 +++++- build.zig.zon | 4 ++-- src/block.zig | 2 +- src/class.zig | 2 +- src/encoding.zig | 20 ++++++++++++++------ 5 files changed, 23 insertions(+), 11 deletions(-) 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].*; } } From c9dc8d2505e5d78a2e2b1e45581d9398d2bd069e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 12 Apr 2024 10:30:21 -0700 Subject: [PATCH 3/3] update zig --- flake.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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": {