Repository navigation
Description
(Background: This reddit post where @andrewrk suggested I file this issue.)
I think it would be useful for Zig to allow compiling the same code both natively and to the Web.
Zig already supports the Web to some extent with the wasi and freestanding wasm targets. The difference with Emscripten is in API support: Emscripten allows common native APIs to be compiled to the Web, such as SDL2, OpenGL, OpenAL, pthreads, and so forth. A very common use case for Emscripten is to port a game engine, and that's why Unity and many others use it. As game engines begin to be written in Zig, allowing the same there as for C++ would be nice I think. (Of course, this isn't limited to games: Photoshop, Figma, AutoCAD, etc. also use Emscripten.)
What this means concretely: this gist has an example of a C program that uses GLES3 and GLFW to render, and relies on Asyncify to handle the Web event loop. That exact same program can be compiled with gcc or clang natively, or with emcc to the Web - that's the big benefit here.
The gist also has an example of a Zig program that I managed to do the same with, with some hacks. With small (I hope) improvements to Zig I think the hacks could be avoided, namely to allow setting the Emscripten target triple, and to get the full Zig stdlib compiling with that triple (hopefully simple, as the differences between the Emscripten wasm triple and the WASI wasm triple are very small). Emscripten itself would do all the rest, when the user runs it on the object files emitted from Zig (see details in that gist; there may also be more interesting Zig build system integration opportunities, but I don't know enough about that).
Why is it useful to compile the exact same code both natively and to the Web? You don't have to, of course - you can have separate codebases for the two platforms. For example, you could add a new rendering backend to your game engine to use WebGL, you can rewrite all your event handling logic to be async like the Web requires, rethink how you handle files, etc., and all the other stuff that is different on the Web. Such rewrites may have benefits, sometimes. But it makes porting harder, it makes debugging harder, and you may end up handling the mismatches between native and the Web in a sub-optimal way. Reusing the large effort put into Emscripten here can be useful.
Activity
kubkon commentedon Feb 10, 2022
I'm happy to look into this as I've got a running example of a cross between C++ that requires both threading and exceptions, and pure Zig code. Ideal for testing the emscripten target triple.
KioriSun commentedon Feb 17, 2022
Hey @kripken thanks for taking the time to help us!
Tried the gist and didn't work, both c and zig examples.
They run, but the screen stays black.
Interesting non-essential observations:
The initial obj gen by zig is around 5 times larger than the clang one, yet the final wasm is only around 5 kb larger.
The js and html emitted are seemingly the same size.
When editing the zig version ZLS(I think) complains about the "//" at the beginning of the shader string.
I will try again later on with my own code, just in case.
I was already going to test gles2 anyways, for webgl 1 support, which I think is more widely available.
Let me know if there is anything you'd like me to test.
I have latest emcc and zig.
kripken commentedon Feb 17, 2022
@KioriSun Oh, sorry, looks like I forgot to write that you need to manually add
_run_zig()in the HTML, or do that in the dev console. That is, the zig code can't use the automatic startup code because that isn't integrated between zig and emcc. Does that fix things for you? If not, is there an error in the dev console?KioriSun commentedon Feb 17, 2022
Thanks it works. I was wondering about the
-s STANDALONE_WASM -s EXPORTED_FUNCTIONS=_run_zig --no-entrypart of the code and why it was necessary, how it would be used.I'm not an expert on zig internals, but hopefully that integration can be reached.
A hint for everyone else that might give this a shot:
Another thing that I did that is important is i didn't just run the files but served them from a file server.
(this prevents cors issues and all, that modern wasm requires)
I couldn't get the _run_zig() automated, tried adding it to all sorts of onload functions, didn't work. only running from the console so far. Now that it's at least running form the console, I wanna try other apis. But I assume they'll run fine.
About automating the run function, for the time being, couldn't a properly placed EM_JS call solve that?
I'm not sure when/where the run function exists, as stated I tried calling it in other contexts and it either didn't exist or was undefined.
kripken commentedon Feb 17, 2022
Yes, exactly - that's one of the things that I think could be worked on, as part of this topic.
Hmm, no, because EM_JS happens after startup.
But a generic solution to this could be to add this to the HTML:
That uses one of the emscripten JS startup hooks. But this is kind of a hack that we should fix with proper integration in the Zig and emcc startup code, as mentioned before.
KioriSun commentedon Feb 17, 2022
This is exactly what i was looking for in terms of lifetimes. But it didn't work for me. Maybe I'm missing something. I tried adding it into a script tag, body onload, and adding it directly into the zig.js that emcc builds. Nothing seems to work only running from the console.
If EM_JS happens after "startup", it also happens before or after "onRuntimeInitialized"? Because this is what I thought might work as a in code hack because if the objs are already there, maybe the function can be added, maybe it can't.
floooh commentedon Feb 18, 2022
I can 'contribute' a little real-world example here for testing:
https://github.com/floooh/pacman.zig
WASM version here:
https://floooh.github.io/pacman.zig/pacman.html
Build instructions:
https://github.com/floooh/pacman.zig#experimental-web-support
The project is a mix of cross-platform C headers which makes use of Emscripten APIs and EM_JS() feature, and platform-agnostic Zig code for the gameplay stuff.
To make the build work I had to compile the Zig parts with
wasm32-freestandinginstead ofwasm32-emscripten, otherwise I would get build errors in Zig standard library. Here's the "hack":https://github.com/floooh/pacman.zig/blob/549a73ecd6f5c9bfe4f8150b08e4b43f02eae331/build.zig#L88-L91
To reproduce the problem just comment out this line:
https://github.com/floooh/pacman.zig/blob/549a73ecd6f5c9bfe4f8150b08e4b43f02eae331/build.zig#L89
...trying to build then produces this error:
Cheers!
floooh commentedon Feb 18, 2022
PS: as I learned from this thread (https://groups.google.com/g/emscripten-discuss/c/fWPEAulgslM), building the C code with
wasm32-emscriptenis required to make the "EM_JS() magic" work. The Javascript source string is placed into a global variable which gets some special treatment by Clang only if the wasm32-emscripten triple is set. The string is then extracted from the object files by the Emscripten linker and placed into the Javascript file that's produced along with the .wasm and .html output.I think the biggest motivation to better support wasm32-emscripten in Zig is that this could pave the road for an EM_JS()-like feature directly in Zig :)
...ideally at some later point it would be great if such special Emscripten linker features could be somehow integrated into the Zig toolchain, or if this is 'out of scope" maybe a better way to integrate a slim EMSDK (just the linker, sysroot and JS shims) with a Zig build.
kripken commentedon Feb 18, 2022
@KioriSun
It's possible that adding that hook in the wrong place will not work. Here is what works for me locally, this is near the end of the HTML file:
}; }; Module.onRuntimeInitialized = function() { _run_zig() }; // this is the new line </script> <script async type="text/javascript" src="a.js"></script>KioriSun commentedon Feb 18, 2022
Thanks a lot! I found the issue. All of the above works if you do
; Module.onRuntimeInitialized = function() { _run_zig() };.Mind the ;. It seems that at the end of the js script in the html, emscripten doesn't emit a last, trailing, ";", and that's why it wasn't working before.
RReverser commentedon Feb 18, 2022
That's odd, it really shouldn't matter.
Luukdegram commentedon Feb 18, 2022
The next steps for this (if this were to be accepted), would be to provide proper std support for the wasm32-emscripten target triple, similar to how we provide support for WASI. This also means that the startup code of Zig must be made aware of Emscripten, so functions such as
_run_zig()are no longer necessary. The Emscripten target could then also possibly become tier 2 supported. I think something like a slim EMSDK serves better as a third-party library that utilizes Zig's extensive build system so users can easily integrate with it.JosiasAurel commentedon Oct 24, 2022
I am using RayLib with Zig and I can compile my program with
zig build-exe main.zig -lc -lraylibbut the web build fails to run properly.Here is the code for
main.zigBuilding object file from zig
zig build-obj -isystem ~/.mybin/emsdk/upstream/emscripten/cache/sysroot/include src/main.zig -I. -L. -target wasm32-wasiFinish building with emscripten
I get an
unreachable code executedon the web build.Here is the
minshell.htmlfrom official raylib source but I addedModule.onRuntimeInitialized = funciton() { _main(); }within script tags at the end of the file.references:
floooh commentedon Aug 5, 2023
(deleted my previous comment because it was full of false information, see end of the comment for current workarounds)
Current state in pacman.zig with the final zig-0.11.0 version is:
Trying to build Zig code with
wasm32-emscripteninto a library still has this stdlib error:Trying to build Zig code which has a
pub fn main()function withwasm32-emscriptennow throws an unsupported arch error in start.zig (which is probably kind of expected, because it's not a WASI target)What I'm doing now in pacman.zig for the 'web browser target':
wasm32-freestandingto avoid the above errorswasm32-emscripten(required for the Emscripten EM_JS magic to work)emccin a build step to build a C file with Emscripten compatible entry point, and link the above to libraries, this then results in the usual .html+.js+.wasm file tripletPS: for reference: https://github.com/floooh/pacman.zig/blob/a2ce623da2a564beea5aea4512c48e9e1d260fc7/build.zig#L48-L134
permutationlock commentedon Sep 2, 2023
I was able to get emscripten builds working for my own projects with guidance from the gist linked above and the build files from projects such as raylib-zig. From there, I ended up testing out some additions to the standard library that would add basic support for the
wasm32-emscriptentarget, see this commit in my fork.I copied the supported portions of
os/linux.zigandc/linux.zigto their emscripten counterparts, modifying as necessary to match the emscripten C headers. I also added definitions for most of the API defined inemscripten.h(emscripten has a ton of other functionality, but not all of it is easy to translate, e.g. the EM_JS macros). Finally, I added a comptime block to export emscripten compatible versions of the__stack_chksymbols that are required when compiling in safe optimization modes (I don't know if this is the best solution, but it satisfied the linker and correctly reported when I tested smashing the stack).To build a native project for the web you should just need to use
zig build-obj -target wasm32-emscripten -lcand then link withemcc(or do the equivalent using the zig build system). When linking zig objects built with a safe optimization mode,emccrequires the flag-sUSE_OFFSET_CONVERTERor else runtime errors will occur related to__builtin_return_address.andrewrk commentedon Sep 22, 2023
It's been possible to target emscripten as the "OS" of zig for a while now. Any improvements to the standard library to support this OS are welcome.
floooh commentedon Jan 11, 2024
Btw, confirmed that this is working now. Nice :)
silbinarywolf commentedon Feb 19, 2024
Not sure where I should note my Emscripten findings but I'm gonna dump them here for now so others can discover them.
This is for Zig version:
0.12.0-dev.2701+d18f52197main.zigto force use of the C allocator to avoid what seems to be memory collisions when using C libraries like SDL. My guess is because you're not allocating with Emscriptens malloc, you end up stomping over things allocated with C libraries.linkWithEmscriptensettings to help with debugging problems and fix memory errors that mostly just give an "unreachable" error in console when using Zig. Based off of: https://github.com/Not-Nik/raylib-zig/blob/183347adaf8fb6dddf25ece3c8a157c8bab212c5/build.zig#L322compileForEmscriptento include.strip = false, this is needed so Emscripten won't hit DWARF file opcode errors: https://github.com/Not-Nik/raylib-zig/blob/183347adaf8fb6dddf25ece3c8a157c8bab212c5/build.zig#L264NOTE: If you're wondering why I did this, it's because even if you do the following when mapping your embed file, to be able to see files in emscripten/web builds, you'd need to prefix each file open with
/, ie.std.fs.cwd().openFile("/my_file.png", .{ .mode = .read_only });which doesn't work on both native desktop and Emscripten.