You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.4 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)
  4. fn add_module(comptime module: []const u8, b: *std.Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode) !*std.Build.Step {
  5. if (target.getOsTag() == .emscripten) {
  6. @panic("Emscripten building via Zig unsupported");
  7. }
  8. const all = b.step(module, "All " ++ module ++ " examples");
  9. const dir = try std.fs.cwd().openIterableDir(module, .{});
  10. var iter = dir.iterate();
  11. while (try iter.next()) |entry| {
  12. if (entry.kind != .file) continue;
  13. const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue;
  14. const name = entry.name[0..extension_idx];
  15. const path = try std.fs.path.join(b.allocator, &.{ module, entry.name });
  16. // zig's mingw headers do not include pthread.h
  17. if (std.mem.eql(u8, "core_loading_thread", name) and target.getOsTag() == .windows) continue;
  18. const exe = b.addExecutable(.{
  19. .name = name,
  20. .target = target,
  21. .optimize = optimize,
  22. });
  23. exe.addCSourceFile(.{ .file = .{ .path = path }, .flags = &.{} });
  24. exe.linkLibC();
  25. exe.addObjectFile(switch (target.getOsTag()) {
  26. .windows => .{ .path = "../zig-out/lib/raylib.lib" },
  27. .linux => .{ .path = "../zig-out/lib/libraylib.a" },
  28. .macos => .{ .path = "../zig-out/lib/libraylib.a" },
  29. .emscripten => .{ .path = "../zig-out/lib/libraylib.a" },
  30. else => @panic("Unsupported OS"),
  31. });
  32. exe.addIncludePath(.{ .path = "../src" });
  33. exe.addIncludePath(.{ .path = "../src/external" });
  34. exe.addIncludePath(.{ .path = "../src/external/glfw/include" });
  35. switch (target.getOsTag()) {
  36. .windows => {
  37. exe.linkSystemLibrary("winmm");
  38. exe.linkSystemLibrary("gdi32");
  39. exe.linkSystemLibrary("opengl32");
  40. exe.addIncludePath(.{ .path = "external/glfw/deps/mingw" });
  41. exe.defineCMacro("PLATFORM_DESKTOP", null);
  42. },
  43. .linux => {
  44. exe.linkSystemLibrary("GL");
  45. exe.linkSystemLibrary("rt");
  46. exe.linkSystemLibrary("dl");
  47. exe.linkSystemLibrary("m");
  48. exe.linkSystemLibrary("X11");
  49. exe.defineCMacro("PLATFORM_DESKTOP", null);
  50. },
  51. .macos => {
  52. exe.linkFramework("Foundation");
  53. exe.linkFramework("Cocoa");
  54. exe.linkFramework("OpenGL");
  55. exe.linkFramework("CoreAudio");
  56. exe.linkFramework("CoreVideo");
  57. exe.linkFramework("IOKit");
  58. exe.defineCMacro("PLATFORM_DESKTOP", null);
  59. },
  60. else => {
  61. @panic("Unsupported OS");
  62. },
  63. }
  64. const install_cmd = b.addInstallArtifact(exe, .{});
  65. const run_cmd = b.addRunArtifact(exe);
  66. run_cmd.step.dependOn(&install_cmd.step);
  67. const run_step = b.step(name, name);
  68. run_step.dependOn(&run_cmd.step);
  69. all.dependOn(&install_cmd.step);
  70. }
  71. return all;
  72. }
  73. pub fn build(b: *std.Build) !void {
  74. // Standard target options allows the person running `zig build` to choose
  75. // what target to build for. Here we do not override the defaults, which
  76. // means any target is allowed, and the default is native. Other options
  77. // for restricting supported target set are available.
  78. const target = b.standardTargetOptions(.{});
  79. // Standard optimization options allow the person running `zig build` to select
  80. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
  81. // set a preferred release mode, allowing the user to decide how to optimize.
  82. const optimize = b.standardOptimizeOption(.{});
  83. const all = b.getInstallStep();
  84. all.dependOn(try add_module("audio", b, target, optimize));
  85. all.dependOn(try add_module("core", b, target, optimize));
  86. all.dependOn(try add_module("models", b, target, optimize));
  87. all.dependOn(try add_module("others", b, target, optimize));
  88. all.dependOn(try add_module("shaders", b, target, optimize));
  89. all.dependOn(try add_module("shapes", b, target, optimize));
  90. all.dependOn(try add_module("text", b, target, optimize));
  91. all.dependOn(try add_module("textures", b, target, optimize));
  92. }