windows: initial support for zig build test

Makes progress getting "zig build test" to work on windows.  Mostly
fixed issues around build configuration and added some branches throughout
the Zig code to return/throw errors for unimplemented parts.

I also added an initial implementation for getting the home dir.
This commit is contained in:
Jonathan Marler
2023-09-14 02:34:43 -06:00
parent e213a93fe3
commit a2e881ff4e
9 changed files with 89 additions and 24 deletions

View File

@@ -13,6 +13,7 @@ const Error = error{
pub inline fn home(buf: []u8) !?[]u8 {
return switch (builtin.os.tag) {
inline .linux, .macos => try homeUnix(buf),
.windows => try homeWindows(buf),
else => @compileError("unimplemented"),
};
}
@@ -77,6 +78,36 @@ fn homeUnix(buf: []u8) !?[]u8 {
return null;
}
fn homeWindows(buf: []u8) !?[]u8 {
const drive_len = blk: {
var fba_instance = std.heap.FixedBufferAllocator.init(buf);
const fba = fba_instance.allocator();
const drive = std.process.getEnvVarOwned(fba, "HOMEDRIVE") catch |err| switch (err) {
error.OutOfMemory => return Error.BufferTooSmall,
error.InvalidUtf8,
error.EnvironmentVariableNotFound => return null,
};
// could shift the contents if this ever happens
if (drive.ptr != buf.ptr) @panic("codebug");
break :blk drive.len;
};
const path_len = blk: {
const path_buf = buf[drive_len..];
var fba_instance = std.heap.FixedBufferAllocator.init(buf[drive_len..]);
const fba = fba_instance.allocator();
const homepath = std.process.getEnvVarOwned(fba, "HOMEPATH") catch |err| switch (err) {
error.OutOfMemory => return Error.BufferTooSmall,
error.InvalidUtf8,
error.EnvironmentVariableNotFound => return null,
};
// could shift the contents if this ever happens
if (homepath.ptr != path_buf.ptr) @panic("codebug");
break :blk homepath.len;
};
return buf[0 .. drive_len + path_len];
}
fn trimSpace(input: []const u8) []const u8 {
return std.mem.trim(u8, input, " \n\t");
}

View File

@@ -15,7 +15,7 @@ comptime {
}
/// Used to determine the default shell and directory on Unixes.
const c = @cImport({
const c = if (builtin.os.tag == .windows) { } else @cImport({
@cInclude("sys/types.h");
@cInclude("unistd.h");
@cInclude("pwd.h");
@@ -30,6 +30,8 @@ pub const Entry = struct {
/// Get the passwd entry for the currently executing user.
pub fn get(alloc: Allocator) !Entry {
if (builtin.os.tag == .windows) @panic("todo: windows");
var buf: [1024]u8 = undefined;
var pw: c.struct_passwd = undefined;
var pw_ptr: ?*c.struct_passwd = null;