const MsvcLibDir = struct fn findInstancesDirViaSetup(gpa: Allocator, io: Io, registry: *Registry) errorfn findInstancesDirViaCLSID(gpa: Allocator, io: Io, registry: *Registry) errorfn findInstancesDir(gpa: Allocator, io: Io, registry: *Registry, environ_map: *const Environ.Map) errorfn parseVersionQuad(version: []const u8) errorIntended to be equivalent to ISetupHelper.ParseVersion Example: 17.4.33205.214 -> 0x0011000481b500d6
fn findViaCOM(gpa: Allocator, io: Io, registry: *Registry, arch: std.Target.Cpu.Arch, environ_map: *const Environ.Map) errorIntended to be equivalent to ISetupConfiguration.EnumInstances: https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.setup.configuration but without the use of COM in order to avoid a dependency on ole32.dll
The logic in this function is intended to match what ISetupConfiguration does under-the-hood, as verified using Procmon.
fn libDirFromInstallationPath(gpa: Allocator, io: Io, installation_path: []const u8, arch: std.Target.Cpu.Arch) errorfn findViaRegistry(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch, environ_map: *const Environ.Map) errorfn findViaVs7Key(gpa: Allocator, io: Io, registry: *Registry, arch: std.Target.Cpu.Arch, environ_map: *const Environ.Map) errorfn verifyLibDir(io: Io, lib_dir_path: []const u8) boolpub fn find(gpa: Allocator, io: Io, registry: *Registry, arch: std.Target.Cpu.Arch, environ_map: *const Environ.Map) errorFind path to MSVC's lib/ directory. Caller owns the result.
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194const MsvcLibDir = struct {
fn findInstancesDirViaSetup(gpa: Allocator, io: Io, registry: *Registry) error{ OutOfMemory, PathNotFound }!Dir {
const vs_setup_key_path = L("Microsoft\\VisualStudio\\Setup");
const vs_setup_key = registry.openSoftwareKey(.{ .root = .local_machine }, vs_setup_key_path) catch |err| switch (err) {
error.KeyNotFound => return error.PathNotFound,
};
defer vs_setup_key.close();
const packages_path = vs_setup_key.getString(gpa, .{ .name = L("CachePath") }, .wtf8) catch |err| switch (err) {
error.NotAString,
error.ValueNameNotFound,
error.StringNotFound,
=> return error.PathNotFound,
error.OutOfMemory => |e| return e,
};
defer gpa.free(packages_path);
if (!std.fs.path.isAbsolute(packages_path)) return error.PathNotFound;
const instances_path = try std.fs.path.join(gpa, &.{ packages_path, "_Instances" });
defer gpa.free(instances_path);
return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch return error.PathNotFound;
}
fn findInstancesDirViaCLSID(gpa: Allocator, io: Io, registry: *Registry) error{ OutOfMemory, PathNotFound }!Dir {
const setup_configuration_clsid = "{177f0c4a-1cd3-4de7-a32c-71dbbb9fa36d}";
// HKEY_CLASSES_ROOT is not a single key but instead a combination of
// HKCU\Software\Classes and HKLM\Software\Classes with HKCU taking precedent
// https://learn.microsoft.com/en-us/windows/win32/sysinfo/hkey-classes-root-key
//
// Instead of a CLASSES_ROOT abstraction, we emulate the behavior with a more
// general abstraction, which also means we need to include `Classes` in the path since
// we're starting from the `Software` keys instead of the "classes root".
//
// The advapi32 APIs with `HKEY_CLASSES_ROOT` go through `\REGISTRY\USER\<SID>_Classes`
// instead of `\REGISTRY\USER\<SID>\Software\Classes`, but we go through the latter
// because it allows us to take advantage of `RtlOpenCurrentUser` to avoid needing to implement
// the logic for getting the current user registry path, and it appears that the two keys are
// effectively equivalent. Further investigation of the relationship of these keys would probably
// be beneficial, though.
const setup_config_key = registry.tryOpenSoftwareKeyWithPrecedence(&.{
.{ .root = .current_user },
.{ .root = .local_machine },
}, L("Classes\\CLSID\\" ++ setup_configuration_clsid)) catch |err| switch (err) {
error.KeyNotFound => return error.PathNotFound,
};
defer setup_config_key.close();
const inproc_server = setup_config_key.open(L("InprocServer32")) catch return error.PathNotFound;
const dll_path = inproc_server.getString(gpa, .default, .wtf8) catch |err| switch (err) {
error.NotAString,
error.ValueNameNotFound,
error.StringNotFound,
=> return error.PathNotFound,
error.OutOfMemory => |e| return e,
};
defer gpa.free(dll_path);
if (!std.fs.path.isAbsolute(dll_path)) return error.PathNotFound;
var path_it = std.fs.path.componentIterator(dll_path);
// the .dll filename
_ = path_it.last();
const root_path = while (path_it.previous()) |dir_component| {
if (std.ascii.eqlIgnoreCase(dir_component.name, "VisualStudio")) {
break dir_component.path;
}
} else {
return error.PathNotFound;
};
const instances_path = try std.fs.path.join(gpa, &.{ root_path, "Packages", "_Instances" });
defer gpa.free(instances_path);
return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch return error.PathNotFound;
}
fn findInstancesDir(
gpa: Allocator,
io: Io,
registry: *Registry,
environ_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }!Dir {
// First, try getting the packages cache path from the registry.
// This only seems to exist when the path is different from the default.
method1: {
return findInstancesDirViaSetup(gpa, io, registry) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.PathNotFound => break :method1,
};
}
// Otherwise, try to get the path from the .dll that would have been
// loaded via COM for SetupConfiguration.
method2: {
return findInstancesDirViaCLSID(gpa, io, registry) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.PathNotFound => break :method2,
};
}
// If that can't be found, fall back to manually appending
// `Microsoft\VisualStudio\Packages\_Instances` to %PROGRAMDATA%
method3: {
const program_data = std.zig.EnvVar.PROGRAMDATA.get(environ_map) orelse break :method3;
if (!std.fs.path.isAbsolute(program_data)) break :method3;
const instances_path = try Dir.path.join(gpa, &.{
program_data, "Microsoft", "VisualStudio", "Packages", "_Instances",
});
defer gpa.free(instances_path);
return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch break :method3;
}
return error.PathNotFound;
}
/// Intended to be equivalent to `ISetupHelper.ParseVersion`
/// Example: 17.4.33205.214 -> 0x0011000481b500d6
fn parseVersionQuad(version: []const u8) error{InvalidVersion}!u64 {
var it = std.mem.splitScalar(u8, version, '.');
const a = it.first();
const b = it.next() orelse return error.InvalidVersion;
const c = it.next() orelse return error.InvalidVersion;
const d = it.next() orelse return error.InvalidVersion;
if (it.next()) |_| return error.InvalidVersion;
var result: u64 = undefined;
var result_bytes = std.mem.asBytes(&result);
std.mem.writeInt(
u16,
result_bytes[0..2],
std.fmt.parseUnsigned(u16, d, 10) catch return error.InvalidVersion,
.little,
);
std.mem.writeInt(
u16,
result_bytes[2..4],
std.fmt.parseUnsigned(u16, c, 10) catch return error.InvalidVersion,
.little,
);
std.mem.writeInt(
u16,
result_bytes[4..6],
std.fmt.parseUnsigned(u16, b, 10) catch return error.InvalidVersion,
.little,
);
std.mem.writeInt(
u16,
result_bytes[6..8],
std.fmt.parseUnsigned(u16, a, 10) catch return error.InvalidVersion,
.little,
);
return result;
}
/// Intended to be equivalent to ISetupConfiguration.EnumInstances:
/// https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.setup.configuration
/// but without the use of COM in order to avoid a dependency on ole32.dll
///
/// The logic in this function is intended to match what ISetupConfiguration does
/// under-the-hood, as verified using Procmon.
fn findViaCOM(
gpa: Allocator,
io: Io,
registry: *Registry,
arch: std.Target.Cpu.Arch,
environ_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }![]const u8 {
// Typically `%PROGRAMDATA%\Microsoft\VisualStudio\Packages\_Instances`
// This will contain directories with names of instance IDs like 80a758ca,
// which will contain `state.json` files that have the version and
// installation directory.
var instances_dir = try findInstancesDir(gpa, io, registry, environ_map);
defer instances_dir.close(io);
var state_subpath_buf: [Dir.max_name_bytes + 32]u8 = undefined;
var latest_version_lib_dir: std.ArrayList(u8) = .empty;
errdefer latest_version_lib_dir.deinit(gpa);
var latest_version: u64 = 0;
var instances_dir_it = instances_dir.iterateAssumeFirstIteration();
while (instances_dir_it.next(io) catch return error.PathNotFound) |entry| {
if (entry.kind != .directory) continue;
var writer: Writer = .fixed(&state_subpath_buf);
writer.writeAll(entry.name) catch unreachable;
writer.writeByte(Dir.path.sep) catch unreachable;
writer.writeAll("state.json") catch unreachable;
const json_contents = instances_dir.readFileAlloc(io, writer.buffered(), gpa, .limited(std.math.maxInt(usize))) catch continue;
defer gpa.free(json_contents);
var parsed = std.json.parseFromSlice(std.json.Value, gpa, json_contents, .{}) catch continue;
defer parsed.deinit();
if (parsed.value != .object) continue;
const catalog_info = parsed.value.object.get("catalogInfo") orelse continue;
if (catalog_info != .object) continue;
const product_version_value = catalog_info.object.get("buildVersion") orelse continue;
if (product_version_value != .string) continue;
const product_version_text = product_version_value.string;
const parsed_version = parseVersionQuad(product_version_text) catch continue;
// We want to end up with the most recent version installed
if (parsed_version <= latest_version) continue;
const installation_path = parsed.value.object.get("installationPath") orelse continue;
if (installation_path != .string) continue;
const lib_dir_path = libDirFromInstallationPath(gpa, io, installation_path.string, arch) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.PathNotFound => continue,
};
defer gpa.free(lib_dir_path);
latest_version_lib_dir.clearRetainingCapacity();
try latest_version_lib_dir.appendSlice(gpa, lib_dir_path);
latest_version = parsed_version;
}
if (latest_version_lib_dir.items.len == 0) return error.PathNotFound;
return latest_version_lib_dir.toOwnedSlice(gpa);
}
fn libDirFromInstallationPath(
gpa: Allocator,
io: Io,
installation_path: []const u8,
arch: std.Target.Cpu.Arch,
) error{ OutOfMemory, PathNotFound }![]const u8 {
var lib_dir_buf = try std.array_list.Managed(u8).initCapacity(gpa, installation_path.len + 64);
errdefer lib_dir_buf.deinit();
lib_dir_buf.appendSliceAssumeCapacity(installation_path);
if (!Dir.path.isSep(lib_dir_buf.last().?)) {
try lib_dir_buf.append('\\');
}
const installation_path_with_trailing_sep_len = lib_dir_buf.items.len;
try lib_dir_buf.appendSlice("VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt");
var default_tools_version_buf: [512]u8 = undefined;
const default_tools_version_contents = Dir.cwd().readFile(io, lib_dir_buf.items, &default_tools_version_buf) catch {
return error.PathNotFound;
};
var tokenizer = std.mem.tokenizeAny(u8, default_tools_version_contents, " \r\n");
const default_tools_version = tokenizer.next() orelse return error.PathNotFound;
lib_dir_buf.shrinkRetainingCapacity(installation_path_with_trailing_sep_len);
try lib_dir_buf.appendSlice("VC\\Tools\\MSVC\\");
try lib_dir_buf.appendSlice(default_tools_version);
try lib_dir_buf.appendSlice("\\Lib\\");
try lib_dir_buf.appendSlice(switch (arch) {
.thumb => "arm",
.aarch64 => "arm64",
.x86 => "x86",
.x86_64 => "x64",
else => unreachable,
});
if (!verifyLibDir(io, lib_dir_buf.items)) {
return error.PathNotFound;
}
return lib_dir_buf.toOwnedSlice();
}
// https://learn.microsoft.com/en-us/visualstudio/install/tools-for-managing-visual-studio-instances?view=vs-2022#editing-the-registry-for-a-visual-studio-instance
fn findViaRegistry(
gpa: Allocator,
io: Io,
arch: std.Target.Cpu.Arch,
environ_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }![]const u8 {
// %localappdata%\Microsoft\VisualStudio\
// %appdata%\Local\Microsoft\VisualStudio\
const local_app_data_path = std.zig.EnvVar.LOCALAPPDATA.get(environ_map) orelse return error.PathNotFound;
const visualstudio_folder_path = try Dir.path.join(gpa, &.{
local_app_data_path, "Microsoft\\VisualStudio\\",
});
defer gpa.free(visualstudio_folder_path);
if (!Dir.path.isAbsolute(visualstudio_folder_path)) return error.PathNotFound;
// To make things easier later on, we open the VisualStudio directory here which
// allows us to pass relative paths to NtLoadKeyEx in order to avoid dealing with
// conversion to NT namespace paths.
var visualstudio_folder = Dir.openDirAbsolute(io, visualstudio_folder_path, .{
.iterate = true,
}) catch return error.PathNotFound;
defer visualstudio_folder.close(io);
const vs_versions: []const []const u8 = vs_versions: {
// enumerate folders that contain `privateregistry.bin`, looking for all versions
// f.i. %localappdata%\Microsoft\VisualStudio\17.0_9e9cbb98\
var iterator = visualstudio_folder.iterate();
break :vs_versions try iterateAndFilterByVersion(&iterator, gpa, io, "");
};
defer {
for (vs_versions) |vs_version| gpa.free(vs_version);
gpa.free(vs_versions);
}
var key_path_buf: [windows.NAME_MAX * 2]u16 = undefined;
var sub_path_buf: [windows.NAME_MAX * 2]u16 = undefined;
const source_directories: []const u8 = source_directories: for (vs_versions) |vs_version| {
const sub_path = blk: {
var buf: std.ArrayList(u16) = .initBuffer(&sub_path_buf);
buf.items.len += std.unicode.wtf8ToWtf16Le(buf.unusedCapacitySlice(), vs_version) catch unreachable;
buf.appendSliceAssumeCapacity(L("\\privateregistry.bin"));
break :blk buf.items;
};
// The goal is to emulate advapi32.RegLoadAppKeyW with a direct call
// to NtLoadKeyEx instead.
//
// RegLoadAppKeyW loads the hive into a registry key of the format:
// \REGISTRY\A\{fdb2baa5-8ca8-ef03-78d0-3b1f868fd2a9}
// where `\REGISTRY\A` is a special unenumerable location intended for
// per-app hives, and the GUID is randomly generated (in testing, it
// was different for each run of the program).
//
// The OS is responsible for cleaning up `\REGISTRY\A` whenever all handles
// to one of its keys are closed, so we don't have to do anything special
// with regards to that.
const temp_key_path = blk: {
var guid: windows.GUID = undefined;
io.random(std.mem.asBytes(&guid));
var guid_buf: [38]u8 = undefined;
const guid_str = std.mem.print(&guid_buf, "{f}", .{guid}) catch unreachable;
var buf: std.ArrayList(u16) = .initBuffer(&key_path_buf);
buf.appendSliceAssumeCapacity(L("\\REGISTRY\\A\\"));
buf.items.len += std.unicode.wtf8ToWtf16Le(buf.unusedCapacitySlice(), guid_str) catch unreachable;
break :blk buf.items;
};
const target_key: windows.OBJECT.ATTRIBUTES = .{
.RootDirectory = null,
.Attributes = .{},
.ObjectName = @constCast(&windows.UNICODE_STRING.init(temp_key_path)),
.SecurityDescriptor = null,
};
const source_file: windows.OBJECT.ATTRIBUTES = .{
.RootDirectory = visualstudio_folder.handle,
.Attributes = .{},
.ObjectName = @constCast(&windows.UNICODE_STRING.init(sub_path)),
.SecurityDescriptor = null,
};
var root_key: Registry.Key = undefined;
const rc = windows.ntdll.NtLoadKeyEx(
&target_key,
&source_file,
.{
.APP_HIVE = true,
// This wasn't set by RegLoadAppKeyW, but it seems relevant
// since we aren't intending to do any modifcation of the hive.
.OPEN_READ_ONLY = true,
},
null,
null,
.{ .SPECIFIC = .{
.KEY = .{
.QUERY_VALUE = true,
.ENUMERATE_SUB_KEYS = true,
},
} },
&root_key.handle,
null,
);
switch (rc) {
.SUCCESS => {},
else => continue,
}
defer root_key.close();
const config_path = blk: {
var buf: std.ArrayList(u16) = .initBuffer(&key_path_buf);
buf.appendSliceAssumeCapacity(L("Software\\Microsoft\\VisualStudio\\"));
buf.items.len += std.unicode.wtf8ToWtf16Le(buf.unusedCapacitySlice(), vs_version) catch unreachable;
buf.appendSliceAssumeCapacity(L("_Config"));
break :blk buf.items;
};
const config_key = root_key.open(config_path) catch continue;
const source_directories_value = config_key.getString(gpa, .{ .name = L("Source Directories") }, .wtf8) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => continue,
};
break :source_directories source_directories_value;
} else return error.PathNotFound;
defer gpa.free(source_directories);
var source_directories_split = std.mem.splitScalar(u8, source_directories, ';');
const msvc_dir: []const u8 = msvc_dir: {
const msvc_include_dir_maybe_with_trailing_slash = try gpa.dupe(u8, source_directories_split.first());
if (msvc_include_dir_maybe_with_trailing_slash.len > Dir.max_path_bytes or !Dir.path.isAbsolute(msvc_include_dir_maybe_with_trailing_slash)) {
gpa.free(msvc_include_dir_maybe_with_trailing_slash);
return error.PathNotFound;
}
var msvc_dir = std.array_list.Managed(u8).fromOwnedSlice(gpa, msvc_include_dir_maybe_with_trailing_slash);
errdefer msvc_dir.deinit();
// String might contain trailing slash, so trim it here
if (msvc_dir.items.len > "C:\\".len and msvc_dir.last().? == '\\') _ = msvc_dir.pop();
// Remove `\include` at the end of path
if (std.mem.endsWith(u8, msvc_dir.items, "\\include")) {
msvc_dir.shrinkRetainingCapacity(msvc_dir.items.len - "\\include".len);
}
try msvc_dir.appendSlice("\\Lib\\");
try msvc_dir.appendSlice(switch (arch) {
.thumb => "arm",
.aarch64 => "arm64",
.x86 => "x86",
.x86_64 => "x64",
else => unreachable,
});
const msvc_dir_with_arch = try msvc_dir.toOwnedSlice();
break :msvc_dir msvc_dir_with_arch;
};
errdefer gpa.free(msvc_dir);
if (!verifyLibDir(io, msvc_dir)) {
return error.PathNotFound;
}
return msvc_dir;
}
fn findViaVs7Key(
gpa: Allocator,
io: Io,
registry: *Registry,
arch: std.Target.Cpu.Arch,
environ_map: *const Environ.Map,
) error{ OutOfMemory, PathNotFound }![]const u8 {
var base_path: std.array_list.Managed(u8) = base_path: {
try_env: {
if (environ_map.get("VS140COMNTOOLS")) |VS140COMNTOOLS| {
if (VS140COMNTOOLS.len < "C:\\Common7\\Tools".len) break :try_env;
if (!Dir.path.isAbsolute(VS140COMNTOOLS)) break :try_env;
var list = std.array_list.Managed(u8).init(gpa);
errdefer list.deinit();
try list.appendSlice(VS140COMNTOOLS); // C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools
// String might contain trailing slash, so trim it here
if (list.items.len > "C:\\".len and list.last().? == '\\') _ = list.pop();
list.shrinkRetainingCapacity(list.items.len - "\\Common7\\Tools".len); // C:\Program Files (x86)\Microsoft Visual Studio 14.0
break :base_path list;
}
}
const vs7_key = registry.openSoftwareKey(.{ .root = .local_machine, .wow64 = .wow64_32 }, L("Microsoft\\VisualStudio\\SxS\\VS7")) catch return error.PathNotFound;
defer vs7_key.close();
try_vs7_key: {
const path_maybe_with_trailing_slash = vs7_key.getString(gpa, .{ .name = L("14.0") }, .wtf8) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
else => break :try_vs7_key,
};
if (path_maybe_with_trailing_slash.len > Dir.max_path_bytes or !Dir.path.isAbsolute(path_maybe_with_trailing_slash)) {
gpa.free(path_maybe_with_trailing_slash);
break :try_vs7_key;
}
var path = std.array_list.Managed(u8).fromOwnedSlice(gpa, path_maybe_with_trailing_slash);
errdefer path.deinit();
// String might contain trailing slash, so trim it here
if (path.items.len > "C:\\".len and path.last().? == '\\') _ = path.pop();
break :base_path path;
}
return error.PathNotFound;
};
errdefer base_path.deinit();
try base_path.appendSlice("\\VC\\lib\\");
try base_path.appendSlice(switch (arch) {
.thumb => "arm",
.aarch64 => "arm64",
.x86 => "", //x86 is in the root of the Lib folder
.x86_64 => "amd64",
else => unreachable,
});
if (!verifyLibDir(io, base_path.items)) {
return error.PathNotFound;
}
const full_path = try base_path.toOwnedSlice();
return full_path;
}
fn verifyLibDir(io: Io, lib_dir_path: []const u8) bool {
std.debug.assert(Dir.path.isAbsolute(lib_dir_path)); // should be already handled in `findVia*`
var dir = Dir.openDirAbsolute(io, lib_dir_path, .{}) catch return false;
defer dir.close(io);
const stat = dir.statFile(io, "vcruntime.lib", .{}) catch return false;
if (stat.kind != .file)
return false;
return true;
}
/// Find path to MSVC's `lib/` directory.
/// Caller owns the result.
pub fn find(
gpa: Allocator,
io: Io,
registry: *Registry,
arch: std.Target.Cpu.Arch,
environ_map: *const Environ.Map,
) error{ OutOfMemory, MsvcLibDirNotFound }![]const u8 {
const full_path = MsvcLibDir.findViaCOM(gpa, io, registry, arch, environ_map) catch |err1| switch (err1) {
error.OutOfMemory => |e| return e,
error.PathNotFound => MsvcLibDir.findViaRegistry(gpa, io, arch, environ_map) catch |err2| switch (err2) {
error.OutOfMemory => |e| return e,
error.PathNotFound => MsvcLibDir.findViaVs7Key(gpa, io, registry, arch, environ_map) catch |err3| switch (err3) {
error.OutOfMemory => |e| return e,
error.PathNotFound => return error.MsvcLibDirNotFound,
},
},
};
errdefer gpa.free(full_path);
return full_path;
}
}