Based on What's the most cursed Zig you can write? - #13 by castholm and making it more cursed, so it becomes a “self-extracting” single-file raylib example:
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
if (std.fs.cwd().createFile("build.zig.zon", .{ .exclusive = true })) |file| {
defer file.close();
std.debug.print("extracting build.zig.zon\n", .{});
const hash = "raylib-5.5.0-whq8uGZGzQDi3_L7tJzgEINoZN-HwmOs0zkkc2g7ysIZ";
// zig fmt: off
try file.writeAll(
\\.{
\\ .name = .foo,
\\ .fingerprint = 0x8c73652187ca3158,
\\ .version = "0.0.0",
\\ .minimum_zig_version = "0.14.0",
\\ .dependencies = .{
\\ .raylib = .{
\\ .url = "git+https://github.com/raysan5/raylib#bbeade636cd8c90e18b7e9e841c20b5f8bd15d94",
++ " .hash = \"" ++ hash ++ "\",\n" ++
\\ },
\\ },
\\ .paths = .{
\\ "build.zig",
\\ "build.zig.zon",
\\ },
\\}
);
// zig fmt: on
// trigger download of dependency and rebuild
b.graph.needed_lazy_dependencies.put(b.graph.arena, hash, {}) catch @panic("OOM");
return;
} else |err| {
if (err != error.PathAlreadyExists) return err;
}
const root = b.createModule(.{
.root_source_file = b.path("build.zig"),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "foo",
.root_module = root,
});
exe.linkLibC();
if (b.available_deps.len > 0) {
const raylib_dep = b.dependency("raylib", .{
.target = target,
.optimize = optimize,
.rmodels = false,
});
const files = b.addWriteFiles();
const translate_header = files.add("rayheaders.h",
\\#include <raylib.h>
\\#include <raymath.h>
\\#include <rlgl.h>
);
const raylib_translate = b.addTranslateC(.{
.link_libc = true,
.target = target,
.optimize = optimize,
.root_source_file = translate_header,
});
raylib_translate.addIncludePath(raylib_dep.path("src"));
root.addImport("ray", raylib_translate.createModule());
exe.linkLibrary(raylib_dep.artifact("raylib"));
}
b.installArtifact(exe);
const run_exe = b.addRunArtifact(exe);
run_exe.addArgs(b.args orelse &.{});
run_exe.step.dependOn(b.getInstallStep());
const run = b.step("run", "Run the app");
run.dependOn(&run_exe.step);
}
pub fn main() !void {
const ray = @import("ray");
const width = 800;
const height = 450;
ray.SetConfigFlags(ray.FLAG_MSAA_4X_HINT | ray.FLAG_VSYNC_HINT);
ray.InitWindow(width, height, "zig raylib example");
defer ray.CloseWindow();
while (!ray.WindowShouldClose()) {
ray.BeginDrawing();
defer ray.EndDrawing();
ray.ClearBackground(ray.WHITE);
ray.DrawText("Hello World!", 40, 40, 20, ray.BLACK);
ray.DrawFPS(width - 100, 10);
}
}