Using raylib with translateC command

Knowing that we would remove @cImport at some point, I had a little trouble trying to import Raylib using std.Build.addTranslateC. It turns out it wasn’t as simple as I thought.

Anyway, here’s the resulting code snippet:

pub fn build(b: *std.Build) void {
    ...

    //get the raylib artifact from dependencies
    const raylib = b.dependency(
        "raylib",
        .{ .target = target, .optimize = optimize },
    ).artifact("raylib");

    //raylib bindings
    const translate_raylib = b.addTranslateC(.{
        .target = target,
        .optimize = optimize,
        .root_source_file = b.addWriteFiles().add("all.h",
            \\#include <raylib.h>
            \\#include <rcamera.h>
            \\#include <raymath.h>
            \\#include <rlgl.h>
        ),
    });

   //Little hack to include the raylib headers
   // another way to do this is just: 
   // translate_raylib.addIncludePath(raylib_dep.path("src"));
   //I do think this is more correct through, as it doesn't
   // depends on the internal folder structure of raylib 
    translate_raylib.include_dirs.append(.{ .other_step = raylib }) 
        catch @panic("OOM");
    translate_raylib.step.dependOn(&raylib.step);

    //Finish by linking to the raylib
    const raylib_mod = translate_raylib.createModule();
    raylib_mod.linkLibrary(raylib);
    
    main_exe.root_module.addImport("raylib", raylib_mod);

    ...

}

Hopefully this makes someone’s work easier, haha!

4 Likes