Allow building raylib directly into zig binaries
Instead of returning a compile step for a static library from `addRaylib`, we make it add the required C source files and their compilation flags onto an existing compile step. This lets us add raylib directly to any zig project by just calling `addRaylib(b, exe)` on our zig compilations within other build files. I've also defined the `-DPLATFORM_DESKTOP` flag only for the C compilation units directly related to raylib, so if we're compiling into another zig project that includes other C source files, they don't see any raylib-related flags.
This commit is contained in:
parent
5e1a81555c
commit
24bed7162f
138
src/build.zig
138
src/build.zig
|
|
@ -1,7 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
// This has been tested to work with zig master branch as of commit 87de821 or May 14 2023
|
||||
pub fn addRaylib(b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, options: Options) *std.Build.CompileStep {
|
||||
pub fn addRaylib(b: *std.Build, compile: *std.Build.Step.Compile , options: Options) void {
|
||||
const raylib_flags = &[_][]const u8{
|
||||
"-std=gnu99",
|
||||
"-D_GNU_SOURCE",
|
||||
|
|
@ -9,16 +9,11 @@ pub fn addRaylib(b: *std.Build, target: std.zig.CrossTarget, optimize: std.built
|
|||
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/1891
|
||||
};
|
||||
|
||||
const raylib = b.addStaticLibrary(.{
|
||||
.name = "raylib",
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
raylib.linkLibC();
|
||||
compile.linkLibC();
|
||||
|
||||
raylib.addIncludePath(srcdir ++ "/external/glfw/include");
|
||||
compile.addIncludePath(srcdir ++ "/external/glfw/include");
|
||||
|
||||
raylib.addCSourceFiles(&.{
|
||||
const c_source = &[_][]const u8{
|
||||
srcdir ++ "/raudio.c",
|
||||
srcdir ++ "/rcore.c",
|
||||
srcdir ++ "/rmodels.c",
|
||||
|
|
@ -26,74 +21,85 @@ pub fn addRaylib(b: *std.Build, target: std.zig.CrossTarget, optimize: std.built
|
|||
srcdir ++ "/rtext.c",
|
||||
srcdir ++ "/rtextures.c",
|
||||
srcdir ++ "/utils.c",
|
||||
}, raylib_flags);
|
||||
};
|
||||
|
||||
const rglfw = &[_][]const u8 {
|
||||
srcdir ++ "/rglfw.c",
|
||||
};
|
||||
|
||||
var gen_step = std.build.Step.WriteFile.create(b);
|
||||
raylib.step.dependOn(&gen_step.step);
|
||||
compile.step.dependOn(&gen_step.step);
|
||||
|
||||
if (options.raygui) {
|
||||
_ = gen_step.add(srcdir ++ "/raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
|
||||
raylib.addCSourceFile(srcdir ++ "/raygui.c", raylib_flags);
|
||||
raylib.addIncludePath(srcdir);
|
||||
raylib.addIncludePath(srcdir ++ "/../../raygui/src");
|
||||
compile.addCSourceFile(srcdir ++ "/raygui.c", raylib_flags);
|
||||
compile.addIncludePath(srcdir);
|
||||
compile.addIncludePath(srcdir ++ "/../../raygui/src");
|
||||
}
|
||||
|
||||
switch (target.getOsTag()) {
|
||||
switch (compile.target.getOsTag()) {
|
||||
.windows => {
|
||||
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
|
||||
raylib.linkSystemLibrary("winmm");
|
||||
raylib.linkSystemLibrary("gdi32");
|
||||
raylib.linkSystemLibrary("opengl32");
|
||||
raylib.addIncludePath("external/glfw/deps/mingw");
|
||||
|
||||
raylib.defineCMacro("PLATFORM_DESKTOP", null);
|
||||
compile.linkSystemLibrary("winmm");
|
||||
compile.linkSystemLibrary("gdi32");
|
||||
compile.linkSystemLibrary("opengl32");
|
||||
compile.addIncludePath("external/glfw/deps/mingw");
|
||||
compile.addCSourceFiles(
|
||||
c_source ++ rglfw,
|
||||
raylib_flags ++ &[_][]const u8{ "-DPLATFORM_DESKTOP" }
|
||||
);
|
||||
},
|
||||
.linux => {
|
||||
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
|
||||
raylib.linkSystemLibrary("GL");
|
||||
raylib.linkSystemLibrary("rt");
|
||||
raylib.linkSystemLibrary("dl");
|
||||
raylib.linkSystemLibrary("m");
|
||||
raylib.linkSystemLibrary("X11");
|
||||
raylib.addIncludePath("/usr/include");
|
||||
|
||||
raylib.defineCMacro("PLATFORM_DESKTOP", null);
|
||||
compile.linkSystemLibrary("GL");
|
||||
compile.linkSystemLibrary("rt");
|
||||
compile.linkSystemLibrary("dl");
|
||||
compile.linkSystemLibrary("m");
|
||||
compile.linkSystemLibrary("X11");
|
||||
compile.addIncludePath("/usr/include");
|
||||
compile.addCSourceFiles(
|
||||
c_source ++ rglfw,
|
||||
raylib_flags ++ &[_][]const u8{ "-DPLATFORM_DESKTOP" }
|
||||
);
|
||||
},
|
||||
.freebsd, .openbsd, .netbsd, .dragonfly => {
|
||||
raylib.addCSourceFiles(&.{srcdir ++ "/rglfw.c"}, raylib_flags);
|
||||
raylib.linkSystemLibrary("GL");
|
||||
raylib.linkSystemLibrary("rt");
|
||||
raylib.linkSystemLibrary("dl");
|
||||
raylib.linkSystemLibrary("m");
|
||||
raylib.linkSystemLibrary("X11");
|
||||
raylib.linkSystemLibrary("Xrandr");
|
||||
raylib.linkSystemLibrary("Xinerama");
|
||||
raylib.linkSystemLibrary("Xi");
|
||||
raylib.linkSystemLibrary("Xxf86vm");
|
||||
raylib.linkSystemLibrary("Xcursor");
|
||||
|
||||
raylib.defineCMacro("PLATFORM_DESKTOP", null);
|
||||
compile.linkSystemLibrary("GL");
|
||||
compile.linkSystemLibrary("rt");
|
||||
compile.linkSystemLibrary("dl");
|
||||
compile.linkSystemLibrary("m");
|
||||
compile.linkSystemLibrary("X11");
|
||||
compile.linkSystemLibrary("Xrandr");
|
||||
compile.linkSystemLibrary("Xinerama");
|
||||
compile.linkSystemLibrary("Xi");
|
||||
compile.linkSystemLibrary("Xxf86vm");
|
||||
compile.linkSystemLibrary("Xcursor");
|
||||
compile.addCSourceFiles(
|
||||
c_source ++ rglfw,
|
||||
raylib_flags ++ &[_][]const u8{ "-DPLATFORM_DESKTOP" }
|
||||
);
|
||||
},
|
||||
.macos => {
|
||||
compile.linkFramework("Foundation");
|
||||
compile.linkFramework("CoreServices");
|
||||
compile.linkFramework("CoreGraphics");
|
||||
compile.linkFramework("AppKit");
|
||||
compile.linkFramework("IOKit");
|
||||
// On macos rglfw.c include Objective-C files.
|
||||
const raylib_flags_extra_macos = &[_][]const u8{
|
||||
"-ObjC",
|
||||
};
|
||||
raylib.addCSourceFiles(
|
||||
&.{srcdir ++ "/rglfw.c"},
|
||||
compile.addCSourceFiles(
|
||||
rglfw,
|
||||
raylib_flags ++ raylib_flags_extra_macos,
|
||||
);
|
||||
raylib.linkFramework("Foundation");
|
||||
raylib.linkFramework("CoreServices");
|
||||
raylib.linkFramework("CoreGraphics");
|
||||
raylib.linkFramework("AppKit");
|
||||
raylib.linkFramework("IOKit");
|
||||
|
||||
raylib.defineCMacro("PLATFORM_DESKTOP", null);
|
||||
compile.addCSourceFiles(
|
||||
c_source,
|
||||
raylib_flags ++ &[_][]const u8{ "-DPLATFORM_DESKTOP" }
|
||||
);
|
||||
},
|
||||
.emscripten => {
|
||||
raylib.defineCMacro("PLATFORM_WEB", null);
|
||||
raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
|
||||
compile.addCSourceFiles(
|
||||
c_source,
|
||||
raylib_flags ++ &[_][]const u8{ "PLATFORM_WEB", "GRAPHICS_API_OPENGL_ES2" }
|
||||
);
|
||||
|
||||
if (b.sysroot == null) {
|
||||
@panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'");
|
||||
|
|
@ -105,14 +111,12 @@ pub fn addRaylib(b: *std.Build, target: std.zig.CrossTarget, optimize: std.built
|
|||
var dir = std.fs.openDirAbsolute(cache_include, std.fs.Dir.OpenDirOptions{ .access_sub_paths = true, .no_follow = true }) catch @panic("No emscripten cache. Generate it!");
|
||||
dir.close();
|
||||
|
||||
raylib.addIncludePath(cache_include);
|
||||
compile.addIncludePath(cache_include);
|
||||
},
|
||||
else => {
|
||||
@panic("Unsupported OS");
|
||||
},
|
||||
}
|
||||
|
||||
return raylib;
|
||||
}
|
||||
|
||||
const Options = struct {
|
||||
|
|
@ -132,19 +136,25 @@ pub fn build(b: *std.Build) void {
|
|||
|
||||
const raygui = b.option(bool, "raygui", "Compile with raygui support");
|
||||
|
||||
const lib = addRaylib(b, target, optimize, .{
|
||||
const raylib = b.addStaticLibrary(.{
|
||||
.name = "raylib",
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
addRaylib(b, raylib, .{
|
||||
.raygui = raygui orelse false,
|
||||
});
|
||||
|
||||
lib.installHeader("src/raylib.h", "raylib.h");
|
||||
lib.installHeader("src/raymath.h", "raymath.h");
|
||||
lib.installHeader("src/rlgl.h", "rlgl.h");
|
||||
raylib.installHeader("src/raylib.h", "raylib.h");
|
||||
raylib.installHeader("src/raymath.h", "raymath.h");
|
||||
raylib.installHeader("src/rlgl.h", "rlgl.h");
|
||||
|
||||
if (raygui orelse false) {
|
||||
lib.installHeader("../raygui/src/raygui.h", "raygui.h");
|
||||
raylib.installHeader("../raygui/src/raygui.h", "raygui.h");
|
||||
}
|
||||
|
||||
b.installArtifact(lib);
|
||||
b.installArtifact(raylib);
|
||||
}
|
||||
|
||||
const srcdir = struct {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user