authorgravatar for rohlemF@gmail.comRohlem <rohlemF@gmail.com> 2020-12-09 10:23:27+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-11 17:48:16-07:00
log2922a483099a0a45d4cbaf2bc7c3b43fcebe4e77
tree01c8aa5d1b3332a119baf57db2101a02fd5f5434
parentfc10c9c4ce40570d0abeda764f1bd76b3e79af61

move windows runtime version detection into std.zig.system.windows


3 files changed, 63 insertions(+), 37 deletions(-)

lib/std/target.zig+14
......@@ -109,6 +109,20 @@ pub const Target = struct {
109109 /// Latest Windows version that the Zig Standard Library is aware of
110110 pub const latest = WindowsVersion.win10_20h1;
111111
112 /// Compared against build numbers reported by the runtime to distinguish win10 versions,
113 /// where 0x0A000000 + index corresponds to the WindowsVersion u32 value.
114 pub const known_win10_build_numbers = [_]u32{
115 10240, //win10
116 10586, //win10_th2
117 14393, //win_rs1
118 15063, //win_rs2
119 16299, //win_rs3
120 17134, //win_rs4
121 17763, //win_rs5
122 18362, //win_19h1
123 19041, //win_20h1
124 };
125
112126 pub const Range = struct {
113127 min: WindowsVersion,
114128 max: WindowsVersion,
lib/std/zig/system.zig+4-37
......@@ -14,6 +14,7 @@ const process = std.process;
1414const Target = std.Target;
1515const CrossTarget = std.zig.CrossTarget;
1616const macos = @import("system/macos.zig");
17pub const windows = @import("system/windows.zig");
1718
1819pub const getSDKPath = macos.getSDKPath;
1920
......@@ -249,43 +250,9 @@ pub const NativeTargetInfo = struct {
249250 }
250251 },
251252 .windows => {
252 var version_info: std.os.windows.RTL_OSVERSIONINFOW = undefined;
253 version_info.dwOSVersionInfoSize = @sizeOf(@TypeOf(version_info));
254
255 switch (std.os.windows.ntdll.RtlGetVersion(&version_info)) {
256 .SUCCESS => {},
257 else => unreachable,
258 }
259
260 // Starting from the system infos build a NTDDI-like version
261 // constant whose format is:
262 // B0 B1 B2 B3
263 // `---` `` ``--> Sub-version (Starting from Windows 10 onwards)
264 // \ `--> Service pack (Always zero in the constants defined)
265 // `--> OS version (Major & minor)
266 const os_ver: u16 = @intCast(u16, version_info.dwMajorVersion & 0xff) << 8 |
267 @intCast(u16, version_info.dwMinorVersion & 0xff);
268 const sp_ver: u8 = 0;
269 const sub_ver: u8 = if (os_ver >= 0x0A00) subver: {
270 // There's no other way to obtain this info beside
271 // checking the build number against a known set of
272 // values
273 const known_build_numbers = [_]u32{
274 10240, 10586, 14393, 15063, 16299, 17134, 17763,
275 18362, 19041,
276 };
277 var last_idx: usize = 0;
278 for (known_build_numbers) |build, i| {
279 if (version_info.dwBuildNumber >= build)
280 last_idx = i;
281 }
282 break :subver @truncate(u8, last_idx);
283 } else 0;
284
285 const version: u32 = @as(u32, os_ver) << 16 | @as(u32, sp_ver) << 8 | sub_ver;
286
287 os.version_range.windows.max = @intToEnum(Target.Os.WindowsVersion, version);
288 os.version_range.windows.min = @intToEnum(Target.Os.WindowsVersion, version);
253 const detected_version = windows.detectRuntimeVersion();
254 os.version_range.windows.min = detected_version;
255 os.version_range.windows.max = detected_version;
289256 },
290257 .macos => {
291258 var scbuf: [32]u8 = undefined;
lib/std/zig/system/windows.zig created+45
......@@ -0,0 +1,45 @@
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2015-2020 Zig Contributors
3// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.
6const std = @import("std");
7
8pub const WindowsVersion = std.Target.Os.WindowsVersion;
9
10/// Returns the highest known WindowsVersion deduced from reported runtime information.
11/// Discards information about in-between versions we don't differentiate.
12pub fn detectRuntimeVersion() WindowsVersion {
13 var version_info: std.os.windows.RTL_OSVERSIONINFOW = undefined;
14 version_info.dwOSVersionInfoSize = @sizeOf(@TypeOf(version_info));
15
16 switch (std.os.windows.ntdll.RtlGetVersion(&version_info)) {
17 .SUCCESS => {},
18 else => unreachable,
19 }
20
21 // Starting from the system infos build a NTDDI-like version
22 // constant whose format is:
23 // B0 B1 B2 B3
24 // `---` `` ``--> Sub-version (Starting from Windows 10 onwards)
25 // \ `--> Service pack (Always zero in the constants defined)
26 // `--> OS version (Major & minor)
27 const os_ver: u16 = @intCast(u16, version_info.dwMajorVersion & 0xff) << 8 |
28 @intCast(u16, version_info.dwMinorVersion & 0xff);
29 const sp_ver: u8 = 0;
30 const sub_ver: u8 = if (os_ver >= 0x0A00) subver: {
31 // There's no other way to obtain this info beside
32 // checking the build number against a known set of
33 // values
34 var last_idx: usize = 0;
35 for (WindowsVersion.known_win10_build_numbers) |build, i| {
36 if (version_info.dwBuildNumber >= build)
37 last_idx = i;
38 }
39 break :subver @truncate(u8, last_idx);
40 } else 0;
41
42 const version: u32 = @as(u32, os_ver) << 16 | @as(u16, sp_ver) << 8 | sub_ver;
43
44 return @intToEnum(WindowsVersion, version);
45}