Skip to content

[build.zig] Refactor - #5764

Merged
raysan5 merged 18 commits into
raysan5:masterfrom
HaxSam:refactor-build-zig
Apr 19, 2026
Merged

[build.zig] Refactor#5764
raysan5 merged 18 commits into
raysan5:masterfrom
HaxSam:refactor-build-zig

Conversation

@HaxSam

@HaxSam HaxSam commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Clean up the build.zig file

The platform implementation was not right and only worked on Windows

  • Enable examples again
  • GLFW Platform
  • RGFW Platform
  • SDL Platform (needs testing win,mac) (can compile for win target)
  • Memory Platform (weird screen flipped behavor)
  • WIN32 Platform
  • DRM Platform
  • Android Platform
  • create small example repo for people to test
  • Add Zig Project example to build C code

Temporarily disabled the tests
@raysan5

raysan5 commented Apr 15, 2026

Copy link
Copy Markdown
Owner

@HaxSam thanks for working on this improvement

EDIT: Maybe @michaelfiber and @Not-Nik (Zig experts) can help with some review/testing

@CrackedPixel

Copy link
Copy Markdown
Contributor

0.15.x worked fine for me but when i tried 0.16.0 it fails:

zig build
build.zig:87:27: error: root source file struct 'mem' has no member named 'trimLeft'
        var flag = std.mem.trimLeft(u8, line, " \t"); // Trim whitespace
                   ~~~~~~~^~~~~~~~~
/home/<REDACTED>/.local/share/zigup/0.16.0/files/lib/std/mem.zig:1:1: note: struct declared here
const std = @import("std.zig");
^~~~~
referenced by:
    compileRaylib: build.zig:144:21
    build: build.zig:488:34
    7 reference(s) hidden; use '-freference-trace=9' to see all references
build.zig:435:113: error: root source file struct 'process' has no member named 'getEnvVarOwned'
            .android_ndk = b.option([]const u8, "android_ndk", "specify path to android ndk") orelse std.process.getEnvVarOwned(b.allocator, "ANDROID_NDK_HOME") catch "",
                                                                                                     ~~~~~~~~~~~^~~~~~~~~~~~~~~
/home/<REDACTED>/.local/share/zigup/0.16.0/files/lib/std/process.zig:1:1: note: struct declared here
const builtin = @import("builtin");

i'll try both on windows and mac next. im using RGFW backend but the error is there for glfw as well

@HaxSam

HaxSam commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

@CrackedPixel great catch, thank you
Will fix it

Edit: It seems like you use an older version of the repo because this line doesn't exist anymore and was changed for 0.16.0, so it can't run.
update the repo by zig fetch --save "git+https://github.com/HaxSam/raylib#refactor-build-zig"

PS. Important: when it can run on 0.15.0 it can't on 0.16.0 and vice versa, there were too many breaking changes

@maiconpintoabreu

maiconpintoabreu commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Hi @HaxSam here is the error that I get when I try to build the lib for Android

maicon@thinkpad02:~/github/personal/raylib$ export ANDROID_NDK_HOME=/home/maicon/Android/Sdk/ndk/28.0.13004108
maicon@thinkpad02:~/github/personal/raylib$ zig build -Dtarget=aarch64-linux-android
install
└─ install raylib
   └─ compile lib raylib Debug aarch64-linux-android failure
error: error: unable to find dynamic system library 'GLX' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'X11' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'Xcursor' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'Xext' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'Xfixes' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'Xi' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'Xinerama' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'Xrandr' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'Xrender' using strategy 'paths_first'. searched paths: none
       error: unable to find dynamic system library 'X11' using strategy 'paths_first'. searched paths: none
       
