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.

110 lines
4.4 KiB

  1. const std = @import("std");
  2. const builtin = @import("builtin");
  3. // This has been tested to work with zig 0.12.0
  4. fn add_module(comptime module: []const u8, b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) !*std.Build.Step {
  5. if (target.result.os.tag == .emscripten) {
  6. @panic("Emscripten building via Zig unsupported");
  7. }
  8. const all = b.step(module, "All " ++ module ++ " examples");
  9. var dir = try std.fs.cwd().openDir(module, .{ .iterate = true });
  10. defer if (comptime builtin.zig_version.minor >= 12) dir.close();
  11. var iter = dir.iterate();
  12. while (try iter.next()) |entry| {
  13. if (entry.kind != .file) continue;
  14. const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue;
  15. const name = entry.name[0..extension_idx];
  16. const path = try std.fs.path.join(b.allocator, &.{ module, entry.name });
  17. // zig's mingw headers do not include pthread.h
  18. if (std.mem.eql(u8, "core_loading_thread", name) and target.result.os.tag == .windows) continue;
  19. const exe = b.addExecutable(.{
  20. .name = name,
  21. .target = target,
  22. .optimize = optimize,
  23. });
  24. exe.addCSourceFile(.{ .file = b.path(path), .flags = &.{} });
  25. exe.linkLibC();
  26. exe.addObjectFile(switch (target.result.os.tag) {
  27. .windows => b.path("../zig-out/lib/raylib.lib"),
  28. .linux => b.path("../zig-out/lib/libraylib.a"),
  29. .macos => b.path("../zig-out/lib/libraylib.a"),
  30. .emscripten => b.path("../zig-out/lib/libraylib.a"),
  31. else => @panic("Unsupported OS"),
  32. });
  33. exe.addIncludePath(b.path("../src"));
  34. exe.addIncludePath(b.path("../src/external"));
  35. exe.addIncludePath(b.path("../src/external/glfw/include"));
  36. switch (target.result.os.tag) {
  37. .windows => {
  38. exe.linkSystemLibrary("winmm");
  39. exe.linkSystemLibrary("gdi32");
  40. exe.linkSystemLibrary("opengl32");
  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.cwd = b.path(module);
  67. run_cmd.step.dependOn(&install_cmd.step);
  68. const run_step = b.step(name, name);
  69. run_step.dependOn(&run_cmd.step);
  70. all.dependOn(&install_cmd.step);
  71. }
  72. return all;
  73. }
  74. pub fn build(b: *std.Build) !void {
  75. // Standard target options allows the person running `zig build` to choose
  76. // what target to build for. Here we do not override the defaults, which
  77. // means any target is allowed, and the default is native. Other options
  78. // for restricting supported target set are available.
  79. const target = b.standardTargetOptions(.{});
  80. // Standard optimization options allow the person running `zig build` to select
  81. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
  82. // set a preferred release mode, allowing the user to decide how to optimize.
  83. const optimize = b.standardOptimizeOption(.{});
  84. const all = b.getInstallStep();
  85. all.dependOn(try add_module("audio", b, target, optimize));
  86. all.dependOn(try add_module("core", b, target, optimize));
  87. all.dependOn(try add_module("models", b, target, optimize));
  88. all.dependOn(try add_module("others", b, target, optimize));
  89. all.dependOn(try add_module("shaders", b, target, optimize));
  90. all.dependOn(try add_module("shapes", b, target, optimize));
  91. all.dependOn(try add_module("text", b, target, optimize));
  92. all.dependOn(try add_module("textures", b, target, optimize));
  93. }