Skip to content

Commit 5ad8ea6

Browse files
committed
pkg/macos: clean up for Zig 0.14, consolidate C imports into one decl
Fixes ghostty-org#6727 The major change in this commit is to consolidate all the C imports in a single decl in main.zig. This is required for Zig 0.14. Without it, the problem in ghostty-org#6727 will happen. I was never able to minimize why this happens in order to open a Zig bug. Beyond this, I fixed the build.zig and build.zig.zon to work with Zig 0.14 so that we can test building `pkg/macos` in isolation. There are no downstream impacting changes in the build.zig files.
1 parent 550edd4 commit 5ad8ea6

File tree

16 files changed

+57
-43
lines changed

16 files changed

+57
-43
lines changed

pkg/macos/animation/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("QuartzCore/CALayer.h");
3-
});
1+
pub const c = @import("../main.zig").c;

pkg/macos/build.zig

+14-5
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ pub fn build(b: *std.Build) !void {
4545
module.linkFramework("CoreVideo", .{});
4646
module.linkFramework("QuartzCore", .{});
4747

48-
if (!target.query.isNative()) {
49-
try apple_sdk.addPaths(b, lib.root_module);
50-
try apple_sdk.addPaths(b, module);
51-
}
48+
try apple_sdk.addPaths(b, lib.root_module);
49+
try apple_sdk.addPaths(b, module);
5250
}
5351
b.installArtifact(lib);
5452

@@ -59,9 +57,20 @@ pub fn build(b: *std.Build) !void {
5957
.target = target,
6058
.optimize = optimize,
6159
});
60+
if (target.result.os.tag.isDarwin()) {
61+
try apple_sdk.addPaths(b, test_exe.root_module);
62+
}
6263
test_exe.linkLibrary(lib);
64+
6365
var it = module.import_table.iterator();
64-
while (it.next()) |entry| test_exe.root_module.addImport(entry.key_ptr.*, entry.value_ptr.*);
66+
while (it.next()) |entry| {
67+
test_exe.root_module.addImport(
68+
entry.key_ptr.*,
69+
entry.value_ptr.*,
70+
);
71+
}
72+
73+
b.installArtifact(test_exe);
6574

6675
const tests_run = b.addRunArtifact(test_exe);
6776
const test_step = b.step("test", "Run tests");

pkg/macos/build.zig.zon

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.{
2-
.name = "macos",
2+
.name = .macos,
33
.version = "0.1.0",
4+
.fingerprint = 0x45e2f6107d5b2b2c,
45
.paths = .{""},
56
.dependencies = .{
67
.apple_sdk = .{ .path = "../apple-sdk" },

pkg/macos/carbon/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("Carbon/Carbon.h");
3-
});
1+
pub const c = @import("../main.zig").c;

pkg/macos/dispatch/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("dispatch/dispatch.h");
3-
});
1+
pub const c = @import("../main.zig").c;

pkg/macos/foundation/attributed_string.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub const MutableAttributedString = opaque {
6767
) void {
6868
const T = @TypeOf(key);
6969
const info = @typeInfo(T);
70-
const Key = if (info != .Pointer) T else info.Pointer.child;
70+
const Key = if (info != .pointer) T else info.pointer.child;
7171
const key_arg = if (@hasDecl(Key, "key"))
7272
key.key()
7373
else

pkg/macos/foundation/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("CoreFoundation/CoreFoundation.h");
3-
});
1+
pub const c = @import("../main.zig").c;

pkg/macos/graphics/bitmap_context.zig

