ryupold / examples-raylib.zig

Example usage of raylib.zig bindings
MIT License
43 stars 4 forks source link

Fails to build on recent Zig nightlies due to recent Zig build system changes #12

Open metiulekm opened 9 months ago

metiulekm commented 9 months ago

Starting from the 2024-01-04 nightly, zig build run fails with

/tmp/examples-raylib.zig/build.zig:26:19: error: no field or member function named 'getOsTag' in 'Build.ResolvedTarget'
    switch (target.getOsTag()) {
            ~~~~~~^~~~~~~~~
/nix/store/gbai9mdz2rxziv3jaissdj68d7x3mc1g-zig-0.12.0-dev.2036+fc79b22a9/lib/std/Build.zig:2065:28: note: struct declared here
pub const ResolvedTarget = struct {
                           ^~~~~~
referenced by:
    runBuild__anon_8081: /nix/store/gbai9mdz2rxziv3jaissdj68d7x3mc1g-zig-0.12.0-dev.2036+fc79b22a9/lib/std/Build.zig:1851:37
    steps__anon_7895: /nix/store/gbai9mdz2rxziv3jaissdj68d7x3mc1g-zig-0.12.0-dev.2036+fc79b22a9/lib/build_runner.zig:1033:29
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

The issue seems to be related to the commit https://github.com/ziglang/zig/commit/142471fcc46070326526e3976f0150fe734df0b6 which introduced many changes to the build system API. I attempted to fix this issue myself, but I am not very familiar with Zig, and I eventually got stuck on another error:

/tmp/examples-raylib.zig/build.zig:126:56: error: no field named 'modules' in struct 'Build.Step.Compile'
                    .{ .name = "raylib", .module = lib.modules.get("raylib").? },
                                                       ^~~~~~~
/nix/store/gbai9mdz2rxziv3jaissdj68d7x3mc1g-zig-0.12.0-dev.2036+fc79b22a9/lib/std/Build/Step/Compile.zig:1:1: note: struct declared here
const builtin = @import("builtin");
^~~~~
referenced by:
    runBuild__anon_8081: /nix/store/gbai9mdz2rxziv3jaissdj68d7x3mc1g-zig-0.12.0-dev.2036+fc79b22a9/lib/std/Build.zig:1851:37
    steps__anon_7895: /nix/store/gbai9mdz2rxziv3jaissdj68d7x3mc1g-zig-0.12.0-dev.2036+fc79b22a9/lib/build_runner.zig:1033:29
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

Here are the changes I made to get to this state, not sure if helpful or not:

diff --git a/build.zig b/build.zig
index b5d215e..82685cd 100644
--- a/build.zig
+++ b/build.zig
@@ -23,7 +23,7 @@ pub fn build(b: *std.Build) !void {
     const exampleNr = b.option(usize, "example", try exampleDescription(b)) orelse 1;
     try writeExampleFile(exampleNr);

-    switch (target.getOsTag()) {
+    switch (target.result.os.tag) {
         .wasi, .emscripten => {
             const emscriptenSrc = "src/raylib/emscripten/";
             const webCachedir = "zig-cache/web/";
@@ -36,22 +36,22 @@ pub fn build(b: *std.Build) !void {
             }
             const lib = b.addStaticLibrary(.{
                 .name = APP_NAME,
-                .root_source_file = std.build.FileSource.relative("src/web.zig"),
+                .root_source_file = std.Build.LazyPath.relative("src/web.zig"),
                 .optimize = mode,
                 .target = target,
             });
             lib.addIncludePath(.{ .path = raylibSrc });
             lib.addIncludePath(.{ .path = rayguiSrc });

-            const emcc_file = switch (b.host.target.os.tag) {
+            const emcc_file = switch (b.host.result.os.tag) {
                 .windows => "emcc.bat",
                 else => "emcc",
             };
-            const emar_file = switch (b.host.target.os.tag) {
+            const emar_file = switch (b.host.result.os.tag) {
                 .windows => "emar.bat",
                 else => "emar",
             };
-            const emranlib_file = switch (b.host.target.os.tag) {
+            const emranlib_file = switch (b.host.result.os.tag) {
                 .windows => "emranlib.bat",
                 else => "emranlib",
             };
@@ -119,10 +119,10 @@ pub fn build(b: *std.Build) !void {
             lib.addIncludePath(.{ .path = raylibSrc });
             lib.addIncludePath(.{ .path = rayguiSrc });
             lib.addIncludePath(.{ .path = raylibSrc ++ "extras/" });
-            lib.addAnonymousModule("raylib", .{ .source_file = .{ .path = raylibBindingSrc ++ "raylib.zig" } });
-            lib.addAnonymousModule("raygui", .{
-                .source_file = .{ .path = rayguiBindingSrc ++ "raygui.zig" },
-                .dependencies = &.{
+            lib.root_module.addAnonymousImport("raylib", .{ .root_source_file = .{ .path = raylibBindingSrc ++ "raylib.zig" } });
+            lib.root_module.addAnonymousImport("raygui", .{
+                .root_source_file = .{ .path = rayguiBindingSrc ++ "raygui.zig" },
+                .imports = &.{
                     .{ .name = "raylib", .module = lib.modules.get("raylib").? },
                 },
             });
ryupold commented 8 months ago

Thanks for the issue and sorry that I come back to it so late 😅 I updated the bindings so that the zig part now builds correctly.

But I still get an error saying that the raylib.h/raygui.h cannot be found during linking:

PS E:\Dev\examples-raylib.zig> zig build run -Dexample=6
info: building for desktop

info: include 'E:\Dev\examples-raylib.zig\src\raylib\raylib\src' to raylib-zig-examples
info: include 'E:\Dev\examples-raylib.zig\src\raylib' to raylib-zig-examples
info: include 'E:\Dev\examples-raylib.zig\src\raylib\raylib\src' to raylib.zig
info: include 'E:\Dev\examples-raylib.zig\src\raylib' to raylib.zig
info: linked raylib.zig
steps [2/8] zig build-lib raylib Debug native... Compile C Objectssteps [2/8] zig build-lib raylib Debug native... Compile C Objectssteps [2/8] zig build-lib raylib Debug native... Compile C Objectssteps [2/8] zig build-lib raylib Debug native... Compile C Objectssteps [2/8] zig build-lib raylib Debug native... Compile C Objectssteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... Compsteps [4/8] zig build-exe raylib-zig-examples Debug native... LLVMsteps [4/8] zig build-exe raylib-zig-examples Debug native... LLVMsteps [4/8] zig build-exe raylib-zig-examples Debug native... LLVMsteps [4/8] zig build-exe raylib-zig-examples Debug native... LLVMrun
└─ run raylib-zig-examples
   └─ install
      └─ install raylib-zig-examples
         └─ zig build-exe raylib-zig-examples Debug native 4 errors
src\raylib\raylib.zig:2:16: error: C import failed
const raylib = @cImport({
               ^~~~~~~~
referenced by:
    mGenImageFontAtlas: src\raylib\raylib.zig:760:14
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
E:\Dev\examples-raylib.zig\zig-cache\o\840a203e749f29650bece1ca7607b2b0\cimport.h:1:10: error: 'raylib.h' file not found
#include <raylib.h>
         ^
src\raygui\raygui.zig:2:16: error: C import failed
const raygui = @cImport({
               ^~~~~~~~
E:\Dev\examples-raylib.zig\zig-cache\o\f122cb2c4097608b3aa6ae058253698f\cimport.h:1:10: error: 'raygui.h' file not found
#include <raygui.h>
         ^
error: the following command failed with 4 compilation errors:    
C:\Applications\zig\zig.exe build-exe E:\Dev\examples-raylib.zig\zig-cache\o\e3b1930347e1f8b7e5cb98ae04857837\raylib.lib E:\Dev\examples-raylib.zig\zig-cache\o\99a423f9c21caa39381fe841fa7669ae\raylib.zig.lib -cflags -DRAYGUI_IMPLEMENTATION -- E:\Dev\examples-raylib.zig\src\raygui\raygui_marshal.c -ODebug -I E:\Dev\examples-raylib.zig\src\raylib\raylib\src -I E:\Dev\examples-raylib.zig\src\raylib -I E:\Dev\examples-raylib.zig\src\raygui\raygui\src -I E:\Dev\examples-raylib.zig\src\raygui --dep raylib --dep raygui -Mroot=E:\Dev\examples-raylib.zig\src\desktop.zig -Mraylib=E:\Dev\examples-raylib.zig\src\raylib\raylib.zig --dep raylib -Mraygui=E:\Dev\examples-raylib.zig\src\raygui\raygui.zig -lwinmm -lgdi32 -lopengl32 -lc --cache-dir E:\Dev\examples-raylib.zig\zig-cache --global-cache-dir C:\Users\ryupo\AppData\Local\zig --name raylib-zig-examples --listen=-
Build Summary: 3/8 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run raylib-zig-examples transitive failure
   ├─ zig build-exe raylib-zig-examples Debug native 4 errors     
   └─ install transitive failure
      └─ install raylib-zig-examples transitive failure
         └─ zig build-exe raylib-zig-examples Debug native (+2 more reused dependencies)
error: the following build command failed with exit code 1:       
E:\Dev\examples-raylib.zig\zig-cache\o\65e301018f3553ae425cd689955e8c1a\build.exe C:\Applications\zig\zig.exe E:\Dev\examples-raylib.zig E:\Dev\examples-raylib.zig\zig-cache C:\Users\ryupo\AppData\Local\zig --seed 0xb66a5341 run -Dexample=6

I don"t get why this error occurs as the include folders are added correctly in build.zig