build.zig 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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.root_module.addCMacro("PLATFORM_DESKTOP_GLFW", ""),
  14. .rgfw => raylib.root_module.addCMacro("PLATFORM_DESKTOP_RGFW", ""),
  15. .sdl => raylib.root_module.addCMacro("PLATFORM_DESKTOP_SDL", ""),
  16. .android => raylib.root_module.addCMacro("PLATFORM_ANDROID", ""),
  17. else => {},
  18. }
  19. }
  20. fn createEmsdkStep(b: *std.Build, emsdk: *std.Build.Dependency) *std.Build.Step.Run {
  21. if (builtin.os.tag == .windows) {
  22. return b.addSystemCommand(&.{emsdk.path("emsdk.bat").getPath(b)});
  23. } else {
  24. return b.addSystemCommand(&.{emsdk.path("emsdk").getPath(b)});
  25. }
  26. }
  27. fn emSdkSetupStep(b: *std.Build, emsdk: *std.Build.Dependency) !?*std.Build.Step.Run {
  28. const dot_emsc_path = emsdk.path(".emscripten").getPath(b);
  29. const dot_emsc_exists = !std.meta.isError(std.fs.accessAbsolute(dot_emsc_path, .{}));
  30. if (!dot_emsc_exists) {
  31. const emsdk_install = createEmsdkStep(b, emsdk);
  32. emsdk_install.addArgs(&.{ "install", "latest" });
  33. const emsdk_activate = createEmsdkStep(b, emsdk);
  34. emsdk_activate.addArgs(&.{ "activate", "latest" });
  35. emsdk_activate.step.dependOn(&emsdk_install.step);
  36. return emsdk_activate;
  37. } else {
  38. return null;
  39. }
  40. }
  41. /// A list of all flags from `src/config.h` that one may override
  42. const config_h_flags = outer: {
  43. // Set this value higher if compile errors happen as `src/config.h` gets larger
  44. @setEvalBranchQuota(1 << 20);
  45. const config_h = @embedFile("src/config.h");
  46. var flags: [std.mem.count(u8, config_h, "\n") + 1][]const u8 = undefined;
  47. var i = 0;
  48. var lines = std.mem.tokenizeScalar(u8, config_h, '\n');
  49. while (lines.next()) |line| {
  50. if (!std.mem.containsAtLeast(u8, line, 1, "SUPPORT")) continue;
  51. if (std.mem.containsAtLeast(u8, line, 1, "MODULE")) continue;
  52. if (std.mem.startsWith(u8, line, "//")) continue;
  53. if (std.mem.startsWith(u8, line, "#if")) continue;
  54. var flag = std.mem.trimLeft(u8, line, " \t"); // Trim whitespace
  55. flag = flag["#define ".len - 1 ..]; // Remove #define
  56. flag = std.mem.trimLeft(u8, flag, " \t"); // Trim whitespace
  57. flag = flag[0 .. std.mem.indexOf(u8, flag, " ") orelse continue]; // Flag is only one word, so capture till space
  58. flag = "-D" ++ flag; // Prepend with -D
  59. flags[i] = flag;
  60. i += 1;
  61. }
  62. // Uncomment this to check what flags normally get passed
  63. //@compileLog(flags[0..i].*);
  64. break :outer flags[0..i].*;
  65. };
  66. fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
  67. var raylib_flags_arr = std.ArrayList([]const u8).init(b.allocator);
  68. defer raylib_flags_arr.deinit();
  69. try raylib_flags_arr.appendSlice(&[_][]const u8{
  70. "-std=gnu99",
  71. "-D_GNU_SOURCE",
  72. "-DGL_SILENCE_DEPRECATION=199309L",
  73. "-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
  74. });
  75. if (options.shared) {
  76. try raylib_flags_arr.appendSlice(&[_][]const u8{
  77. "-fPIC",
  78. "-DBUILD_LIBTYPE_SHARED",
  79. });
  80. }
  81. // Sets a flag indiciating the use of a custom `config.h`
  82. try raylib_flags_arr.append("-DEXTERNAL_CONFIG_FLAGS");
  83. if (options.config.len > 0) {
  84. // Splits a space-separated list of config flags into multiple flags
  85. //
  86. // Note: This means certain flags like `-x c++` won't be processed properly.
  87. // `-xc++` or similar should be used when possible
  88. var config_iter = std.mem.tokenizeScalar(u8, options.config, ' ');
  89. // Apply config flags supplied by the user
  90. while (config_iter.next()) |config_flag|
  91. try raylib_flags_arr.append(config_flag);
  92. // Apply all relevant configs from `src/config.h` *except* the user-specified ones
  93. //
  94. // Note: Currently using a suboptimal `O(m*n)` time algorithm where:
  95. // `m` corresponds roughly to the number of lines in `src/config.h`
  96. // `n` corresponds to the number of user-specified flags
  97. outer: for (config_h_flags) |flag| {
  98. // If a user already specified the flag, skip it
  99. config_iter.reset();
  100. while (config_iter.next()) |config_flag| {
  101. // For a user-specified flag to match, it must share the same prefix and have the
  102. // same length or be followed by an equals sign
  103. if (!std.mem.startsWith(u8, config_flag, flag)) continue;
  104. if (config_flag.len == flag.len or config_flag[flag.len] == '=') continue :outer;
  105. }
  106. // Otherwise, append default value from config.h to compile flags
  107. try raylib_flags_arr.append(flag);
  108. }
  109. } else {
  110. // Set default config if no custome config got set
  111. try raylib_flags_arr.appendSlice(&config_h_flags);
  112. }
  113. const raylib = if (options.shared)
  114. b.addSharedLibrary(.{
  115. .name = "raylib",
  116. .target = target,
  117. .optimize = optimize,
  118. })
  119. else
  120. b.addStaticLibrary(.{
  121. .name = "raylib",
  122. .target = target,
  123. .optimize = optimize,
  124. });
  125. raylib.linkLibC();
  126. // No GLFW required on PLATFORM_DRM
  127. if (options.platform != .drm) {
  128. raylib.addIncludePath(b.path("src/external/glfw/include"));
  129. }
  130. var c_source_files = try std.ArrayList([]const u8).initCapacity(b.allocator, 2);
  131. c_source_files.appendSliceAssumeCapacity(&.{ "src/rcore.c", "src/utils.c" });
  132. if (options.rshapes) {
  133. try c_source_files.append("src/rshapes.c");
  134. try raylib_flags_arr.append("-DSUPPORT_MODULE_RSHAPES");
  135. }
  136. if (options.rtextures) {
  137. try c_source_files.append("src/rtextures.c");
  138. try raylib_flags_arr.append("-DSUPPORT_MODULE_RTEXTURES");
  139. }
  140. if (options.rtext) {
  141. try c_source_files.append("src/rtext.c");
  142. try raylib_flags_arr.append("-DSUPPORT_MODULE_RTEXT");
  143. }
  144. if (options.rmodels) {
  145. try c_source_files.append("src/rmodels.c");
  146. try raylib_flags_arr.append("-DSUPPORT_MODULE_RMODELS");
  147. }
  148. if (options.raudio) {
  149. try c_source_files.append("src/raudio.c");
  150. try raylib_flags_arr.append("-DSUPPORT_MODULE_RAUDIO");
  151. }
  152. if (options.opengl_version != .auto) {
  153. raylib.root_module.addCMacro(options.opengl_version.toCMacroStr(), "");
  154. }
  155. raylib.addIncludePath(b.path("src/platforms"));
  156. switch (target.result.os.tag) {
  157. .windows => {
  158. switch (options.platform) {
  159. .glfw => try c_source_files.append("src/rglfw.c"),
  160. .rgfw, .sdl, .drm, .android => {},
  161. }
  162. raylib.linkSystemLibrary("winmm");
  163. raylib.linkSystemLibrary("gdi32");
  164. raylib.linkSystemLibrary("opengl32");
  165. setDesktopPlatform(raylib, options.platform);
  166. },
  167. .linux => {
  168. if (options.platform == .drm) {
  169. if (options.opengl_version == .auto) {
  170. raylib.linkSystemLibrary("GLESv2");
  171. raylib.root_module.addCMacro("GRAPHICS_API_OPENGL_ES2", "");
  172. }
  173. raylib.linkSystemLibrary("EGL");
  174. raylib.linkSystemLibrary("gbm");
  175. raylib.linkSystemLibrary2("libdrm", .{ .use_pkg_config = .force });
  176. raylib.root_module.addCMacro("PLATFORM_DRM", "");
  177. raylib.root_module.addCMacro("EGL_NO_X11", "");
  178. raylib.root_module.addCMacro("DEFAULT_BATCH_BUFFER_ELEMENT", "");
  179. } else if (target.result.abi == .android) {
  180. //these are the only tag options per https://developer.android.com/ndk/guides/other_build_systems
  181. const hostTuple = switch (builtin.target.os.tag) {
  182. .linux => "linux-x86_64",
  183. .windows => "windows-x86_64",
  184. .macos => "darwin-x86_64",
  185. else => @panic("unsupported host OS"),
  186. };
  187. const androidTriple = try target.result.linuxTriple(b.allocator);
  188. const androidNdkPathString: []const u8 = options.android_ndk;
  189. if (androidNdkPathString.len < 1) @panic("no ndk path provided and ANDROID_NDK_HOME is not set");
  190. const androidApiLevel: []const u8 = options.android_api_version;
  191. const androidSysroot = try std.fs.path.join(b.allocator, &.{ androidNdkPathString, "/toolchains/llvm/prebuilt/", hostTuple, "/sysroot" });
  192. const androidLibPath = try std.fs.path.join(b.allocator, &.{ androidSysroot, "/usr/lib/", androidTriple });
  193. const androidApiSpecificPath = try std.fs.path.join(b.allocator, &.{ androidLibPath, androidApiLevel });
  194. const androidIncludePath = try std.fs.path.join(b.allocator, &.{ androidSysroot, "/usr/include" });
  195. const androidArchIncludePath = try std.fs.path.join(b.allocator, &.{ androidIncludePath, androidTriple });
  196. const androidAsmPath = try std.fs.path.join(b.allocator, &.{ androidIncludePath, "/asm-generic" });
  197. const androidGluePath = try std.fs.path.join(b.allocator, &.{ androidNdkPathString, "/sources/android/native_app_glue/" });
  198. raylib.addLibraryPath(.{ .cwd_relative = androidLibPath });
  199. raylib.root_module.addLibraryPath(.{ .cwd_relative = androidApiSpecificPath });
  200. raylib.addSystemIncludePath(.{ .cwd_relative = androidIncludePath });
  201. raylib.addSystemIncludePath(.{ .cwd_relative = androidArchIncludePath });
  202. raylib.addSystemIncludePath(.{ .cwd_relative = androidAsmPath });
  203. raylib.addSystemIncludePath(.{ .cwd_relative = androidGluePath });
  204. var libcData = std.ArrayList(u8).init(b.allocator);
  205. const writer = libcData.writer();
  206. try (std.zig.LibCInstallation{
  207. .include_dir = androidIncludePath,
  208. .sys_include_dir = androidIncludePath,
  209. .crt_dir = androidApiSpecificPath,
  210. }).render(writer);
  211. const libcFile = b.addWriteFiles().add("android-libc.txt", try libcData.toOwnedSlice());
  212. raylib.setLibCFile(libcFile);
  213. if (options.opengl_version == .auto) {
  214. raylib.root_module.linkSystemLibrary("GLESv2", .{});
  215. raylib.root_module.addCMacro("GRAPHICS_API_OPENGL_ES2", "");
  216. }
  217. setDesktopPlatform(raylib, .android);
  218. } else {
  219. try c_source_files.append("src/rglfw.c");
  220. if (options.linux_display_backend == .X11 or options.linux_display_backend == .Both) {
  221. raylib.root_module.addCMacro("_GLFW_X11", "");
  222. raylib.linkSystemLibrary("GLX");
  223. raylib.linkSystemLibrary("X11");
  224. raylib.linkSystemLibrary("Xcursor");
  225. raylib.linkSystemLibrary("Xext");
  226. raylib.linkSystemLibrary("Xfixes");
  227. raylib.linkSystemLibrary("Xi");
  228. raylib.linkSystemLibrary("Xinerama");
  229. raylib.linkSystemLibrary("Xrandr");
  230. raylib.linkSystemLibrary("Xrender");
  231. }
  232. if (options.linux_display_backend == .Wayland or options.linux_display_backend == .Both) {
  233. _ = b.findProgram(&.{"wayland-scanner"}, &.{}) catch {
  234. std.log.err(
  235. \\ `wayland-scanner` may not be installed on the system.
  236. \\ You can switch to X11 in your `build.zig` by changing `Options.linux_display_backend`
  237. , .{});
  238. @panic("`wayland-scanner` not found");
  239. };
  240. raylib.root_module.addCMacro("_GLFW_WAYLAND", "");
  241. raylib.linkSystemLibrary("EGL");
  242. raylib.linkSystemLibrary("wayland-client");
  243. raylib.linkSystemLibrary("xkbcommon");
  244. waylandGenerate(b, raylib, "wayland.xml", "wayland-client-protocol");
  245. waylandGenerate(b, raylib, "xdg-shell.xml", "xdg-shell-client-protocol");
  246. waylandGenerate(b, raylib, "xdg-decoration-unstable-v1.xml", "xdg-decoration-unstable-v1-client-protocol");
  247. waylandGenerate(b, raylib, "viewporter.xml", "viewporter-client-protocol");
  248. waylandGenerate(b, raylib, "relative-pointer-unstable-v1.xml", "relative-pointer-unstable-v1-client-protocol");
  249. waylandGenerate(b, raylib, "pointer-constraints-unstable-v1.xml", "pointer-constraints-unstable-v1-client-protocol");
  250. waylandGenerate(b, raylib, "fractional-scale-v1.xml", "fractional-scale-v1-client-protocol");
  251. waylandGenerate(b, raylib, "xdg-activation-v1.xml", "xdg-activation-v1-client-protocol");
  252. waylandGenerate(b, raylib, "idle-inhibit-unstable-v1.xml", "idle-inhibit-unstable-v1-client-protocol");
  253. }
  254. setDesktopPlatform(raylib, options.platform);
  255. }
  256. },
  257. .freebsd, .openbsd, .netbsd, .dragonfly => {
  258. try c_source_files.append("rglfw.c");
  259. raylib.linkSystemLibrary("GL");
  260. raylib.linkSystemLibrary("rt");
  261. raylib.linkSystemLibrary("dl");
  262. raylib.linkSystemLibrary("m");
  263. raylib.linkSystemLibrary("X11");
  264. raylib.linkSystemLibrary("Xrandr");
  265. raylib.linkSystemLibrary("Xinerama");
  266. raylib.linkSystemLibrary("Xi");
  267. raylib.linkSystemLibrary("Xxf86vm");
  268. raylib.linkSystemLibrary("Xcursor");
  269. setDesktopPlatform(raylib, options.platform);
  270. },
  271. .macos => {
  272. // Include xcode_frameworks for cross compilation
  273. if (b.lazyDependency("xcode_frameworks", .{})) |dep| {
  274. raylib.addSystemFrameworkPath(dep.path("Frameworks"));
  275. raylib.addSystemIncludePath(dep.path("include"));
  276. raylib.addLibraryPath(dep.path("lib"));
  277. }
  278. // On macos rglfw.c include Objective-C files.
  279. try raylib_flags_arr.append("-ObjC");
  280. raylib.root_module.addCSourceFile(.{
  281. .file = b.path("src/rglfw.c"),
  282. .flags = raylib_flags_arr.items,
  283. });
  284. _ = raylib_flags_arr.pop();
  285. raylib.linkFramework("Foundation");
  286. raylib.linkFramework("CoreServices");
  287. raylib.linkFramework("CoreGraphics");
  288. raylib.linkFramework("AppKit");
  289. raylib.linkFramework("IOKit");
  290. setDesktopPlatform(raylib, options.platform);
  291. },
  292. .emscripten => {
  293. // Include emscripten for cross compilation
  294. if (b.lazyDependency("emsdk", .{})) |dep| {
  295. if (try emSdkSetupStep(b, dep)) |emSdkStep| {
  296. raylib.step.dependOn(&emSdkStep.step);
  297. }
  298. raylib.addIncludePath(dep.path("upstream/emscripten/cache/sysroot/include"));
  299. }
  300. raylib.root_module.addCMacro("PLATFORM_WEB", "");
  301. if (options.opengl_version == .auto) {
  302. raylib.root_module.addCMacro("GRAPHICS_API_OPENGL_ES2", "");
  303. }
  304. },
  305. else => {
  306. @panic("Unsupported OS");
  307. },
  308. }
  309. raylib.root_module.addCSourceFiles(.{
  310. .files = c_source_files.items,
  311. .flags = raylib_flags_arr.items,
  312. });
  313. return raylib;
  314. }
  315. pub fn addRaygui(b: *std.Build, raylib: *std.Build.Step.Compile, raygui_dep: *std.Build.Dependency) void {
  316. const raylib_dep = b.dependencyFromBuildZig(@This(), .{});
  317. var gen_step = b.addWriteFiles();
  318. raylib.step.dependOn(&gen_step.step);
  319. const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
  320. raylib.addCSourceFile(.{ .file = raygui_c_path });
  321. raylib.addIncludePath(raygui_dep.path("src"));
  322. raylib.addIncludePath(raylib_dep.path("src"));
  323. raylib.installHeader(raygui_dep.path("src/raygui.h"), "raygui.h");
  324. }
  325. pub const Options = struct {
  326. raudio: bool = true,
  327. rmodels: bool = true,
  328. rshapes: bool = true,
  329. rtext: bool = true,
  330. rtextures: bool = true,
  331. platform: PlatformBackend = .glfw,
  332. shared: bool = false,
  333. linux_display_backend: LinuxDisplayBackend = .Both,
  334. opengl_version: OpenglVersion = .auto,
  335. android_ndk: []const u8 = "",
  336. android_api_version: []const u8 = "35",
  337. /// config should be a list of space-separated cflags, eg, "-DSUPPORT_CUSTOM_FRAME_CONTROL"
  338. config: []const u8 = &.{},
  339. const defaults = Options{};
  340. pub fn getOptions(b: *std.Build) Options {
  341. return .{
  342. .platform = b.option(PlatformBackend, "platform", "Choose the platform backedn for desktop target") orelse defaults.platform,
  343. .raudio = b.option(bool, "raudio", "Compile with audio support") orelse defaults.raudio,
  344. .rmodels = b.option(bool, "rmodels", "Compile with models support") orelse defaults.rmodels,
  345. .rtext = b.option(bool, "rtext", "Compile with text support") orelse defaults.rtext,
  346. .rtextures = b.option(bool, "rtextures", "Compile with textures support") orelse defaults.rtextures,
  347. .rshapes = b.option(bool, "rshapes", "Compile with shapes support") orelse defaults.rshapes,
  348. .shared = b.option(bool, "shared", "Compile as shared library") orelse defaults.shared,
  349. .linux_display_backend = b.option(LinuxDisplayBackend, "linux_display_backend", "Linux display backend to use") orelse defaults.linux_display_backend,
  350. .opengl_version = b.option(OpenglVersion, "opengl_version", "OpenGL version to use") orelse defaults.opengl_version,
  351. .config = b.option([]const u8, "config", "Compile with custom define macros overriding config.h") orelse &.{},
  352. .android_ndk = b.option([]const u8, "android_ndk", "specify path to android ndk") orelse std.process.getEnvVarOwned(b.allocator, "ANDROID_NDK_HOME") catch "",
  353. .android_api_version = b.option([]const u8, "android_api_version", "specify target android API level") orelse defaults.android_api_version,
  354. };
  355. }
  356. };
  357. pub const OpenglVersion = enum {
  358. auto,
  359. gl_1_1,
  360. gl_2_1,
  361. gl_3_3,
  362. gl_4_3,
  363. gles_2,
  364. gles_3,
  365. pub fn toCMacroStr(self: @This()) []const u8 {
  366. switch (self) {
  367. .auto => @panic("OpenglVersion.auto cannot be turned into a C macro string"),
  368. .gl_1_1 => return "GRAPHICS_API_OPENGL_11",
  369. .gl_2_1 => return "GRAPHICS_API_OPENGL_21",
  370. .gl_3_3 => return "GRAPHICS_API_OPENGL_33",
  371. .gl_4_3 => return "GRAPHICS_API_OPENGL_43",
  372. .gles_2 => return "GRAPHICS_API_OPENGL_ES2",
  373. .gles_3 => return "GRAPHICS_API_OPENGL_ES3",
  374. }
  375. }
  376. };
  377. pub const LinuxDisplayBackend = enum {
  378. X11,
  379. Wayland,
  380. Both,
  381. };
  382. pub const PlatformBackend = enum {
  383. glfw,
  384. rgfw,
  385. sdl,
  386. drm,
  387. android,
  388. };
  389. pub fn build(b: *std.Build) !void {
  390. // Standard target options allows the person running `zig build` to choose
  391. // what target to build for. Here we do not override the defaults, which
  392. // means any target is allowed, and the default is native. Other options
  393. // for restricting supported target set are available.
  394. const target = b.standardTargetOptions(.{});
  395. // Standard optimization options allow the person running `zig build` to select
  396. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
  397. // set a preferred release mode, allowing the user to decide how to optimize.
  398. const optimize = b.standardOptimizeOption(.{});
  399. const lib = try compileRaylib(b, target, optimize, Options.getOptions(b));
  400. lib.installHeader(b.path("src/raylib.h"), "raylib.h");
  401. lib.installHeader(b.path("src/rcamera.h"), "rcamera.h");
  402. lib.installHeader(b.path("src/raymath.h"), "raymath.h");
  403. lib.installHeader(b.path("src/rlgl.h"), "rlgl.h");
  404. b.installArtifact(lib);
  405. const examples = b.step("examples", "Build/Install all examples");
  406. examples.dependOn(try addExamples("audio", b, target, optimize, lib));
  407. examples.dependOn(try addExamples("core", b, target, optimize, lib));
  408. examples.dependOn(try addExamples("models", b, target, optimize, lib));
  409. examples.dependOn(try addExamples("others", b, target, optimize, lib));
  410. examples.dependOn(try addExamples("shaders", b, target, optimize, lib));
  411. examples.dependOn(try addExamples("shapes", b, target, optimize, lib));
  412. examples.dependOn(try addExamples("text", b, target, optimize, lib));
  413. examples.dependOn(try addExamples("textures", b, target, optimize, lib));
  414. }
  415. fn waylandGenerate(
  416. b: *std.Build,
  417. raylib: *std.Build.Step.Compile,
  418. comptime protocol: []const u8,
  419. comptime basename: []const u8,
  420. ) void {
  421. const waylandDir = "src/external/glfw/deps/wayland";
  422. const protocolDir = b.pathJoin(&.{ waylandDir, protocol });
  423. const clientHeader = basename ++ ".h";
  424. const privateCode = basename ++ "-code.h";
  425. const client_step = b.addSystemCommand(&.{ "wayland-scanner", "client-header" });
  426. client_step.addFileArg(b.path(protocolDir));
  427. raylib.addIncludePath(client_step.addOutputFileArg(clientHeader).dirname());
  428. const private_step = b.addSystemCommand(&.{ "wayland-scanner", "private-code" });
  429. private_step.addFileArg(b.path(protocolDir));
  430. raylib.addIncludePath(private_step.addOutputFileArg(privateCode).dirname());
  431. raylib.step.dependOn(&client_step.step);
  432. raylib.step.dependOn(&private_step.step);
  433. }
  434. fn addExamples(
  435. comptime module: []const u8,
  436. b: *std.Build,
  437. target: std.Build.ResolvedTarget,
  438. optimize: std.builtin.OptimizeMode,
  439. raylib: *std.Build.Step.Compile,
  440. ) !*std.Build.Step {
  441. if (target.result.os.tag == .emscripten) {
  442. return &b.addFail("Emscripten building via Zig unsupported").step;
  443. }
  444. const all = b.step(module, "All " ++ module ++ " examples");
  445. const module_subpath = b.pathJoin(&.{ "examples", module });
  446. var dir = try std.fs.cwd().openDir(b.pathFromRoot(module_subpath), .{ .iterate = true });
  447. defer if (comptime builtin.zig_version.minor >= 12) dir.close();
  448. var iter = dir.iterate();
  449. while (try iter.next()) |entry| {
  450. if (entry.kind != .file) continue;
  451. const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue;
  452. const name = entry.name[0..extension_idx];
  453. const path = b.pathJoin(&.{ module_subpath, entry.name });
  454. // zig's mingw headers do not include pthread.h
  455. if (std.mem.eql(u8, "core_loading_thread", name) and target.result.os.tag == .windows) continue;
  456. const exe = b.addExecutable(.{
  457. .name = name,
  458. .target = target,
  459. .optimize = optimize,
  460. });
  461. exe.addCSourceFile(.{ .file = b.path(path), .flags = &.{} });
  462. exe.linkLibC();
  463. // special examples that test using these external dependencies directly
  464. // alongside raylib
  465. if (std.mem.eql(u8, name, "rlgl_standalone")) {
  466. exe.addIncludePath(b.path("src"));
  467. exe.addIncludePath(b.path("src/external/glfw/include"));
  468. if (!hasCSource(raylib.root_module, "rglfw.c")) {
  469. exe.addCSourceFile(.{ .file = b.path("src/rglfw.c"), .flags = &.{} });
  470. }
  471. }
  472. if (std.mem.eql(u8, name, "raylib_opengl_interop")) {
  473. exe.addIncludePath(b.path("src/external"));
  474. }
  475. exe.linkLibrary(raylib);
  476. switch (target.result.os.tag) {
  477. .windows => {
  478. exe.linkSystemLibrary("winmm");
  479. exe.linkSystemLibrary("gdi32");
  480. exe.linkSystemLibrary("opengl32");
  481. exe.root_module.addCMacro("PLATFORM_DESKTOP", "");
  482. },
  483. .linux => {
  484. exe.linkSystemLibrary("GL");
  485. exe.linkSystemLibrary("rt");
  486. exe.linkSystemLibrary("dl");
  487. exe.linkSystemLibrary("m");
  488. exe.linkSystemLibrary("X11");
  489. exe.root_module.addCMacro("PLATFORM_DESKTOP", "");
  490. },
  491. .macos => {
  492. exe.linkFramework("Foundation");
  493. exe.linkFramework("Cocoa");
  494. exe.linkFramework("OpenGL");
  495. exe.linkFramework("CoreAudio");
  496. exe.linkFramework("CoreVideo");
  497. exe.linkFramework("IOKit");
  498. exe.root_module.addCMacro("PLATFORM_DESKTOP", "");
  499. },
  500. else => {
  501. @panic("Unsupported OS");
  502. },
  503. }
  504. const install_cmd = b.addInstallArtifact(exe, .{});
  505. const run_cmd = b.addRunArtifact(exe);
  506. run_cmd.cwd = b.path(module_subpath);
  507. run_cmd.step.dependOn(&install_cmd.step);
  508. const run_step = b.step(name, name);
  509. run_step.dependOn(&run_cmd.step);
  510. all.dependOn(&install_cmd.step);
  511. }
  512. return all;
  513. }
  514. fn hasCSource(module: *std.Build.Module, name: []const u8) bool {
  515. for (module.link_objects.items) |o| switch (o) {
  516. .c_source_file => |c| if (switch (c.file) {
  517. .src_path => |s| std.ascii.endsWithIgnoreCase(s.sub_path, name),
  518. .generated, .cwd_relative, .dependency => false,
  519. }) return true,
  520. .c_source_files => |s| for (s.files) |c| if (std.ascii.endsWithIgnoreCase(c, name)) return true,
  521. else => {},
  522. };
  523. return false;
  524. }