Skip to content

Commit

Permalink
initial support for haiku continue clean up
Browse files Browse the repository at this point in the history
* remove unused definitions
* setup os specific blocks
  • Loading branch information
Al Hoang committed Dec 29, 2020
1 parent 5a60045 commit 70c0055
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 58 deletions.
8 changes: 3 additions & 5 deletions lib/std/c/haiku.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ const builtin = std.builtin;
usingnamespace std.c;

extern "c" fn _errnop() *c_int;

pub const _errno = _errnop;

// not supported in haiku
pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
pub extern "c" fn _kern_read_dir(fd: c_int, buf_ptr: [*]u8, nbytes: usize, maxcount: u32) usize;

pub const dl_iterate_phdr_callback = fn (info: *dl_phdr_info, size: usize, data: ?*c_void) callconv(.C) c_int;
//pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
pub extern "c" fn _get_next_image_info(team: c_int, cookie: *i32, image_info: *image_info) usize;

//
pub const sem_t = extern struct {
_magic: u32,
_kern: extern struct {
Expand Down
14 changes: 1 addition & 13 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,6 @@ pub const DebugInfo = struct {

return &di;
}

};

const SymbolInfo = struct {
Expand Down Expand Up @@ -1692,17 +1691,6 @@ pub const ModuleDebugInfo = switch (builtin.os.tag) {
unreachable;
}
},
.haiku => struct {
// Haiku should implement dl_iterat_phdr (https://dev.haiku-os.org/ticket/15743)
base_address: usize,
dwarf: DW.DwarfInfo,
mapped_memory: []const u8,

pub fn getSymbolAtAddress(self: *@This(), address: usize) !SymbolInfo {
// TODO: implement me
return SymbolInfo{};
}
},
else => DW.DwarfInfo,
};

