Skip to content

std/c: allow overriding c imports for custom OS - #20241

Closed
silbinarywolf wants to merge 1 commit into
ziglang:masterfrom
silbinarywolf:feature/override-os-c
Closed

std/c: allow overriding c imports for custom OS#20241
silbinarywolf wants to merge 1 commit into
ziglang:masterfrom
silbinarywolf:feature/override-os-c

Conversation

@silbinarywolf

@silbinarywolfsilbinarywolf commented Jun 9, 2024

Copy link
Copy Markdown
Contributor

Description

I've been experimenting with getting my app running on the Wii via the DevkitPro toolchain, and I've gotten various things working using the standard library which includes reading/writing files to an SD card.

However to make it work, it required patching upstream with something like this.

How I use it

Used for Wii SDL2 application here:

Example C OS zig files:

main.zig

pubconstos=struct {
pubconstc=@import("c/os.zig");
};

c/os.zig

conststd=@import("std");
constc=@import("c.zig");
/// os is the C operating system definitions, ie. c/wasi.zigpubconstos=@import("wii.zig");
pubconstpthread_mutex_t=@compileError("pthread not supported by Wii library. Must compile single threaded unless we polyfill pthread functions");
// NOTE(jae): 2024-06-03// AT is seemingly not supported for the Wii as it also doesn't support "openat"// we polyfill "openat" ourselves.pubconstAT=struct {
/// Special value used to indicate openat should use the current working directorypubconstFDCWD=-2;
};
// NOTE(jae): 2024-06-03// Copied from lib/std/os/linux.zig, section: .powerpc, .powerpcle, .powerpc64, .powerpc64lepubconstO=packedstruct(u32) {
ACCMODE: std.posix.ACCMODE=.RDONLY,
_2: u4=0,
CREAT: bool=false,
EXCL: bool=false,
NOCTTY: bool=false,
TRUNC: bool=false,
APPEND: bool=false,
NONBLOCK: bool=false,
DSYNC: bool=false,
ASYNC: bool=false,
DIRECTORY: bool=false,
NOFOLLOW: bool=false,
LARGEFILE: bool=false,
DIRECT: bool=false,
NOATIME: bool=false,
CLOEXEC: bool=false,
SYNC: bool=false,
PATH: bool=false,
TMPFILE: bool=false,
_: u9=0,
};

c/wii.zig

constbuiltin=@import("builtin");
conststd=@import("std");
constc=@import("c.zig");
pubconst_errno=struct {
externfn__errno() *c_int;
}.__errno;
pubconstPATH_MAX=4096;
pubconstmode_t=u32;
pubconsttime_t=i64;
pubconstSTDIN_FILENO=0;
pubconstSTDOUT_FILENO=1;
pubconstSTDERR_FILENO=2;
constclockid_t=i32;
// ... much more things in here, etc ...

c/c.zig

pubusingnamespace@cImport({
// OGC Library@cInclude("ogcsys.h");
@cInclude("gccore.h");
// C library@cInclude("stdio.h");
@cInclude("errno.h");
@cInclude("fcntl.h");
@cInclude("dirent.h");
});

Seperate to this we also have a "runtime" we build to polyfill things.
c/runtime.zig

constroot=@import("root");
conststd=@import("std");
constbuiltin=@import("builtin");
constc=@import("c.zig");
comptime {
@export(openat, .{ .name="openat", .linkage=.strong });
@export(write, .{ .name="__wrap_write", .linkage=.strong });
if (builtin.os.tag==.wasi) {
@export(wasi_errno, .{ .name="errno", .linkage=.strong });
}
}
/// openat is polyfilled as it doesn't have an implementation for devkitPPCfnopenat(dirfd: c_int, pathname: [*c]constu8, flags: c_int) callconv(.C) c_int {
// TODO(jae): 2024-06-02// Make this actually use dirfd_=dirfd; // autofixconstfile=c.open(pathname, flags);
returnfile;
}
/// wrap "write" to get printf debugging in Dolphin emulator, if the "fd" is STDOUT or STDERR we call print./// otherwise fallback to writing to the file descriptorfnwrite(fd: i32, buf: [*]constu8, count: usize) callconv(.C) isize {
const__real_write=struct {
extern"c"fn__real_write(fd: std.c.fd_t, buf: [*]constu8, nbyte: usize) isize;
}.__real_write;
if (fd==std.c.STDOUT_FILENOorfd==std.c.STDERR_FILENO) {
// https://stackoverflow.com/a/3767300return@intCast(c.printf("%.*s", count, buf));
}
return__real_write(fd, buf, count);
}
/// wasi_errno calls the underlying Wii toolchain errno for builds targetting .wasifnwasi_errno() callconv(.C) *c_int {
const_errno=struct {
externfn__errno() *c_int;
}.__errno;
return_errno();
}

silbinarywolf added a commit to silbinarywolf/zig-wii-sdk that referenced this pull request Jun 11, 2024
…g#20241 is merged and working for now)
- update gcc to use -O2 flag is optimize is set to .ReleaseFast
- improve printf by buffering a little before \n so that in Dolphin the text prints out together
@alexrp

Copy link
Copy Markdown
Member

I can't speak to whether this direction is desirable, but in any case, it will need rebasing after #20679.

@silbinarywolf

Copy link
Copy Markdown
ContributorAuthor

mmm, I think a better version of this would be good in the future but for the time-being, I've opted to go with a hacky approach with my zig-wii-sdk and target .wasi, then provide those Wasi functions and call the underlying C library code.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@silbinarywolf@alexrp