#Is it possible to cross-compile graphical applications using Sokol?

1 messages · Page 1 of 1 (latest)

flat wharf
#

I'm very interested in Zigs cross-compilation abilities and especially Zig CC; while it seems to work pretty well with CLI applications, I haven't figured out how to cross-compile programs using sokol_app.h (https://github.com/floooh/sokol-zig) yet. It looks like I need to at least add the GL headers manually, but I'm not even sure if that's enough. Am I doing anything wrong, or is this currently just not possible?
This is my first time seriously using Zig, so please don't be too harsh if I act stupid 😄

GitHub

Zig bindings for the sokol headers (https://github.com/floooh/sokol) - GitHub - floooh/sokol-zig: Zig bindings for the sokol headers (https://github.com/floooh/sokol)

brave badge
#

Maybe @mental igloo can help with this one :)

mental igloo
#

@flat wharf check out the pacman.zig project (the main branch is still on zig 0.10 though). The readme contains examples for cross-compiling to iOS and Emscripten (although those are the complex cases)

On a Mac, I'm able to create a Windows exe with zig build -Dtarget=x86_64-windows-gnu (although I didn't check if it actually runs)

#

there are two important pieces, first the buildSokol function in build.zig:

https://github.com/floooh/pacman.zig/blob/364e1d28f304a50fb0bd1dcf5c8305b9ab972e5e/build.zig#L126-L170

...and then the sokol.c file which includes and compiles the C header implementations with platform-specific configuration macros:

https://github.com/floooh/pacman.zig/blob/main/src/sokol/sokol.c

GitHub

Simple Pacman clone written in Zig. Contribute to floooh/pacman.zig development by creating an account on GitHub.

GitHub

Simple Pacman clone written in Zig. Contribute to floooh/pacman.zig development by creating an account on GitHub.

#

(in this case the 3D backends are hardwired to D3D11 on Windows, Metal on macOS/iOS and GL/GLES otherwise => one caveat: GLES2 support has been removed recently from the sokol headers, but I haven't updated pacman.zig accordingly yet, so SOKOL_GLES2 would need to be replaced with SOKOL_GLES3 for Emscripten)

#

PS: one thing to keep in mind for cross-compilation is that the sokol headers depend on non-POSIX system headers (like d3d11.h, emscripten.h, X11/Xlib.h or Metal.h) which must be included in the Zig distribution for the cross-compilation to "just work". For Windows that's definitely the case, but for Emscripten a separate Emscripten SDK install is needed to provide the necessary headers.

For macOS and Linux I'm actually not sure. Maybe those need similar special case handling like Emscripten, or maybe the required headers are also included with Zig (I didn't try yet to cross-compile into that direction)

flat wharf
#

Thanks for the info & links! It was nice playing a round of good old pacman again 😄

one thing to keep in mind for cross-compilation is that the sokol headers depend on non-POSIX system headers (like d3d11.h, emscripten.h, X11/Xlib.h or Metal.h) which must be included in the Zig distribution for the cross-compilation to "just work".
I think that's exactly the issue I'm facing. Zigs abilitiy to cross compile even to e.g. macOS with very few restrictions is really fascinating, but it looks like it currently doesn't work with graphical apps, since the headers aren't included in the Zig distribution.

For macOS and Linux I'm actually not sure. Maybe those need similar special case handling like Emscripten, or maybe the required headers are also included with Zig (I didn't try yet to cross-compile into that direction)
From what I have tried it seems that the headers aren't included, as already mentioned above. For Linux it's basically just KHR, GL and X11, but macOS requires dozens of headers and every time I add one it seems to become more and more...

#

Maybe I should just open an issue and suggest adding more headers, but I have no idea if that's pratical or not.

#

btw., just FYI, the build.zig of the 0.11 branch seems to be outdated

mental igloo
#

btw., just FYI, the build.zig of the 0.11 branch seems to be outdated
Yeah, I try to keep the 0.11 branch in sokol-zig somewhat uptodate, but the other 'demo projects' are definitely behind

#

my hope is that once the package-manager-stuff is sorted, that cross-compilation "sysroots" will move from the core distribution into packages, and that we can have more complete cross-compilation-packages (like Metal and Cocoa headers for macOS and X11/GL headers for Linux), so that cross-compiling graphical applications "just works"

#

The included Windows headers seem to be a bit special in that those are the mingw2 headers(?), which are complete enough to write "multimedia-applications" and not just basic POSIX stuff

#

...the long-term goal for sokol-zig is definitely to move this into a package that "just works" also for cross-compiling

#

one thing I keep thinking about is to move the required system header declarations directly into the sokol headers, I experimented with this idea in sokol_audio.h a bit for the macOS/iOS stuff, and generally it works, but doing this manually is too brittle for big headers like Cocoa, Metal or D3D11

flat wharf
#

Hm, interesting approach, but it sounds like quite a lot to maintain for libraries with frequent updates

mental igloo
#

If I ever go this way I would write a header extraction tool via libclang where I provide it a list of declarations I want to extract, and I get a header snippet back with only those declarations.

I've been tinkering in this area for quite a while already (but with clang's ast-dump feature), this is how the various sokol header bindings are generated (by parsing the C header declarations into a very verbose AST, and then trimming this down to the information that's actually needed for the language bindings, e.g. struct names and layouts, and function signatures).

But for "system headers", and especially Objective-C headers, this gets tricky very quickly, but it can be done.