pkg/macos: clean up for Zig 0.14, consolidate C imports into one decl

Fixes #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 #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.
This commit is contained in:
Mitchell Hashimoto 2025-03-14 14:43:45 -07:00
parent 550edd4262
commit 5ad8ea6b22
No known key found for this signature in database
GPG Key ID: 523D5DC389D273BC
16 changed files with 57 additions and 43 deletions

View File

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

View File

@ -45,10 +45,8 @@ pub fn build(b: *std.Build) !void {
module.linkFramework("CoreVideo", .{});
module.linkFramework("QuartzCore", .{});
if (!target.query.isNative()) {
try apple_sdk.addPaths(b, lib.root_module);
try apple_sdk.addPaths(b, module);
}
try apple_sdk.addPaths(b, lib.root_module);
try apple_sdk.addPaths(b, module);
}
b.installArtifact(lib);
@ -59,9 +57,20 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
if (target.result.os.tag.isDarwin()) {
try apple_sdk.addPaths(b, test_exe.root_module);
}
test_exe.linkLibrary(lib);
var it = module.import_table.iterator();
while (it.next()) |entry| test_exe.root_module.addImport(entry.key_ptr.*, entry.value_ptr.*);
while (it.next()) |entry| {
test_exe.root_module.addImport(
entry.key_ptr.*,
entry.value_ptr.*,
);
}
b.installArtifact(test_exe);
const tests_run = b.addRunArtifact(test_exe);
const test_step = b.step("test", "Run tests");

View File

@ -1,6 +1,7 @@
.{
.name = "macos",
.name = .macos,
.version = "0.1.0",
.fingerprint = 0x45e2f6107d5b2b2c,
.paths = .{""},
.dependencies = .{
.apple_sdk = .{ .path = "../apple-sdk" },

View File

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

View File

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

View File

@ -67,7 +67,7 @@ pub const MutableAttributedString = opaque {
) void {
const T = @TypeOf(key);
const info = @typeInfo(T);
const Key = if (info != .Pointer) T else info.Pointer.child;
const Key = if (info != .pointer) T else info.pointer.child;
const key_arg = if (@hasDecl(Key, "key"))
key.key()
else

View File

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

View File

@ -38,13 +38,13 @@ test {
const cs = try graphics.ColorSpace.createDeviceGray();
defer cs.release();
const ctx = try BitmapContext.create(null, 80, 80, 8, 80, cs, 0);
defer ctx.release();
ctx.setShouldAntialias(true);
ctx.setShouldSmoothFonts(false);
ctx.setGrayFillColor(1, 1);
ctx.setGrayStrokeColor(1, 1);
ctx.setTextDrawingMode(.fill);
ctx.setTextMatrix(graphics.AffineTransform.identity());
ctx.setTextPosition(0, 0);
const context = BitmapContext.context;
defer context.release(ctx);
context.setShouldAntialias(ctx, true);
context.setShouldSmoothFonts(ctx, false);
context.setGrayFillColor(ctx, 1, 1);
context.setGrayStrokeColor(ctx, 1, 1);
context.setTextDrawingMode(ctx, .fill);
context.setTextMatrix(ctx, graphics.AffineTransform.identity());
context.setTextPosition(ctx, 0, 0);
}

View File

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

View File

@ -3,7 +3,7 @@ const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const foundation = @import("../foundation.zig");
const graphics = @import("../graphics.zig");
const c = @import("c.zig");
const c = @import("c.zig").c;
pub const Path = opaque {
pub fn createWithRect(

View File

@ -1,3 +1,5 @@
const builtin = @import("builtin");
pub const carbon = @import("carbon.zig");
pub const foundation = @import("foundation.zig");
pub const animation = @import("animation.zig");
@ -7,6 +9,23 @@ pub const os = @import("os.zig");
pub const text = @import("text.zig");
pub const video = @import("video.zig");
// All of our C imports consolidated into one place. We used to
// import them one by one in each package but Zig 0.14 has some
// kind of issue with that I wasn't able to minimize.
pub const c = @cImport({
@cInclude("CoreFoundation/CoreFoundation.h");
@cInclude("CoreGraphics/CoreGraphics.h");
@cInclude("CoreText/CoreText.h");
@cInclude("CoreVideo/CoreVideo.h");
@cInclude("QuartzCore/CALayer.h");
@cInclude("dispatch/dispatch.h");
@cInclude("os/log.h");
if (builtin.os.tag == .macos) {
@cInclude("Carbon/Carbon.h");
}
});
test {
@import("std").testing.refAllDecls(@This());
}

View File

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

View File

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

View File

@ -280,7 +280,8 @@ test {
const cs = try graphics.ColorSpace.createDeviceGray();
defer cs.release();
const ctx = try graphics.BitmapContext.create(null, 80, 80, 8, 80, cs, 0);
defer ctx.release();
const context = graphics.BitmapContext.context;
defer context.release(ctx);
var pos = [_]graphics.Point{.{ .x = 0, .y = 0 }};
font.drawGlyphs(

View File

@ -276,7 +276,7 @@ test "descriptor" {
const v = try FontDescriptor.createWithNameAndSize(name, 12);
defer v.release();
const copy_name = v.copyAttribute(.name);
const copy_name = v.copyAttribute(.name).?;
defer copy_name.release();
{

View File

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