authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-04 13:48:45-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-04 13:48:45-05:00
loge1c03d9e8eb52b489d9b47bbe2f12cacac8a999f
treee5a79ed89a82f5db8a9ce963f4edd0d7b5f72295
parent0cd63b28f32fdf5aaedf9daa8ff60ec58de2f7f4

self-hosted compiler works on windows

* better error message for realpath failing * fix bug in std.io.readFileAllocExtra incorrectly returning error.EndOfStream * implement std.os.selfExePath and std.os.selfExeDirPath for windows

4 files changed, 42 insertions(+), 15 deletions(-)

src-self-hosted/module.zig+1-1
......@@ -208,7 +208,7 @@ pub const Module = struct {
208208
209209 const root_src_path = self.root_src_path ?? @panic("TODO handle null root src path");
210210 const root_src_real_path = os.path.real(self.allocator, root_src_path) %% |err| {
211 %return printError("unable to open '{}': {}", root_src_path, err);
211 %return printError("unable to get real path '{}': {}", root_src_path, err);
212212 return err;
213213 };
214214 %defer self.allocator.free(root_src_real_path);
std/io.zig+1-1
......@@ -513,7 +513,7 @@ pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len
513513 %defer allocator.free(buf);
514514
515515 var adapter = FileInStream.init(&file);
516 %return adapter.stream.readNoEof(buf);
516 %return adapter.stream.readNoEof(buf[0..size]);
517517 return buf;
518518}
519519
std/os/index.zig+38-13
......@@ -1544,6 +1544,40 @@ pub fn openSelfExe() -> %io.File {
15441544 }
15451545}
15461546
1547/// Get the path to the current executable.
1548/// If you only need the directory, use selfExeDirPath.
1549/// If you only want an open file handle, use openSelfExe.
1550/// This function may return an error if the current executable
1551/// was deleted after spawning.
1552/// Caller owns returned memory.
1553pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
1554 switch (builtin.os) {
1555 Os.linux => {
1556 @compileError("TODO: selfExePath for linux");
1557 },
1558 Os.windows => {
1559 var out_path = %return Buffer.initSize(allocator, 256);
1560 %defer out_path.deinit();
1561 while (true) {
1562 const dword_len = %return math.cast(windows.DWORD, out_path.len());
1563 const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len);
1564 if (copied_amt <= 0) {
1565 const err = windows.GetLastError();
1566 return switch (err) {
1567 else => unexpectedErrorWindows(err),
1568 };
1569 }
1570 if (copied_amt < out_path.len()) {
1571 out_path.shrink(copied_amt);
1572 return out_path.toOwnedSlice();
1573 }
1574 %return out_path.resize(out_path.len() * 2);
1575 }
1576 },
1577 else => @compileError("Unsupported OS"),
1578 }
1579}
1580
15471581/// Get the directory path that contains the current executable.
15481582/// Caller owns returned memory.
15491583pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 {
......@@ -1559,19 +1593,10 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 {
15591593 return allocator.shrink(u8, full_exe_path, dir.len);
15601594 },
15611595 Os.windows => {
1562 @panic("TODO windows std.os.selfExeDirPath");
1563 //buf_resize(out_path, 256);
1564 //for (;;) {
1565 // DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path));
1566 // if (copied_amt <= 0) {
1567 // return ErrorFileNotFound;
1568 // }
1569 // if (copied_amt < buf_len(out_path)) {
1570 // buf_resize(out_path, copied_amt);
1571 // return 0;
1572 // }
1573 // buf_resize(out_path, buf_len(out_path) * 2);
1574 //}
1596 const self_exe_path = %return selfExePath(allocator);
1597 %defer allocator.free(self_exe_path);
1598 const dirname = os.path.dirname(self_exe_path);
1599 return allocator.shrink(u8, self_exe_path, dirname.len);
15751600 },
15761601 else => @compileError("unimplemented: std.os.selfExeDirPath for " ++ @tagName(builtin.os)),
15771602 }
std/os/windows/index.zig+2
......@@ -48,6 +48,8 @@ pub extern "kernel32" stdcallcc fn GetExitCodeProcess(hProcess: HANDLE, lpExitCo
4848
4949pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: &LARGE_INTEGER) -> BOOL;
5050
51pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: LPSTR, nSize: DWORD) -> DWORD;
52
5153pub extern "kernel32" stdcallcc fn GetLastError() -> DWORD;
5254
5355pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(in_hFile: HANDLE,