error: process exited with error code 1
failed command: /home/maicon/.config/VSCodium/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/zig build-lib -lGLX -lX11 -lXcursor -lXext -lXfixes -lXi -lXinerama -lXrandr -lXrender -lX11 -cflags -std=c99 -- /home/maicon/github/personal/raylib/src/rcore.c /home/maicon/github/personal/raylib/src/rshapes.c /home/maicon/github/personal/raylib/src/rtextures.c /home/maicon/github/personal/raylib/src/rtext.c /home/maicon/github/personal/raylib/src/rmodels.c /home/maicon/github/personal/raylib/src/raudio.c /home/maicon/github/personal/raylib/src/rglfw.c -ODebug -target aarch64-linux-android -mcpu baseline -I /home/maicon/github/personal/raylib/src/platforms -I /home/maicon/github/personal/raylib/src/external/glfw/include -D_GNU_SOURCE= -DGL_SILENCE_DEPRECATION=199309L -DSUPPORT_MODULE_RSHAPES=1 -DSUPPORT_MODULE_RTEXTURES=1 -DSUPPORT_MODULE_RTEXT=1 -DSUPPORT_MODULE_RMODELS=1 -DSUPPORT_MODULE_RAUDIO=1 -DPLATFORM_DESKTOP_GLFW= -D_GLFW_X11= -DGRAPHICS_API_OPENGL_33= -Mroot -lc --cache-dir .zig-cache --global-cache-dir /home/maicon/.cache/zig --name raylib -static --zig-lib-dir /home/maicon/.config/VSCodium/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib/ --listen=-

Build Summary: 0/3 steps succeeded (1 failed)
install transitive failure
└─ install raylib transitive failure
   └─ compile lib raylib Debug aarch64-linux-android failure

error: the following build command failed with exit code 1:
.zig-cache/o/4731e80c2be947344c0f6138cac2fb18/build /home/maicon/.config/VSCodium/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/zig /home/maicon/.config/VSCodium/User/globalStorage/ziglang.vscode-zig/zig/x86_64-linux-0.16.0/lib /home/maicon/github/personal/raylib .zig-cache /home/maicon/.cache/zig --seed 0x3d20ae4f -Z44a635df048f1af0 -Dtarget=aarch64-linux-android

Error before the refactor:

maicon@thinkpad02:~/github/personal/raylib$ zig build -Dtarget=aarch64-linux-android
install
└─ install raylib
   └─ compile lib raylib Debug aarch64-linux-android failure
error: error(libc_installation): missing field: include_dir
       error: unable to parse libc paths file at path .zig-cache/o/8aec3a751f602066c78d2081b830bcdd/android-libc.txt: ParseError

@HaxSam

HaxSam commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

Oh yeh good catch

you need to set the platform to .andoid

@maiconpintoabreu

Copy link
Copy Markdown
Contributor

If possible can you fix the generation of android-libc.txt as well from the previous version.

@HaxSam

HaxSam commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

@maiconpintoabreu
what exectly do you mean?

Iam dont really deep into anroid target so I need more details to fix it.

@HaxSam

HaxSam commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

So its done
https://github.com/HaxSam/raylib-platform
just run zig build for creating the binary and zig build run to run it on your target with glfw
to change the platform use -Dplatform=

here all the options

glfw
rgfw
sdl
sdl2
sdl3
memory
win32
drm
android

sdl and sdl2 are not in this example would be nice to see the platform testest on win and mac and maybe drm on a raspberry
Thank you all for helping testing and cleanup the build.zig file

@HaxSam

HaxSam commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

Tomorrow is the weekend.
So after work I will do the last touchups, remove unnecessary linkages (inspired by Makefile),
and add a helper function for people who want to compile C code and need the right linkages for different platforms and targets.

Thanks for all the help and for waiting for the official 6.0.0 release even though it's "just" a third-party build system.
It's really nice not to feel like a third-type citizen even though it would be legitimate.

@maiconpintoabreu

Copy link
Copy Markdown
Contributor

Running in my android device. (Used java classes from raymob to speedup the test)

image

@HaxSam

HaxSam commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

@maiconpintoabreu nice so android is now working with zig + raylib

@raysan5

raysan5 commented Apr 17, 2026

Copy link
Copy Markdown
Owner

@HaxSam @CrackedPixel please, let me know when ready to merge!

@Not-Nik

Not-Nik commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

Big patch, and I didn't have time to go over it thoroughly, but I feel there is a lot of redundant code. I like that we now have clean support for all backends, but the way it written here looks like it's done with brute force.

We have a bunch of linkSystemLibrary/linkFramework calls that I think could be done more cleanly. Maybe we can have an array of link requirements that is set in the big switch and then applied afterwards.

This comes mostly from the similarities between glfw and rgfw. Code readability is already acceptable overall though, so a (hesitant, because first sentence) r+.

@HaxSam

HaxSam commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

Yeh Iam still not 100% happy about the linking toptic but I think I got it reduced a lot.
Link I said I took a lot of inspreation from the Makefile and the linking looked kinda like that rgfw and glfw dont have 100% of the same.

