authorgravatar for 3811822-hoanga@users.noreply.gitlab.comAl Hoang <3811822-hoanga@users.noreply.gitlab.com> 2021-01-17 13:29:00-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-02-25 16:41:42-07:00
logc17396691ce116a949618cb03d740997c7e80788
treea7d9f8fbf9cb4b765609af2ec842b17e6f0e7be5
parent6b0372229da4fa4cf32f0ee1eb079f530554b094

initial support for haiku sync update

* add cpu count * use haiku find_directory * add definitions and exports for building in haiku

9 files changed, 136 insertions(+), 33 deletions(-)

lib/std/Thread.zig+10
......@@ -504,6 +504,13 @@ pub fn cpuCount() CpuCountError!usize {
504504 };
505505 return @intCast(usize, count);
506506 }
507 if (std.Target.current.os.tag == .haiku) {
508 var count: u32 = undefined;
509 var system_info: os.system_info = undefined;
510 const rc = os.system.get_system_info(&system_info);
511 count = system_info.cpu_count;
512 return @intCast(usize, count);
513 }
507514 var count: c_int = undefined;
508515 var count_len: usize = @sizeOf(c_int);
509516 const name = if (comptime std.Target.current.isDarwin()) "hw.logicalcpu" else "hw.ncpu";
......@@ -538,6 +545,9 @@ pub fn getCurrentThreadId() u64 {
538545 .openbsd => {
539546 return @bitCast(u32, c.getthrid());
540547 },
548 .haiku => {
549 return @bitCast(u32, c.find_thread(null));
550 },
541551 else => {
542552 @compileError("getCurrentThreadId not implemented for this platform");
543553 },
lib/std/c/haiku.zig+20-2
......@@ -14,10 +14,21 @@ extern "c" fn _errnop() *c_int;
1414
1515pub const _errno = _errnop;
1616
17pub extern "c" fn _kern_read_dir(fd: c_int, buf_ptr: [*]u8, nbytes: usize, maxcount: u32) usize;
17pub extern "c" fn find_directory(which: c_int, volume: i32, createIt: bool, path_ptr: [*]u8, length: i32) u64;
18
19pub extern "c" fn find_thread(thread_name: ?*c_void) i32;
20
21pub extern "c" fn get_system_info(system_info: *system_info) usize;
1822
23// TODO revisit if abi changes or better option becomes apparent
1924pub extern "c" fn _get_next_image_info(team: c_int, cookie: *i32, image_info: *image_info) usize;
2025
26pub extern "c" fn _kern_read_dir(fd: c_int, buf_ptr: [*]u8, nbytes: usize, maxcount: u32) usize;
27
28pub extern "c" fn _kern_read_stat(fd: c_int, path_ptr: [*]u8, traverse_link: bool, libc_stat: *libc_stat, stat_size: i32) usize;
29
30pub extern "c" fn _kern_get_current_team() i32;
31
2132pub const sem_t = extern struct {
2233 _magic: u32,
2334 _kern: extern struct {
......@@ -27,6 +38,14 @@ pub const sem_t = extern struct {
2738 _padding: u32,
2839};
2940
41pub const pthread_attr_t = extern struct {
42 __detach_state: i32,
43 __sched_priority: i32,
44 __stack_size: i32,
45 __guard_size: i32,
46 __stack_address: ?*c_void,
47};
48
3049pub const pthread_mutex_t = extern struct {
3150 flags: u32 = 0,
3251 lock: i32 = 0,
......@@ -34,7 +53,6 @@ pub const pthread_mutex_t = extern struct {
3453 owner: i32 = -1,
3554 owner_count: i32 = 0,
3655};
37
3856pub const pthread_cond_t = extern struct {
3957 flags: u32 = 0,
4058 unused: i32 = -42,
lib/std/crypto/tlcsprng.zig+1
......@@ -29,6 +29,7 @@ const os_has_fork = switch (std.Target.current.os.tag) {
2929 .solaris,
3030 .tvos,
3131 .watchos,
32 .haiku,
3233 => true,
3334
3435 else => false,
lib/std/fs.zig+26-3
......@@ -472,8 +472,25 @@ pub const Dir = struct {
472472 continue :start_over;
473473 }
474474
475 // TODO: determine entry kind
476 const entry_kind = Entry.Kind.File;
475 var stat_info: os.libc_stat = undefined;
476 const rc2 = os.system._kern_read_stat(
477 self.dir.fd,
478 &haiku_entry.d_name,
479 false,
480 &stat_info,
481 0,
482 );
483 const statmode = stat_info.mode & os.S_IFMT;
484
485 const entry_kind = switch (statmode) {
486 os.S_IFDIR => Entry.Kind.Directory,
487 os.S_IFBLK => Entry.Kind.BlockDevice,
488 os.S_IFCHR => Entry.Kind.CharacterDevice,
489 os.S_IFLNK => Entry.Kind.SymLink,
490 os.S_IFREG => Entry.Kind.File,
491 os.S_IFIFO => Entry.Kind.NamedPipe,
492 else => Entry.Kind.Unknown,
493 };
477494
478495 return Entry{
479496 .name = name,
......@@ -676,7 +693,13 @@ pub const Dir = struct {
676693
677694 pub fn iterate(self: Dir) Iterator {
678695 switch (builtin.os.tag) {
679 .macos, .ios, .freebsd, .netbsd, .dragonfly, .openbsd, => return Iterator{
696 .macos,
697 .ios,
698 .freebsd,
699 .netbsd,
700 .dragonfly,
701 .openbsd,
702 => return Iterator{
680703 .dir = self,
681704 .seek = 0,
682705 .index = 0,
lib/std/fs/get_app_data_dir.zig+10-5
......@@ -57,11 +57,16 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
5757 return fs.path.join(allocator, &[_][]const u8{ home_dir, ".local", "share", appname });
5858 },
5959 .haiku => {
60 const home_dir = os.getenv("HOME") orelse {
61 // TODO look in /etc/passwd
62 return error.AppDataDirUnavailable;
63 };
64 return fs.path.join(allocator, &[_][]const u8{ home_dir, "config", "settings", appname });
60 var dir_path_ptr: [*:0]u8 = undefined;
61 // TODO look into directory_which
62 const be_user_settings = 0xbbe;
63 const rc = os.system.find_directory(be_user_settings, -1, true, dir_path_ptr, 1) ;
64 const settings_dir = try allocator.dupeZ(u8, mem.spanZ(dir_path_ptr));
65 defer allocator.free(settings_dir);
66 switch (rc) {
67 0 => return fs.path.join(allocator, &[_][]const u8{ settings_dir, appname }),
68 else => return error.AppDataDirUnavailable,
69 }
6570 },
6671 else => @compileError("Unsupported OS"),
6772 }
lib/std/os.zig+6
......@@ -3941,6 +3941,9 @@ pub fn sysctl(
39413941 if (builtin.os.tag == .wasi) {
39423942 @panic("unsupported");
39433943 }
3944 if (builtin.os.tag == .haiku) {
3945 @panic("unsupported");
3946 }
39443947
39453948 const name_len = math.cast(c_uint, name.len) catch return error.NameTooLong;
39463949 switch (errno(system.sysctl(name.ptr, name_len, oldp, oldlenp, newp, newlen))) {
......@@ -3965,6 +3968,9 @@ pub fn sysctlbynameZ(
39653968 if (builtin.os.tag == .wasi) {
39663969 @panic("unsupported");
39673970 }
3971 if (builtin.os.tag == .haiku) {
3972 @panic("unsupported");
3973 }
39683974
39693975 switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) {
39703976 0 => return,
lib/std/os/bits/haiku.zig+61-21
......@@ -6,8 +6,6 @@
66const std = @import("../../std.zig");
77const maxInt = std.math.maxInt;
88
9// See https://svnweb.freebsd.org/base/head/sys/sys/_types.h?view=co
10// TODO: audit pid_t/mode_t. They should likely be i32 and u16, respectively
119pub const fd_t = c_int;
1210pub const pid_t = c_int;
1311pub const uid_t = u32;
......@@ -30,6 +28,11 @@ pub const Kevent = extern struct {
3028// Modes and flags for dlopen()
3129// include/dlfcn.h
3230
31pub const POLLIN = 0x0001;
32pub const POLLERR = 0x0004;
33pub const POLLNVAL = 0x1000;
34pub const POLLHUP = 0x0080;
35
3336/// Bind function calls lazily.
3437pub const RTLD_LAZY = 1;
3538
......@@ -119,41 +122,43 @@ pub const msghdr_const = extern struct {
119122pub const off_t = i64;
120123pub const ino_t = u64;
121124
122pub const libc_stat = extern struct {
123 dev: u64,
124 ino: ino_t,
125 nlink: usize,
125pub const nfds_t = u32;
126126
127 mode: u16,
128 __pad0: u16,
129 uid: uid_t,
130 gid: gid_t,
131 __pad1: u32,
132 rdev: u64,
127pub const pollfd = extern struct {
128 fd: i32,
129 events: i16,
130 revents: i16,
131};
133132
133pub const libc_stat = extern struct {
134 dev: i32,
135 ino: u64,
136 mode: u32,
137 nlink: i32,
138 uid: i32,
139 gid: i32,
140 size: i64,
141 rdev: i32,
142 blksize: i32,
134143 atim: timespec,
135144 mtim: timespec,
136145 ctim: timespec,
137 birthtim: timespec,
138
139 size: off_t,
146 crtim: timespec,
147 st_type: u32,
140148 blocks: i64,
141 blksize: isize,
142 flags: u32,
143 gen: u64,
144 __spare: [10]u64,
145149
146150 pub fn atime(self: @This()) timespec {
147151 return self.atim;
148152 }
149
150153 pub fn mtime(self: @This()) timespec {
151154 return self.mtim;
152155 }
153
154156 pub fn ctime(self: @This()) timespec {
155157 return self.ctim;
156158 }
159 pub fn crtime(self: @This()) timespec {
160 return self.crtim;
161 }
157162};
158163
159164pub const timespec = extern struct {
......@@ -192,6 +197,34 @@ pub const image_info = extern struct {
192197 abi: i32,
193198};
194199
200pub const system_info = extern struct {
201 boot_time: i64,
202 cpu_count: u32,
203 max_pages: u64,
204 used_pages: u64,
205 cached_pages: u64,
206 block_cache_pages: u64,
207 ignored_pages: u64,
208 needed_memory: u64,
209 free_memory: u64,
210 max_swap_pages: u64,
211 free_swap_pages: u64,
212 page_faults: u32,
213 max_sems: u32,
214 used_sems: u32,
215 max_ports: u32,
216 used_ports: u32,
217 max_threads: u32,
218 used_threads: u32,
219 max_teams: u32,
220 used_teams: u32,
221 kernel_name: [256]u8,
222 kernel_build_date: [32]u8,
223 kernel_build_time: [32]u8,
224 kernel_version: i64,
225 abi: u32,
226};
227
195228pub const in_port_t = u16;
196229pub const sa_family_t = u8;
197230
......@@ -1408,3 +1441,10 @@ pub const rlimit = extern struct {
14081441pub const SHUT_RD = 0;
14091442pub const SHUT_WR = 1;
14101443pub const SHUT_RDWR = 2;
1444
1445// TODO fill out if needed
1446pub const directory_which = extern enum(c_int) {
1447 B_USER_SETTINGS_DIRECTORY = 0xbbe,
1448
1449 _,
1450};
lib/std/target.zig+1-2
......@@ -1566,8 +1566,7 @@ pub const Target = struct {
15661566 .other,
15671567 => return result,
15681568
1569 // Operating systems in this list have been verified as not having a standard
1570 // dynamic linker path.
1569 // TODO revisit when multi-arch for Haiku is available
15711570 .haiku => return copy(&result, "/system/runtime_loader"),
15721571
15731572 // TODO go over each item in this list and either move it to the above list, or
src/libc_installation.zig+1
......@@ -196,6 +196,7 @@ pub const LibCInstallation = struct {
196196 switch (Target.current.os.tag) {
197197 .freebsd, .netbsd, .openbsd => self.crt_dir = try std.mem.dupeZ(args.allocator, u8, "/usr/lib"),
198198 .linux, .dragonfly => batch.add(&async self.findNativeCrtDirPosix(args)),
199 .haiku => self.crt_dir = try std.mem.dupeZ(args.allocator, u8, "/system/develop/lib"),
199200 else => {},
200201 }
201202 break :blk batch.wait();