aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-02-28 03:11:37 -0500
committerAndrew Kelley <andrew@ziglang.org>2020-02-28 14:51:56 -0500
commit07f52119de2a8bdb84389c73332e113cf12ac997 (patch)
tree807c9e2724d46f23fda91d25db212601df8ee5cc /lib/std
parentef24f2dd93729493531c427aaac54444597f6e66 (diff)
downloadzig-07f52119de2a8bdb84389c73332e113cf12ac997.tar.gz
zig-07f52119de2a8bdb84389c73332e113cf12ac997.zip
implement native OS version detection for linux
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/c.zig1
-rw-r--r--lib/std/os.zig24
-rw-r--r--lib/std/zig/system.zig17
3 files changed, 30 insertions, 12 deletions
diff --git a/lib/std/c.zig b/lib/std/c.zig
index 9d7d9524d6..48a3039f51 100644
--- a/lib/std/c.zig
+++ b/lib/std/c.zig
@@ -125,6 +125,7 @@ pub extern "c" fn sysctlnametomib(name: [*:0]const u8, mibp: ?*c_int, sizep: ?*u
pub extern "c" fn tcgetattr(fd: fd_t, termios_p: *termios) c_int;
pub extern "c" fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) c_int;
pub extern "c" fn fcntl(fd: fd_t, cmd: c_int, ...) c_int;
+pub extern "c" fn uname(buf: *utsname) c_int;
pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
diff --git a/lib/std/os.zig b/lib/std/os.zig
index 9f349e7dc4..49e88bf9c7 100644
--- a/lib/std/os.zig
+++ b/lib/std/os.zig
@@ -3295,22 +3295,24 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
}
}
if (builtin.os.tag == .linux) {
- var uts: utsname = undefined;
- switch (errno(system.uname(&uts))) {
- 0 => {
- const hostname = mem.toSlice(u8, @ptrCast([*:0]u8, &uts.nodename));
- mem.copy(u8, name_buffer, hostname);
- return name_buffer[0..hostname.len];
- },
- EFAULT => unreachable,
- EPERM => return error.PermissionDenied,
- else => |err| return unexpectedErrno(err),
- }
+ const uts = uname();
+ const hostname = mem.toSliceConst(u8, @ptrCast([*:0]const u8, &uts.nodename));
+ mem.copy(u8, name_buffer, hostname);
+ return name_buffer[0..hostname.len];
}
@compileError("TODO implement gethostname for this OS");
}
+pub fn uname() utsname {
+ var uts: utsname = undefined;
+ switch (errno(system.uname(&uts))) {
+ 0 => return uts,
+ EFAULT => unreachable,
+ else => unreachable,
+ }
+}
+
pub fn res_mkquery(
op: u4,
dname: []const u8,
diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig
index fe2f4c7e04..ffae5c6015 100644
--- a/lib/std/zig/system.zig
+++ b/lib/std/zig/system.zig
@@ -200,7 +200,22 @@ pub const NativeTargetInfo = struct {
const cpu = Target.Cpu.baseline(arch);
// TODO Detect native operating system version. Until that is implemented we use the default range.
- const os = Target.Os.defaultVersionRange(os_tag);
+ var os = Target.Os.defaultVersionRange(os_tag);
+ switch (Target.current.os.tag) {
+ .linux => {
+ const uts = std.os.uname();
+ const release = mem.toSliceConst(u8, @ptrCast([*:0]const u8, &uts.release));
+ if (std.builtin.Version.parse(release)) |ver| {
+ os.version_range.linux.range.min = ver;
+ os.version_range.linux.range.max = ver;
+ } else |err| switch (err) {
+ error.Overflow => {},
+ error.InvalidCharacter => {},
+ error.InvalidVersion => {},
+ }
+ },
+ else => {},
+ }
return detectAbiAndDynamicLinker(allocator, cpu, os);
}