+9-9
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ test {
3838
const cs = try graphics.ColorSpace.createDeviceGray();
3939
defer cs.release();
4040
const ctx = try BitmapContext.create(null, 80, 80, 8, 80, cs, 0);
41-
defer ctx.release();
42-
43-
ctx.setShouldAntialias(true);
44-
ctx.setShouldSmoothFonts(false);
45-
ctx.setGrayFillColor(1, 1);
46-
ctx.setGrayStrokeColor(1, 1);
47-
ctx.setTextDrawingMode(.fill);
48-
ctx.setTextMatrix(graphics.AffineTransform.identity());
49-
ctx.setTextPosition(0, 0);
41+
const context = BitmapContext.context;
42+
defer context.release(ctx);
43+
context.setShouldAntialias(ctx, true);
44+
context.setShouldSmoothFonts(ctx, false);
45+
context.setGrayFillColor(ctx, 1, 1);
46+
context.setGrayStrokeColor(ctx, 1, 1);
47+
context.setTextDrawingMode(ctx, .fill);
48+
context.setTextMatrix(ctx, graphics.AffineTransform.identity());
49+
context.setTextPosition(ctx, 0, 0);
5050
}

pkg/macos/graphics/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("CoreGraphics/CoreGraphics.h");
3-
});
1+
pub const c = @import("../main.zig").c;

pkg/macos/graphics/path.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const assert = std.debug.assert;
33
const Allocator = std.mem.Allocator;
44
const foundation = @import("../foundation.zig");
55
const graphics = @import("../graphics.zig");
6-
const c = @import("c.zig");
6+
const c = @import("c.zig").c;
77

88
pub const Path = opaque {
99
pub fn createWithRect(

pkg/macos/main.zig

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const builtin = @import("builtin");
2+
13
pub const carbon = @import("carbon.zig");
24
pub const foundation = @import("foundation.zig");
35
pub const animation = @import("animation.zig");
@@ -7,6 +9,23 @@ pub const os = @import("os.zig");
79
pub const text = @import("text.zig");
810
pub const video = @import("video.zig");
911

12+
// All of our C imports consolidated into one place. We used to
13+
// import them one by one in each package but Zig 0.14 has some
14+
// kind of issue with that I wasn't able to minimize.
15+
pub const c = @cImport({
16+
@cInclude("CoreFoundation/CoreFoundation.h");
17+
@cInclude("CoreGraphics/CoreGraphics.h");
18+
@cInclude("CoreText/CoreText.h");
19+
@cInclude("CoreVideo/CoreVideo.h");
20+
@cInclude("QuartzCore/CALayer.h");
21+
@cInclude("dispatch/dispatch.h");
22+
@cInclude("os/log.h");
23+
24+
if (builtin.os.tag == .macos) {
25+
@cInclude("Carbon/Carbon.h");
26+
}
27+
});
28+
1029
test {
1130
@import("std").testing.refAllDecls(@This());
1231
}

pkg/macos/os/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("os/log.h");
3-
});
1+
pub const c = @import("../main.zig").c;

pkg/macos/text/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("CoreText/CoreText.h");
3-
});
1+
pub const c = @import("../main.zig").c;

pkg/macos/text/font.zig

+2-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ test {
280280
const cs = try graphics.ColorSpace.createDeviceGray();
281281
defer cs.release();
282282
const ctx = try graphics.BitmapContext.create(null, 80, 80, 8, 80, cs, 0);
283-
defer ctx.release();
283+
const context = graphics.BitmapContext.context;
284+
defer context.release(ctx);
284285

285286
var pos = [_]graphics.Point{.{ .x = 0, .y = 0 }};
286287
font.drawGlyphs(

pkg/macos/text/font_descriptor.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ test "descriptor" {
276276
const v = try FontDescriptor.createWithNameAndSize(name, 12);
277277
defer v.release();
278278

279-
const copy_name = v.copyAttribute(.name);
279+
const copy_name = v.copyAttribute(.name).?;
280280
defer copy_name.release();
281281

282282
{

pkg/macos/video/c.zig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub const c = @cImport({
2-
@cInclude("CoreVideo/CoreVideo.h");
3-
});
1+
pub const c = @import("../main.zig").c;

0 commit comments

Comments
 (0)