Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

target: Implement OS version detection for OSX #4589

Merged
merged 3 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,7 @@ pub const SysCtlError = error{
PermissionDenied,
SystemResources,
NameTooLong,
UnknownName,
} || UnexpectedError;

pub fn sysctl(
Expand All @@ -2779,6 +2780,7 @@ pub fn sysctl(
EFAULT => unreachable,
EPERM => return error.PermissionDenied,
ENOMEM => return error.SystemResources,
ENOENT => return error.UnknownName,
else => |err| return unexpectedErrno(err),
}
}
Expand All @@ -2795,6 +2797,7 @@ pub fn sysctlbynameC(
EFAULT => unreachable,
EPERM => return error.PermissionDenied,
ENOMEM => return error.SystemResources,
ENOENT => return error.UnknownName,
else => |err| return unexpectedErrno(err),
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub const Thread = struct {
var count_len: usize = @sizeOf(c_int);
const name = if (comptime std.Target.current.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
os.sysctlbynameC(name, &count, &count_len, null, 0) catch |err| switch (err) {
error.NameTooLong => unreachable,
error.NameTooLong, error.UnknownName => unreachable,
else => |e| return e,
};
return @intCast(usize, count);
Expand Down
26 changes: 25 additions & 1 deletion lib/std/zig/system.zig
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,31 @@ pub const NativeTargetInfo = struct {
// TODO Detect native operating system version.
},
.macosx => {
// TODO Detect native operating system version.
var product_version: [32]u8 = undefined;
var size: usize = product_version.len;

// The osproductversion sysctl was introduced first with
// High Sierra, thankfully that's also the baseline that Zig
// supports
std.os.sysctlbynameC(
"kern.osproductversion",
&product_version[0],
&size,
null,
0,
) catch |err| switch (err) {
error.UnknownName => unreachable,
else => unreachable,
};

if (std.builtin.Version.parse(product_version[0..size])) |ver| {
os.version_range.semver.min = ver;
os.version_range.semver.max = ver;
} else |err| switch (err) {
error.Overflow => {},
error.InvalidCharacter => {},
error.InvalidVersion => {},
}
},
.freebsd => {
// TODO Detect native operating system version.
Expand Down