If you have ideas I thankfully take them

@HaxSam

HaxSam commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

@Not-Nik just cleaned it up

@HaxSam

HaxSam commented Apr 17, 2026

Copy link
Copy Markdown
Contributor Author

From my side I feel like I fished everything I wanted

@HaxSam
HaxSam marked this pull request as ready for review April 17, 2026 19:01
@CrackedPixel

CrackedPixel commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

@HaxSam RGFW tested on zig 0.16.0


linux ✔️

the build fails. i removed .lazy = true to get around it
i re-ran and it worked fine. huh (with cache clearing and such, of course)

zig build -Dplatform=rgfw
build.zig:5:33: error: no module named 'zemscripten' available within module 'root.@build'
    const zemscripten = @import("zemscripten");
                                ^~~~~~~~~~~~~
referenced by:
    compileRaylib: build.zig:280:54
    build: build.zig:618:34
    7 reference(s) hidden; use '-freference-trace=9' to see all references

windows ✔️

  • no issues

mac (arm) ✔️

there were 2 quirks:

  • it did not like git+https://github.com/emscripten-core/emsdk#4.0.9 but i suspect this is something with my system and not a flaw? i changed it to .lazy = true to get around it i tried again and it worked, so it must have just been my internet or something
  • it did not like to build the opengl_interop example, i removed the examples to get around it. if possible, this specific example should be skipped on mac

mac (intel) ❓

  • my intel mac is 10.13.6 (high sierra) and im not able to run zig 0.16.0, but i suspect it will be identical to the quirks on arm. chances are anyone using zig on mac has arm, so i dont think we should worry about this

@CrackedPixel

Copy link
Copy Markdown
Contributor

@HaxSam tested win32, the only issue is that "lshcore" should be "shcore". after that change it worked great!

@CrackedPixel

CrackedPixel commented Apr 18, 2026

Copy link
Copy Markdown
Contributor

@HaxSam tested drm. there are 2 issues:

  • it doesnt like the absolute path for this. i removed the line and it compiled perfectly still: raylib_mod.addSystemIncludePath(b.path("/usr/include/libdrm"));
  • it does not like the opengl_interop example (of course). it should probably also be removed from compilation if possible. the rest worked fine

to test on linux you just need to switch to a different TTY (ctrl+alt+f3, etc) and then run it like so:

cd projects/Zig
zig build -Dplatform=drm run

with those 2 changes it worked perfectly

@HaxSam

HaxSam commented Apr 18, 2026

Copy link
Copy Markdown
Contributor Author

@CrackedPixel Thank you for your testing

Comment thread build.zig Outdated
Comment thread build.zig
@CrackedPixel

Copy link
Copy Markdown
Contributor

win32 and drm work perfect in the most recent version. RGFW linux+windows+mac is perfect as well. nice work!

@HaxSam

HaxSam commented Apr 18, 2026

Copy link
Copy Markdown
Contributor Author

I really enjoy zig also for its build system thats why I started with it
I hope people see project/Zig and see that they can also use zig to build there C projects if they wanna

@raysan5

raysan5 commented Apr 18, 2026

Copy link
Copy Markdown
Owner

@HaxSam @CrackedPixel thanks for working on this big improvement! please, let me know when ready!

I feel there is a lot of redundant code. I like that we now have clean support for all backends, but the way it written here looks like it's done with brute force.

@Not-Nik Actually, I prefer more redundant code if it is more simple and every platform is kind-of self-contained. I'm using a similar strategy with Makefile, I prefer to discretize directly per platform, even if there is duplicate code.

@HaxSam

HaxSam commented Apr 18, 2026

Copy link
Copy Markdown
Contributor Author

Iam finished with all the changes and approvements.
If @Not-Nik @maiconpintoabreu @CrackedPixel give they go we can merge it :)

@maiconpintoabreu

Copy link
Copy Markdown
Contributor

LGFM (Android)

@raysan5
raysan5 merged commit a32b53f into raysan5:master Apr 19, 2026
2 checks passed
@raysan5

raysan5 commented Apr 19, 2026

Copy link
Copy Markdown
Owner

@HaxSam @CrackedPixel @Not-Nik I'm running out of time for the new release, so meerging it and hope for the best!

@CrackedPixel

Copy link
Copy Markdown
Contributor

Looks great! Zig people will appreciate it 😎

Sign up for free to 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.

5 participants