-
Notifications
You must be signed in to change notification settings - Fork 2
/
raylib.build.zig
461 lines (401 loc) · 20.7 KB
/
raylib.build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
const std = @import("std");
const builtin = @import("builtin");
const LazyPath = std.Build.LazyPath;
pub const RCameraConfiguration = struct {
// Generates the implementation of the library into the included file.
// If not defined, the library is in header only mode and can be included in other headers
// or source files without problems. But only ONE file should hold the implementation.
rcamera_implementation: ?bool = null,
// If defined, the library can be used as standalone as a camera system but some
// functions must be redefined to manage inputs accordingly.
rcamera_standalone: ?bool = null,
};
pub const GraphicsApi = enum(usize) {
OPENGL_11 = 1,
OPENGL_21,
OPENGL_33,
OPENGL_43,
OPENGL_ES2,
OPENGL_ES3,
};
pub const RlglConfiguration = struct {
// Use selected OpenGL graphics backend, should be supported by platform
// Those preprocessor defines are only used on rlgl module, if OpenGL version is
// required by any other module, use rlGetVersion() to check it
graphics_api: GraphicsApi = .OPENGL_33,
// Generates the implementation of the library into the included file.
// If not defined, the library is in header only mode and can be included in other headers
// or source files without problems. But only ONE file should hold the implementation.
rlgl_implementation: ?bool = null,
// Enable framebuffer objects (fbo) support (enabled by default)
// Some GPUs could not support them despite the OpenGL version
rlgl_render_textures_hint: ?bool = null,
// Show OpenGL extensions and capabilities detailed logs on init
rlgl_show_gl_details_info: ?bool = null,
// Enable debug context (only available on OpenGL 4.3)
rlgl_enable_opengl_debug_context: ?bool = null,
// Default internal render batch elements limits
rl_default_batch_buffer_elements: ?[]const u8 = null,
// Default number of batch buffers (multi-buffering)
rl_default_batch_buffers: ?[]const u8 = null,
// Default number of batch draw calls (by state changes: mode
rl_default_batch_drawcalls: ?[]const u8 = null,
// Maximum number of textures units that can be activated on
rl_default_batch_max_texture_units: ?[]const u8 = null,
// Maximum size of internal Matrix stack
rl_max_matrix_stack_size: ?[]const u8 = null,
// Maximum number of shader locations supported
rl_max_shader_locations: ?[]const u8 = null,
// Default projection matrix near cull distance
rl_cull_distance_near: ?[]const u8 = null,
// Default projection matrix far cull distance
rl_cull_distance_far: ?[]const u8 = null,
};
pub const RayguiConfiguration = struct {
// Generates the implementation of the library into the included file.
// If not defined, the library is in header only mode and can be included in other headers
// or source files without problems. But only ONE file should hold the implementation.
raygui_implementation: ?bool = true,
// Avoid raylib.h header inclusion in this file. Data types defined on raylib are defined
// internally in the library and input management and drawing functions must be provided by
// the user (check library implementation for further details).
raygui_standalone: ?bool = null,
// Avoid including embedded ricons data (256 icons, 16x16 pixels, 1-bit per pixel, 2KB)
raygui_no_icons: ?bool = null,
// Includes custom ricons.h header defining a set of custom icons,
// this file can be generated using rGuiIcons tool
raygui_custom_icons: ?bool = null,
// Draw control bounds rectangles for debug
raygui_debug_recs_bounds: ?bool = null,
// Draw text bounds rectangles for debug
raygui_debug_text_bounds: ?bool = null,
};
// Default c files to add during init
pub const RaylibIncludes = struct {
rcore: bool = true,
utils: bool = true,
raudio: bool = true,
rmodels: bool = true,
rshapes: bool = true,
rtext: bool = true,
rtextures: bool = true,
};
// Linux specific options when linking libraries
pub const LinuxPlatform = enum { DESKTOP, DRM };
pub const LinuxBackend = enum { X11, WAYLAND };
pub const LinuxOptions = struct {
platform: LinuxPlatform = LinuxPlatform.DESKTOP,
// On platform DRM, backend field is not used.
backend: LinuxBackend = LinuxBackend.X11,
};
pub const RaylibSetup = struct {
raylibSrcPath: LazyPath,
flags: std.ArrayList([]const u8),
files: std.ArrayList([]const u8),
rcamera: ?RCameraConfiguration = null,
rlgl: ?RlglConfiguration = null,
raygui: ?RayguiConfiguration = null,
// Initialize RaylibSetup with specified options.
pub fn init(b: *std.Build, raylibSrcPath: LazyPath, includes: RaylibIncludes) !RaylibSetup {
var flags = std.ArrayList([]const u8).init(b.allocator);
var files = std.ArrayList([]const u8).init(b.allocator);
try flags.append("-std=gnu99");
try flags.append("-D_GNU_SOURCE");
try flags.append("-DGL_SILENCE_DEPRECATION=199309L");
if (includes.rcore) try files.append("rcore.c");
if (includes.utils) try files.append("utils.c");
if (includes.raudio) try files.append("raudio.c");
if (includes.rmodels) try files.append("rmodels.c");
if (includes.rshapes) try files.append("rshapes.c");
if (includes.rtext) try files.append("rtext.c");
if (includes.rtextures) try files.append("rtextures.c");
return RaylibSetup{
.raylibSrcPath = raylibSrcPath,
.flags = flags,
.files = files,
};
}
// Move raygui file to raylib's directory and include the file to the c files list.
pub fn addRayguiToRaylibSrc(
self: *RaylibSetup,
b: *std.Build,
rayguiFile: LazyPath,
) !void {
const cRaygui = b.pathJoin(&[_][]const u8{ self.raylibSrcPath.getPath(b), "raygui.c" });
try std.fs.copyFileAbsolute(rayguiFile.getPath(b), cRaygui, .{});
try self.files.append("raygui.c");
}
// Update `step` to build for windows.
pub fn linkWindows(
self: *RaylibSetup,
b: *std.Build,
step: *std.Build.Step.Compile,
) !void {
const glfwInclude = b.pathJoin(&[_][]const u8{ self.raylibSrcPath.getPath(b), "external", "glfw", "include" });
step.addIncludePath(.{ .cwd_relative = glfwInclude });
try self.files.append("rglfw.c");
step.linkSystemLibrary("winmm");
step.linkSystemLibrary("gdi32");
step.linkSystemLibrary("opengl32");
step.defineCMacro("PLATFORM_DESKTOP", null);
}
// Update `step` to build for macos.
pub fn linkMacos(
self: *RaylibSetup,
b: *std.Build,
step: *std.Build.Step.Compile,
) !void {
const glfwInclude = b.pathJoin(&[_][]const u8{ self.raylibSrcPath.getPath(b), "external", "glfw", "include" });
step.addIncludePath(.{ .cwd_relative = glfwInclude });
// On macos rglfw.c include Objective-C files.
try self.flags.append("-ObjC");
const cRglfw = b.pathJoin(&[_][]const u8{ self.raylibSrcPath.getPath(b), "rglfw.c" });
step.root_module.addCSourceFile(.{
.file = .{ .cwd_relative = cRglfw },
.flags = self.flags.items,
});
_ = self.flags.pop();
step.linkFramework("Foundation");
step.linkFramework("CoreServices");
step.linkFramework("CoreGraphics");
step.linkFramework("AppKit");
step.linkFramework("IOKit");
step.defineCMacro("PLATFORM_DESKTOP", null);
}
// Update `step` to build for linux.
// If you set the option to build with platform DRM,
// it will use setRlglOptions with opengl_es2 and link to glwfv2.
pub fn linkLinux(
self: *RaylibSetup,
b: *std.Build,
step: *std.Build.Step.Compile,
options: LinuxOptions,
) !void {
switch (options.platform) {
.DESKTOP => {
step.defineCMacro("PLATFORM_DESKTOP", null);
try self.files.append("rglfw.c");
step.linkSystemLibrary("GL");
step.linkSystemLibrary("rt");
step.linkSystemLibrary("dl");
step.linkSystemLibrary("m");
const glfwInclude = b.pathJoin(&[_][]const u8{ self.raylibSrcPath.getPath(b), "external", "glfw", "include" });
step.addIncludePath(.{ .cwd_relative = glfwInclude });
step.addIncludePath(.{ .cwd_relative = "/usr/include" });
step.addLibraryPath(.{ .cwd_relative = "/usr/lib" });
switch (options.backend) {
.X11 => {
step.defineCMacro("_GLFW_X11", null);
step.linkSystemLibrary("X11");
},
.WAYLAND => {
step.defineCMacro("_GLFW_WAYLAND", null);
step.linkSystemLibrary("wayland-client");
step.linkSystemLibrary("wayland-cursor");
step.linkSystemLibrary("wayland-egl");
step.linkSystemLibrary("xkbcommon");
waylandGenerate(self, b, step, "wayland.xml", "wayland-client-protocol");
waylandGenerate(self, b, step, "xdg-shell.xml", "xdg-shell-client-protocol");
waylandGenerate(self, b, step, "xdg-decoration-unstable-v1.xml", "xdg-decoration-unstable-v1-client-protocol");
waylandGenerate(self, b, step, "viewporter.xml", "viewporter-client-protocol");
waylandGenerate(self, b, step, "relative-pointer-unstable-v1.xml", "relative-pointer-unstable-v1-client-protocol");
waylandGenerate(self, b, step, "pointer-constraints-unstable-v1.xml", "pointer-constraints-unstable-v1-client-protocol");
waylandGenerate(self, b, step, "fractional-scale-v1.xml", "fractional-scale-v1-client-protocol");
waylandGenerate(self, b, step, "xdg-activation-v1.xml", "xdg-activation-v1-client-protocol");
waylandGenerate(self, b, step, "idle-inhibit-unstable-v1.xml", "idle-inhibit-unstable-v1-client-protocol");
},
}
},
.DRM => {
step.linkSystemLibrary("EGL");
step.linkSystemLibrary("drm");
step.linkSystemLibrary("gbm");
step.linkSystemLibrary("pthread");
step.linkSystemLibrary("rt");
step.linkSystemLibrary("m");
step.linkSystemLibrary("dl");
step.addIncludePath(.{ .cwd_relative = "/usr/include/libdrm" });
step.defineCMacro("PLATFORM_DRM", null);
step.defineCMacro("EGL_NO_X11", null);
// If opengl_es2 is selected, we link against it.
if (self.rlgl) |opts| {
if (opts.graphics_api == .OPENGL_ES2) {
step.linkSystemLibrary("GLESv2");
}
} else {
setRlglOptions(self, b, step, .{
.graphics_api = .OPENGL_ES2,
.rl_default_batch_buffer_elements = "2048",
});
step.linkSystemLibrary("GLESv2");
}
},
}
}
// Commands to generate wayland bindings.
fn waylandGenerate(
self: *RaylibSetup,
b: *std.Build,
step: *std.Build.Step.Compile,
comptime protocol: []const u8,
comptime basename: []const u8,
) void {
const waylandDir = b.pathJoin(&[_][]const u8{ self.raylibSrcPath.getPath(b), "external", "glfw", "deps", "wayland" });
const protocolDir = b.pathJoin(&[_][]const u8{ waylandDir, protocol });
const clientHeader = basename ++ ".h";
const privateCode = basename ++ "-code.h";
const client_step = b.addSystemCommand(&.{ "wayland-scanner", "client-header" });
client_step.addFileArg(.{ .cwd_relative = protocolDir });
step.addIncludePath(client_step.addOutputFileArg(clientHeader).dirname());
const private_step = b.addSystemCommand(&.{ "wayland-scanner", "private-code" });
private_step.addFileArg(.{ .cwd_relative = protocolDir });
step.addIncludePath(private_step.addOutputFileArg(privateCode).dirname());
step.step.dependOn(&client_step.step);
step.step.dependOn(&private_step.step);
}
// Options
// C macros and build options for rcamera
pub fn setRCameraOptions(
self: *RaylibSetup,
build: *std.Build,
step: *std.Build.Step.Compile,
conf: RCameraConfiguration,
) void {
self.rcamera = conf;
const buildOpts = build.addOptions();
if (conf.rcamera_implementation) |implementation| {
buildOpts.addOption(@TypeOf(implementation), "implementation", implementation);
if (implementation) step.defineCMacro("RCAMERA_IMPLEMENTATION", null) else step.forceUndefinedSymbol("RCAMERA_IMPLEMENTATION");
}
if (conf.rcamera_standalone) |standalone| {
buildOpts.addOption(@TypeOf(standalone), "standalone", standalone);
if (standalone) step.defineCMacro("RCAMERA_STANDALONE", null) else step.forceUndefinedSymbol("RCAMERA_STANDALONE");
}
step.root_module.addOptions("raygui_options", buildOpts);
}
// C macros and build options for rlgl
pub fn setRlglOptions(
self: *RaylibSetup,
build: *std.Build,
step: *std.Build.Step.Compile,
conf: RlglConfiguration,
) void {
self.rlgl = conf;
const buildOpts = build.addOptions();
buildOpts.addOption(usize, "graphics_api", @intFromEnum(conf.graphics_api));
switch (conf.graphics_api) {
GraphicsApi.OPENGL_11 => step.defineCMacro("GRAPHICS_API_OPENGL_11", null),
GraphicsApi.OPENGL_21 => step.defineCMacro("GRAPHICS_API_OPENGL_21", null),
GraphicsApi.OPENGL_33 => step.defineCMacro("GRAPHICS_API_OPENGL_33", null),
GraphicsApi.OPENGL_43 => step.defineCMacro("GRAPHICS_API_OPENGL_43", null),
GraphicsApi.OPENGL_ES2 => step.defineCMacro("GRAPHICS_API_OPENGL_ES2", null),
GraphicsApi.OPENGL_ES3 => step.defineCMacro("GRAPHICS_API_OPENGL_ES3", null),
}
if (conf.rlgl_implementation) |implementation| {
buildOpts.addOption(@TypeOf(implementation), "implementation", implementation);
if (implementation) step.defineCMacro("RLGL_IMPLEMENTATION", null) else step.forceUndefinedSymbol("RLGL_IMPLEMENTATION");
}
if (conf.rlgl_render_textures_hint) |render_textures_hint| {
buildOpts.addOption(@TypeOf(render_textures_hint), "render_textures_hint", render_textures_hint);
if (render_textures_hint) step.defineCMacro("RLGL_RENDER_TEXTURES_HINT", null) else step.forceUndefinedSymbol("RLGL_RENDER_TEXTURES_HINT");
}
if (conf.rlgl_show_gl_details_info) |show_gl_details_info| {
buildOpts.addOption(@TypeOf(show_gl_details_info), "show_gl_details_info", show_gl_details_info);
if (show_gl_details_info) step.defineCMacro("RLGL_SHOW_GL_DETAILS_INFO", null) else step.forceUndefinedSymbol("RLGL_SHOW_GL_DETAILS_INFO");
}
if (conf.rlgl_enable_opengl_debug_context) |enable_opengl_debug_context| {
buildOpts.addOption(@TypeOf(enable_opengl_debug_context), "enable_opengl_debug_context", enable_opengl_debug_context);
if (enable_opengl_debug_context) step.defineCMacro("RLGL_ENABLE_OPENGL_DEBUG_CONTEXT", null) else step.forceUndefinedSymbol("RLGL_ENABLE_OPENGL_DEBUG_CONTEXT");
}
if (conf.rl_default_batch_buffer_elements) |default_batch_buffer_elements| {
buildOpts.addOption(@TypeOf(default_batch_buffer_elements), "default_batch_buffer_elements", default_batch_buffer_elements);
step.defineCMacro("RL_DEFAULT_BATCH_BUFFER_ELEMENTS", default_batch_buffer_elements);
}
if (conf.rl_default_batch_buffers) |default_batch_buffers| {
buildOpts.addOption(@TypeOf(default_batch_buffers), "default_batch_buffers", default_batch_buffers);
step.defineCMacro("RL_DEFAULT_BATCH_BUFFERS", default_batch_buffers);
}
if (conf.rl_default_batch_drawcalls) |default_batch_drawcalls| {
buildOpts.addOption(@TypeOf(default_batch_drawcalls), "default_batch_drawcalls", default_batch_drawcalls);
step.defineCMacro("RL_DEFAULT_BATCH_DRAWCALLS", default_batch_drawcalls);
}
if (conf.rl_default_batch_max_texture_units) |default_batch_max_texture_units| {
buildOpts.addOption(@TypeOf(default_batch_max_texture_units), "default_batch_max_texture_units", default_batch_max_texture_units);
step.defineCMacro("RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS", default_batch_max_texture_units);
}
if (conf.rl_max_matrix_stack_size) |max_matrix_stack_size| {
buildOpts.addOption(@TypeOf(max_matrix_stack_size), "max_matrix_stack_size", max_matrix_stack_size);
step.defineCMacro("RL_MAX_MATRIX_STACK_SIZE", max_matrix_stack_size);
}
if (conf.rl_max_shader_locations) |max_shader_locations| {
buildOpts.addOption(@TypeOf(max_shader_locations), "max_shader_locations", max_shader_locations);
step.defineCMacro("RL_MAX_SHADER_LOCATIONS", max_shader_locations);
}
if (conf.rl_cull_distance_near) |cull_distance_near| {
buildOpts.addOption(@TypeOf(cull_distance_near), "cull_distance_near", cull_distance_near);
step.defineCMacro("RL_CULL_DISTANCE_NEAR", cull_distance_near);
}
if (conf.rl_cull_distance_far) |cull_distance_far| {
buildOpts.addOption(@TypeOf(cull_distance_far), "cull_distance_far", cull_distance_far);
step.defineCMacro("RL_CULL_DISTANCE_FAR", cull_distance_far);
}
step.root_module.addOptions("rlgl_options", buildOpts);
}
// C macros and build options for raygui
pub fn setRayguiOptions(
self: *RaylibSetup,
build: *std.Build,
step: *std.Build.Step.Compile,
conf: RayguiConfiguration,
) void {
self.raygui = conf;
const buildOpts = build.addOptions();
if (conf.raygui_implementation) |implementation| {
buildOpts.addOption(@TypeOf(implementation), "implementation", implementation);
if (implementation) step.defineCMacro("RAYGUI_IMPLEMENTATION", null) else step.forceUndefinedSymbol("RAYGUI_IMPLEMENTATION");
}
if (conf.raygui_standalone) |standalone| {
buildOpts.addOption(@TypeOf(standalone), "standalone", standalone);
if (standalone) step.defineCMacro("RAYGUI_STANDALONE", null) else step.forceUndefinedSymbol("RAYGUI_STANDALONE");
}
if (conf.raygui_no_icons) |no_icons| {
buildOpts.addOption(@TypeOf(no_icons), "no_icons", no_icons);
if (no_icons) step.defineCMacro("RAYGUI_NO_ICONS", null) else step.forceUndefinedSymbol("RAYGUI_NO_ICONS");
}
if (conf.raygui_custom_icons) |custom_icons| {
buildOpts.addOption(@TypeOf(custom_icons), "custom_icons", custom_icons);
if (custom_icons) step.defineCMacro("RAYGUI_CUSTOM_ICONS", null) else step.forceUndefinedSymbol("RAYGUI_CUSTOM_ICONS");
}
if (conf.raygui_debug_recs_bounds) |debug_recs_bounds| {
buildOpts.addOption(@TypeOf(debug_recs_bounds), "debug_recs_bounds", debug_recs_bounds);
if (debug_recs_bounds) step.defineCMacro("RAYGUI_DEBUG_RECS_BOUNDS", null) else step.forceUndefinedSymbol("RAYGUI_DEBUG_RECS_BOUNDS");
}
if (conf.raygui_debug_text_bounds) |debug_text_bounds| {
buildOpts.addOption(@TypeOf(debug_text_bounds), "debug_text_bounds", debug_text_bounds);
if (debug_text_bounds) step.defineCMacro("RAYGUI_DEBUG_TEXT_BOUNDS", null) else step.forceUndefinedSymbol("RAYGUI_DEBUG_TEXT_BOUNDS");
}
step.root_module.addOptions("raygui_options", buildOpts);
}
pub fn finalize(
self: *RaylibSetup,
build: *std.Build,
step: *std.Build.Step.Compile,
) void {
// Set unsetted options
if (self.rlgl == null) setRlglOptions(self, build, step, .{});
if (self.rcamera == null) setRCameraOptions(self, build, step, .{});
if (self.raygui == null) setRayguiOptions(self, build, step, .{});
// libc is required to work with raylib c files.
if (!step.is_linking_libc) step.linkLibC();
// Add raylib sources to the step's module.
step.root_module.addCSourceFiles(.{
.root = self.raylibSrcPath,
.files = self.files.items,
.flags = self.flags.items,
});
}
pub fn deinit(self: *RaylibSetup) void {
self.flags.deinit();
self.files.deinit();
}
};