Expand All @@ -1721,7 +1709,7 @@ fn getDebugInfoAllocator() *mem.Allocator {
pub const have_segfault_handling_support = switch (builtin.os.tag) {
.linux, .netbsd => true,
.windows => true,
.freebsd, .openbsd, .haiku => @hasDecl(os, "ucontext_t"),
.freebsd, .openbsd => @hasDecl(os, "ucontext_t"),
else => false,
};
pub const enable_segfault_handler: bool = if (@hasDecl(root, "enable_segfault_handler"))
Expand Down
68 changes: 56 additions & 12 deletions lib/std/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub const Dir = struct {
const IteratorError = error{AccessDenied} || os.UnexpectedError;

pub const Iterator = switch (builtin.os.tag) {
.macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd, .haiku => struct {
.macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd => struct {
dir: Dir,
seek: i64,
buf: [8192]u8, // TODO align(@alignOf(os.dirent)),
Expand All @@ -319,7 +319,6 @@ pub const Dir = struct {
switch (builtin.os.tag) {
.macos, .ios => return self.nextDarwin(),
.freebsd, .netbsd, .dragonfly, .openbsd => return self.nextBsd(),
.haiku => return self.nextHaiku(),
else => @compileError("unimplemented"),
}
}
Expand Down Expand Up @@ -427,16 +426,61 @@ pub const Dir = struct {
};
}
}
},
.haiku => struct {
dir: Dir,
buf: [8192]u8, // TODO align(@alignOf(os.dirent64)),
index: usize,
end_index: usize,

fn nextHaiku(self: *Self) !?Entry {
//const name = @ptrCast([*]u8, "placeholder-please-implement-me");
//const name = "placeholder-please-implement-me";
return Entry{
.name = base64_alphabet,
.kind = Entry.Kind.File,
};
}
const Self = @This();

pub const Error = IteratorError;

/// Memory such as file names referenced in this returned entry becomes invalid
/// with subsequent calls to `next`, as well as when this `Dir` is deinitialized.
pub fn next(self: *Self) Error!?Entry {
start_over: while (true) {
// TODO: find a better max
const HAIKU_MAX_COUNT = 10000;
if (self.index >= self.end_index) {
const rc = os.system._kern_read_dir(
self.dir.fd,
&self.buf,
self.buf.len,
HAIKU_MAX_COUNT,
);
if (rc == 0) return null;
if (rc < 0) {
switch (os.errno(rc)) {
os.EBADF => unreachable, // Dir is invalid or was opened without iteration ability
os.EFAULT => unreachable,
os.ENOTDIR => unreachable,
os.EINVAL => unreachable,
else => |err| return os.unexpectedErrno(err),
}
}
self.index = 0;
self.end_index = @intCast(usize, rc);
}
const haiku_entry = @ptrCast(*align(1) os.dirent, &self.buf[self.index]);
const next_index = self.index + haiku_entry.reclen();
self.index = next_index;
const name = mem.spanZ(@ptrCast([*:0]u8, &haiku_entry.d_name));

if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..") or (haiku_entry.d_ino == 0)) {
continue :start_over;
}

// TODO: determine entry kind
const entry_kind = Entry.Kind.File;

return Entry{
.name = name,
.kind = entry_kind,
};
}
}
},
.linux => struct {
dir: Dir,
Expand Down Expand Up @@ -632,14 +676,14 @@ pub const Dir = struct {

pub fn iterate(self: Dir) Iterator {
switch (builtin.os.tag) {
.macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd, .haiku => return Iterator{
.macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd, => return Iterator{
.dir = self,
.seek = 0,
.index = 0,
.end_index = 0,
.buf = undefined,
},
.linux => return Iterator{
.linux, .haiku => return Iterator{
.dir = self,
.index = 0,
.end_index = 0,
Expand Down
20 changes: 3 additions & 17 deletions lib/std/os.zig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ test "" {
_ = uefi;
_ = wasi;
_ = windows;
_ = haiku;

_ = @import("os/test.zig");
}
Expand Down Expand Up @@ -587,7 +588,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
/// On these systems, the read races with concurrent writes to the same file descriptor.
pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
const have_pread_but_not_preadv = switch (std.Target.current.os.tag) {
.windows, .macos, .ios, .watchos, .tvos => true,
.windows, .macos, .ios, .watchos, .tvos, .haiku => true,
else => false,
};
if (have_pread_but_not_preadv) {
Expand Down Expand Up @@ -3832,22 +3833,7 @@ pub fn pipe() PipeError![2]fd_t {
}

pub fn pipe2(flags: u32) PipeError![2]fd_t {
if (comptime std.Target.current.isDarwin()) {
var fds: [2]fd_t = try pipe();
if (flags == 0) return fds;
errdefer {
close(fds[0]);
close(fds[1]);
}
for (fds) |fd| switch (errno(system.fcntl(fd, F_SETFL, flags))) {
0 => {},
EINVAL => unreachable, // Invalid flags
EBADF => unreachable, // Always a race condition
else => |err| return unexpectedErrno(err),
};
return fds;
}
if (comptime std.Target.current.isHaiku()) {
if (comptime (std.Target.current.isDarwin() or std.Target.current.isHaiku())) {
var fds: [2]fd_t = try pipe();
if (flags == 0) return fds;
errdefer {
Expand Down
28 changes: 22 additions & 6 deletions lib/std/os/bits/haiku.zig
Original file line number Diff line number Diff line change
Expand Up @@ -162,20 +162,36 @@ pub const timespec = extern struct {
};

pub const dirent = extern struct {
d_fileno: usize,
d_off: i64,
d_dev: i32,
d_pdev: i32,
d_ino: i64,
d_pino: i64,
d_reclen: u16,
d_type: u8,
d_pad0: u8,
d_namlen: u16,
d_pad1: u16,
d_name: [256]u8,

pub fn reclen(self: dirent) u16 {
return self.d_reclen;
}
};

pub const image_info = extern struct {
id: u32, //image_id
type: u32, // image_type
sequence: i32,
init_order: i32,
init_routine: *c_void,
term_routine: *c_void,
device: i32,
node: i32,
name: [1024]u8,
text: *c_void,
data: *c_void,
text_size: i32,
data_size: i32,
api_version: i32,
abi: i32,
};

pub const in_port_t = u16;
pub const sa_family_t = u8;

Expand Down
9 changes: 7 additions & 2 deletions lib/std/process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ pub fn getSelfExeSharedLibPaths(allocator: *Allocator) error{OutOfMemory}![][:0]
}
return paths.toOwnedSlice();
},
// Haiku should implement dl_iterat_phdr (https://dev.haiku-os.org/ticket/15743)
// revisit if Haiku implements dl_iterat_phdr (https://dev.haiku-os.org/ticket/15743)
.haiku => {
var paths = List.init(allocator);
errdefer {
Expand All @@ -785,7 +785,12 @@ pub fn getSelfExeSharedLibPaths(allocator: *Allocator) error{OutOfMemory}![][:0]
}
allocator.free(slice);
}
//TODO: fill out placeholder

var b = "/boot/system/runtime_loader";
const item = try allocator.dupeZ(u8, mem.spanZ(b));
errdefer allocator.free(item);
try paths.append(item);

return paths.toOwnedSlice();
},
else => @compileError("getSelfExeSharedLibPaths unimplemented for this target"),
Expand Down
9 changes: 6 additions & 3 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ pub const Target = struct {
.watchos,
.dragonfly,
.openbsd,
.haiku,
=> true,

.linux,
Expand All @@ -358,7 +359,6 @@ pub const Target = struct {
.kfreebsd,
.lv2,
.solaris,
.haiku,
.minix,
.rtems,
.nacl,
Expand Down Expand Up @@ -432,7 +432,6 @@ pub const Target = struct {
.dragonfly,
.lv2,
.solaris,
.haiku,
.minix,
.rtems,
.nacl,
Expand All @@ -459,6 +458,7 @@ pub const Target = struct {
.kfreebsd,
.netbsd,
.hurd,
.haiku,
=> return .gnu,
.windows,
.uefi,
Expand Down Expand Up @@ -1505,6 +1505,10 @@ pub const Target = struct {
.other,
=> return result,

// Operating systems in this list have been verified as not having a standard
// dynamic linker path.
.haiku => return copy(&result, "/system/runtime_loader"),

// TODO go over each item in this list and either move it to the above list, or
// implement the standard dynamic linker path code for it.
.ananas,
Expand All @@ -1513,7 +1517,6 @@ pub const Target = struct {
.kfreebsd,
.lv2,
.solaris,
.haiku,
.minix,
.rtems,
.nacl,
Expand Down

0 comments on commit 70c0055

Please sign in to comment.