aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-01-04 15:30:22 -0500
committerAndrew Kelley <superjoe30@gmail.com>2018-01-04 15:30:22 -0500
commitd008e209e7446311bda1b555284978dfabb91308 (patch)
tree0e2fd31e36f0db7eaf431ca92bd621919b5a508c /std
parente1c03d9e8eb52b489d9b47bbe2f12cacac8a999f (diff)
downloadzig-d008e209e7446311bda1b555284978dfabb91308.tar.gz
zig-d008e209e7446311bda1b555284978dfabb91308.zip
self-hosted compiler works on macos
Diffstat (limited to 'std')
-rw-r--r--std/c/darwin.zig2
-rw-r--r--std/os/index.zig21
2 files changed, 19 insertions, 4 deletions
diff --git a/std/c/darwin.zig b/std/c/darwin.zig
index 433463fde9..006dcc066b 100644
--- a/std/c/darwin.zig
+++ b/std/c/darwin.zig
@@ -1,4 +1,6 @@
extern "c" fn __error() -> &c_int;
+pub extern "c" fn _NSGetExecutablePath(buf: &u8, bufsize: &u32) -> c_int;
+
pub use @import("../os/darwin_errno.zig");
diff --git a/std/os/index.zig b/std/os/index.zig
index 96d374503f..952108ffcc 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -1553,10 +1553,12 @@ pub fn openSelfExe() -> %io.File {
pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
switch (builtin.os) {
Os.linux => {
- @compileError("TODO: selfExePath for linux");
+ // If the currently executing binary has been deleted,
+ // the file path looks something like `/a/b/c/exe (deleted)`
+ return readLink(allocator, "/proc/self/exe");
},
Os.windows => {
- var out_path = %return Buffer.initSize(allocator, 256);
+ var out_path = %return Buffer.initSize(allocator, 0xff);
%defer out_path.deinit();
while (true) {
const dword_len = %return math.cast(windows.DWORD, out_path.len());
@@ -1571,9 +1573,20 @@ pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
out_path.shrink(copied_amt);
return out_path.toOwnedSlice();
}
- %return out_path.resize(out_path.len() * 2);
+ const new_len = (%return math.shlExact(out_path.len(), 1)) | 0b1;
+ %return out_path.resize(new_len);
}
},
+ Os.darwin, Os.macosx, Os.ios => {
+ var u32_len: u32 = 0;
+ const ret1 = c._NSGetExecutablePath(undefined, &u32_len);
+ assert(ret1 != 0);
+ const bytes = %return allocator.alloc(u8, u32_len);
+ %defer allocator.free(bytes);
+ const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len);
+ assert(ret2 == 0);
+ return bytes;
+ },
else => @compileError("Unsupported OS"),
}
}
@@ -1592,7 +1605,7 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 {
const dir = path.dirname(full_exe_path);
return allocator.shrink(u8, full_exe_path, dir.len);
},
- Os.windows => {
+ Os.windows, Os.darwin, Os.macosx, Os.ios => {
const self_exe_path = %return selfExePath(allocator);
%defer allocator.free(self_exe_path);
const dirname = os.path.dirname(self_exe_path);