| ... | @@ -1,5 +1,5 @@ | ... | @@ -1,5 +1,5 @@ |
| 1 | windows10sdk: ?Windows10Sdk, | 1 | windows10sdk: ?Installation, |
| 2 | windows81sdk: ?Windows81Sdk, | 2 | windows81sdk: ?Installation, |
| 3 | msvc_lib_dir: ?[]const u8, | 3 | msvc_lib_dir: ?[]const u8, |
| 4 | | 4 | |
| 5 | const WindowsSdk = @This(); | 5 | const WindowsSdk = @This(); |
| ... | @@ -9,7 +9,7 @@ const builtin = @import("builtin"); | ... | @@ -9,7 +9,7 @@ const builtin = @import("builtin"); |
| 9 | const windows = std.os.windows; | 9 | const windows = std.os.windows; |
| 10 | const RRF = windows.advapi32.RRF; | 10 | const RRF = windows.advapi32.RRF; |
| 11 | | 11 | |
| 12 | const WINDOWS_KIT_REG_KEY = "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots"; | 12 | const windows_kits_reg_key = "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots"; |
| 13 | | 13 | |
| 14 | // https://learn.microsoft.com/en-us/windows/win32/msi/productversion | 14 | // https://learn.microsoft.com/en-us/windows/win32/msi/productversion |
| 15 | const version_major_minor_max_length = "255.255".len; | 15 | const version_major_minor_max_length = "255.255".len; |
| ... | @@ -23,34 +23,24 @@ pub fn find(allocator: std.mem.Allocator) error{ OutOfMemory, NotFound, PathTooL | ... | @@ -23,34 +23,24 @@ pub fn find(allocator: std.mem.Allocator) error{ OutOfMemory, NotFound, PathTooL |
| 23 | if (builtin.os.tag != .windows) return error.NotFound; | 23 | if (builtin.os.tag != .windows) return error.NotFound; |
| 24 | | 24 | |
| 25 | //note(dimenus): If this key doesn't exist, neither the Win 8 SDK nor the Win 10 SDK is installed | 25 | //note(dimenus): If this key doesn't exist, neither the Win 8 SDK nor the Win 10 SDK is installed |
| 26 | const roots_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, WINDOWS_KIT_REG_KEY) catch |err| switch (err) { | 26 | const roots_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, windows_kits_reg_key) catch |err| switch (err) { |
| 27 | error.KeyNotFound => return error.NotFound, | 27 | error.KeyNotFound => return error.NotFound, |
| 28 | }; | 28 | }; |
| 29 | defer roots_key.closeKey(); | 29 | defer roots_key.closeKey(); |
| 30 | | 30 | |
| 31 | const windows10sdk: ?Windows10Sdk = blk: { | 31 | const windows10sdk = Installation.find(allocator, roots_key, "KitsRoot10", "", "v10.0") catch |err| switch (err) { |
| 32 | const windows10sdk = Windows10Sdk.find(allocator) catch |err| switch (err) { | 32 | error.InstallationNotFound => null, |
| 33 | error.Windows10SdkNotFound, | 33 | error.PathTooLong => null, |
| 34 | error.PathTooLong, | 34 | error.VersionTooLong => null, |
| 35 | error.VersionTooLong, | 35 | error.OutOfMemory => return error.OutOfMemory, |
| 36 | => break :blk null, | | |
| 37 | error.OutOfMemory => return error.OutOfMemory, | | |
| 38 | }; | | |
| 39 | const is_valid_version = windows10sdk.isValidVersion(); | | |
| 40 | if (!is_valid_version) break :blk null; | | |
| 41 | break :blk windows10sdk; | | |
| 42 | }; | 36 | }; |
| 43 | errdefer if (windows10sdk) |*w| w.free(allocator); | 37 | errdefer if (windows10sdk) |*w| w.free(allocator); |
| 44 | | 38 | |
| 45 | const windows81sdk: ?Windows81Sdk = blk: { | 39 | const windows81sdk = Installation.find(allocator, roots_key, "KitsRoot81", "winver", "v8.1") catch |err| switch (err) { |
| 46 | const windows81sdk = Windows81Sdk.find(allocator, &roots_key) catch |err| switch (err) { | 40 | error.InstallationNotFound => null, |
| 47 | error.Windows81SdkNotFound => break :blk null, | 41 | error.PathTooLong => null, |
| 48 | error.PathTooLong => break :blk null, | 42 | error.VersionTooLong => null, |
| 49 | error.VersionTooLong => break :blk null, | 43 | error.OutOfMemory => return error.OutOfMemory, |
| 50 | error.OutOfMemory => return error.OutOfMemory, | | |
| 51 | }; | | |
| 52 | // no check | | |
| 53 | break :blk windows81sdk; | | |
| 54 | }; | 44 | }; |
| 55 | errdefer if (windows81sdk) |*w| w.free(allocator); | 45 | errdefer if (windows81sdk) |*w| w.free(allocator); |
| 56 | | 46 | |
| ... | @@ -60,96 +50,91 @@ pub fn find(allocator: std.mem.Allocator) error{ OutOfMemory, NotFound, PathTooL | ... | @@ -60,96 +50,91 @@ pub fn find(allocator: std.mem.Allocator) error{ OutOfMemory, NotFound, PathTooL |
| 60 | }; | 50 | }; |
| 61 | errdefer allocator.free(msvc_lib_dir); | 51 | errdefer allocator.free(msvc_lib_dir); |
| 62 | | 52 | |
| 63 | return WindowsSdk{ | 53 | return .{ |
| 64 | .windows10sdk = windows10sdk, | 54 | .windows10sdk = windows10sdk, |
| 65 | .windows81sdk = windows81sdk, | 55 | .windows81sdk = windows81sdk, |
| 66 | .msvc_lib_dir = msvc_lib_dir, | 56 | .msvc_lib_dir = msvc_lib_dir, |
| 67 | }; | 57 | }; |
| 68 | } | 58 | } |
| 69 | | 59 | |
| 70 | pub fn free(self: *const WindowsSdk, allocator: std.mem.Allocator) void { | 60 | pub fn free(sdk: WindowsSdk, allocator: std.mem.Allocator) void { |
| 71 | if (self.windows10sdk) |*w10sdk| { | 61 | if (sdk.windows10sdk) |*w10sdk| { |
| 72 | w10sdk.free(allocator); | 62 | w10sdk.free(allocator); |
| 73 | } | 63 | } |
| 74 | if (self.windows81sdk) |*w81sdk| { | 64 | if (sdk.windows81sdk) |*w81sdk| { |
| 75 | w81sdk.free(allocator); | 65 | w81sdk.free(allocator); |
| 76 | } | 66 | } |
| 77 | if (self.msvc_lib_dir) |msvc_lib_dir| { | 67 | if (sdk.msvc_lib_dir) |msvc_lib_dir| { |
| 78 | allocator.free(msvc_lib_dir); | 68 | allocator.free(msvc_lib_dir); |
| 79 | } | 69 | } |
| 80 | } | 70 | } |
| 81 | | 71 | |
| 82 | /// Iterates via `iterator` and collects all folders with names starting with `optional_prefix` | 72 | /// Iterates via `iterator` and collects all folders with names starting with `strip_prefix` |
| 83 | /// and similar to SemVer. Returns slice of folder names sorted in descending order. | 73 | /// and a version. Returns slice of version strings sorted in descending order. |
| 84 | /// Caller owns result. | 74 | /// Caller owns result. |
| 85 | fn iterateAndFilterBySemVer( | 75 | fn iterateAndFilterByVersion( |
| 86 | iterator: *std.fs.Dir.Iterator, | 76 | iterator: *std.fs.Dir.Iterator, |
| 87 | allocator: std.mem.Allocator, | 77 | allocator: std.mem.Allocator, |
| 88 | comptime optional_prefix: ?[]const u8, | 78 | prefix: []const u8, |
| 89 | ) error{ OutOfMemory, VersionNotFound }![][]const u8 { | 79 | ) error{OutOfMemory}![][]const u8 { |
| 90 | var dirs_filtered_list = std.ArrayList([]const u8).init(allocator); | 80 | const Version = struct { |
| 91 | errdefer { | 81 | nums: [4]u32, |
| 92 | for (dirs_filtered_list.items) |filtered_dir| allocator.free(filtered_dir); | 82 | build: []const u8, |
| 93 | dirs_filtered_list.deinit(); | 83 | |
| 94 | } | 84 | fn parseNum(num: []const u8) ?u32 { |
| 95 | | 85 | if (num[0] == '0' and num.len > 1) return null; |
| 96 | var normalized_name_buf: [std.fs.MAX_NAME_BYTES + ".0+build.0".len]u8 = undefined; | 86 | return std.fmt.parseInt(u32, num, 10) catch null; |
| 97 | var normalized_name_fbs = std.io.fixedBufferStream(&normalized_name_buf); | 87 | } |
| 98 | const normalized_name_w = normalized_name_fbs.writer(); | | |
| 99 | iterate_folder: while (true) : (normalized_name_fbs.reset()) { | | |
| 100 | const maybe_entry = iterator.next() catch continue :iterate_folder; | | |
| 101 | const entry = maybe_entry orelse break :iterate_folder; | | |
| 102 | | | |
| 103 | if (entry.kind != .directory) | | |
| 104 | continue :iterate_folder; | | |
| 105 | | | |
| 106 | // invalidated on next iteration | | |
| 107 | const subfolder_name = blk: { | | |
| 108 | if (comptime optional_prefix) |prefix| { | | |
| 109 | if (!std.mem.startsWith(u8, entry.name, prefix)) continue :iterate_folder; | | |
| 110 | break :blk entry.name[prefix.len..]; | | |
| 111 | } else break :blk entry.name; | | |
| 112 | }; | | |
| 113 | | | |
| 114 | { // check if subfolder name looks similar to SemVer | | |
| 115 | switch (std.mem.count(u8, subfolder_name, ".")) { | | |
| 116 | 0 => normalized_name_w.print("{s}.0.0+build.0", .{subfolder_name}) catch unreachable, // 17 => 17.0.0+build.0 | | |
| 117 | 1 => if (std.mem.indexOfScalar(u8, subfolder_name, '_')) |underscore_pos| blk: { // 17.0_9e9cbb98 => 17.0.1+build.9e9cbb98 | | |
| 118 | var subfolder_name_tmp_copy_buf: [std.fs.MAX_NAME_BYTES]u8 = undefined; | | |
| 119 | const subfolder_name_tmp_copy = subfolder_name_tmp_copy_buf[0..subfolder_name.len]; | | |
| 120 | @memcpy(subfolder_name_tmp_copy, subfolder_name); | | |
| 121 | | | |
| 122 | subfolder_name_tmp_copy[underscore_pos] = '.'; // 17.0_9e9cbb98 => 17.0.9e9cbb98 | | |
| 123 | var subfolder_name_parts = std.mem.splitScalar(u8, subfolder_name_tmp_copy, '.'); // [ 17, 0, 9e9cbb98 ] | | |
| 124 | | | |
| 125 | const first = subfolder_name_parts.first(); // 17 | | |
| 126 | const second = subfolder_name_parts.next().?; // 0 | | |
| 127 | const third = subfolder_name_parts.rest(); // 9e9cbb98 | | |
| 128 | | 88 | |
| 129 | break :blk normalized_name_w.print("{s}.{s}.1+build.{s}", .{ first, second, third }) catch unreachable; // [ 17, 0, 9e9cbb98 ] => 17.0.1+build.9e9cbb98 | 89 | fn order(lhs: @This(), rhs: @This()) std.math.Order { |
| 130 | } else normalized_name_w.print("{s}.0+build.0", .{subfolder_name}) catch unreachable, // 17.0 => 17.0.0+build.0 | 90 | return std.mem.order(u32, &lhs.nums, &rhs.nums).differ() orelse |
| 131 | else => normalized_name_w.print("{s}+build.0", .{subfolder_name}) catch unreachable, // 17.0.0 => 17.0.0+build.0 | 91 | std.mem.order(u8, lhs.build, rhs.build); |
| 132 | } | | |
| 133 | const subfolder_name_normalized: []const u8 = normalized_name_fbs.getWritten(); | | |
| 134 | const sem_ver = std.SemanticVersion.parse(subfolder_name_normalized); | | |
| 135 | _ = sem_ver catch continue :iterate_folder; | | |
| 136 | } | 92 | } |
| 137 | // entry.name passed check | 93 | }; |
| | 94 | var versions = std.ArrayList(Version).init(allocator); |
| | 95 | var dirs = std.ArrayList([]const u8).init(allocator); |
| | 96 | defer { |
| | 97 | versions.deinit(); |
| | 98 | for (dirs.items) |filtered_dir| allocator.free(filtered_dir); |
| | 99 | dirs.deinit(); |
| | 100 | } |
| | 101 | |
| | 102 | iterate: while (iterator.next() catch null) |entry| { |
| | 103 | if (entry.kind != .directory) continue; |
| | 104 | if (!std.mem.startsWith(u8, entry.name, prefix)) continue; |
| 138 | | 105 | |
| 139 | const subfolder_name_allocated = try allocator.dupe(u8, subfolder_name); | 106 | var version: Version = .{ |
| 140 | errdefer allocator.free(subfolder_name_allocated); | 107 | .nums = .{0} ** 4, |
| 141 | try dirs_filtered_list.append(subfolder_name_allocated); | 108 | .build = "", |
| | 109 | }; |
| | 110 | const suffix = entry.name[prefix.len..]; |
| | 111 | const underscore = std.mem.indexOfScalar(u8, entry.name, '_'); |
| | 112 | var num_it = std.mem.splitScalar(u8, suffix[0 .. underscore orelse suffix.len], '.'); |
| | 113 | version.nums[0] = Version.parseNum(num_it.first()) orelse continue; |
| | 114 | for (version.nums[1..]) |*num| |
| | 115 | num.* = Version.parseNum(num_it.next() orelse break) orelse continue :iterate |
| | 116 | else if (num_it.next()) |_| continue; |
| | 117 | |
| | 118 | const name = try allocator.dupe(u8, suffix); |
| | 119 | errdefer allocator.free(name); |
| | 120 | if (underscore) |pos| version.build = name[pos + 1 ..]; |
| | 121 | |
| | 122 | try versions.append(version); |
| | 123 | try dirs.append(name); |
| 142 | } | 124 | } |
| 143 | | 125 | |
| 144 | const dirs_filtered_slice = try dirs_filtered_list.toOwnedSlice(); | 126 | std.mem.sortUnstableContext(0, dirs.items.len, struct { |
| 145 | // Keep in mind that order of these names is not guaranteed by Windows, | 127 | versions: []Version, |
| 146 | // so we cannot just reverse or "while (popOrNull())" this ArrayList. | 128 | dirs: [][]const u8, |
| 147 | std.mem.sortUnstable([]const u8, dirs_filtered_slice, {}, struct { | 129 | pub fn lessThan(context: @This(), lhs: usize, rhs: usize) bool { |
| 148 | fn desc(_: void, lhs: []const u8, rhs: []const u8) bool { | 130 | return context.versions[lhs].order(context.versions[rhs]).compare(.gt); |
| 149 | return std.mem.order(u8, lhs, rhs) == .gt; | 131 | } |
| | 132 | pub fn swap(context: @This(), lhs: usize, rhs: usize) void { |
| | 133 | std.mem.swap(Version, &context.versions[lhs], &context.versions[rhs]); |
| | 134 | std.mem.swap([]const u8, &context.dirs[lhs], &context.dirs[rhs]); |
| 150 | } | 135 | } |
| 151 | }.desc); | 136 | }{ .versions = versions.items, .dirs = dirs.items }); |
| 152 | return dirs_filtered_slice; | 137 | return dirs.toOwnedSlice(); |
| 153 | } | 138 | } |
| 154 | | 139 | |
| 155 | const RegistryWtf8 = struct { | 140 | const RegistryWtf8 = struct { |
| ... | @@ -167,12 +152,12 @@ const RegistryWtf8 = struct { | ... | @@ -167,12 +152,12 @@ const RegistryWtf8 = struct { |
| 167 | }; | 152 | }; |
| 168 | | 153 | |
| 169 | const registry_wtf16le = try RegistryWtf16Le.openKey(hkey, key_wtf16le); | 154 | const registry_wtf16le = try RegistryWtf16Le.openKey(hkey, key_wtf16le); |
| 170 | return RegistryWtf8{ .key = registry_wtf16le.key }; | 155 | return .{ .key = registry_wtf16le.key }; |
| 171 | } | 156 | } |
| 172 | | 157 | |
| 173 | /// Closes key, after that usage is invalid | 158 | /// Closes key, after that usage is invalid |
| 174 | pub fn closeKey(self: *const RegistryWtf8) void { | 159 | pub fn closeKey(reg: RegistryWtf8) void { |
| 175 | const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(self.key); | 160 | const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(reg.key); |
| 176 | const return_code: windows.Win32Error = @enumFromInt(return_code_int); | 161 | const return_code: windows.Win32Error = @enumFromInt(return_code_int); |
| 177 | switch (return_code) { | 162 | switch (return_code) { |
| 178 | .SUCCESS => {}, | 163 | .SUCCESS => {}, |
| ... | @@ -182,7 +167,7 @@ const RegistryWtf8 = struct { | ... | @@ -182,7 +167,7 @@ const RegistryWtf8 = struct { |
| 182 | | 167 | |
| 183 | /// Get string from registry. | 168 | /// Get string from registry. |
| 184 | /// Caller owns result. | 169 | /// Caller owns result. |
| 185 | pub fn getString(self: *const RegistryWtf8, allocator: std.mem.Allocator, subkey: []const u8, value_name: []const u8) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]u8 { | 170 | pub fn getString(reg: RegistryWtf8, allocator: std.mem.Allocator, subkey: []const u8, value_name: []const u8) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]u8 { |
| 186 | const subkey_wtf16le: [:0]const u16 = subkey_wtf16le: { | 171 | const subkey_wtf16le: [:0]const u16 = subkey_wtf16le: { |
| 187 | var subkey_wtf16le_buf: [RegistryWtf16Le.key_name_max_len]u16 = undefined; | 172 | var subkey_wtf16le_buf: [RegistryWtf16Le.key_name_max_len]u16 = undefined; |
| 188 | const subkey_wtf16le_len: usize = std.unicode.wtf8ToWtf16Le(subkey_wtf16le_buf[0..], subkey) catch unreachable; | 173 | const subkey_wtf16le_len: usize = std.unicode.wtf8ToWtf16Le(subkey_wtf16le_buf[0..], subkey) catch unreachable; |
| ... | @@ -197,7 +182,7 @@ const RegistryWtf8 = struct { | ... | @@ -197,7 +182,7 @@ const RegistryWtf8 = struct { |
| 197 | break :value_name_wtf16le value_name_wtf16le_buf[0..value_name_wtf16le_len :0]; | 182 | break :value_name_wtf16le value_name_wtf16le_buf[0..value_name_wtf16le_len :0]; |
| 198 | }; | 183 | }; |
| 199 | | 184 | |
| 200 | const registry_wtf16le = RegistryWtf16Le{ .key = self.key }; | 185 | const registry_wtf16le: RegistryWtf16Le = .{ .key = reg.key }; |
| 201 | const value_wtf16le = try registry_wtf16le.getString(allocator, subkey_wtf16le, value_name_wtf16le); | 186 | const value_wtf16le = try registry_wtf16le.getString(allocator, subkey_wtf16le, value_name_wtf16le); |
| 202 | defer allocator.free(value_wtf16le); | 187 | defer allocator.free(value_wtf16le); |
| 203 | | 188 | |
| ... | @@ -208,7 +193,7 @@ const RegistryWtf8 = struct { | ... | @@ -208,7 +193,7 @@ const RegistryWtf8 = struct { |
| 208 | } | 193 | } |
| 209 | | 194 | |
| 210 | /// Get DWORD (u32) from registry. | 195 | /// Get DWORD (u32) from registry. |
| 211 | pub fn getDword(self: *const RegistryWtf8, subkey: []const u8, value_name: []const u8) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 { | 196 | pub fn getDword(reg: RegistryWtf8, subkey: []const u8, value_name: []const u8) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 { |
| 212 | const subkey_wtf16le: [:0]const u16 = subkey_wtf16le: { | 197 | const subkey_wtf16le: [:0]const u16 = subkey_wtf16le: { |
| 213 | var subkey_wtf16le_buf: [RegistryWtf16Le.key_name_max_len]u16 = undefined; | 198 | var subkey_wtf16le_buf: [RegistryWtf16Le.key_name_max_len]u16 = undefined; |
| 214 | const subkey_wtf16le_len: usize = std.unicode.wtf8ToWtf16Le(subkey_wtf16le_buf[0..], subkey) catch unreachable; | 199 | const subkey_wtf16le_len: usize = std.unicode.wtf8ToWtf16Le(subkey_wtf16le_buf[0..], subkey) catch unreachable; |
| ... | @@ -223,8 +208,8 @@ const RegistryWtf8 = struct { | ... | @@ -223,8 +208,8 @@ const RegistryWtf8 = struct { |
| 223 | break :value_name_wtf16le value_name_wtf16le_buf[0..value_name_wtf16le_len :0]; | 208 | break :value_name_wtf16le value_name_wtf16le_buf[0..value_name_wtf16le_len :0]; |
| 224 | }; | 209 | }; |
| 225 | | 210 | |
| 226 | const registry_wtf16le = RegistryWtf16Le{ .key = self.key }; | 211 | const registry_wtf16le: RegistryWtf16Le = .{ .key = reg.key }; |
| 227 | return try registry_wtf16le.getDword(subkey_wtf16le, value_name_wtf16le); | 212 | return registry_wtf16le.getDword(subkey_wtf16le, value_name_wtf16le); |
| 228 | } | 213 | } |
| 229 | | 214 | |
| 230 | /// Under private space with flags: | 215 | /// Under private space with flags: |
| ... | @@ -239,7 +224,7 @@ const RegistryWtf8 = struct { | ... | @@ -239,7 +224,7 @@ const RegistryWtf8 = struct { |
| 239 | }; | 224 | }; |
| 240 | | 225 | |
| 241 | const registry_wtf16le = try RegistryWtf16Le.loadFromPath(absolute_path_wtf16le); | 226 | const registry_wtf16le = try RegistryWtf16Le.loadFromPath(absolute_path_wtf16le); |
| 242 | return RegistryWtf8{ .key = registry_wtf16le.key }; | 227 | return .{ .key = registry_wtf16le.key }; |
| 243 | } | 228 | } |
| 244 | }; | 229 | }; |
| 245 | | 230 | |
| ... | @@ -272,12 +257,12 @@ const RegistryWtf16Le = struct { | ... | @@ -272,12 +257,12 @@ const RegistryWtf16Le = struct { |
| 272 | | 257 | |
| 273 | else => return error.KeyNotFound, | 258 | else => return error.KeyNotFound, |
| 274 | } | 259 | } |
| 275 | return RegistryWtf16Le{ .key = key }; | 260 | return .{ .key = key }; |
| 276 | } | 261 | } |
| 277 | | 262 | |
| 278 | /// Closes key, after that usage is invalid | 263 | /// Closes key, after that usage is invalid |
| 279 | fn closeKey(self: *const RegistryWtf16Le) void { | 264 | fn closeKey(reg: RegistryWtf16Le) void { |
| 280 | const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(self.key); | 265 | const return_code_int: windows.HRESULT = windows.advapi32.RegCloseKey(reg.key); |
| 281 | const return_code: windows.Win32Error = @enumFromInt(return_code_int); | 266 | const return_code: windows.Win32Error = @enumFromInt(return_code_int); |
| 282 | switch (return_code) { | 267 | switch (return_code) { |
| 283 | .SUCCESS => {}, | 268 | .SUCCESS => {}, |
| ... | @@ -286,13 +271,13 @@ const RegistryWtf16Le = struct { | ... | @@ -286,13 +271,13 @@ const RegistryWtf16Le = struct { |
| 286 | } | 271 | } |
| 287 | | 272 | |
| 288 | /// Get string ([:0]const u16) from registry. | 273 | /// Get string ([:0]const u16) from registry. |
| 289 | fn getString(self: *const RegistryWtf16Le, allocator: std.mem.Allocator, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]const u16 { | 274 | fn getString(reg: RegistryWtf16Le, allocator: std.mem.Allocator, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ OutOfMemory, ValueNameNotFound, NotAString, StringNotFound }![]const u16 { |
| 290 | var actual_type: windows.ULONG = undefined; | 275 | var actual_type: windows.ULONG = undefined; |
| 291 | | 276 | |
| 292 | // Calculating length to allocate | 277 | // Calculating length to allocate |
| 293 | var value_wtf16le_buf_size: u32 = 0; // in bytes, including any terminating NUL character or characters. | 278 | var value_wtf16le_buf_size: u32 = 0; // in bytes, including any terminating NUL character or characters. |
| 294 | var return_code_int: windows.HRESULT = windows.advapi32.RegGetValueW( | 279 | var return_code_int: windows.HRESULT = windows.advapi32.RegGetValueW( |
| 295 | self.key, | 280 | reg.key, |
| 296 | subkey_wtf16le, | 281 | subkey_wtf16le, |
| 297 | value_name_wtf16le, | 282 | value_name_wtf16le, |
| 298 | RRF.RT_REG_SZ, | 283 | RRF.RT_REG_SZ, |
| ... | @@ -319,7 +304,7 @@ const RegistryWtf16Le = struct { | ... | @@ -319,7 +304,7 @@ const RegistryWtf16Le = struct { |
| 319 | errdefer allocator.free(value_wtf16le_buf); | 304 | errdefer allocator.free(value_wtf16le_buf); |
| 320 | | 305 | |
| 321 | return_code_int = windows.advapi32.RegGetValueW( | 306 | return_code_int = windows.advapi32.RegGetValueW( |
| 322 | self.key, | 307 | reg.key, |
| 323 | subkey_wtf16le, | 308 | subkey_wtf16le, |
| 324 | value_name_wtf16le, | 309 | value_name_wtf16le, |
| 325 | RRF.RT_REG_SZ, | 310 | RRF.RT_REG_SZ, |
| ... | @@ -355,13 +340,13 @@ const RegistryWtf16Le = struct { | ... | @@ -355,13 +340,13 @@ const RegistryWtf16Le = struct { |
| 355 | } | 340 | } |
| 356 | | 341 | |
| 357 | /// Get DWORD (u32) from registry. | 342 | /// Get DWORD (u32) from registry. |
| 358 | fn getDword(self: *const RegistryWtf16Le, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 { | 343 | fn getDword(reg: RegistryWtf16Le, subkey_wtf16le: [:0]const u16, value_name_wtf16le: [:0]const u16) error{ ValueNameNotFound, NotADword, DwordTooLong, DwordNotFound }!u32 { |
| 359 | var actual_type: windows.ULONG = undefined; | 344 | var actual_type: windows.ULONG = undefined; |
| 360 | var reg_size: u32 = @sizeOf(u32); | 345 | var reg_size: u32 = @sizeOf(u32); |
| 361 | var reg_value: u32 = 0; | 346 | var reg_value: u32 = 0; |
| 362 | | 347 | |
| 363 | const return_code_int: windows.HRESULT = windows.advapi32.RegGetValueW( | 348 | const return_code_int: windows.HRESULT = windows.advapi32.RegGetValueW( |
| 364 | self.key, | 349 | reg.key, |
| 365 | subkey_wtf16le, | 350 | subkey_wtf16le, |
| 366 | value_name_wtf16le, | 351 | value_name_wtf16le, |
| 367 | RRF.RT_REG_DWORD, | 352 | RRF.RT_REG_DWORD, |
| ... | @@ -405,28 +390,118 @@ const RegistryWtf16Le = struct { | ... | @@ -405,28 +390,118 @@ const RegistryWtf16Le = struct { |
| 405 | else => return error.KeyNotFound, | 390 | else => return error.KeyNotFound, |
| 406 | } | 391 | } |
| 407 | | 392 | |
| 408 | return RegistryWtf16Le{ .key = key }; | 393 | return .{ .key = key }; |
| 409 | } | 394 | } |
| 410 | }; | 395 | }; |
| 411 | | 396 | |
| 412 | pub const Windows10Sdk = struct { | 397 | pub const Installation = struct { |
| 413 | path: []const u8, | 398 | path: []const u8, |
| 414 | version: []const u8, | 399 | version: []const u8, |
| 415 | | 400 | |
| 416 | /// Find path and version of Windows 10 SDK. | 401 | /// Find path and version of Windows SDK. |
| 417 | /// Caller owns the result's fields. | 402 | /// Caller owns the result's fields. |
| 418 | /// After finishing work, call `free(allocator)`. | 403 | /// After finishing work, call `free(allocator)`. |
| 419 | fn find(allocator: std.mem.Allocator) error{ OutOfMemory, Windows10SdkNotFound, PathTooLong, VersionTooLong }!Windows10Sdk { | 404 | fn find( |
| 420 | const v10_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\v10.0") catch |err| switch (err) { | 405 | allocator: std.mem.Allocator, |
| 421 | error.KeyNotFound => return error.Windows10SdkNotFound, | 406 | roots_key: RegistryWtf8, |
| | 407 | roots_subkey: []const u8, |
| | 408 | prefix: []const u8, |
| | 409 | version_key_name: []const u8, |
| | 410 | ) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation { |
| | 411 | roots: { |
| | 412 | const installation = findFromRoot(allocator, roots_key, roots_subkey, prefix) catch |
| | 413 | break :roots; |
| | 414 | if (installation.isValidVersion()) return installation; |
| | 415 | installation.free(allocator); |
| | 416 | } |
| | 417 | { |
| | 418 | const installation = try findFromInstallationFolder(allocator, version_key_name); |
| | 419 | if (installation.isValidVersion()) return installation; |
| | 420 | installation.free(allocator); |
| | 421 | } |
| | 422 | return error.InstallationNotFound; |
| | 423 | } |
| | 424 | |
| | 425 | fn findFromRoot( |
| | 426 | allocator: std.mem.Allocator, |
| | 427 | roots_key: RegistryWtf8, |
| | 428 | roots_subkey: []const u8, |
| | 429 | prefix: []const u8, |
| | 430 | ) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation { |
| | 431 | const path = path: { |
| | 432 | const path_maybe_with_trailing_slash = roots_key.getString(allocator, "", roots_subkey) catch |err| switch (err) { |
| | 433 | error.NotAString => return error.InstallationNotFound, |
| | 434 | error.ValueNameNotFound => return error.InstallationNotFound, |
| | 435 | error.StringNotFound => return error.InstallationNotFound, |
| | 436 | |
| | 437 | error.OutOfMemory => return error.OutOfMemory, |
| | 438 | }; |
| | 439 | if (path_maybe_with_trailing_slash.len > std.fs.MAX_PATH_BYTES or !std.fs.path.isAbsolute(path_maybe_with_trailing_slash)) { |
| | 440 | allocator.free(path_maybe_with_trailing_slash); |
| | 441 | return error.PathTooLong; |
| | 442 | } |
| | 443 | |
| | 444 | var path = std.ArrayList(u8).fromOwnedSlice(allocator, path_maybe_with_trailing_slash); |
| | 445 | errdefer path.deinit(); |
| | 446 | |
| | 447 | // String might contain trailing slash, so trim it here |
| | 448 | if (path.items.len > "C:\\".len and path.getLast() == '\\') _ = path.pop(); |
| | 449 | break :path try path.toOwnedSlice(); |
| | 450 | }; |
| | 451 | errdefer allocator.free(path); |
| | 452 | |
| | 453 | const version = version: { |
| | 454 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| | 455 | const sdk_lib_dir_path = std.fmt.bufPrint(buf[0..], "{s}\\Lib\\", .{path}) catch |err| switch (err) { |
| | 456 | error.NoSpaceLeft => return error.PathTooLong, |
| | 457 | }; |
| | 458 | if (!std.fs.path.isAbsolute(sdk_lib_dir_path)) return error.InstallationNotFound; |
| | 459 | |
| | 460 | // enumerate files in sdk path looking for latest version |
| | 461 | var sdk_lib_dir = std.fs.openDirAbsolute(sdk_lib_dir_path, .{ |
| | 462 | .iterate = true, |
| | 463 | }) catch |err| switch (err) { |
| | 464 | error.NameTooLong => return error.PathTooLong, |
| | 465 | else => return error.InstallationNotFound, |
| | 466 | }; |
| | 467 | defer sdk_lib_dir.close(); |
| | 468 | |
| | 469 | var iterator = sdk_lib_dir.iterate(); |
| | 470 | const versions = try iterateAndFilterByVersion(&iterator, allocator, prefix); |
| | 471 | defer { |
| | 472 | for (versions[1..]) |version| allocator.free(version); |
| | 473 | allocator.free(versions); |
| | 474 | } |
| | 475 | break :version versions[0]; |
| 422 | }; | 476 | }; |
| 423 | defer v10_key.closeKey(); | 477 | errdefer allocator.free(version); |
| 424 | | 478 | |
| 425 | const path: []const u8 = path10: { | 479 | return .{ .path = path, .version = version }; |
| 426 | const path_maybe_with_trailing_slash = v10_key.getString(allocator, "", "InstallationFolder") catch |err| switch (err) { | 480 | } |
| 427 | error.NotAString => return error.Windows10SdkNotFound, | 481 | |
| 428 | error.ValueNameNotFound => return error.Windows10SdkNotFound, | 482 | fn findFromInstallationFolder( |
| 429 | error.StringNotFound => return error.Windows10SdkNotFound, | 483 | allocator: std.mem.Allocator, |
| | 484 | version_key_name: []const u8, |
| | 485 | ) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation { |
| | 486 | var key_name_buf: [RegistryWtf16Le.key_name_max_len]u8 = undefined; |
| | 487 | const key = key: for ([_][]const u8{ "\\Wow6432Node", "" }) |wow6432node| { |
| | 488 | for ([_]windows.HKEY{ windows.HKEY_LOCAL_MACHINE, windows.HKEY_CURRENT_USER }) |hkey| { |
| | 489 | break :key RegistryWtf8.openKey(hkey, std.fmt.bufPrint( |
| | 490 | &key_name_buf, |
| | 491 | "SOFTWARE{s}\\Microsoft\\Microsoft SDKs\\Windows\\{s}", |
| | 492 | .{ wow6432node, version_key_name }, |
| | 493 | ) catch unreachable) catch |err| switch (err) { |
| | 494 | error.KeyNotFound => return error.InstallationNotFound, |
| | 495 | }; |
| | 496 | } |
| | 497 | } else return error.InstallationNotFound; |
| | 498 | defer key.closeKey(); |
| | 499 | |
| | 500 | const path: []const u8 = path: { |
| | 501 | const path_maybe_with_trailing_slash = key.getString(allocator, "", "InstallationFolder") catch |err| switch (err) { |
| | 502 | error.NotAString => return error.InstallationNotFound, |
| | 503 | error.ValueNameNotFound => return error.InstallationNotFound, |
| | 504 | error.StringNotFound => return error.InstallationNotFound, |
| 430 | | 505 | |
| 431 | error.OutOfMemory => return error.OutOfMemory, | 506 | error.OutOfMemory => return error.OutOfMemory, |
| 432 | }; | 507 | }; |
| ... | @@ -443,17 +518,17 @@ pub const Windows10Sdk = struct { | ... | @@ -443,17 +518,17 @@ pub const Windows10Sdk = struct { |
| 443 | if (path.items.len > "C:\\".len and path.getLast() == '\\') _ = path.pop(); | 518 | if (path.items.len > "C:\\".len and path.getLast() == '\\') _ = path.pop(); |
| 444 | | 519 | |
| 445 | const path_without_trailing_slash = try path.toOwnedSlice(); | 520 | const path_without_trailing_slash = try path.toOwnedSlice(); |
| 446 | break :path10 path_without_trailing_slash; | 521 | break :path path_without_trailing_slash; |
| 447 | }; | 522 | }; |
| 448 | errdefer allocator.free(path); | 523 | errdefer allocator.free(path); |
| 449 | | 524 | |
| 450 | const version: []const u8 = version10: { | 525 | const version: []const u8 = version: { |
| 451 | | 526 | |
| 452 | // note(dimenus): Microsoft doesn't include the .0 in the ProductVersion key.... | 527 | // note(dimenus): Microsoft doesn't include the .0 in the ProductVersion key.... |
| 453 | const version_without_0 = v10_key.getString(allocator, "", "ProductVersion") catch |err| switch (err) { | 528 | const version_without_0 = key.getString(allocator, "", "ProductVersion") catch |err| switch (err) { |
| 454 | error.NotAString => return error.Windows10SdkNotFound, | 529 | error.NotAString => return error.InstallationNotFound, |
| 455 | error.ValueNameNotFound => return error.Windows10SdkNotFound, | 530 | error.ValueNameNotFound => return error.InstallationNotFound, |
| 456 | error.StringNotFound => return error.Windows10SdkNotFound, | 531 | error.StringNotFound => return error.InstallationNotFound, |
| 457 | | 532 | |
| 458 | error.OutOfMemory => return error.OutOfMemory, | 533 | error.OutOfMemory => return error.OutOfMemory, |
| 459 | }; | 534 | }; |
| ... | @@ -468,21 +543,27 @@ pub const Windows10Sdk = struct { | ... | @@ -468,21 +543,27 @@ pub const Windows10Sdk = struct { |
| 468 | try version.appendSlice(".0"); | 543 | try version.appendSlice(".0"); |
| 469 | | 544 | |
| 470 | const version_with_0 = try version.toOwnedSlice(); | 545 | const version_with_0 = try version.toOwnedSlice(); |
| 471 | break :version10 version_with_0; | 546 | break :version version_with_0; |
| 472 | }; | 547 | }; |
| 473 | errdefer allocator.free(version); | 548 | errdefer allocator.free(version); |
| 474 | | 549 | |
| 475 | return Windows10Sdk{ .path = path, .version = version }; | 550 | return .{ .path = path, .version = version }; |
| 476 | } | 551 | } |
| 477 | | 552 | |
| 478 | /// Check whether this version is enumerated in registry. | 553 | /// Check whether this version is enumerated in registry. |
| 479 | fn isValidVersion(windows10sdk: *const Windows10Sdk) bool { | 554 | fn isValidVersion(installation: Installation) bool { |
| 480 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | 555 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; |
| 481 | const reg_query_as_wtf8 = std.fmt.bufPrint(buf[0..], "{s}\\{s}\\Installed Options", .{ WINDOWS_KIT_REG_KEY, windows10sdk.version }) catch |err| switch (err) { | 556 | const reg_query_as_wtf8 = std.fmt.bufPrint(buf[0..], "{s}\\{s}\\Installed Options", .{ |
| | 557 | windows_kits_reg_key, |
| | 558 | installation.version, |
| | 559 | }) catch |err| switch (err) { |
| 482 | error.NoSpaceLeft => return false, | 560 | error.NoSpaceLeft => return false, |
| 483 | }; | 561 | }; |
| 484 | | 562 | |
| 485 | const options_key = RegistryWtf8.openKey(windows.HKEY_LOCAL_MACHINE, reg_query_as_wtf8) catch |err| switch (err) { | 563 | const options_key = RegistryWtf8.openKey( |
| | 564 | windows.HKEY_LOCAL_MACHINE, |
| | 565 | reg_query_as_wtf8, |
| | 566 | ) catch |err| switch (err) { |
| 486 | error.KeyNotFound => return false, | 567 | error.KeyNotFound => return false, |
| 487 | }; | 568 | }; |
| 488 | defer options_key.closeKey(); | 569 | defer options_key.closeKey(); |
| ... | @@ -492,87 +573,16 @@ pub const Windows10Sdk = struct { | ... | @@ -492,87 +573,16 @@ pub const Windows10Sdk = struct { |
| 492 | .aarch64 => "OptionId.DesktopCPParm64", | 573 | .aarch64 => "OptionId.DesktopCPParm64", |
| 493 | .x86_64 => "OptionId.DesktopCPPx64", | 574 | .x86_64 => "OptionId.DesktopCPPx64", |
| 494 | .x86 => "OptionId.DesktopCPPx86", | 575 | .x86 => "OptionId.DesktopCPPx86", |
| 495 | else => |tag| @compileError("Windows 10 SDK cannot be detected on architecture " ++ tag), | 576 | else => |tag| @compileError("Windows SDK cannot be detected on architecture " ++ tag), |
| 496 | }; | 577 | }; |
| 497 | | 578 | |
| 498 | const reg_value = options_key.getDword("", option_name) catch return false; | 579 | const reg_value = options_key.getDword("", option_name) catch return false; |
| 499 | return (reg_value == 1); | 580 | return (reg_value == 1); |
| 500 | } | 581 | } |
| 501 | | 582 | |
| 502 | fn free(self: *const Windows10Sdk, allocator: std.mem.Allocator) void { | 583 | fn free(install: Installation, allocator: std.mem.Allocator) void { |
| 503 | allocator.free(self.path); | 584 | allocator.free(install.path); |
| 504 | allocator.free(self.version); | 585 | allocator.free(install.version); |
| 505 | } | | |
| 506 | }; | | |
| 507 | | | |
| 508 | pub const Windows81Sdk = struct { | | |
| 509 | path: []const u8, | | |
| 510 | version: []const u8, | | |
| 511 | | | |
| 512 | /// Find path and version of Windows 8.1 SDK. | | |
| 513 | /// Caller owns the result's fields. | | |
| 514 | /// After finishing work, call `free(allocator)`. | | |
| 515 | fn find(allocator: std.mem.Allocator, roots_key: *const RegistryWtf8) error{ OutOfMemory, Windows81SdkNotFound, PathTooLong, VersionTooLong }!Windows81Sdk { | | |
| 516 | const path: []const u8 = path81: { | | |
| 517 | const path_maybe_with_trailing_slash = roots_key.getString(allocator, "", "KitsRoot81") catch |err| switch (err) { | | |
| 518 | error.NotAString => return error.Windows81SdkNotFound, | | |
| 519 | error.ValueNameNotFound => return error.Windows81SdkNotFound, | | |
| 520 | error.StringNotFound => return error.Windows81SdkNotFound, | | |
| 521 | | | |
| 522 | error.OutOfMemory => return error.OutOfMemory, | | |
| 523 | }; | | |
| 524 | if (path_maybe_with_trailing_slash.len > std.fs.MAX_PATH_BYTES or !std.fs.path.isAbsolute(path_maybe_with_trailing_slash)) { | | |
| 525 | allocator.free(path_maybe_with_trailing_slash); | | |
| 526 | return error.PathTooLong; | | |
| 527 | } | | |
| 528 | | | |
| 529 | var path = std.ArrayList(u8).fromOwnedSlice(allocator, path_maybe_with_trailing_slash); | | |
| 530 | errdefer path.deinit(); | | |
| 531 | | | |
| 532 | // String might contain trailing slash, so trim it here | | |
| 533 | if (path.items.len > "C:\\".len and path.getLast() == '\\') _ = path.pop(); | | |
| 534 | | | |
| 535 | const path_without_trailing_slash = try path.toOwnedSlice(); | | |
| 536 | break :path81 path_without_trailing_slash; | | |
| 537 | }; | | |
| 538 | errdefer allocator.free(path); | | |
| 539 | | | |
| 540 | const version: []const u8 = version81: { | | |
| 541 | var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; | | |
| 542 | const sdk_lib_dir_path = std.fmt.bufPrint(buf[0..], "{s}\\Lib\\", .{path}) catch |err| switch (err) { | | |
| 543 | error.NoSpaceLeft => return error.PathTooLong, | | |
| 544 | }; | | |
| 545 | if (!std.fs.path.isAbsolute(sdk_lib_dir_path)) return error.Windows81SdkNotFound; | | |
| 546 | | | |
| 547 | // enumerate files in sdk path looking for latest version | | |
| 548 | var sdk_lib_dir = std.fs.openDirAbsolute(sdk_lib_dir_path, .{ | | |
| 549 | .iterate = true, | | |
| 550 | }) catch |err| switch (err) { | | |
| 551 | error.NameTooLong => return error.PathTooLong, | | |
| 552 | else => return error.Windows81SdkNotFound, | | |
| 553 | }; | | |
| 554 | defer sdk_lib_dir.close(); | | |
| 555 | | | |
| 556 | var iterator = sdk_lib_dir.iterate(); | | |
| 557 | const versions = iterateAndFilterBySemVer(&iterator, allocator, "winv") catch |err| switch (err) { | | |
| 558 | error.OutOfMemory => return error.OutOfMemory, | | |
| 559 | error.VersionNotFound => return error.Windows81SdkNotFound, | | |
| 560 | }; | | |
| 561 | defer { | | |
| 562 | for (versions) |version| allocator.free(version); | | |
| 563 | allocator.free(versions); | | |
| 564 | } | | |
| 565 | const latest_version = try allocator.dupe(u8, versions[0]); | | |
| 566 | break :version81 latest_version; | | |
| 567 | }; | | |
| 568 | errdefer allocator.free(version); | | |
| 569 | | | |
| 570 | return Windows81Sdk{ .path = path, .version = version }; | | |
| 571 | } | | |
| 572 | | | |
| 573 | fn free(self: *const Windows81Sdk, allocator: std.mem.Allocator) void { | | |
| 574 | allocator.free(self.path); | | |
| 575 | allocator.free(self.version); | | |
| 576 | } | 586 | } |
| 577 | }; | 587 | }; |
| 578 | | 588 | |
| ... | @@ -634,7 +644,7 @@ const MsvcLibDir = struct { | ... | @@ -634,7 +644,7 @@ const MsvcLibDir = struct { |
| 634 | /// Example: 17.4.33205.214 -> 0x0011000481b500d6 | 644 | /// Example: 17.4.33205.214 -> 0x0011000481b500d6 |
| 635 | fn parseVersionQuad(version: []const u8) error{InvalidVersion}!u64 { | 645 | fn parseVersionQuad(version: []const u8) error{InvalidVersion}!u64 { |
| 636 | var it = std.mem.splitScalar(u8, version, '.'); | 646 | var it = std.mem.splitScalar(u8, version, '.'); |
| 637 | const a = it.next() orelse return error.InvalidVersion; | 647 | const a = it.first(); |
| 638 | const b = it.next() orelse return error.InvalidVersion; | 648 | const b = it.next() orelse return error.InvalidVersion; |
| 639 | const c = it.next() orelse return error.InvalidVersion; | 649 | const c = it.next() orelse return error.InvalidVersion; |
| 640 | const d = it.next() orelse return error.InvalidVersion; | 650 | const d = it.next() orelse return error.InvalidVersion; |
| ... | @@ -791,11 +801,7 @@ const MsvcLibDir = struct { | ... | @@ -791,11 +801,7 @@ const MsvcLibDir = struct { |
| 791 | defer visualstudio_folder.close(); | 801 | defer visualstudio_folder.close(); |
| 792 | | 802 | |
| 793 | var iterator = visualstudio_folder.iterate(); | 803 | var iterator = visualstudio_folder.iterate(); |
| 794 | const versions = iterateAndFilterBySemVer(&iterator, allocator, null) catch |err| switch (err) { | 804 | break :vs_versions try iterateAndFilterByVersion(&iterator, allocator, ""); |
| 795 | error.OutOfMemory => return error.OutOfMemory, | | |
| 796 | error.VersionNotFound => return error.PathNotFound, | | |
| 797 | }; | | |
| 798 | break :vs_versions versions; | | |
| 799 | }; | 805 | }; |
| 800 | defer { | 806 | defer { |
| 801 | for (vs_versions) |vs_version| allocator.free(vs_version); | 807 | for (vs_versions) |vs_version| allocator.free(vs_version); |