跳转到内容

std.zig.system.darwin

标准库Zig 0.17.0官方原文
源码std/zig/system/darwin.zig

第 1 行

std.zig.system.darwin.Allocator

别名

第 4 行

别名目标 std.mem.Allocator

std.zig.system.darwin.getSdk

函数

第 38 行

检测 Darwin 上的 SDK。 调用 xcrun --sdk <target_sdk> --show-sdk-path 获取 SDK 路径。 调用者拥有内存的所有权。 忽略 xcrun 的 stderr。 若 Allocator 发生 error.OutOfMemory,此函数返回 null。

pub fn getSdk(gpa: Allocator, io: Io, target: *const Target) ?[]const u8

参数

gpa: Allocator
io: Io
target: *const Target

std.zig.system.darwin.Io

别名

第 2 行

别名目标 std.Io

std.zig.system.darwin.isSdkInstalled

函数

第 19 行

检查 Darwin 上是否安装 SDK,不触发 CLT 安装弹窗。

直接调用 xcrun 必然触发 CLT 安装弹窗。 因此改为调用 xcode-select --print-path, 并检查退出状态是否非零。

忽略 xcode-select 的 stderr。

若 Allocator 发生 error.OutOfMemory,此函数返回 null。

pub fn isSdkInstalled(gpa: Allocator, io: Io) bool

参数

gpa: Allocator
io: Io

std.zig.system.darwin.macos

别名

第 8 行

别名目标 std.zig.system.darwin.macos

std.zig.system.darwin.mem

别名

第 3 行

别名目标 std.mem

std.zig.system.darwin.std

别名

第 1 行

别名目标 std

std.zig.system.darwin.Target

别名

第 5 行

别名目标 std.Target

std.zig.system.darwin.Version

别名

第 6 行

别名目标 std.SemanticVersion

源码

std/zig/system/darwin.zig

const std = @import("std");
const Io = std.Io;
const mem = std.mem;
const Allocator = std.mem.Allocator;
const Target = std.Target;
const Version = std.SemanticVersion;

pub const macos = @import("darwin/macos.zig");

/// 检查 Darwin 上是否安装 SDK,不触发 CLT 安装弹窗。
///
/// 直接调用 `xcrun` 必然触发 CLT 安装弹窗。
/// 因此改为调用 `xcode-select --print-path`,
/// 并检查退出状态是否非零。
///
/// 忽略 xcode-select 的 stderr。
///
/// 若 Allocator 发生 error.OutOfMemory,此函数返回 null。
pub fn isSdkInstalled(gpa: Allocator, io: Io) bool {
    const result = std.process.run(gpa, io, .{
        .argv = &.{ "xcode-select", "--print-path" },
    }) catch return false;
    defer {
        gpa.free(result.stderr);
        gpa.free(result.stdout);
    }
    return switch (result.term) {
        .exited => |code| if (code == 0) result.stdout.len > 0 else false,
        else => false,
    };
}

/// 检测 Darwin 上的 SDK。
/// 调用 `xcrun --sdk <target_sdk> --show-sdk-path` 获取 SDK 路径。
/// 调用者拥有内存的所有权。
/// 忽略 xcrun 的 stderr。
/// 若 Allocator 发生 error.OutOfMemory,此函数返回 null。
pub fn getSdk(gpa: Allocator, io: Io, target: *const Target) ?[]const u8 {
    const is_simulator_abi = target.abi == .simulator;
    const sdk = switch (target.os.tag) {
        .driverkit => "driverkit",
        .ios => if (is_simulator_abi) "iphonesimulator" else "iphoneos",
        .maccatalyst, .macos => "macosx",
        .tvos => if (is_simulator_abi) "appletvsimulator" else "appletvos",
        .visionos => if (is_simulator_abi) "xrsimulator" else "xros",
        .watchos => if (is_simulator_abi) "watchsimulator" else "watchos",
        else => return null,
    };
    const argv = &[_][]const u8{ "xcrun", "--sdk", sdk, "--show-sdk-path" };
    const result = std.process.run(gpa, io, .{ .argv = argv }) catch return null;
    defer {
        gpa.free(result.stderr);
        gpa.free(result.stdout);
    }
    switch (result.term) {
        .exited => |code| if (code != 0) return null,
        else => return null,
    }
    return gpa.dupe(u8, mem.trimEnd(u8, result.stdout, "\r\n")) catch null;
}

test {
    _ = macos;
}