Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

135 строки
5.7 KiB

  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. // This has been tested to work with zig 0.11.0 (67709b6, Aug 4 2023) and zig 0.12.0-dev.2075+f5978181e (Jan 8 2024)
  4. //
  5. // anytype is used here to preserve compatibility, in 0.12.0dev the std.zig.CrossTarget type
  6. // was reworked into std.Target.Query and std.Build.ResolvedTarget. Using anytype allows
  7. // us to accept both CrossTarget and ResolvedTarget and act accordingly in getOsTagVersioned.
  8. fn add_module(comptime module: []const u8, b: *std.Build, target: anytype, optimize: std.builtin.OptimizeMode) !*std.Build.Step {
  9. if (comptime builtin.zig_version.minor >= 12 and @TypeOf(target) != std.Build.ResolvedTarget) {
  10. @compileError("Expected 'std.Build.ResolvedTarget' for argument 2 'target' in 'add_module', found '" ++ @typeName(@TypeOf(target)) ++ "'");
  11. } else if (comptime builtin.zig_version.minor == 11 and @TypeOf(target) != std.zig.CrossTarget) {
  12. @compileError("Expected 'std.zig.CrossTarget' for argument 2 'target' in 'add_module', found '" ++ @typeName(@TypeOf(target)) ++ "'");
  13. }
  14. if (getOsTagVersioned(target) == .emscripten) {
  15. @panic("Emscripten building via Zig unsupported");
  16. }
  17. const all = b.step(module, "All " ++ module ++ " examples");
  18. var dir = try openIterableDirVersioned(std.fs.cwd(), module);
  19. defer if (comptime builtin.zig_version.minor >= 12) dir.close();
  20. var iter = dir.iterate();
  21. while (try iter.next()) |entry| {
  22. if (entry.kind != .file) continue;
  23. const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue;
  24. const name = entry.name[0..extension_idx];
  25. const path = try std.fs.path.join(b.allocator, &.{ module, entry.name });
  26. // zig's mingw headers do not include pthread.h
  27. if (std.mem.eql(u8, "core_loading_thread", name) and getOsTagVersioned(target) == .windows) continue;
  28. const exe = b.addExecutable(.{
  29. .name = name,
  30. .target = target,
  31. .optimize = optimize,
  32. });
  33. exe.addCSourceFile(.{ .file = .{ .path = path }, .flags = &.{} });
  34. exe.linkLibC();
  35. exe.addObjectFile(switch (getOsTagVersioned(target)) {
  36. .windows => .{ .path = "../zig-out/lib/raylib.lib" },
  37. .linux => .{ .path = "../zig-out/lib/libraylib.a" },
  38. .macos => .{ .path = "../zig-out/lib/libraylib.a" },
  39. .emscripten => .{ .path = "../zig-out/lib/libraylib.a" },
  40. else => @panic("Unsupported OS"),
  41. });
  42. exe.addIncludePath(.{ .path = "../src" });
  43. exe.addIncludePath(.{ .path = "../src/external" });
  44. exe.addIncludePath(.{ .path = "../src/external/glfw/include" });
  45. switch (getOsTagVersioned(target)) {
  46. .windows => {
  47. exe.linkSystemLibrary("winmm");
  48. exe.linkSystemLibrary("gdi32");
  49. exe.linkSystemLibrary("opengl32");
  50. exe.defineCMacro("PLATFORM_DESKTOP", null);
  51. },
  52. .linux => {
  53. exe.linkSystemLibrary("GL");
  54. exe.linkSystemLibrary("rt");
  55. exe.linkSystemLibrary("dl");
  56. exe.linkSystemLibrary("m");
  57. exe.linkSystemLibrary("X11");
  58. exe.defineCMacro("PLATFORM_DESKTOP", null);
  59. },
  60. .macos => {
  61. exe.linkFramework("Foundation");
  62. exe.linkFramework("Cocoa");
  63. exe.linkFramework("OpenGL");
  64. exe.linkFramework("CoreAudio");
  65. exe.linkFramework("CoreVideo");
  66. exe.linkFramework("IOKit");
  67. exe.defineCMacro("PLATFORM_DESKTOP", null);
  68. },
  69. else => {
  70. @panic("Unsupported OS");
  71. },
  72. }
  73. const install_cmd = b.addInstallArtifact(exe, .{});
  74. const run_cmd = b.addRunArtifact(exe);
  75. run_cmd.step.dependOn(&install_cmd.step);
  76. const run_step = b.step(name, name);
  77. run_step.dependOn(&run_cmd.step);
  78. all.dependOn(&install_cmd.step);
  79. }
  80. return all;
  81. }
  82. pub fn build(b: *std.Build) !void {
  83. // Standard target options allows the person running `zig build` to choose
  84. // what target to build for. Here we do not override the defaults, which
  85. // means any target is allowed, and the default is native. Other options
  86. // for restricting supported target set are available.
  87. const target = b.standardTargetOptions(.{});
  88. // Standard optimization options allow the person running `zig build` to select
  89. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
  90. // set a preferred release mode, allowing the user to decide how to optimize.
  91. const optimize = b.standardOptimizeOption(.{});
  92. const all = b.getInstallStep();
  93. all.dependOn(try add_module("audio", b, target, optimize));
  94. all.dependOn(try add_module("core", b, target, optimize));
  95. all.dependOn(try add_module("models", b, target, optimize));
  96. all.dependOn(try add_module("others", b, target, optimize));
  97. all.dependOn(try add_module("shaders", b, target, optimize));
  98. all.dependOn(try add_module("shapes", b, target, optimize));
  99. all.dependOn(try add_module("text", b, target, optimize));
  100. all.dependOn(try add_module("textures", b, target, optimize));
  101. }
  102. fn getOsTagVersioned(target: anytype) std.Target.Os.Tag {
  103. if (comptime builtin.zig_version.minor >= 12) {
  104. return target.result.os.tag;
  105. } else {
  106. return target.getOsTag();
  107. }
  108. }
  109. fn openIterableDirVersioned(dir: std.fs.Dir, path: []const u8) !(if (builtin.zig_version.minor >= 12) std.fs.Dir else std.fs.IterableDir) {
  110. if (comptime builtin.zig_version.minor >= 12) {
  111. return dir.openDir(path, .{ .iterate = true });
  112. } else {
  113. return dir.openIterableDir(path, .{});
  114. }
  115. }