-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
implement libc in zig #514
Comments
On windows, from what part of libc should people start? |
Can you elaborate on the question? |
Do you need all libc, or only some parts of it? Which parts are more important right now? |
The goal is to provide API compatibility for existing C projects. We can let that drive the use case. So to figure out what is more important, we would try to, for example, build ffmpeg against a zig-libc. |
I'd start with libjpeg, but I need exceptions for that. Or at least setjmp/longjmp. |
You can implement setjmp/longjmp with inline assembly. |
For anyone interested in this, I've started a simple repository which should give you some ideas here. Anyone reading, feel free to use this as a base or as inspiration. I won't be doing much implementation here (too many things I already have to work on) but I may use this to figure out some zig -> c questions and some missing build system details needed in zig to accomodate the required goals. |
@andrewrk would you mind making a |
@suirad sorry, I missed your comment above. The libc code is going to live in this repository itself; contributions can be done via pull requests. I'll get the effort started so that people can see the pattern. Now that we have #490 done, this issue of having libc is even more valuable, and easier to implement. How it's going to work is that Zig will have a set of supported libcs corresponding to a target.
Once this is done, it raises the bar for Tier 1. A target can only be Tier 1 if Zig can cross-compile link against its libc. Zig will no longer look for the native libc and try to build against it. |
I've done a proof of concept of this with glibc and it's merged into master. (The CI build is broken but I have a fix coming shortly) There's a new tool Here are some screenshots on Windows, Linux, macOS: So at this point it's a checklist:
|
There probably be a new The reason why it should be exposed and not assumed based on OS, is because some platforms are able to use multiple types, such as linux with musl & glibc. |
That's |
Repeating this here: Another nice optimization/research area would be to not have a separate .a file for libc, but actually export some additional functions from the same object file as the root zig source file. In this way the libc and the zig code could share implementations and shave off bloat. This would probably be helped a lot by an incremental compiler. |
For Windows, someone pointed me to MidiPix which could be another source of inspiration. |
* introduce wasm32-freestanding-musl .h files to fix conflicts with stddef.h and errno.h * fix an issue with zig build system regarding installation of webassembly libraries * add implementations to zig's libc: - strcmp - strncmp - strerror - strlen See #514
* introduce wasm32-freestanding-musl .h files to fix conflicts with stddef.h and errno.h * fix an issue with zig build system regarding installation of webassembly libraries * add implementations to zig's libc: - strcmp - strncmp - strerror - strlen See #514
#2868 has been merged and now we have a libc for Windows. I think the proof-of-concept is done and now this issue can be split into separate issues:
Future work will be consolidating common functionality, e.g. the math functions, using one canonical implementation. See #2879 for that. |
@andrewrk Hi, I am building a small programming language, I can see that zig can compile libc, I've found the sources for libc's that zig compiles in the zig-bootstrap repository, Can you tell me which command is used, how are these libc's compiled, I am trying to implement cross compilation in my llvm programming language https://github.com/Qinetik/chemical The problem is that when I use lld to link a single object file that I created from llvm::Module, it gives error for unresolved printf symbols, Now I need to link against standard library using -lc but that also gives error that library not found, In this case this is a problem because I don't want my compiler to fail because lld can't find standard lib, the right way probably is to just compile libc from sources and link against it |
If Zig ships with a libc implementation, it seals the deal for streamlined cross platform compilation. It improves the situation for native compilation as well. When a zig project depends on a C library such as GLFW, one could package it up with zig build system to build with zig (See #490). This accomplishes several things:
zig build
from the root project on any platform will fetch the project's dependencies and perform a deterministic build using only zig and no system dependencies.Our goal is for developers to reach for zig instead of gcc, clang, or msvc, even if they intend to use it only to compile C code.
For linux, we can port musl. A lot of our stdlib is ported from there anyway - a lot of this code would be shared between zig standard library and the libc implementation. We should also look at glibc, for example for the
getpwnam
function, which in musl reads/etc/passwd
but in glibc has an extension for a more advanced user account system.For windows it might be worth looking into newlib-cygwin, reactos libc, and wine libc.
For macos, we always link dynamically against libSystem which has libc in it because this is the stable syscall interface. When cross compiling we allow functions to be unresolved that come from libc. We still need to ship .h files with appropriate constant values, however. I think the .h files from apple might be BSD licensed and thus we could redistribute those.
The text was updated successfully, but these errors were encountered: