Skip to content

Commit 77b9623

Browse files
authored
Merge pull request #15657 from BratishkaErik/windows-sdk-finder-port-to-zig
src/windows_sdk.cpp: port to Zig
2 parents e8fa199 + 38d10ee commit 77b9623

File tree

8 files changed

+820
-1412
lines changed

8 files changed

+820
-1412
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ set(ZIG_CPP_SOURCES
193193
"${CMAKE_SOURCE_DIR}/src/zig_clang_driver.cpp"
194194
"${CMAKE_SOURCE_DIR}/src/zig_clang_cc1_main.cpp"
195195
"${CMAKE_SOURCE_DIR}/src/zig_clang_cc1as_main.cpp"
196-
# https://github.com/ziglang/zig/issues/6363
197-
"${CMAKE_SOURCE_DIR}/src/windows_sdk.cpp"
198196
)
199197
# Needed because we use cmake, not the zig build system, to build zig2.o.
200198
set(ZIG_STAGE2_SOURCES

build.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,6 @@ const zig_cpp_sources = [_][]const u8{
927927
"src/zig_clang_driver.cpp",
928928
"src/zig_clang_cc1_main.cpp",
929929
"src/zig_clang_cc1as_main.cpp",
930-
// https://github.com/ziglang/zig/issues/6363
931-
"src/windows_sdk.cpp",
932930
};
933931

934932
const clang_libs = [_][]const u8{

lib/std/os/windows/advapi32.zig

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,41 @@ pub extern "advapi32" fn RegCloseKey(hKey: HKEY) callconv(WINAPI) LSTATUS;
3333
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
3434
pub extern "advapi32" fn SystemFunction036(output: [*]u8, length: ULONG) callconv(WINAPI) BOOL;
3535
pub const RtlGenRandom = SystemFunction036;
36+
37+
pub const RRF = struct {
38+
pub const RT_ANY: DWORD = 0x0000ffff;
39+
40+
pub const RT_DWORD: DWORD = 0x00000018;
41+
pub const RT_QWORD: DWORD = 0x00000048;
42+
43+
pub const RT_REG_BINARY: DWORD = 0x00000008;
44+
pub const RT_REG_DWORD: DWORD = 0x00000010;
45+
pub const RT_REG_EXPAND_SZ: DWORD = 0x00000004;
46+
pub const RT_REG_MULTI_SZ: DWORD = 0x00000020;
47+
pub const RT_REG_NONE: DWORD = 0x00000001;
48+
pub const RT_REG_QWORD: DWORD = 0x00000040;
49+
pub const RT_REG_SZ: DWORD = 0x00000002;
50+
51+
pub const NOEXPAND: DWORD = 0x10000000;
52+
pub const ZEROONFAILURE: DWORD = 0x20000000;
53+
pub const SUBKEY_WOW6464KEY: DWORD = 0x00010000;
54+
pub const SUBKEY_WOW6432KEY: DWORD = 0x00020000;
55+
};
56+
57+
pub extern "advapi32" fn RegGetValueW(
58+
hkey: HKEY,
59+
lpSubKey: LPCWSTR,
60+
lpValue: LPCWSTR,
61+
dwFlags: DWORD,
62+
pdwType: ?*DWORD,
63+
pvData: ?*anyopaque,
64+
pcbData: ?*DWORD,
65+
) callconv(WINAPI) LSTATUS;
66+
67+
pub extern "advapi32" fn RegLoadAppKeyW(
68+
lpFile: LPCWSTR,
69+
phkResult: *HKEY,
70+
samDesired: REGSAM,
71+
dwOptions: DWORD,
72+
reserved: DWORD,
73+
) callconv(WINAPI) LSTATUS;

src/libc_installation.zig

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,19 @@ pub const LibCInstallation = struct {
186186
} else if (is_windows) {
187187
if (!build_options.have_llvm)
188188
return error.WindowsSdkNotFound;
189-
var sdk: *ZigWindowsSDK = undefined;
190-
switch (ZigWindowsSDK.find(&sdk)) {
191-
.None => {
192-
defer sdk.free();
193-
194-
try self.findNativeMsvcIncludeDir(args, sdk);
195-
try self.findNativeMsvcLibDir(args, sdk);
196-
try self.findNativeKernel32LibDir(args, sdk);
197-
try self.findNativeIncludeDirWindows(args, sdk);
198-
try self.findNativeCrtDirWindows(args, sdk);
199-
},
200-
.OutOfMemory => return error.OutOfMemory,
201-
.NotFound => return error.WindowsSdkNotFound,
202-
.PathTooLong => return error.WindowsSdkNotFound,
203-
}
189+
190+
var sdk: ZigWindowsSDK = ZigWindowsSDK.find(args.allocator) catch |err| switch (err) {
191+
error.NotFound => return error.WindowsSdkNotFound,
192+
error.PathTooLong => return error.WindowsSdkNotFound,
193+
error.OutOfMemory => return error.OutOfMemory,
194+
};
195+
defer sdk.free(args.allocator);
196+
197+
try self.findNativeMsvcIncludeDir(args, &sdk);
198+
try self.findNativeMsvcLibDir(args, &sdk);
199+
try self.findNativeKernel32LibDir(args, &sdk);
200+
try self.findNativeIncludeDirWindows(args, &sdk);
201+
try self.findNativeCrtDirWindows(args, &sdk);
204202
} else if (is_haiku) {
205203
try self.findNativeIncludeDirPosix(args);
206204
try self.findNativeCrtBeginDirHaiku(args);
@@ -512,8 +510,7 @@ pub const LibCInstallation = struct {
512510
) FindError!void {
513511
const allocator = args.allocator;
514512

515-
const msvc_lib_dir_ptr = sdk.msvc_lib_dir_ptr orelse return error.LibCStdLibHeaderNotFound;
516-
const msvc_lib_dir = msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len];
513+
const msvc_lib_dir = sdk.msvc_lib_dir orelse return error.LibCStdLibHeaderNotFound;
517514
const up1 = fs.path.dirname(msvc_lib_dir) orelse return error.LibCStdLibHeaderNotFound;
518515
const up2 = fs.path.dirname(up1) orelse return error.LibCStdLibHeaderNotFound;
519516

@@ -544,8 +541,8 @@ pub const LibCInstallation = struct {
544541
sdk: *ZigWindowsSDK,
545542
) FindError!void {
546543
const allocator = args.allocator;
547-
const msvc_lib_dir_ptr = sdk.msvc_lib_dir_ptr orelse return error.LibCRuntimeNotFound;
548-
self.msvc_lib_dir = try allocator.dupeZ(u8, msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len]);
544+
const msvc_lib_dir = sdk.msvc_lib_dir orelse return error.LibCRuntimeNotFound;
545+
self.msvc_lib_dir = try allocator.dupe(u8, msvc_lib_dir);
549546
}
550547
};
551548

@@ -657,23 +654,19 @@ const Search = struct {
657654

658655
fn fillSearch(search_buf: *[2]Search, sdk: *ZigWindowsSDK) []Search {
659656
var search_end: usize = 0;
660-
if (sdk.path10_ptr) |path10_ptr| {
661-
if (sdk.version10_ptr) |version10_ptr| {
662-
search_buf[search_end] = Search{
663-
.path = path10_ptr[0..sdk.path10_len],
664-
.version = version10_ptr[0..sdk.version10_len],
665-
};
666-
search_end += 1;
667-
}
657+
if (sdk.windows10sdk) |windows10sdk| {
658+
search_buf[search_end] = .{
659+
.path = windows10sdk.path,
660+
.version = windows10sdk.version,
661+
};
662+
search_end += 1;
668663
}
669-
if (sdk.path81_ptr) |path81_ptr| {
670-
if (sdk.version81_ptr) |version81_ptr| {
671-
search_buf[search_end] = Search{
672-
.path = path81_ptr[0..sdk.path81_len],
673-
.version = version81_ptr[0..sdk.version81_len],
674-
};
675-
search_end += 1;
676-
}
664+
if (sdk.windows81sdk) |windows81sdk| {
665+
search_buf[search_end] = .{
666+
.path = windows81sdk.path,
667+
.version = windows81sdk.version,
668+
};
669+
search_end += 1;
677670
}
678671
return search_buf[0..search_end];
679672
}

0 commit comments

Comments
 (0)