Skip to content
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

[Zig] Zig build fails to compile #3895

Closed
GustavoJCL opened this issue Apr 3, 2024 · 1 comment · Fixed by #3896
Closed

[Zig] Zig build fails to compile #3895

GustavoJCL opened this issue Apr 3, 2024 · 1 comment · Fixed by #3896

Comments

@GustavoJCL
Copy link

Please, before submitting a new issue verify and check:

  • [x ] I tested it on latest raylib version from master branch
  • [ x] I checked there is no similar issue already reported
  • [ x] I checked the documentation on the wiki
  • [ x] My code has no errors or misuse of raylib

Issue description

I imported raylib in a zig project, following the guide of this tutorial of the reddit of raylib: https://www.reddit.com/r/raylib/comments/17y0gdx/get_raylib_up_and_running_in_just_4_minutes_using/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

Environment

Provide your Platform, Operating System, OpenGL version, GPU details where you experienced the issue.
Operating System: Arch Linux
OpenGL: Intel TigerLake-LP GT2 [Iris Xe Graphics]

Issue Screenshot

image

Code Example

clone raylib
execute:
zig init-exe

edit build.zig:

const std = @import("std");
const raylibSDK = @import("./raylib/src/build.zig");

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
    // Standard target options allows the person running `zig build` to choose
    // what target to build for. Here we do not override the defaults, which
    // means any target is allowed, and the default is native. Other options
    // for restricting supported target set are available.
    const target = b.standardTargetOptions(.{});

    // Standard optimization options allow the person running `zig build` to select
    // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
    // set a preferred release mode, allowing the user to decide how to optimize.
    const optimize = b.standardOptimizeOption(.{});

    const raylib = raylibSDK.addRaylib(b, target, optimize, .{});

    const exe = b.addExecutable(.{
        .name = "wolfenstein_3d_zig",
        // In this case the main source file is merely a path, however, in more
        // complicated build scripts, this could be a generated file.
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    exe.addIncludePath(.{ .path = "./raylib/src/" });
    exe.linkLibrary(raylib);
    // This declares intent for the executable to be installed into the
    // standard location when the user invokes the "install" step (the default
    // step when running `zig build`).
    b.installArtifact(exe);

    // This *creates* a Run step in the build graph, to be executed when another
    // step is evaluated that depends on it. The next line below will establish
    // such a dependency.
    const run_cmd = b.addRunArtifact(exe);

    // By making the run step depend on the install step, it will be run from the
    // installation directory rather than directly from within the cache directory.
    // This is not necessary, however, if the application depends on other installed
    // files, this ensures they will be present and in the expected location.
    run_cmd.step.dependOn(b.getInstallStep());

    // This allows the user to pass arguments to the application in the build
    // command itself, like this: `zig build run -- arg1 arg2 etc`
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    // This creates a build step. It will be visible in the `zig build --help` menu,
    // and can be selected like this: `zig build run`
    // This will evaluate the `run` step rather than the default, which is "install".
    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);

    // Creates a step for unit testing. This only builds the test executable
    // but does not run it.
    const unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const run_unit_tests = b.addRunArtifact(unit_tests);

    // Similar to creating the run step earlier, this exposes a `test` step to
    // the `zig build --help` menu, providing a way for the user to request
    // running the unit tests.
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_unit_tests.step);
}

build the zig code:

zig build run
/media/gus/proyectos/wolfenstein_3d_zig/build.zig:31:21: error: expected type '*Build.Step.C
ompile', found '@typeInfo(@typeInfo(@TypeOf(raylib.src.build.addRaylib__anon_13349)).Fn.retu
rn_type.?).ErrorUnion.error_set!*Build.Step.Compile'
    exe.linkLibrary(raylib);
                    ^~~~~~
/media/gus/proyectos/wolfenstein_3d_zig/build.zig:31:21: note: cannot convert error union to
 payload type
/media/gus/proyectos/wolfenstein_3d_zig/build.zig:31:21: note: consider using 'try', 'catch'
, or 'if'
/usr/lib/zig/std/Build/Step/Compile.zig:672:41: note: parameter type declared here
pub fn linkLibrary(self: *Compile, lib: *Compile) void {
                                        ^~~~~~~~
referenced by:
    runBuild__anon_7136: /usr/lib/zig/std/Build.zig:1638:27
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
/media/gus/proyectos/wolfenstein_3d_zig/raylib/src/build.zig:309:30: error: struct 'child_pr
ocess.ChildProcess' has no member named 'run'
    _ = try std.process.Child.run(.{
            ~~~~~~~~~~~~~~~~~^~~~
/usr/lib/zig/std/child_process.zig:20:26: note: struct declared here
pub const ChildProcess = struct {
                         ^~~~~~

@iarkn
Copy link
Contributor

iarkn commented Apr 4, 2024

#3891 introduced a change which breaks building Raylib with Zig version 0.11.0, due to the use of std.process.Child.run (which was renamed from std.process.Child.exec in 0.11.0, see this commit from the Zig repository). An easy fix would be adding a version check and using the correct function name in the waylandGenerate function in src/build.zig.

You can use the master version of Zig instead to temporarily fix this.

For the other error, you probably forgot to add a try before calling raylibSDK.addRaylib():

- const raylib = raylibSDK.addRaylib(b, target, optimize, .{});
+ const raylib = try raylibSDK.addRaylib(b, target, optimize, .{});

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 a pull request may close this issue.

2 participants