Not-Nik / raylib-zig

Manually tweaked, auto-generated raylib bindings for zig. https://github.com/raysan5/raylib
MIT License
459 stars 101 forks source link

Streamlining `build.zig` for easier integration with Zig's Package Manager #72

Closed Ev1lT3rm1nal closed 3 months ago

Ev1lT3rm1nal commented 3 months ago

To my knowledge, using this library requires you to clone it. However, I suspect there's a more efficient method to incorporate it as a dependency in build.zig.zon.

I implemented the following adjustments to make it work:

In build.zig.zon

.dependencies = .{
        .@"raylib-zig" = .{
            .url = "https://github.com/Not-Nik/raylib-zig/archive/f8735a8cc79db221d3c651c93778cfdb5066818d.tar.gz",
            .hash = "1220ec9abcca33f803b667c901c8d6e9686e2206aabb1ed6712926e457c7a61e1f60",
        },
        .raylib = .{
            .url = "https://github.com/raysan5/raylib/archive/5.0.tar.gz",
            .hash = "1220c28847ca8e8756734ae84355802b764c9d9cf4de057dbc6fc2b15c56e726f27b",
        },
    },

raylib-zig's build.zig

// Get the directory of the lib
fn getPath() []const u8 {
    return std.fs.path.dirname(@src().file).?;
}

pub fn getModule(b: *std.Build, comptime rl_path: []const u8) *std.Build.Module {
   // ...
}

// Now I can just 'const raylib = rl.getModuleNoPath(b);'
pub fn getModuleNoPath(b: *std.Build) *std.Build.Module {
    if (b.modules.contains("raylib")) {
        return b.modules.get("raylib").?;
    }
    return b.addModule("raylib", .{ .source_file = .{ .path = comptime getPath() ++ "/lib/raylib-zig.zig" } });
}

Similarly for math:

pub fn getModuleNoPath(b: *std.Build) *std.Build.Module {
        const raylib = rl.getModuleNoPath(b);
        return b.addModule("raylib-math", .{
            .source_file = .{ .path = comptime getPath() ++ "/lib/raylib-zig-math.zig" },
            .dependencies = &.{.{ .name = "raylib-zig", .module = raylib }},
        });
    }

However, I've encountered an issue with getRaylib() as it requires adding the original raylib dependency, which necessitates a workaround to resolve.

Also, it’s better to use path.join rather than just sticking paths together with concatenation.

Ev1lT3rm1nal commented 3 months ago

@Not-Nik What do you think?

stefanpartheym commented 3 months ago

Hi @Ev1lT3rm1nal,

wouldn't this already be enough?:

snippet from a build.zig using raylib-zig:

    // ...
    const exe = b.addExecutable(.{
        .name = "my-executable-name",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const raylib_dep = b.dependency("raylib-zig", .{
        .target = target,
        .optimize = optimize,
    });

    exe.root_module.addImport("raylib", raylib_dep.module("raylib"));
    exe.linkLibrary(raylib_dep.artifact("raylib"));
    // ...

snippet from a build.zig.zon using raylib-zig:

//...
    .dependencies = .{
        .@"raylib-zig" = .{
            .url = "https://github.com/stefanpartheym/raylib-zig/archive/0c94e9f48626ad3cae3f1bd2dea218d334a1839f.tar.gz",
            .hash = "12207d73b118b050394da32101996d615e2385380b99843c942efcd8e64222dc3e23",
        },
    },
// ...

Note: I'm using my own fork of raylib-zig for compatibility with zig 0.12.0-dev. But in your case, you could probably already use the main repo, if you're on an older zig version.

Best regards Stefan

Not-Nik commented 3 months ago

@stefanpartheym You are absolutely right, this is something I should have done ages ago, when package manager support was first added. The build code looks slightly different on 0.11.0 (which is the Zig version this project currently mainly targets), but it works all the same. I have now updated the project_setup.sh script to reflect this. The emscripten build still uses part of the binding build script, but I have isolated that code into its own file, so it can easily be copied. Also the project generation script will always be behind the main repository by a few commits, but everything should still work. Too bad you can't leave out the hash in a dependency.