zig-objc/build.zig

87 lines
3.1 KiB
Zig
Raw Normal View History

2023-01-02 14:48:21 -08:00
const std = @import("std");
2023-02-14 18:03:43 -08:00
2024-11-03 14:57:58 -08:00
pub fn build(b: *std.Build) !void {
2023-02-14 18:03:43 -08:00
const optimize = b.standardOptimizeOption(.{});
2023-01-02 14:48:21 -08:00
const target = b.standardTargetOptions(.{});
2024-11-03 14:57:58 -08:00
const add_paths = b.option(
bool,
"add-paths",
"add apple SDK paths from Xcode installation",
) orelse true;
2023-01-02 14:48:21 -08:00
const objc = b.addModule("objc", .{
2024-06-08 19:57:59 -07:00
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
2024-11-03 14:57:58 -08:00
if (add_paths) try addAppleSDK(b, objc);
objc.linkSystemLibrary("objc", .{});
objc.linkFramework("Foundation", .{});
2023-08-03 16:53:26 -07:00
2023-02-14 18:03:43 -08:00
const tests = b.addTest(.{
.name = "objc-test",
2024-06-08 19:57:59 -07:00
.root_source_file = b.path("src/main.zig"),
2023-02-14 18:03:43 -08:00
.target = target,
.optimize = optimize,
});
2023-01-02 14:48:21 -08:00
tests.linkSystemLibrary("objc");
2023-10-20 15:36:55 -07:00
tests.linkFramework("Foundation");
2025-01-18 15:05:30 +01:00
try addAppleSDK(b, tests.root_module);
2023-06-30 09:49:12 -07:00
b.installArtifact(tests);
2023-01-02 14:48:21 -08:00
const test_step = b.step("test", "Run tests");
2023-06-30 09:49:12 -07:00
const tests_run = b.addRunArtifact(tests);
2023-01-02 14:48:21 -08:00
test_step.dependOn(&tests_run.step);
}
2024-11-03 14:57:58 -08:00
/// Add the SDK framework, include, and library paths to the given module.
/// The module target is used to determine the SDK to use so it must have
/// a resolved target.
///
/// The Apple SDK is determined based on the build target and found using
/// xcrun, so it requires a valid Xcode installation.
pub fn addAppleSDK(b: *std.Build, m: *std.Build.Module) !void {
// The cache. This always uses b.allocator and never frees memory
// (which is idiomatic for a Zig build exe).
const Cache = struct {
const Key = struct {
arch: std.Target.Cpu.Arch,
os: std.Target.Os.Tag,
abi: std.Target.Abi,
};
var map: std.AutoHashMapUnmanaged(Key, ?[]const u8) = .{};
};
const target = m.resolved_target.?.result;
const gop = try Cache.map.getOrPut(b.allocator, .{
.arch = target.cpu.arch,
.os = target.os.tag,
.abi = target.abi,
});
// This executes `xcrun` to get the SDK path. We don't want to execute
// this multiple times so we cache the value.
if (!gop.found_existing) {
gop.value_ptr.* = std.zig.system.darwin.getSdk(
b.allocator,
m.resolved_target.?.result,
);
}
// The active SDK we want to use
const path = gop.value_ptr.* orelse return switch (target.os.tag) {
// Return a more descriptive error. Before we just returned the
// generic error but this was confusing a lot of community members.
// It costs us nothing in the build script to return something better.
.macos => error.XcodeMacOSSDKNotFound,
.ios => error.XcodeiOSSDKNotFound,
.tvos => error.XcodeTVOSSDKNotFound,
.watchos => error.XcodeWatchOSSDKNotFound,
else => error.XcodeAppleSDKNotFound,
};
m.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/System/Library/Frameworks" }) });
m.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/include" }) });
m.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/lib" }) });
}