| 1 |
const std = @import("std"); |
| 2 |
const builtin = @import("builtin"); |
| 3 |
|
| 4 |
pub const emsdk = struct { |
| 5 |
const zemscripten = @import("zemscripten"); |
| 6 |
|
| 7 |
pub fn shell(raylib_dep: *std.Build.Dependency) std.Build.LazyPath { |
| 8 |
return raylib_dep.path("src/shell.html"); |
| 9 |
} |
| 10 |
|
| 11 |
pub const FlagsOptions = struct { |
| 12 |
optimize: std.builtin.OptimizeMode, |
| 13 |
asyncify: bool = true, |
| 14 |
}; |
| 15 |
|
| 16 |
pub fn emccDefaultFlags(allocator: std.mem.Allocator, options: FlagsOptions) zemscripten.EmccFlags { |
| 17 |
var emcc_flags = zemscripten.emccDefaultFlags(allocator, .{ |
| 18 |
.optimize = options.optimize, |
| 19 |
.fsanitize = true, |
| 20 |
}); |
| 21 |
|
| 22 |
if (options.asyncify) |
| 23 |
emcc_flags.put("-sASYNCIFY", {}) catch unreachable; |
| 24 |
|
| 25 |
return emcc_flags; |
| 26 |
} |
| 27 |
|
| 28 |
pub const SettingsOptions = struct { |
| 29 |
optimize: std.builtin.OptimizeMode, |
| 30 |
es3: bool = false, |
| 31 |
glfw3: bool = true, |
| 32 |
memory_growth: bool = false, |
| 33 |
total_memory: u32 = 134217728, |
| 34 |
emsdk_allocator: zemscripten.EmsdkAllocator = .emmalloc, |
| 35 |
}; |
| 36 |
|
| 37 |
pub fn emccDefaultSettings(allocator: std.mem.Allocator, options: SettingsOptions) zemscripten.EmccSettings { |
| 38 |
var emcc_settings = zemscripten.emccDefaultSettings(allocator, .{ |
| 39 |
.optimize = options.optimize, |
| 40 |
.emsdk_allocator = options.emsdk_allocator, |
| 41 |
}); |
| 42 |
|
| 43 |
if (options.es3) { |
| 44 |
emcc_settings.put("FULL_ES3", "1") catch unreachable; |
| 45 |
emcc_settings.put("MIN_WEBGL_VERSION", "2") catch unreachable; |
| 46 |
emcc_settings.put("MAX_WEBGL_VERSION", "2") catch unreachable; |
| 47 |
} |
| 48 |
if (options.glfw3) { |
| 49 |
emcc_settings.put("USE_GLFW", "3") catch unreachable; |
| 50 |
} |
| 51 |
|
| 52 |
const total_memory = std.fmt.allocPrint(allocator, "{d}", .{options.total_memory}) catch unreachable; |
| 53 |
|
| 54 |
emcc_settings.put("EXPORTED_RUNTIME_METHODS", "['requestFullscreen']") catch unreachable; |
| 55 |
emcc_settings.put("TOTAL_MEMORY", total_memory) catch unreachable; |
| 56 |
emcc_settings.put("FORCE_FILESYSTEM", "1") catch unreachable; |
| 57 |
emcc_settings.put("EXPORTED_RUNTIME_METHODS", "ccall") catch unreachable; |
| 58 |
|
| 59 |
if (options.memory_growth) |
| 60 |
emcc_settings.put("ALLOW_MEMORY_GROWTH", "1") catch unreachable; |
| 61 |
|
| 62 |
return emcc_settings; |
| 63 |
} |
| 64 |
|
| 65 |
pub fn emccStep(b: *std.Build, raylib: *std.Build.Step.Compile, wasm: *std.Build.Step.Compile, options: zemscripten.StepOptions) *std.Build.Step { |
| 66 |
const activate_emsdk_step = zemscripten.activateEmsdkStep(b); |
| 67 |
|
| 68 |
const emsdk_dep = b.dependency("emsdk", .{}); |
| 69 |
raylib.root_module.addIncludePath(emsdk_dep.path("upstream/emscripten/cache/sysroot/include")); |
| 70 |
wasm.root_module.addIncludePath(emsdk_dep.path("upstream/emscripten/cache/sysroot/include")); |
| 71 |
|
| 72 |
const emcc_step = zemscripten.emccStep(b, wasm, options); |
| 73 |
emcc_step.dependOn(activate_emsdk_step); |
| 74 |
|
| 75 |
return emcc_step; |
| 76 |
} |
| 77 |
|
| 78 |
pub fn emrunStep( |
| 79 |
b: *std.Build, |
| 80 |
html_path: []const u8, |
| 81 |
extra_args: []const []const u8, |
| 82 |
) *std.Build.Step { |
| 83 |
return zemscripten.emrunStep(b, html_path, extra_args); |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
pub fn linkWindows(mod: *std.Build.Module, opengl: bool, comptime shcore: bool) void { |
| 88 |
if (opengl) mod.linkSystemLibrary("opengl32", .{}); |
| 89 |
mod.linkSystemLibrary("winmm", .{}); |
| 90 |
mod.linkSystemLibrary("gdi32", .{}); |
| 91 |
if (shcore) mod.linkSystemLibrary("shcore", .{}); |
| 92 |
} |
| 93 |
|
| 94 |
fn findWaylandScanner(b: *std.Build) void { |
| 95 |
_ = b.findProgram(&.{"wayland-scanner"}, &.{}) catch { |
| 96 |
std.log.err( |
| 97 |
\\ `wayland-scanner` may not be installed on the system. |
| 98 |
\\ You can switch to X11 in your `build.zig` by changing `Options.linux_display_backend` |
| 99 |
, .{}); |
| 100 |
@panic("`wayland-scanner` not found"); |
| 101 |
}; |
| 102 |
} |
| 103 |
|
| 104 |
pub fn linkLinux(mod: *std.Build.Module, comptime display_backend: LinuxDisplayBackend) void { |
| 105 |
if (display_backend == .None) { |
| 106 |
mod.linkSystemLibrary("GL", .{}); |
| 107 |
} |
| 108 |
|
| 109 |
if (display_backend == .X11) { |
| 110 |
mod.linkSystemLibrary("X11", .{}); |
| 111 |
mod.linkSystemLibrary("Xrandr", .{}); |
| 112 |
mod.linkSystemLibrary("Xinerama", .{}); |
| 113 |
mod.linkSystemLibrary("Xi", .{}); |
| 114 |
mod.linkSystemLibrary("Xcursor", .{}); |
| 115 |
} |
| 116 |
|
| 117 |
if (display_backend == .Wayland) { |
| 118 |
mod.linkSystemLibrary("wayland-client", .{}); |
| 119 |
mod.linkSystemLibrary("wayland-cursor", .{}); |
| 120 |
mod.linkSystemLibrary("wayland-egl", .{}); |
| 121 |
mod.linkSystemLibrary("xkbcommon", .{}); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
pub fn linkBSD(_: *std.Build, mod: *std.Build.Module) void { |
| 126 |
mod.linkSystemLibrary("GL", .{}); |
| 127 |
} |
| 128 |
|
| 129 |
pub fn linkMacOS(b: *std.Build, mod: *std.Build.Module) void { |
| 130 |
// Include xcode_frameworks for cross compilation |
| 131 |
if (b.lazyDependency("xcode_frameworks", .{})) |dep| { |
| 132 |
mod.addSystemFrameworkPath(dep.path("Frameworks")); |
| 133 |
mod.addSystemIncludePath(dep.path("include")); |
| 134 |
mod.addLibraryPath(dep.path("lib")); |
| 135 |
} |
| 136 |
|
| 137 |
mod.linkFramework("Foundation", .{}); |
| 138 |
mod.linkFramework("CoreServices", .{}); |
| 139 |
mod.linkFramework("CoreGraphics", .{}); |
| 140 |
mod.linkFramework("AppKit", .{}); |
| 141 |
mod.linkFramework("IOKit", .{}); |
| 142 |
} |
| 143 |
|
| 144 |
fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile { |
| 145 |
const raylib_mod = b.createModule(.{ |
| 146 |
.optimize = optimize, |
| 147 |
.target = target, |
| 148 |
.link_libc = true, |
| 149 |
}); |
| 150 |
|
| 151 |
const raylib = b.addLibrary(.{ |
| 152 |
.name = "raylib", |
| 153 |
.linkage = options.linkage, |
| 154 |
.root_module = raylib_mod, |
| 155 |
}); |
| 156 |
|
| 157 |
raylib_mod.addCMacro("_GNU_SOURCE", ""); |
| 158 |
raylib_mod.addCMacro("GL_SILENCE_DEPRECATION", "199309L"); |
| 159 |
|
| 160 |
var arena: std.heap.ArenaAllocator = .init(b.allocator); |
| 161 |
defer arena.deinit(); |
| 162 |
|
| 163 |
var raylib_flags_arr: std.array_list.Managed([]const u8) = .init(arena.allocator()); |
| 164 |
var c_source_files: std.array_list.Managed([]const u8) = .init(arena.allocator()); |
| 165 |
|
| 166 |
try c_source_files.append("src/rcore.c"); |
| 167 |
|
| 168 |
if (target.result.os.tag == .emscripten) { |
| 169 |
try raylib_flags_arr.append("-std=gnu99"); |
| 170 |
} else { |
| 171 |
try raylib_flags_arr.append("-std=c99"); |
| 172 |
} |
| 173 |
|
| 174 |
if (options.linkage == .dynamic) { |
| 175 |
raylib_mod.pic = true; |
| 176 |
raylib_mod.addCMacro("BUILD_LIBTYPE_SHARED", ""); |
| 177 |
} |
| 178 |
|
| 179 |
if (options.config.len > 0) { |
| 180 |
// Splits a space-separated list of config flags into multiple flags |
| 181 |
// |
| 182 |
// Note: This means certain flags like `-x c++` won't be processed properly. |
| 183 |
// `-xc++` or similar should be used when possible |
| 184 |
var config_iter = std.mem.tokenizeScalar(u8, options.config, ' '); |
| 185 |
|
| 186 |
// Apply config flags supplied by the user |
| 187 |
while (config_iter.next()) |config_flag| { |
| 188 |
try raylib_flags_arr.append(config_flag); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
raylib_mod.addCMacro("SUPPORT_MODULE_RSHAPES", &.{@as(u8, @intFromBool(options.rshapes)) + 0x30}); |
| 193 |
if (options.rshapes) { |
| 194 |
try c_source_files.append("src/rshapes.c"); |
| 195 |
} |
| 196 |
raylib_mod.addCMacro("SUPPORT_MODULE_RTEXTURES", &.{@as(u8, @intFromBool(options.rtextures)) + 0x30}); |
| 197 |
if (options.rtextures) { |
| 198 |
try c_source_files.append("src/rtextures.c"); |
| 199 |
} |
| 200 |
raylib_mod.addCMacro("SUPPORT_MODULE_RTEXT", &.{@as(u8, @intFromBool(options.rtext)) + 0x30}); |
| 201 |
if (options.rtext) { |
| 202 |
try c_source_files.append("src/rtext.c"); |
| 203 |
} |
| 204 |
raylib_mod.addCMacro("SUPPORT_MODULE_RMODELS", &.{@as(u8, @intFromBool(options.rmodels)) + 0x30}); |
| 205 |
if (options.rmodels) { |
| 206 |
try c_source_files.append("src/rmodels.c"); |
| 207 |
} |
| 208 |
raylib_mod.addCMacro("SUPPORT_MODULE_RAUDIO", &.{@as(u8, @intFromBool(options.raudio)) + 0x30}); |
| 209 |
if (options.raudio) { |
| 210 |
try c_source_files.append("src/raudio.c"); |
| 211 |
} |
| 212 |
|
| 213 |
raylib_mod.addIncludePath(b.path("src/platforms")); |
| 214 |
switch (options.platform) { |
| 215 |
.glfw => { |
| 216 |
var opengl_version: OpenglVersion = options.opengl_version; |
| 217 |
if (opengl_version == .gl_soft) { |
| 218 |
@panic("The opengl version is not supported by this platform"); |
| 219 |
} |
| 220 |
|
| 221 |
raylib_mod.addIncludePath(b.path("src/external/glfw/include")); |
| 222 |
|
| 223 |
if (target.result.os.tag != .emscripten) { |
| 224 |
if (opengl_version == .auto) { |
| 225 |
opengl_version = OpenglVersion.gl_3_3; |
| 226 |
} |
| 227 |
raylib_mod.addCMacro("PLATFORM_DESKTOP_GLFW", ""); |
| 228 |
try c_source_files.append("src/rglfw.c"); |
| 229 |
} |
| 230 |
|
| 231 |
switch (target.result.os.tag) { |
| 232 |
.windows => linkWindows(raylib_mod, true, false), |
| 233 |
.linux => { |
| 234 |
if (target.result.abi.isAndroid()) { |
| 235 |
@panic("Target is not supported with this platform"); |
| 236 |
} |
| 237 |
|
| 238 |
linkLinux(raylib_mod, .None); |
| 239 |
|
| 240 |
if (options.linux_display_backend == .X11 or options.linux_display_backend == .Both) { |
| 241 |
raylib_mod.addCMacro("_GLFW_X11", ""); |
| 242 |
linkLinux(raylib_mod, .X11); |
| 243 |
} |
| 244 |
|
| 245 |
if (options.linux_display_backend == .Wayland or options.linux_display_backend == .Both) { |
| 246 |
findWaylandScanner(b); |
| 247 |
|
| 248 |
raylib_mod.addCMacro("_GLFW_WAYLAND", ""); |
| 249 |
linkLinux(raylib_mod, .Wayland); |
| 250 |
try waylandGenerate(b, raylib, "src/external/glfw/deps/wayland/", false); |
| 251 |
} |
| 252 |
}, |
| 253 |
.freebsd, .openbsd, .netbsd, .dragonfly => linkBSD(b, raylib_mod), |
| 254 |
.macos => { |
| 255 |
// On macos rglfw.c include Objective-C files. |
| 256 |
_ = c_source_files.pop(); |
| 257 |
try raylib_flags_arr.append("-ObjC"); |
| 258 |
raylib_mod.addCSourceFile(.{ |
| 259 |
.file = b.path("src/rglfw.c"), |
| 260 |
.flags = raylib_flags_arr.items, |
| 261 |
}); |
| 262 |
_ = raylib_flags_arr.pop(); |
| 263 |
|
| 264 |
linkMacOS(b, raylib_mod); |
| 265 |
}, |
| 266 |
.emscripten => { |
| 267 |
switch (opengl_version) { |
| 268 |
.auto => opengl_version = OpenglVersion.gles_2, |
| 269 |
.gles_2, .gles_3, .gl_soft => {}, |
| 270 |
else => @panic("opengl version not supported"), |
| 271 |
} |
| 272 |
|
| 273 |
raylib_mod.addCMacro("PLATFORM_WEB", ""); |
| 274 |
|
| 275 |
const activate_emsdk_step = emsdk.zemscripten.activateEmsdkStep(b); |
| 276 |
raylib.step.dependOn(activate_emsdk_step); |
| 277 |
}, |
| 278 |
else => @panic("Target is not supported with this platform"), |
| 279 |
} |
| 280 |
raylib_mod.addCMacro(opengl_version.toCMacroStr(), ""); |
| 281 |
}, |
| 282 |
.rgfw => { |
| 283 |
var opengl_version: OpenglVersion = options.opengl_version; |
| 284 |
|
| 285 |
if (target.result.os.tag != .emscripten) { |
| 286 |
if (opengl_version == .auto) { |
| 287 |
opengl_version = OpenglVersion.gl_3_3; |
| 288 |
} |
| 289 |
raylib_mod.addCMacro("PLATFORM_DESKTOP_RGFW", ""); |
| 290 |
} |
| 291 |
|
| 292 |
switch (target.result.os.tag) { |
| 293 |
.windows => linkWindows(raylib_mod, true, false), |
| 294 |
.linux => { |
| 295 |
if (target.result.abi.isAndroid()) { |
| 296 |
@panic("Target is not supported with this platform"); |
| 297 |
} |
| 298 |
|
| 299 |
linkLinux(raylib_mod, .None); |
| 300 |
|
| 301 |
if (options.linux_display_backend == .X11 or options.linux_display_backend == .Both) { |
| 302 |
raylib_mod.addCMacro("RGFW_X11", ""); |
| 303 |
raylib_mod.addCMacro("RGFW_UNIX", ""); |
| 304 |
|
| 305 |
linkLinux(raylib_mod, .X11); |
| 306 |
} |
| 307 |
|
| 308 |
if (options.linux_display_backend == .Wayland or options.linux_display_backend == .Both) { |
| 309 |
findWaylandScanner(b); |
| 310 |
|
| 311 |
if (options.linux_display_backend != .Both) { |
| 312 |
raylib_mod.addCMacro("RGFW_NO_X11", ""); |
| 313 |
} |
| 314 |
|
| 315 |
raylib_mod.addCMacro("RGFW_WAYLAND", ""); |
| 316 |
raylib_mod.addCMacro("EGLAPIENTRY", ""); |
| 317 |
|
| 318 |
linkLinux(raylib_mod, .Wayland); |
| 319 |
|
| 320 |
try waylandGenerate(b, raylib, "src/external/RGFW/deps/wayland/", true); |
| 321 |
} |
| 322 |
}, |
| 323 |
.freebsd, .openbsd, .netbsd, .dragonfly => linkBSD(b, raylib_mod), |
| 324 |
.macos => linkMacOS(b, raylib_mod), |
| 325 |
.emscripten => { |
| 326 |
switch (opengl_version) { |
| 327 |
.auto => opengl_version = OpenglVersion.gles_2, |
| 328 |
.gles_2, .gles_3, .gl_soft => {}, |
| 329 |
else => @panic("opengl version not supported"), |
| 330 |
} |
| 331 |
|
| 332 |
raylib_mod.addCMacro("PLATFORM_WEB_RGFW", ""); |
| 333 |
const activate_emsdk_step = emsdk.zemscripten.activateEmsdkStep(b); |
| 334 |
raylib.step.dependOn(activate_emsdk_step); |
| 335 |
}, |
| 336 |
else => @panic("Target is not supported with this platform"), |
| 337 |
} |
| 338 |
|
| 339 |
raylib_mod.addCMacro(opengl_version.toCMacroStr(), ""); |
| 340 |
}, |
| 341 |
.sdl, .sdl2, .sdl3 => { |
| 342 |
if (options.opengl_version == .auto) { |
| 343 |
raylib_mod.addCMacro(OpenglVersion.gl_3_3.toCMacroStr(), ""); |
| 344 |
} else { |
| 345 |
raylib_mod.addCMacro(options.opengl_version.toCMacroStr(), ""); |
| 346 |
} |
| 347 |
|
| 348 |
raylib_mod.addCMacro("PLATFORM_DESKTOP_SDL", ""); |
| 349 |
|
| 350 |
if (options.platform == .sdl2) { |
| 351 |
raylib_mod.addCMacro("USING_SDL2_PACKAGE", ""); |
| 352 |
} |
| 353 |
if (options.platform == .sdl3) { |
| 354 |
raylib_mod.addCMacro("USING_SDL3_PACKAGE", ""); |
| 355 |
} |
| 356 |
}, |
| 357 |
.memory => { |
| 358 |
if (options.opengl_version != .auto and options.opengl_version != .gl_soft) { |
| 359 |
@panic("The opengl version is not supported by this platform"); |
| 360 |
} |
| 361 |
raylib_mod.addCMacro(OpenglVersion.gl_soft.toCMacroStr(), ""); |
| 362 |
raylib_mod.addCMacro("PLATFORM_MEMORY", ""); |
| 363 |
}, |
| 364 |
.win32 => { |
| 365 |
if (target.result.os.tag != .windows) { |
| 366 |
@panic("Target is not supported with this platform"); |
| 367 |
} |
| 368 |
|
| 369 |
if (options.opengl_version == .auto) { |
| 370 |
raylib_mod.addCMacro(OpenglVersion.gl_3_3.toCMacroStr(), ""); |
| 371 |
} else { |
| 372 |
raylib_mod.addCMacro(options.opengl_version.toCMacroStr(), ""); |
| 373 |
} |
| 374 |
|
| 375 |
raylib_mod.addCMacro("PLATFORM_DESKTOP_WIN32", ""); |
| 376 |
|
| 377 |
linkWindows(raylib_mod, options.opengl_version != .gl_soft, true); |
| 378 |
}, |
| 379 |
.drm => { |
| 380 |
if (target.result.os.tag != .linux) { |
| 381 |
@panic("Target is not supported with this platform"); |
| 382 |
} |
| 383 |
|
| 384 |
raylib_mod.addCMacro("PLATFORM_DRM", ""); |
| 385 |
raylib_mod.addCMacro("EGL_NO_X11", ""); |
| 386 |
raylib_mod.addCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", ""); |
| 387 |
|
| 388 |
try raylib_flags_arr.append("-Werror=implicit-function-declaration"); |
| 389 |
|
| 390 |
raylib_mod.linkSystemLibrary("libdrm", .{ .use_pkg_config = .force }); |
| 391 |
raylib_mod.linkSystemLibrary("drm", .{}); |
| 392 |
raylib_mod.linkSystemLibrary("gbm", .{}); |
| 393 |
|
| 394 |
switch (options.opengl_version) { |
| 395 |
.auto, .gles_2 => { |
| 396 |
raylib_mod.addCMacro(OpenglVersion.gles_2.toCMacroStr(), ""); |
| 397 |
raylib_mod.linkSystemLibrary("GLESv2", .{}); |
| 398 |
raylib_mod.linkSystemLibrary("EGL", .{}); |
| 399 |
}, |
| 400 |
.gl_soft => {}, |
| 401 |
else => @panic("The opengl version is not supported by this platform"), |
| 402 |
} |
| 403 |
}, |
| 404 |
.android => { |
| 405 |
if (!target.result.abi.isAndroid()) { |
| 406 |
@panic("Target is not supported with this platform"); |
| 407 |
} |
| 408 |
|
| 409 |
raylib_mod.addCMacro("PLATFORM_ANDROID", ""); |
| 410 |
|
| 411 |
raylib_mod.linkSystemLibrary("EGL", .{}); |
| 412 |
switch (options.opengl_version) { |
| 413 |
.auto, .gles_2 => { |
| 414 |
raylib_mod.addCMacro(OpenglVersion.gles_2.toCMacroStr(), ""); |
| 415 |
raylib_mod.linkSystemLibrary("GLESv2", .{}); |
| 416 |
}, |
| 417 |
else => @panic("The opengl version is not supported by this platform"), |
| 418 |
} |
| 419 |
|
| 420 |
//these are the only tag options per https://developer.android.com/ndk/guides/other_build_systems |
| 421 |
const hostTuple = switch (builtin.target.os.tag) { |
| 422 |
.linux => "linux-x86_64", |
| 423 |
.windows => "windows-x86_64", |
| 424 |
.macos => "darwin-x86_64", |
| 425 |
else => @panic("unsupported host OS"), |
| 426 |
}; |
| 427 |
|
| 428 |
const androidTriple = switch (target.result.cpu.arch) { |
| 429 |
.x86 => "i686-linux-android", |
| 430 |
.x86_64 => "x86_64-linux-android", |
| 431 |
.arm => "arm-linux-androideabi", |
| 432 |
.aarch64 => "aarch64-linux-android", |
| 433 |
.riscv64 => "riscv64-linux-android", |
| 434 |
else => error.InvalidAndroidTarget, |
| 435 |
} catch @panic("invalid android target!"); |
| 436 |
const androidNdkPathString: []const u8 = options.android_ndk; |
| 437 |
if (androidNdkPathString.len < 1) @panic("no ndk path provided and ANDROID_NDK_HOME is not set"); |
| 438 |
const androidApiLevel: []const u8 = options.android_api_version; |
| 439 |
|
| 440 |
const androidSysroot = try std.fs.path.join(b.allocator, &.{ androidNdkPathString, "/toolchains/llvm/prebuilt/", hostTuple, "/sysroot" }); |
| 441 |
const androidLibPath = try std.fs.path.join(b.allocator, &.{ androidSysroot, "/usr/lib/", androidTriple }); |
| 442 |
const androidApiSpecificPath = try std.fs.path.join(b.allocator, &.{ androidLibPath, androidApiLevel }); |
| 443 |
const androidIncludePath = try std.fs.path.join(b.allocator, &.{ androidSysroot, "/usr/include" }); |
| 444 |
const androidArchIncludePath = try std.fs.path.join(b.allocator, &.{ androidIncludePath, androidTriple }); |
| 445 |
const androidAsmPath = try std.fs.path.join(b.allocator, &.{ androidIncludePath, "/asm-generic" }); |
| 446 |
const androidGluePath = try std.fs.path.join(b.allocator, &.{ androidNdkPathString, "/sources/android/native_app_glue/" }); |
| 447 |
|
| 448 |
raylib_mod.addLibraryPath(.{ .cwd_relative = androidLibPath }); |
| 449 |
raylib_mod.addLibraryPath(.{ .cwd_relative = androidApiSpecificPath }); |
| 450 |
raylib_mod.addSystemIncludePath(.{ .cwd_relative = androidIncludePath }); |
| 451 |
raylib_mod.addSystemIncludePath(.{ .cwd_relative = androidArchIncludePath }); |
| 452 |
raylib_mod.addSystemIncludePath(.{ .cwd_relative = androidAsmPath }); |
| 453 |
raylib_mod.addSystemIncludePath(.{ .cwd_relative = androidGluePath }); |
| 454 |
|
| 455 |
const libc_data = try std.fmt.allocPrint(b.allocator, |
| 456 |
\\include_dir={0s}/sysroot/usr/include |
| 457 |
\\sys_include_dir={0s}/sysroot/usr/include/aarch64-linux-android |
| 458 |
\\crt_dir={0s}/sysroot/usr/lib/aarch64-linux-android/24 |
| 459 |
\\static_lib_dir={0s}/sysroot/usr/lib/aarch64-linux-android/24 |
| 460 |
\\msvc_lib_dir= |
| 461 |
\\kernel32_lib_dir= |
| 462 |
\\gcc_dir= |
| 463 |
\\ |
| 464 |
, .{androidNdkPathString}); |
| 465 |
const write_step = b.addWriteFiles(); |
| 466 |
const libcFile = write_step.add("android-libc.txt", libc_data); |
| 467 |
raylib.setLibCFile(libcFile); |
| 468 |
}, |
| 469 |
} |
| 470 |
|
| 471 |
raylib_mod.addCSourceFiles(.{ |
| 472 |
.files = c_source_files.items, |
| 473 |
.flags = raylib_flags_arr.items, |
| 474 |
}); |
| 475 |
|
| 476 |
return raylib; |
| 477 |
} |
| 478 |
|
| 479 |
fn addRaygui(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, raylib: *std.Build.Step.Compile) void { |
| 480 |
if (b.lazyDependency("raygui", .{ |
| 481 |
.target = target, |
| 482 |
.optimize = optimize, |
| 483 |
.link_libc = true, |
| 484 |
})) |raygui_dep| { |
| 485 |
var gen_step = b.addWriteFiles(); |
| 486 |
raylib.step.dependOn(&gen_step.step); |
| 487 |
|
| 488 |
const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n"); |
| 489 |
raylib.root_module.addCSourceFile(.{ .file = raygui_c_path }); |
| 490 |
raylib.root_module.addIncludePath(raygui_dep.path("src")); |
| 491 |
raylib.root_module.addIncludePath(b.path("src")); |
| 492 |
|
| 493 |
raylib.installHeader(raygui_dep.path("src/raygui.h"), "raygui.h"); |
| 494 |
|
| 495 |
const c = b.addTranslateC(.{ |
| 496 |
.root_source_file = raygui_dep.path("src/raygui.h"), |
| 497 |
.target = target, |
| 498 |
.optimize = optimize, |
| 499 |
.link_libc = true, |
| 500 |
}); |
| 501 |
c.addIncludePath(b.path("src")); |
| 502 |
const c_mod = c.createModule(); |
| 503 |
c_mod.linkLibrary(raylib); |
| 504 |
b.modules.put(b.graph.arena, "raygui", c_mod) catch @panic("OOM"); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
pub const Options = struct { |
| 509 |
raudio: bool = true, |
| 510 |
rmodels: bool = true, |
| 511 |
rshapes: bool = true, |
| 512 |
rtext: bool = true, |
| 513 |
rtextures: bool = true, |
| 514 |
raygui: bool = false, |
| 515 |
platform: PlatformBackend = .glfw, |
| 516 |
linkage: std.builtin.LinkMode = .static, |
| 517 |
linux_display_backend: LinuxDisplayBackend = .X11, |
| 518 |
opengl_version: OpenglVersion = .auto, |
| 519 |
android_ndk: []const u8 = "", |
| 520 |
android_api_version: []const u8 = "35", |
| 521 |
/// config should be a list of space-separated cflags, eg, "-DSUPPORT_CUSTOM_FRAME_CONTROL" |
| 522 |
config: []const u8 = &.{}, |
| 523 |
|
| 524 |
const defaults = Options{}; |
| 525 |
|
| 526 |
pub fn getOptions(b: *std.Build) Options { |
| 527 |
return .{ |
| 528 |
.platform = b.option(PlatformBackend, "platform", "Choose the platform backend for desktop target") orelse defaults.platform, |
| 529 |
.raudio = b.option(bool, "raudio", "Compile with audio support") orelse defaults.raudio, |
| 530 |
.rmodels = b.option(bool, "rmodels", "Compile with models support") orelse defaults.rmodels, |
| 531 |
.rtext = b.option(bool, "rtext", "Compile with text support") orelse defaults.rtext, |
| 532 |
.rtextures = b.option(bool, "rtextures", "Compile with textures support") orelse defaults.rtextures, |
| 533 |
.rshapes = b.option(bool, "rshapes", "Compile with shapes support") orelse defaults.rshapes, |
| 534 |
.raygui = b.option(bool, "raygui", "Include raygui") orelse defaults.raygui, |
| 535 |
.linkage = b.option(std.builtin.LinkMode, "linkage", "Compile as shared or static library") orelse defaults.linkage, |
| 536 |
.linux_display_backend = b.option(LinuxDisplayBackend, "linux_display_backend", "Linux display backend to use") orelse defaults.linux_display_backend, |
| 537 |
.opengl_version = b.option(OpenglVersion, "opengl_version", "OpenGL version to use") orelse defaults.opengl_version, |
| 538 |
.config = b.option([]const u8, "config", "Compile with custom define macros overriding config.h") orelse &.{}, |
| 539 |
.android_ndk = b.option([]const u8, "android_ndk", "specify path to android ndk") orelse b.graph.environ_map.get("ANDROID_NDK_HOME") orelse "", |
| 540 |
.android_api_version = b.option([]const u8, "android_api_version", "specify target android API level") orelse defaults.android_api_version, |
| 541 |
}; |
| 542 |
} |
| 543 |
}; |
| 544 |
|
| 545 |
pub const OpenglVersion = enum { |
| 546 |
auto, |
| 547 |
gl_soft, |
| 548 |
gl_1_1, |
| 549 |
gl_2_1, |
| 550 |
gl_3_3, |
| 551 |
gl_4_3, |
| 552 |
gles_2, |
| 553 |
gles_3, |
| 554 |
|
| 555 |
pub fn toCMacroStr(self: @This()) []const u8 { |
| 556 |
switch (self) { |
| 557 |
.auto => @panic("OpenglVersion.auto cannot be turned into a C macro string"), |
| 558 |
.gl_soft => return "GRAPHICS_API_OPENGL_SOFTWARE", |
| 559 |
.gl_1_1 => return "GRAPHICS_API_OPENGL_11", |
| 560 |
.gl_2_1 => return "GRAPHICS_API_OPENGL_21", |
| 561 |
.gl_3_3 => return "GRAPHICS_API_OPENGL_33", |
| 562 |
.gl_4_3 => return "GRAPHICS_API_OPENGL_43", |
| 563 |
.gles_2 => return "GRAPHICS_API_OPENGL_ES2", |
| 564 |
.gles_3 => return "GRAPHICS_API_OPENGL_ES3", |
| 565 |
} |
| 566 |
} |
| 567 |
}; |
| 568 |
|
| 569 |
pub const LinuxDisplayBackend = enum { |
| 570 |
None, |
| 571 |
X11, |
| 572 |
Wayland, |
| 573 |
Both, |
| 574 |
}; |
| 575 |
|
| 576 |
pub const PlatformBackend = enum { |
| 577 |
glfw, |
| 578 |
rgfw, |
| 579 |
sdl, |
| 580 |
sdl2, |
| 581 |
sdl3, |
| 582 |
memory, |
| 583 |
win32, |
| 584 |
drm, |
| 585 |
android, |
| 586 |
}; |
| 587 |
|
| 588 |
fn translateCMod( |
| 589 |
comptime header: []const u8, |
| 590 |
b: *std.Build, |
| 591 |
target: std.Build.ResolvedTarget, |
| 592 |
optimize: std.builtin.OptimizeMode, |
| 593 |
raylib: *std.Build.Step.Compile, |
| 594 |
) void { |
| 595 |
const c = b.addTranslateC(.{ |
| 596 |
.root_source_file = b.path("src/" ++ header ++ ".h"), |
| 597 |
.target = target, |
| 598 |
.optimize = optimize, |
| 599 |
.link_libc = true, |
| 600 |
}); |
| 601 |
const c_mod = c.createModule(); |
| 602 |
c_mod.linkLibrary(raylib); |
| 603 |
b.modules.put(b.graph.arena, header, c_mod) catch @panic("OOM"); |
| 604 |
} |
| 605 |
|
| 606 |
pub fn build(b: *std.Build) !void { |
| 607 |
const target = b.standardTargetOptions(.{}); |
| 608 |
const optimize = b.standardOptimizeOption(.{}); |
| 609 |
const options: Options = .getOptions(b); |
| 610 |
|
| 611 |
const lib = try compileRaylib(b, target, optimize, options); |
| 612 |
|
| 613 |
lib.installHeader(b.path("src/raylib.h"), "raylib.h"); |
| 614 |
lib.installHeader(b.path("src/rcamera.h"), "rcamera.h"); |
| 615 |
lib.installHeader(b.path("src/raymath.h"), "raymath.h"); |
| 616 |
lib.installHeader(b.path("src/rlgl.h"), "rlgl.h"); |
| 617 |
|
| 618 |
b.installArtifact(lib); |
| 619 |
|
| 620 |
translateCMod("raylib", b, target, optimize, lib); |
| 621 |
translateCMod("rcamera", b, target, optimize, lib); |
| 622 |
translateCMod("raymath", b, target, optimize, lib); |
| 623 |
translateCMod("rlgl", b, target, optimize, lib); |
| 624 |
|
| 625 |
if (options.raygui) { |
| 626 |
addRaygui(b, target, optimize, lib); |
| 627 |
} |
| 628 |
|
| 629 |
const examples = b.step("examples", "build/install all examples"); |
| 630 |
examples.dependOn(try addExamples("core", b, target, optimize, lib, options.platform)); |
| 631 |
examples.dependOn(try addExamples("audio", b, target, optimize, lib, options.platform)); |
| 632 |
examples.dependOn(try addExamples("models", b, target, optimize, lib, options.platform)); |
| 633 |
examples.dependOn(try addExamples("shaders", b, target, optimize, lib, options.platform)); |
| 634 |
examples.dependOn(try addExamples("shapes", b, target, optimize, lib, options.platform)); |
| 635 |
examples.dependOn(try addExamples("text", b, target, optimize, lib, options.platform)); |
| 636 |
examples.dependOn(try addExamples("textures", b, target, optimize, lib, options.platform)); |
| 637 |
examples.dependOn(try addExamples("others", b, target, optimize, lib, options.platform)); |
| 638 |
} |
| 639 |
|
| 640 |
fn addExamples( |
| 641 |
comptime module: []const u8, |
| 642 |
b: *std.Build, |
| 643 |
target: std.Build.ResolvedTarget, |
| 644 |
optimize: std.builtin.OptimizeMode, |
| 645 |
raylib: *std.Build.Step.Compile, |
| 646 |
platform: PlatformBackend, |
| 647 |
) !*std.Build.Step { |
| 648 |
const all = b.step(module, "All " ++ module ++ " examples"); |
| 649 |
const module_subpath = b.pathJoin(&.{ "examples", module }); |
| 650 |
|
| 651 |
var dir = try b.build_root.handle.openDir(b.graph.io, module_subpath, .{ .iterate = true }); |
| 652 |
defer dir.close(b.graph.io); |
| 653 |
|
| 654 |
var iter = dir.iterate(); |
| 655 |
while (try iter.next(b.graph.io)) |entry| { |
| 656 |
if (entry.kind != .file) continue; |
| 657 |
|
| 658 |
const filetype = std.fs.path.extension(entry.name); |
| 659 |
if (!std.mem.eql(u8, filetype, ".c")) continue; |
| 660 |
|
| 661 |
const filename = std.fs.path.stem(entry.name); |
| 662 |
const path = b.pathJoin(&.{ module_subpath, entry.name }); |
| 663 |
|
| 664 |
const exe_mod = b.createModule(.{ |
| 665 |
.target = target, |
| 666 |
.optimize = optimize, |
| 667 |
.link_libc = true, |
| 668 |
}); |
| 669 |
exe_mod.addCSourceFile(.{ .file = b.path(path) }); |
| 670 |
exe_mod.linkLibrary(raylib); |
| 671 |
|
| 672 |
if (platform == .sdl) { |
| 673 |
exe_mod.linkSystemLibrary("SDL2", .{}); |
| 674 |
exe_mod.linkSystemLibrary("SDL3", .{}); |
| 675 |
} |
| 676 |
if (platform == .sdl2) { |
| 677 |
exe_mod.linkSystemLibrary("SDL2", .{}); |
| 678 |
} |
| 679 |
if (platform == .sdl3) { |
| 680 |
exe_mod.linkSystemLibrary("SDL3", .{}); |
| 681 |
} |
| 682 |
|
| 683 |
if (std.mem.eql(u8, filename, "rlgl_standalone")) { |
| 684 |
if (platform != .glfw) continue; |
| 685 |
exe_mod.addIncludePath(b.path("src")); |
| 686 |
exe_mod.addIncludePath(b.path("src/external/glfw/include")); |
| 687 |
} |
| 688 |
if (std.mem.eql(u8, filename, "raylib_opengl_interop")) { |
| 689 |
if (platform == .drm) continue; |
| 690 |
if (target.result.os.tag == .macos) continue; |
| 691 |
exe_mod.addIncludePath(b.path("src/external")); |
| 692 |
} |
| 693 |
|
| 694 |
const run_step = b.step(filename, filename); |
| 695 |
|
| 696 |
// web exports are completely separate |
| 697 |
if (target.query.os_tag == .emscripten) { |
| 698 |
exe_mod.addCMacro("PLATFORM_WEB", ""); |
| 699 |
|
| 700 |
const wasm = b.addLibrary(.{ |
| 701 |
.name = filename, |
| 702 |
.root_module = exe_mod, |
| 703 |
}); |
| 704 |
|
| 705 |
const install_dir: std.Build.InstallDir = .{ .custom = b.fmt("web/{s}/{s}", .{ module, filename }) }; |
| 706 |
const emcc_flags = emsdk.emccDefaultFlags(b.allocator, .{ .optimize = optimize }); |
| 707 |
const emcc_settings = emsdk.emccDefaultSettings(b.allocator, .{ .optimize = optimize }); |
| 708 |
|
| 709 |
const emcc_step = emsdk.emccStep(b, raylib, wasm, .{ |
| 710 |
.optimize = optimize, |
| 711 |
.flags = emcc_flags, |
| 712 |
.settings = emcc_settings, |
| 713 |
.shell_file_path = b.path("src/shell.html"), |
| 714 |
.install_dir = install_dir, |
| 715 |
}); |
| 716 |
b.getInstallStep().dependOn(emcc_step); |
| 717 |
|
| 718 |
const html_filename = try std.fmt.allocPrint(b.allocator, "{s}.html", .{wasm.name}); |
| 719 |
const emrun_step = emsdk.emrunStep( |
| 720 |
b, |
| 721 |
b.getInstallPath(install_dir, html_filename), |
| 722 |
&.{}, |
| 723 |
); |
| 724 |
|
| 725 |
emrun_step.dependOn(emcc_step); |
| 726 |
run_step.dependOn(emrun_step); |
| 727 |
all.dependOn(emcc_step); |
| 728 |
} else { |
| 729 |
exe_mod.addCMacro("PLATFORM_DESKTOP", ""); |
| 730 |
|
| 731 |
const exe = b.addExecutable(.{ |
| 732 |
.name = filename, |
| 733 |
.root_module = exe_mod, |
| 734 |
.use_lld = target.result.os.tag == .windows, |
| 735 |
}); |
| 736 |
b.installArtifact(exe); |
| 737 |
|
| 738 |
const install_cmd = b.addInstallArtifact(exe, .{ .dest_sub_path = b.fmt("{s}/{s}", .{ module, filename }) }); |
| 739 |
|
| 740 |
const run_cmd = b.addRunArtifact(exe); |
| 741 |
run_cmd.cwd = b.path(module_subpath); |
| 742 |
run_cmd.step.dependOn(&install_cmd.step); |
| 743 |
|
| 744 |
run_step.dependOn(&run_cmd.step); |
| 745 |
all.dependOn(&install_cmd.step); |
| 746 |
} |
| 747 |
} |
| 748 |
return all; |
| 749 |
} |
| 750 |
|
| 751 |
fn waylandGenerate( |
| 752 |
b: *std.Build, |
| 753 |
raylib: *std.Build.Step.Compile, |
| 754 |
comptime waylandDir: []const u8, |
| 755 |
comptime source: bool, |
| 756 |
) !void { |
| 757 |
const dir = try b.build_root.handle.openDir(b.graph.io, waylandDir, .{ .iterate = true }); |
| 758 |
defer dir.close(b.graph.io); |
| 759 |
|
| 760 |
var iter = dir.iterate(); |
| 761 |
while (try iter.next(b.graph.io)) |entry| { |
| 762 |
if (entry.kind != .file) continue; |
| 763 |
const protocolDir = b.pathJoin(&.{ waylandDir, entry.name }); |
| 764 |
|
| 765 |
const filename = std.fs.path.stem(entry.name); |
| 766 |
|
| 767 |
const clientHeader = b.fmt("{s}-client-protocol.h", .{filename}); |
| 768 |
const client_step = b.addSystemCommand(&.{ "wayland-scanner", "client-header" }); |
| 769 |
client_step.addFileArg(b.path(protocolDir)); |
| 770 |
raylib.root_module.addIncludePath(client_step.addOutputFileArg(clientHeader).dirname()); |
| 771 |
raylib.step.dependOn(&client_step.step); |
| 772 |
|
| 773 |
if (comptime source) { |
| 774 |
const privateCode = b.fmt("{s}-client-protocol-code.c", .{filename}); |
| 775 |
const private_step = b.addSystemCommand(&.{ "wayland-scanner", "private-code" }); |
| 776 |
private_step.addFileArg(b.path(protocolDir)); |
| 777 |
raylib.root_module.addCSourceFile(.{ |
| 778 |
.file = private_step.addOutputFileArg(privateCode), |
| 779 |
.flags = &.{ "-std=c99", "-O2" }, |
| 780 |
}); |
| 781 |
raylib.step.dependOn(&private_step.step); |
| 782 |
} else { |
| 783 |
const privateCodeHeader = b.fmt("{s}-client-protocol-code.h", .{filename}); |
| 784 |
const private_head_step = b.addSystemCommand(&.{ "wayland-scanner", "private-code" }); |
| 785 |
private_head_step.addFileArg(b.path(protocolDir)); |
| 786 |
raylib.root_module.addIncludePath(private_head_step.addOutputFileArg(privateCodeHeader).dirname()); |
| 787 |
raylib.step.dependOn(&private_head_step.step); |
| 788 |
} |
| 789 |
} |
| 790 |
} |