Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

432 wiersze
18 KiB

  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. /// Minimum supported version of Zig
  4. const min_ver = "0.13.0";
  5. comptime {
  6. const order = std.SemanticVersion.order;
  7. const parse = std.SemanticVersion.parse;
  8. if (order(builtin.zig_version, parse(min_ver) catch unreachable) == .lt)
  9. @compileError("Raylib requires zig version " ++ min_ver);
  10. }
  11. fn setDesktopPlatform(raylib: *std.Build.Step.Compile, platform: PlatformBackend) void {
  12. switch (platform) {
  13. .glfw => raylib.defineCMacro("PLATFORM_DESKTOP_GLFW", null),
  14. .rgfw => raylib.defineCMacro("PLATFORM_DESKTOP_RGFW", null),
  15. .sdl => raylib.defineCMacro("PLATFORM_DESKTOP_SDL", null),
  16. else => {},
  17. }
  18. }
  19. fn createEmsdkStep(b: *std.Build, emsdk: *std.Build.Dependency) *std.Build.Step.Run {
  20. if (builtin.os.tag == .windows) {
  21. return b.addSystemCommand(&.{emsdk.path("emsdk.bat").getPath(b)});
  22. } else {
  23. return b.addSystemCommand(&.{emsdk.path("emsdk").getPath(b)});
  24. }
  25. }
  26. fn emSdkSetupStep(b: *std.Build, emsdk: *std.Build.Dependency) !?*std.Build.Step.Run {
  27. const dot_emsc_path = emsdk.path(".emscripten").getPath(b);
  28. const dot_emsc_exists = !std.meta.isError(std.fs.accessAbsolute(dot_emsc_path, .{}));
  29. if (!dot_emsc_exists) {
  30. const emsdk_install = createEmsdkStep(b, emsdk);
  31. emsdk_install.addArgs(&.{ "install", "latest" });
  32. const emsdk_activate = createEmsdkStep(b, emsdk);
  33. emsdk_activate.addArgs(&.{ "activate", "latest" });
  34. emsdk_activate.step.dependOn(&emsdk_install.step);
  35. return emsdk_activate;
  36. } else {
  37. return null;
  38. }
  39. }
  40. /// A list of all flags from `src/config.h` that one may override
  41. const config_h_flags = outer: {
  42. // Set this value higher if compile errors happen as `src/config.h` gets larger
  43. @setEvalBranchQuota(1 << 20);
  44. const config_h = @embedFile("src/config.h");
  45. var flags: [std.mem.count(u8, config_h, "\n") + 1][]const u8 = undefined;
  46. var i = 0;
  47. var lines = std.mem.tokenizeScalar(u8, config_h, '\n');
  48. while (lines.next()) |line| {
  49. if (!std.mem.containsAtLeast(u8, line, 1, "SUPPORT")) continue;
  50. if (std.mem.containsAtLeast(u8, line, 1, "MODULE")) continue;
  51. if (std.mem.startsWith(u8, line, "//")) continue;
  52. if (std.mem.startsWith(u8, line, "#if")) continue;
  53. var flag = std.mem.trimLeft(u8, line, " \t"); // Trim whitespace
  54. flag = flag["#define ".len - 1 ..]; // Remove #define
  55. flag = std.mem.trimLeft(u8, flag, " \t"); // Trim whitespace
  56. flag = flag[0 .. std.mem.indexOf(u8, flag, " ") orelse continue]; // Flag is only one word, so capture till space
  57. flag = "-D" ++ flag; // Prepend with -D
  58. flags[i] = flag;
  59. i += 1;
  60. }
  61. // Uncomment this to check what flags normally get passed
  62. //@compileLog(flags[0..i].*);
  63. break :outer flags[0..i].*;
  64. };
  65. fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
  66. var raylib_flags_arr = std.ArrayList([]const u8).init(b.allocator);
  67. defer raylib_flags_arr.deinit();
  68. try raylib_flags_arr.appendSlice(&[_][]const u8{
  69. "-std=gnu99",
  70. "-D_GNU_SOURCE",
  71. "-DGL_SILENCE_DEPRECATION=199309L",
  72. "-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
  73. });
  74. if (options.shared) {
  75. try raylib_flags_arr.appendSlice(&[_][]const u8{
  76. "-fPIC",
  77. "-DBUILD_LIBTYPE_SHARED",
  78. });
  79. }
  80. // Sets a flag indiciating the use of a custom `config.h`
  81. try raylib_flags_arr.append("-DEXTERNAL_CONFIG_FLAGS");
  82. if (options.config.len > 0) {
  83. // Splits a space-separated list of config flags into multiple flags
  84. //
  85. // Note: This means certain flags like `-x c++` won't be processed properly.
  86. // `-xc++` or similar should be used when possible
  87. var config_iter = std.mem.tokenizeScalar(u8, options.config, ' ');
  88. // Apply config flags supplied by the user
  89. while (config_iter.next()) |config_flag|
  90. try raylib_flags_arr.append(config_flag);
  91. // Apply all relevant configs from `src/config.h` *except* the user-specified ones
  92. //
  93. // Note: Currently using a suboptimal `O(m*n)` time algorithm where:
  94. // `m` corresponds roughly to the number of lines in `src/config.h`
  95. // `n` corresponds to the number of user-specified flags
  96. outer: for (config_h_flags) |flag| {
  97. // If a user already specified the flag, skip it
  98. config_iter.reset();
  99. while (config_iter.next()) |config_flag| {
  100. // For a user-specified flag to match, it must share the same prefix and have the
  101. // same length or be followed by an equals sign
  102. if (!std.mem.startsWith(u8, config_flag, flag)) continue;
  103. if (config_flag.len == flag.len or config_flag[flag.len] == '=') continue :outer;
  104. }
  105. // Otherwise, append default value from config.h to compile flags
  106. try raylib_flags_arr.append(flag);
  107. }
  108. } else {
  109. // Set default config if no custome config got set
  110. try raylib_flags_arr.appendSlice(&config_h_flags);
  111. }
  112. const raylib = if (options.shared)
  113. b.addSharedLibrary(.{
  114. .name = "raylib",
  115. .target = target,
  116. .optimize = optimize,
  117. })
  118. else
  119. b.addStaticLibrary(.{
  120. .name = "raylib",
  121. .target = target,
  122. .optimize = optimize,
  123. });
  124. raylib.linkLibC();
  125. // No GLFW required on PLATFORM_DRM
  126. if (options.platform != .drm) {
  127. raylib.addIncludePath(b.path("src/external/glfw/include"));
  128. }
  129. var c_source_files = try std.ArrayList([]const u8).initCapacity(b.allocator, 2);
  130. c_source_files.appendSliceAssumeCapacity(&.{ "src/rcore.c", "src/utils.c" });
  131. if (options.rshapes) {
  132. try c_source_files.append("src/rshapes.c");
  133. try raylib_flags_arr.append("-DSUPPORT_MODULE_RSHAPES");
  134. }
  135. if (options.rtextures) {
  136. try c_source_files.append("src/rtextures.c");
  137. try raylib_flags_arr.append("-DSUPPORT_MODULE_RTEXTURES");
  138. }
  139. if (options.rtext) {
  140. try c_source_files.append("src/rtext.c");
  141. try raylib_flags_arr.append("-DSUPPORT_MODULE_RTEXT");
  142. }
  143. if (options.rmodels) {
  144. try c_source_files.append("src/rmodels.c");
  145. try raylib_flags_arr.append("-DSUPPORT_MODULE_RMODELS");
  146. }
  147. if (options.raudio) {
  148. try c_source_files.append("src/raudio.c");
  149. try raylib_flags_arr.append("-DSUPPORT_MODULE_RAUDIO");
  150. }
  151. if (options.opengl_version != .auto) {
  152. raylib.defineCMacro(options.opengl_version.toCMacroStr(), null);
  153. }
  154. raylib.addIncludePath(b.path("src/platforms"));
  155. switch (target.result.os.tag) {
  156. .windows => {
  157. try c_source_files.append("src/rglfw.c");
  158. raylib.linkSystemLibrary("winmm");
  159. raylib.linkSystemLibrary("gdi32");
  160. raylib.linkSystemLibrary("opengl32");
  161. setDesktopPlatform(raylib, options.platform);
  162. },
  163. .linux => {
  164. if (options.platform != .drm) {
  165. try c_source_files.append("src/rglfw.c");
  166. if (options.linux_display_backend == .X11 or options.linux_display_backend == .Both) {
  167. raylib.defineCMacro("_GLFW_X11", null);
  168. raylib.linkSystemLibrary("GLX");
  169. raylib.linkSystemLibrary("X11");
  170. raylib.linkSystemLibrary("Xcursor");
  171. raylib.linkSystemLibrary("Xext");
  172. raylib.linkSystemLibrary("Xfixes");
  173. raylib.linkSystemLibrary("Xi");
  174. raylib.linkSystemLibrary("Xinerama");
  175. raylib.linkSystemLibrary("Xrandr");
  176. raylib.linkSystemLibrary("Xrender");
  177. }
  178. if (options.linux_display_backend == .Wayland or options.linux_display_backend == .Both) {
  179. _ = b.findProgram(&.{"wayland-scanner"}, &.{}) catch {
  180. std.log.err(
  181. \\ `wayland-scanner` may not be installed on the system.
  182. \\ You can switch to X11 in your `build.zig` by changing `Options.linux_display_backend`
  183. , .{});
  184. @panic("`wayland-scanner` not found");
  185. };
  186. raylib.defineCMacro("_GLFW_WAYLAND", null);
  187. raylib.linkSystemLibrary("EGL");
  188. raylib.linkSystemLibrary("wayland-client");
  189. raylib.linkSystemLibrary("xkbcommon");
  190. waylandGenerate(b, raylib, "wayland.xml", "wayland-client-protocol");
  191. waylandGenerate(b, raylib, "xdg-shell.xml", "xdg-shell-client-protocol");
  192. waylandGenerate(b, raylib, "xdg-decoration-unstable-v1.xml", "xdg-decoration-unstable-v1-client-protocol");
  193. waylandGenerate(b, raylib, "viewporter.xml", "viewporter-client-protocol");
  194. waylandGenerate(b, raylib, "relative-pointer-unstable-v1.xml", "relative-pointer-unstable-v1-client-protocol");
  195. waylandGenerate(b, raylib, "pointer-constraints-unstable-v1.xml", "pointer-constraints-unstable-v1-client-protocol");
  196. waylandGenerate(b, raylib, "fractional-scale-v1.xml", "fractional-scale-v1-client-protocol");
  197. waylandGenerate(b, raylib, "xdg-activation-v1.xml", "xdg-activation-v1-client-protocol");
  198. waylandGenerate(b, raylib, "idle-inhibit-unstable-v1.xml", "idle-inhibit-unstable-v1-client-protocol");
  199. }
  200. setDesktopPlatform(raylib, options.platform);
  201. } else {
  202. if (options.opengl_version == .auto) {
  203. raylib.linkSystemLibrary("GLESv2");
  204. raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
  205. }
  206. raylib.linkSystemLibrary("EGL");
  207. raylib.linkSystemLibrary("gbm");
  208. raylib.linkSystemLibrary2("libdrm", .{ .use_pkg_config = .force });
  209. raylib.defineCMacro("PLATFORM_DRM", null);
  210. raylib.defineCMacro("EGL_NO_X11", null);
  211. raylib.defineCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", "2048");
  212. }
  213. },
  214. .freebsd, .openbsd, .netbsd, .dragonfly => {
  215. try c_source_files.append("rglfw.c");
  216. raylib.linkSystemLibrary("GL");
  217. raylib.linkSystemLibrary("rt");
  218. raylib.linkSystemLibrary("dl");
  219. raylib.linkSystemLibrary("m");
  220. raylib.linkSystemLibrary("X11");
  221. raylib.linkSystemLibrary("Xrandr");
  222. raylib.linkSystemLibrary("Xinerama");
  223. raylib.linkSystemLibrary("Xi");
  224. raylib.linkSystemLibrary("Xxf86vm");
  225. raylib.linkSystemLibrary("Xcursor");
  226. setDesktopPlatform(raylib, options.platform);
  227. },
  228. .macos => {
  229. // Include xcode_frameworks for cross compilation
  230. if (b.lazyDependency("xcode_frameworks", .{})) |dep| {
  231. raylib.addSystemFrameworkPath(dep.path("Frameworks"));
  232. raylib.addSystemIncludePath(dep.path("include"));
  233. raylib.addLibraryPath(dep.path("lib"));
  234. }
  235. // On macos rglfw.c include Objective-C files.
  236. try raylib_flags_arr.append("-ObjC");
  237. raylib.root_module.addCSourceFile(.{
  238. .file = b.path("src/rglfw.c"),
  239. .flags = raylib_flags_arr.items,
  240. });
  241. _ = raylib_flags_arr.pop();
  242. raylib.linkFramework("Foundation");
  243. raylib.linkFramework("CoreServices");
  244. raylib.linkFramework("CoreGraphics");
  245. raylib.linkFramework("AppKit");
  246. raylib.linkFramework("IOKit");
  247. setDesktopPlatform(raylib, options.platform);
  248. },
  249. .emscripten => {
  250. // Include emscripten for cross compilation
  251. if (b.lazyDependency("emsdk", .{})) |dep| {
  252. if (try emSdkSetupStep(b, dep)) |emSdkStep| {
  253. raylib.step.dependOn(&emSdkStep.step);
  254. }
  255. raylib.addIncludePath(dep.path("upstream/emscripten/cache/sysroot/include"));
  256. }
  257. raylib.defineCMacro("PLATFORM_WEB", null);
  258. if (options.opengl_version == .auto) {
  259. raylib.defineCMacro("GRAPHICS_API_OPENGL_ES2", null);
  260. }
  261. },
  262. else => {
  263. @panic("Unsupported OS");
  264. },
  265. }
  266. raylib.root_module.addCSourceFiles(.{
  267. .files = c_source_files.items,
  268. .flags = raylib_flags_arr.items,
  269. });
  270. return raylib;
  271. }
  272. pub fn addRaygui(b: *std.Build, raylib: *std.Build.Step.Compile, raygui_dep: *std.Build.Dependency) void {
  273. const raylib_dep = b.dependencyFromBuildZig(@This(), .{});
  274. var gen_step = b.addWriteFiles();
  275. raylib.step.dependOn(&gen_step.step);
  276. const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
  277. raylib.addCSourceFile(.{ .file = raygui_c_path });
  278. raylib.addIncludePath(raygui_dep.path("src"));
  279. raylib.addIncludePath(raylib_dep.path("src"));
  280. raylib.installHeader(raygui_dep.path("src/raygui.h"), "raygui.h");
  281. }
  282. pub const Options = struct {
  283. raudio: bool = true,
  284. rmodels: bool = true,
  285. rshapes: bool = true,
  286. rtext: bool = true,
  287. rtextures: bool = true,
  288. platform: PlatformBackend = .glfw,
  289. shared: bool = false,
  290. linux_display_backend: LinuxDisplayBackend = .Both,
  291. opengl_version: OpenglVersion = .auto,
  292. /// config should be a list of space-separated cflags, eg, "-DSUPPORT_CUSTOM_FRAME_CONTROL"
  293. config: []const u8 = &.{},
  294. const defaults = Options{};
  295. pub fn getOptions(b: *std.Build) Options {
  296. return .{
  297. .platform = b.option(PlatformBackend, "platform", "Choose the platform backedn for desktop target") orelse defaults.platform,
  298. .raudio = b.option(bool, "raudio", "Compile with audio support") orelse defaults.raudio,
  299. .rmodels = b.option(bool, "rmodels", "Compile with models support") orelse defaults.rmodels,
  300. .rtext = b.option(bool, "rtext", "Compile with text support") orelse defaults.rtext,
  301. .rtextures = b.option(bool, "rtextures", "Compile with textures support") orelse defaults.rtextures,
  302. .rshapes = b.option(bool, "rshapes", "Compile with shapes support") orelse defaults.rshapes,
  303. .shared = b.option(bool, "shared", "Compile as shared library") orelse defaults.shared,
  304. .linux_display_backend = b.option(LinuxDisplayBackend, "linux_display_backend", "Linux display backend to use") orelse defaults.linux_display_backend,
  305. .opengl_version = b.option(OpenglVersion, "opengl_version", "OpenGL version to use") orelse defaults.opengl_version,
  306. .config = b.option([]const u8, "config", "Compile with custom define macros overriding config.h") orelse &.{},
  307. };
  308. }
  309. };
  310. pub const OpenglVersion = enum {
  311. auto,
  312. gl_1_1,
  313. gl_2_1,
  314. gl_3_3,
  315. gl_4_3,
  316. gles_2,
  317. gles_3,
  318. pub fn toCMacroStr(self: @This()) []const u8 {
  319. switch (self) {
  320. .auto => @panic("OpenglVersion.auto cannot be turned into a C macro string"),
  321. .gl_1_1 => return "GRAPHICS_API_OPENGL_11",
  322. .gl_2_1 => return "GRAPHICS_API_OPENGL_21",
  323. .gl_3_3 => return "GRAPHICS_API_OPENGL_33",
  324. .gl_4_3 => return "GRAPHICS_API_OPENGL_43",
  325. .gles_2 => return "GRAPHICS_API_OPENGL_ES2",
  326. .gles_3 => return "GRAPHICS_API_OPENGL_ES3",
  327. }
  328. }
  329. };
  330. pub const LinuxDisplayBackend = enum {
  331. X11,
  332. Wayland,
  333. Both,
  334. };
  335. pub const PlatformBackend = enum {
  336. glfw,
  337. rgfw,
  338. sdl,
  339. drm,
  340. };
  341. pub fn build(b: *std.Build) !void {
  342. // Standard target options allows the person running `zig build` to choose
  343. // what target to build for. Here we do not override the defaults, which
  344. // means any target is allowed, and the default is native. Other options
  345. // for restricting supported target set are available.
  346. const target = b.standardTargetOptions(.{});
  347. // Standard optimization options allow the person running `zig build` to select
  348. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
  349. // set a preferred release mode, allowing the user to decide how to optimize.
  350. const optimize = b.standardOptimizeOption(.{});
  351. const lib = try compileRaylib(b, target, optimize, Options.getOptions(b));
  352. lib.installHeader(b.path("src/raylib.h"), "raylib.h");
  353. lib.installHeader(b.path("src/rcamera.h"), "rcamera.h");
  354. lib.installHeader(b.path("src/raymath.h"), "raymath.h");
  355. lib.installHeader(b.path("src/rlgl.h"), "rlgl.h");
  356. b.installArtifact(lib);
  357. }
  358. fn waylandGenerate(
  359. b: *std.Build,
  360. raylib: *std.Build.Step.Compile,
  361. comptime protocol: []const u8,
  362. comptime basename: []const u8,
  363. ) void {
  364. const waylandDir = "src/external/glfw/deps/wayland";
  365. const protocolDir = b.pathJoin(&.{ waylandDir, protocol });
  366. const clientHeader = basename ++ ".h";
  367. const privateCode = basename ++ "-code.h";
  368. const client_step = b.addSystemCommand(&.{ "wayland-scanner", "client-header" });
  369. client_step.addFileArg(b.path(protocolDir));
  370. raylib.addIncludePath(client_step.addOutputFileArg(clientHeader).dirname());
  371. const private_step = b.addSystemCommand(&.{ "wayland-scanner", "private-code" });
  372. private_step.addFileArg(b.path(protocolDir));
  373. raylib.addIncludePath(private_step.addOutputFileArg(privateCode).dirname());
  374. raylib.step.dependOn(&client_step.step);
  375. raylib.step.dependOn(&private_step.step);
  376. }