build.zig 19 KB

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