|
| 1 | +const std = @import("std"); |
| 2 | +const builtin = @import("builtin"); |
| 3 | +const Builder = std.build.Builder; |
| 4 | +const CompileStep = std.build.CompileStep; |
| 5 | +const CrossTarget = std.zig.CrossTarget; |
| 6 | +const OptimizeMode = std.builtin.OptimizeMode; |
| 7 | + |
| 8 | +pub const Backend = enum { |
| 9 | + auto, // Windows: D3D11, macOS/iOS: Metal, otherwise: GL |
| 10 | + d3d11, |
| 11 | + metal, |
| 12 | + gl, |
| 13 | + gles2, |
| 14 | + gles3, |
| 15 | + wgpu, |
| 16 | +}; |
| 17 | + |
| 18 | +pub const Config = struct { |
| 19 | + backend: Backend = .auto, |
| 20 | + force_egl: bool = false, |
| 21 | + enable_x11: bool = true, |
| 22 | + enable_wayland: bool = false, |
| 23 | +}; |
| 24 | + |
| 25 | +// build sokol into a static library |
| 26 | +pub fn buildSokol(b: *Builder, target: CrossTarget, optimize: OptimizeMode, config: Config, comptime prefix_path: []const u8) *CompileStep { |
| 27 | + const lib = b.addStaticLibrary(.{ |
| 28 | + .name = "sokol", |
| 29 | + .target = target, |
| 30 | + .optimize = optimize, |
| 31 | + }); |
| 32 | + lib.linkLibC(); |
| 33 | + const sokol_path = prefix_path ++ "src/sokol/c/"; |
| 34 | + const csources = [_][]const u8{ |
| 35 | + "sokol_log.c", |
| 36 | + "sokol_app.c", |
| 37 | + "sokol_gfx.c", |
| 38 | + "sokol_time.c", |
| 39 | + "sokol_audio.c", |
| 40 | + "sokol_gl.c", |
| 41 | + "sokol_debugtext.c", |
| 42 | + "sokol_shape.c", |
| 43 | + }; |
| 44 | + var _backend = config.backend; |
| 45 | + if (_backend == .auto) { |
| 46 | + if (lib.target.isDarwin()) { |
| 47 | + _backend = .metal; |
| 48 | + } else if (lib.target.isWindows()) { |
| 49 | + _backend = .d3d11; |
| 50 | + } else { |
| 51 | + _backend = .gl; |
| 52 | + } |
| 53 | + } |
| 54 | + const backend_option = switch (_backend) { |
| 55 | + .d3d11 => "-DSOKOL_D3D11", |
| 56 | + .metal => "-DSOKOL_METAL", |
| 57 | + .gl => "-DSOKOL_GLCORE33", |
| 58 | + .gles2 => "-DSOKOL_GLES2", |
| 59 | + .gles3 => "-DSOKOL_GLES3", |
| 60 | + .wgpu => "-DSOKOL_WGPU", |
| 61 | + else => unreachable, |
| 62 | + }; |
| 63 | + |
| 64 | + if (lib.target.isDarwin()) { |
| 65 | + inline for (csources) |csrc| { |
| 66 | + lib.addCSourceFile(.{ |
| 67 | + .file = .{ .path = sokol_path ++ csrc }, |
| 68 | + .flags = &[_][]const u8{ "-ObjC", "-DIMPL", backend_option }, |
| 69 | + }); |
| 70 | + } |
| 71 | + lib.linkFramework("Foundation"); |
| 72 | + lib.linkFramework("AudioToolbox"); |
| 73 | + if (.metal == _backend) { |
| 74 | + lib.linkFramework("MetalKit"); |
| 75 | + lib.linkFramework("Metal"); |
| 76 | + } |
| 77 | + if (lib.target.getOsTag() == .ios) { |
| 78 | + lib.linkFramework("UIKit"); |
| 79 | + lib.linkFramework("AVFoundation"); |
| 80 | + if (.gl == _backend) { |
| 81 | + lib.linkFramework("OpenGLES"); |
| 82 | + lib.linkFramework("GLKit"); |
| 83 | + } |
| 84 | + } else if (lib.target.getOsTag() == .macos) { |
| 85 | + lib.linkFramework("Cocoa"); |
| 86 | + lib.linkFramework("QuartzCore"); |
| 87 | + if (.gl == _backend) { |
| 88 | + lib.linkFramework("OpenGL"); |
| 89 | + } |
| 90 | + } |
| 91 | + } else { |
| 92 | + var egl_flag = if (config.force_egl) "-DSOKOL_FORCE_EGL " else ""; |
| 93 | + var x11_flag = if (!config.enable_x11) "-DSOKOL_DISABLE_X11 " else ""; |
| 94 | + var wayland_flag = if (!config.enable_wayland) "-DSOKOL_DISABLE_WAYLAND" else ""; |
| 95 | + |
| 96 | + inline for (csources) |csrc| { |
| 97 | + lib.addCSourceFile(.{ |
| 98 | + .file = .{ .path = sokol_path ++ csrc }, |
| 99 | + .flags = &[_][]const u8{ "-DIMPL", backend_option, egl_flag, x11_flag, wayland_flag }, |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + if (lib.target.isLinux()) { |
| 104 | + var link_egl = config.force_egl or config.enable_wayland; |
| 105 | + var egl_ensured = (config.force_egl and config.enable_x11) or config.enable_wayland; |
| 106 | + |
| 107 | + lib.linkSystemLibrary("asound"); |
| 108 | + |
| 109 | + if (.gles2 == _backend) { |
| 110 | + lib.linkSystemLibrary("glesv2"); |
| 111 | + if (!egl_ensured) { |
| 112 | + @panic("GLES2 in Linux only available with Config.force_egl and/or Wayland"); |
| 113 | + } |
| 114 | + } else { |
| 115 | + lib.linkSystemLibrary("GL"); |
| 116 | + } |
| 117 | + if (config.enable_x11) { |
| 118 | + lib.linkSystemLibrary("X11"); |
| 119 | + lib.linkSystemLibrary("Xi"); |
| 120 | + lib.linkSystemLibrary("Xcursor"); |
| 121 | + } |
| 122 | + if (config.enable_wayland) { |
| 123 | + lib.linkSystemLibrary("wayland-client"); |
| 124 | + lib.linkSystemLibrary("wayland-cursor"); |
| 125 | + lib.linkSystemLibrary("wayland-egl"); |
| 126 | + lib.linkSystemLibrary("xkbcommon"); |
| 127 | + } |
| 128 | + if (link_egl) { |
| 129 | + lib.linkSystemLibrary("egl"); |
| 130 | + } |
| 131 | + } else if (lib.target.isWindows()) { |
| 132 | + lib.linkSystemLibraryName("kernel32"); |
| 133 | + lib.linkSystemLibraryName("user32"); |
| 134 | + lib.linkSystemLibraryName("gdi32"); |
| 135 | + lib.linkSystemLibraryName("ole32"); |
| 136 | + if (.d3d11 == _backend) { |
| 137 | + lib.linkSystemLibraryName("d3d11"); |
| 138 | + lib.linkSystemLibraryName("dxgi"); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + return lib; |
| 143 | +} |
| 144 | + |
| 145 | +// build one of the example exes |
| 146 | +fn buildExample(b: *Builder, target: CrossTarget, optimize: OptimizeMode, sokol: *CompileStep, comptime name: []const u8) void { |
| 147 | + const e = b.addExecutable(.{ |
| 148 | + .name = name, |
| 149 | + .root_source_file = .{ .path = "src/examples/" ++ name ++ ".zig" }, |
| 150 | + .target = target, |
| 151 | + .optimize = optimize, |
| 152 | + }); |
| 153 | + e.linkLibrary(sokol); |
| 154 | + e.addAnonymousModule("sokol", .{ .source_file = .{ .path = "src/sokol/sokol.zig" } }); |
| 155 | + b.installArtifact(e); |
| 156 | + const run = b.addRunArtifact(e); |
| 157 | + b.step("run-" ++ name, "Run " ++ name).dependOn(&run.step); |
| 158 | +} |
| 159 | + |
| 160 | +pub fn build(b: *Builder) void { |
| 161 | + var config: Config = .{}; |
| 162 | + |
| 163 | + const force_gl = b.option(bool, "gl", "Force GL backend") orelse false; |
| 164 | + config.backend = if (force_gl) .gl else .auto; |
| 165 | + |
| 166 | + // NOTE: Wayland support is *not* currently supported in the standard sokol-zig bindings, |
| 167 | + // you need to generate your own bindings using this PR: https://github.com/floooh/sokol/pull/425 |
| 168 | + config.enable_wayland = b.option(bool, "wayland", "Compile with wayland-support (default: false)") orelse false; |
| 169 | + config.enable_x11 = b.option(bool, "x11", "Compile with x11-support (default: true)") orelse true; |
| 170 | + config.force_egl = b.option(bool, "egl", "Use EGL instead of GLX if possible (default: false)") orelse false; |
| 171 | + |
| 172 | + const target = b.standardTargetOptions(.{}); |
| 173 | + const optimize = b.standardOptimizeOption(.{}); |
| 174 | + const sokol = buildSokol(b, target, optimize, config, ""); |
| 175 | + const examples = .{ |
| 176 | + "clear", |
| 177 | + "triangle", |
| 178 | + "quad", |
| 179 | + "bufferoffsets", |
| 180 | + "cube", |
| 181 | + "noninterleaved", |
| 182 | + "texcube", |
| 183 | + "blend", |
| 184 | + "offscreen", |
| 185 | + "instancing", |
| 186 | + "mrt", |
| 187 | + "saudio", |
| 188 | + "sgl", |
| 189 | + "sgl-context", |
| 190 | + "sgl-points", |
| 191 | + "debugtext", |
| 192 | + "debugtext-print", |
| 193 | + "debugtext-userfont", |
| 194 | + "shapes", |
| 195 | + }; |
| 196 | + inline for (examples) |example| { |
| 197 | + buildExample(b, target, optimize, sokol, example); |
| 198 | + } |
| 199 | + buildShaders(b); |
| 200 | +} |
| 201 | + |
| 202 | +// a separate step to compile shaders, expects the shader compiler in ../sokol-tools-bin/ |
| 203 | +fn buildShaders(b: *Builder) void { |
| 204 | + const sokol_tools_bin_dir = "../sokol-tools-bin/bin/"; |
| 205 | + const shaders_dir = "src/examples/shaders/"; |
| 206 | + const shaders = .{ |
| 207 | + "bufferoffsets.glsl", |
| 208 | + "cube.glsl", |
| 209 | + "instancing.glsl", |
| 210 | + "mrt.glsl", |
| 211 | + "noninterleaved.glsl", |
| 212 | + "offscreen.glsl", |
| 213 | + "quad.glsl", |
| 214 | + "shapes.glsl", |
| 215 | + "texcube.glsl", |
| 216 | + "blend.glsl", |
| 217 | + }; |
| 218 | + const optional_shdc: ?[:0]const u8 = comptime switch (builtin.os.tag) { |
| 219 | + .windows => "win32/sokol-shdc.exe", |
| 220 | + .linux => "linux/sokol-shdc", |
| 221 | + .macos => if (builtin.cpu.arch.isX86()) "osx/sokol-shdc" else "osx_arm64/sokol-shdc", |
| 222 | + else => null, |
| 223 | + }; |
| 224 | + if (optional_shdc == null) { |
| 225 | + std.log.warn("unsupported host platform, skipping shader compiler step", .{}); |
| 226 | + return; |
| 227 | + } |
| 228 | + const shdc_path = sokol_tools_bin_dir ++ optional_shdc.?; |
| 229 | + const shdc_step = b.step("shaders", "Compile shaders (needs ../sokol-tools-bin)"); |
| 230 | + inline for (shaders) |shader| { |
| 231 | + const cmd = b.addSystemCommand(&.{ |
| 232 | + shdc_path, |
| 233 | + "-i", |
| 234 | + shaders_dir ++ shader, |
| 235 | + "-o", |
| 236 | + shaders_dir ++ shader ++ ".zig", |
| 237 | + "-l", |
| 238 | + "glsl330:metal_macos:hlsl4", |
| 239 | + "-f", |
| 240 | + "sokol_zig", |
| 241 | + }); |
| 242 | + shdc_step.dependOn(&cmd.step); |
| 243 | + } |
| 244 | +} |
0 commit comments