Skip to content

Commit

Permalink
target: Implement OS version detection for FreeBSD
Browse files Browse the repository at this point in the history
  • Loading branch information
jethron committed May 2, 2020
1 parent ef92949 commit 821f82b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/std/zig/system.zig
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,21 @@ pub const NativeTargetInfo = struct {
}
},
.freebsd => {
// Unimplemented, fall back to default.
// https://github.com/ziglang/zig/issues/4582
var osreldate: u32 = undefined;
var len: usize = undefined;

std.os.sysctlbynameZ("kern.osreldate", &osreldate, &len, null, 0) catch unreachable;

// https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/versions.html
// Major * 100,000 has been convention since FreeBSD 2.2 (1997)
// Minor * 1(0),000 summed has been convention since FreeBSD 2.2 (1997)
// e.g. 492101 = 4.11-STABLE = 4.(9+2)
const major = osreldate / 100_000;
const minor1 = osreldate % 100_000 / 10_000; // usually 0 since 5.1
const minor2 = osreldate % 10_000 / 1_000; // 0 before 5.1, minor version since
const patch = osreldate % 1_000;
os.version_range.semver.min = .{ .major = major, .minor = minor1 + minor2, .patch = patch };
os.version_range.semver.max = .{ .major = major, .minor = minor1 + minor2, .patch = patch };
},
else => {
// Unimplemented, fall back to default version range.
Expand Down

0 comments on commit 821f82b

Please sign in to comment.