Skip to content

Pointers needed on WASILibc #2313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
yonihemi opened this issue Dec 10, 2020 · 4 comments
Open

Pointers needed on WASILibc #2313

yonihemi opened this issue Dec 10, 2020 · 4 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@yonihemi
Copy link
Member

(no pun intended)

Hi!
I'm trying to get ZipFoundation to work in SwiftWasm.
Already got zlib building so we're getting there.

Now, when Swift code calls types like FILE or values like SEEK_SET which are missing, I'm assuming those can be included by tweaking https://github.com/swiftwasm/swift/blob/swiftwasm/stdlib/public/Platform/wasi.modulemap.gyb, correct?

(For example, perhaps these should do the trick:

#undef __DEFINED_FILE
#define __NEED_struct__IO_FILE
#define __need_FILE

).
On that note, I'm trying to track down the exact source of /usr/share/wasi-sysroot/share/wasm32-wasi/predefined-macros.txt included in the toolchain, (which includes #define __DEFINED_FILE, which we probably don't want) and how/if it affects the build.

@yonihemi yonihemi added help wanted Extra attention is needed question Further information is requested labels Dec 10, 2020
@MaxDesiatov
Copy link

MaxDesiatov commented Dec 10, 2020

Did you have a look at wasi-libc source code? https://github.com/WebAssembly/wasi-libc/blob/378fd4b21aab6d390f3a1c1817d53c422ad00a62/libc-top-half/musl/include/stdio.h#L14

Usually it's just easier to #if !os(WASI) pieces with file IO in library code than to make it work on the lower level. It also won't require you to rebuild the toolchain after you make these adjustments. Besides, I hope that in the future we'll be able to provide slim builds option that avoids WASI completely. The WASI polyfill that bridges these low-level APIs to JavaScript is about ~300kb, not even taking WASI libc itself into account.

OTOH, if a library relies on Foundation, then yes, tweaking WASI libc may be more appropriate, I'm not sure if we'll ever make Foundation work without WASI...

@yonihemi
Copy link
Member Author

Thanks! I am focusing on some Foundation or lower things ATM so seems like have no choice.

@MaxDesiatov
Copy link

I think predefined-macros.txt is automatically generated from wasi-libc source code, but I may be wrong. @kateinoigakukun worked on setting the WASILibc module up, so maybe he could provide some pointers 😄

@kateinoigakukun
Copy link
Member

kateinoigakukun commented Dec 10, 2020

I faced similar problems when developing chibi-link

Now FILE is not defined in wasi-libc and ClangImporter can't import incomplete named type, so FILE is not imported and FILE * is translated as OpaquePointer.

If wasi-libc provides complete FILE structure, FILE type will be imported in Swift. But wasi-libc doesn't use FILE as static sized type, so I think wasi-libc team makes FILE as incomplete type intentionally.

ref: https://github.com/WebAssembly/wasi-libc/blob/378fd4b21aab6d390f3a1c1817d53c422ad00a62/libc-top-half/musl/src/stdio/__fdopen.c#L27

And SEEK_SET can't be imported because it's defined as a function like macro. ClangImporter can't import function like macros

I think we can re-define those constants in WASI.swift.gyb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants