Skip to content

src/windows_sdk.cpp: port to Zig #15657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ set(ZIG_CPP_SOURCES
"${CMAKE_SOURCE_DIR}/src/zig_clang_driver.cpp"
"${CMAKE_SOURCE_DIR}/src/zig_clang_cc1_main.cpp"
"${CMAKE_SOURCE_DIR}/src/zig_clang_cc1as_main.cpp"
# https://github.com/ziglang/zig/issues/6363
"${CMAKE_SOURCE_DIR}/src/windows_sdk.cpp"
)
# Needed because we use cmake, not the zig build system, to build zig2.o.
set(ZIG_STAGE2_SOURCES
Expand Down
2 changes: 0 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,6 @@ const zig_cpp_sources = [_][]const u8{
"src/zig_clang_driver.cpp",
"src/zig_clang_cc1_main.cpp",
"src/zig_clang_cc1as_main.cpp",
// https://github.com/ziglang/zig/issues/6363
"src/windows_sdk.cpp",
};

const clang_libs = [_][]const u8{
Expand Down
38 changes: 38 additions & 0 deletions lib/std/os/windows/advapi32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,41 @@ pub extern "advapi32" fn RegCloseKey(hKey: HKEY) callconv(WINAPI) LSTATUS;
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa387694.aspx */
pub extern "advapi32" fn SystemFunction036(output: [*]u8, length: ULONG) callconv(WINAPI) BOOL;
pub const RtlGenRandom = SystemFunction036;

pub const RRF = struct {
pub const RT_ANY: DWORD = 0x0000ffff;

pub const RT_DWORD: DWORD = 0x00000018;
pub const RT_QWORD: DWORD = 0x00000048;

pub const RT_REG_BINARY: DWORD = 0x00000008;
pub const RT_REG_DWORD: DWORD = 0x00000010;
pub const RT_REG_EXPAND_SZ: DWORD = 0x00000004;
pub const RT_REG_MULTI_SZ: DWORD = 0x00000020;
pub const RT_REG_NONE: DWORD = 0x00000001;
pub const RT_REG_QWORD: DWORD = 0x00000040;
pub const RT_REG_SZ: DWORD = 0x00000002;

pub const NOEXPAND: DWORD = 0x10000000;
pub const ZEROONFAILURE: DWORD = 0x20000000;
pub const SUBKEY_WOW6464KEY: DWORD = 0x00010000;
pub const SUBKEY_WOW6432KEY: DWORD = 0x00020000;
};

pub extern "advapi32" fn RegGetValueW(
hkey: HKEY,
lpSubKey: LPCWSTR,
lpValue: LPCWSTR,
dwFlags: DWORD,
pdwType: ?*DWORD,
pvData: ?*anyopaque,
pcbData: ?*DWORD,
) callconv(WINAPI) LSTATUS;

pub extern "advapi32" fn RegLoadAppKeyW(
lpFile: LPCWSTR,
phkResult: *HKEY,
samDesired: REGSAM,
dwOptions: DWORD,
reserved: DWORD,
) callconv(WINAPI) LSTATUS;
63 changes: 28 additions & 35 deletions src/libc_installation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,19 @@ pub const LibCInstallation = struct {
} else if (is_windows) {
if (!build_options.have_llvm)
return error.WindowsSdkNotFound;
var sdk: *ZigWindowsSDK = undefined;
switch (ZigWindowsSDK.find(&sdk)) {
.None => {
defer sdk.free();

try self.findNativeMsvcIncludeDir(args, sdk);
try self.findNativeMsvcLibDir(args, sdk);
try self.findNativeKernel32LibDir(args, sdk);
try self.findNativeIncludeDirWindows(args, sdk);
try self.findNativeCrtDirWindows(args, sdk);
},
.OutOfMemory => return error.OutOfMemory,
.NotFound => return error.WindowsSdkNotFound,
.PathTooLong => return error.WindowsSdkNotFound,
}

var sdk: ZigWindowsSDK = ZigWindowsSDK.find(args.allocator) catch |err| switch (err) {
error.NotFound => return error.WindowsSdkNotFound,
error.PathTooLong => return error.WindowsSdkNotFound,
error.OutOfMemory => return error.OutOfMemory,
};
defer sdk.free(args.allocator);

try self.findNativeMsvcIncludeDir(args, &sdk);
try self.findNativeMsvcLibDir(args, &sdk);
try self.findNativeKernel32LibDir(args, &sdk);
try self.findNativeIncludeDirWindows(args, &sdk);
try self.findNativeCrtDirWindows(args, &sdk);
} else if (is_haiku) {
try self.findNativeIncludeDirPosix(args);
try self.findNativeCrtBeginDirHaiku(args);
Expand Down Expand Up @@ -512,8 +510,7 @@ pub const LibCInstallation = struct {
) FindError!void {
const allocator = args.allocator;

const msvc_lib_dir_ptr = sdk.msvc_lib_dir_ptr orelse return error.LibCStdLibHeaderNotFound;
const msvc_lib_dir = msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len];
const msvc_lib_dir = sdk.msvc_lib_dir orelse return error.LibCStdLibHeaderNotFound;
const up1 = fs.path.dirname(msvc_lib_dir) orelse return error.LibCStdLibHeaderNotFound;
const up2 = fs.path.dirname(up1) orelse return error.LibCStdLibHeaderNotFound;

Expand Down Expand Up @@ -544,8 +541,8 @@ pub const LibCInstallation = struct {
sdk: *ZigWindowsSDK,
) FindError!void {
const allocator = args.allocator;
const msvc_lib_dir_ptr = sdk.msvc_lib_dir_ptr orelse return error.LibCRuntimeNotFound;
self.msvc_lib_dir = try allocator.dupeZ(u8, msvc_lib_dir_ptr[0..sdk.msvc_lib_dir_len]);
const msvc_lib_dir = sdk.msvc_lib_dir orelse return error.LibCRuntimeNotFound;
self.msvc_lib_dir = try allocator.dupe(u8, msvc_lib_dir);
}
};

Expand Down Expand Up @@ -657,23 +654,19 @@ const Search = struct {

fn fillSearch(search_buf: *[2]Search, sdk: *ZigWindowsSDK) []Search {
var search_end: usize = 0;
if (sdk.path10_ptr) |path10_ptr| {
if (sdk.version10_ptr) |version10_ptr| {
search_buf[search_end] = Search{
.path = path10_ptr[0..sdk.path10_len],
.version = version10_ptr[0..sdk.version10_len],
};
search_end += 1;
}
if (sdk.windows10sdk) |windows10sdk| {
search_buf[search_end] = .{
.path = windows10sdk.path,
.version = windows10sdk.version,
};
search_end += 1;
}
if (sdk.path81_ptr) |path81_ptr| {
if (sdk.version81_ptr) |version81_ptr| {
search_buf[search_end] = Search{
.path = path81_ptr[0..sdk.path81_len],
.version = version81_ptr[0..sdk.version81_len],
};
search_end += 1;
}
if (sdk.windows81sdk) |windows81sdk| {
search_buf[search_end] = .{
.path = windows81sdk.path,
.version = windows81sdk.version,
};
search_end += 1;
}
return search_buf[0..search_end];
}
Expand Down
Loading