pub const Installation = struct path: []const u8version: []const u8fn find(gpa: Allocator, io: Io, registry: *Registry, roots_key: Registry.Key, roots_subkey: []const u16, prefix: []const u8, version_key_name: []const u16) errorFind path and version of Windows SDK. Caller owns the result's fields.
fn findFromRoot(gpa: Allocator, io: Io, roots_key: Registry.Key, roots_subkey: []const u16, prefix: []const u8) errorfn findFromInstallationFolder(gpa: Allocator, registry: *Registry, version_key_name: []const u16) errorfn isValidVersion(installation: Installation, roots_key: Registry.Key) boolCheck whether this version is enumerated in registry.
fn free(install: Installation, gpa: Allocator) void458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651pub const Installation = struct {
path: []const u8,
version: []const u8,
/// Find path and version of Windows SDK.
/// Caller owns the result's fields.
fn find(
gpa: Allocator,
io: Io,
registry: *Registry,
roots_key: Registry.Key,
roots_subkey: []const u16,
prefix: []const u8,
version_key_name: []const u16,
) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation {
roots: {
const installation = findFromRoot(gpa, io, roots_key, roots_subkey, prefix) catch
break :roots;
if (installation.isValidVersion(roots_key)) return installation;
installation.free(gpa);
}
{
const installation = try findFromInstallationFolder(gpa, registry, version_key_name);
if (installation.isValidVersion(roots_key)) return installation;
installation.free(gpa);
}
return error.InstallationNotFound;
}
fn findFromRoot(
gpa: Allocator,
io: Io,
roots_key: Registry.Key,
roots_subkey: []const u16,
prefix: []const u8,
) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation {
const path = path: {
const path_w_maybe_with_trailing_slash = roots_key.getString(gpa, .{ .name = roots_subkey }, .wtf16) catch |err| switch (err) {
error.NotAString,
error.ValueNameNotFound,
error.StringNotFound,
=> return error.InstallationNotFound,
error.OutOfMemory => |e| return e,
};
defer gpa.free(path_w_maybe_with_trailing_slash);
if (!std.fs.path.isAbsoluteWindowsWtf16(path_w_maybe_with_trailing_slash)) {
return error.InstallationNotFound;
}
const path_w = std.mem.trimEnd(u16, path_w_maybe_with_trailing_slash, L("\\/"));
break :path try std.unicode.wtf16LeToWtf8Alloc(gpa, path_w);
};
errdefer gpa.free(path);
const version = version: {
var buf: [Dir.max_path_bytes]u8 = undefined;
const sdk_lib_dir_path = std.mem.print(buf[0..], "{s}\\Lib\\", .{path}) catch |err| switch (err) {
error.NoSpaceLeft => return error.PathTooLong,
};
if (!Dir.path.isAbsolute(sdk_lib_dir_path)) return error.InstallationNotFound;
// enumerate files in sdk path looking for latest version
var sdk_lib_dir = Dir.openDirAbsolute(io, sdk_lib_dir_path, .{
.iterate = true,
}) catch |err| switch (err) {
error.NameTooLong => return error.PathTooLong,
else => return error.InstallationNotFound,
};
defer sdk_lib_dir.close(io);
var iterator = sdk_lib_dir.iterate();
const versions = try iterateAndFilterByVersion(&iterator, gpa, io, prefix);
if (versions.len == 0) return error.InstallationNotFound;
defer {
for (versions[1..]) |version| gpa.free(version);
gpa.free(versions);
}
break :version versions[0];
};
errdefer gpa.free(version);
return .{ .path = path, .version = version };
}
fn findFromInstallationFolder(
gpa: Allocator,
registry: *Registry,
version_key_name: []const u16,
) error{ OutOfMemory, InstallationNotFound, PathTooLong, VersionTooLong }!Installation {
const key_name = try std.mem.concat(gpa, u16, &.{ L("Microsoft\\Microsoft SDKs\\Windows\\"), version_key_name });
defer gpa.free(key_name);
const key = registry.tryOpenSoftwareKeyWithPrecedence(switch (is_32_bit) {
true => &.{
.{ .root = .local_machine },
.{ .root = .current_user },
},
false => &.{
.{ .root = .local_machine, .wow64 = .wow64_32 },
.{ .root = .current_user, .wow64 = .wow64_32 },
.{ .root = .local_machine, .wow64 = .native },
.{ .root = .current_user, .wow64 = .native },
},
}, key_name) catch {
return error.InstallationNotFound;
};
defer key.close();
const path: []const u8 = path: {
const path_w_maybe_with_trailing_slash = key.getString(gpa, .{ .name = L("InstallationFolder") }, .wtf16) catch |err| switch (err) {
error.NotAString,
error.ValueNameNotFound,
error.StringNotFound,
=> return error.InstallationNotFound,
error.OutOfMemory => |e| return e,
};
defer gpa.free(path_w_maybe_with_trailing_slash);
if (!std.fs.path.isAbsoluteWindowsWtf16(path_w_maybe_with_trailing_slash)) {
return error.InstallationNotFound;
}
const path_w = std.mem.trimEnd(u16, path_w_maybe_with_trailing_slash, L("\\/"));
break :path try std.unicode.wtf16LeToWtf8Alloc(gpa, path_w);
};
errdefer gpa.free(path);
const version: []const u8 = version: {
// Microsoft doesn't include the .0 in the ProductVersion key
const version_without_0 = key.getString(gpa, .{ .name = L("ProductVersion") }, .wtf16) catch |err| switch (err) {
error.NotAString,
error.ValueNameNotFound,
error.StringNotFound,
=> return error.InstallationNotFound,
error.OutOfMemory => |e| return e,
};
defer gpa.free(version_without_0);
if (version_without_0.len + ".0".len > product_version_max_length) {
return error.VersionTooLong;
}
var version: std.array_list.Managed(u8) = try .initCapacity(gpa, version_without_0.len + 2);
errdefer version.deinit();
try std.unicode.wtf16LeToWtf8ArrayList(&version, version_without_0);
try version.appendSlice(".0");
break :version try version.toOwnedSlice();
};
errdefer gpa.free(version);
return .{ .path = path, .version = version };
}
/// Check whether this version is enumerated in registry.
fn isValidVersion(installation: Installation, roots_key: Registry.Key) bool {
var version_buf: [product_version_max_length]u16 = undefined;
const version_len = std.unicode.wtf8ToWtf16Le(&version_buf, installation.version) catch return false;
const version = version_buf[0..version_len];
const options_key_name = "Installed Options";
const buf_len = product_version_max_length + options_key_name.len + 2;
var buf: [buf_len]u16 = undefined;
var query: std.ArrayList(u16) = .initBuffer(&buf);
query.appendSliceAssumeCapacity(version);
query.appendAssumeCapacity('\\');
query.appendSliceAssumeCapacity(L(options_key_name));
const options_key = roots_key.open(query.items) catch |err| switch (err) {
error.KeyNotFound => return false,
};
defer options_key.close();
const option_name = comptime switch (builtin.target.cpu.arch) {
.thumb => "OptionId.DesktopCPParm",
.aarch64 => "OptionId.DesktopCPParm64",
.x86 => "OptionId.DesktopCPPx86",
.x86_64 => "OptionId.DesktopCPPx64",
else => |tag| @compileError("Windows SDK cannot be detected on architecture " ++ tag),
};
const reg_value = options_key.getDword(.{ .name = L(option_name) }) catch return false;
return (reg_value == 1);
}
fn free(install: Installation, gpa: Allocator) void {
gpa.free(install.path);
gpa.free(install.version);
}
}