|
| 1 | +const builtin = @import("builtin"); |
| 2 | +const std = @import("std"); |
| 3 | +const assert = std.debug.assert; |
| 4 | + |
| 5 | +pub fn build(b: *std.Build) void { |
| 6 | + const zsdl2_module = b.addModule("zsdl2", .{ |
| 7 | + .root_source_file = b.path("src/sdl2.zig"), |
| 8 | + }); |
| 9 | + |
| 10 | + _ = b.addModule("zsdl2_ttf", .{ |
| 11 | + .root_source_file = b.path("src/sdl2_ttf.zig"), |
| 12 | + .imports = &.{ |
| 13 | + .{ .name = "zsdl2", .module = zsdl2_module }, |
| 14 | + }, |
| 15 | + }); |
| 16 | + |
| 17 | + _ = b.addModule("zsdl2_image", .{ |
| 18 | + .root_source_file = b.path("src/sdl2_image.zig"), |
| 19 | + .imports = &.{ |
| 20 | + .{ .name = "zsdl2", .module = zsdl2_module }, |
| 21 | + }, |
| 22 | + }); |
| 23 | + |
| 24 | + _ = b.addModule("zsdl3", .{ |
| 25 | + .root_source_file = b.path("src/sdl3.zig"), |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +pub fn link_SDL2(compile_step: *std.Build.Step.Compile) void { |
| 30 | + switch (compile_step.rootModuleTarget().os.tag) { |
| 31 | + .windows => { |
| 32 | + compile_step.linkSystemLibrary("SDL2"); |
| 33 | + compile_step.linkSystemLibrary("SDL2main"); |
| 34 | + }, |
| 35 | + .linux => { |
| 36 | + compile_step.linkSystemLibrary("SDL2"); |
| 37 | + compile_step.root_module.addRPathSpecial("$ORIGIN"); |
| 38 | + }, |
| 39 | + .macos => { |
| 40 | + compile_step.linkFramework("SDL2"); |
| 41 | + compile_step.root_module.addRPathSpecial("@executable_path"); |
| 42 | + }, |
| 43 | + else => {}, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +pub fn link_SDL2_ttf(compile_step: *std.Build.Step.Compile) void { |
| 48 | + switch (compile_step.rootModuleTarget().os.tag) { |
| 49 | + .windows => { |
| 50 | + compile_step.linkSystemLibrary("SDL2_ttf"); |
| 51 | + }, |
| 52 | + .linux => { |
| 53 | + compile_step.linkSystemLibrary("SDL2_ttf"); |
| 54 | + compile_step.root_module.addRPathSpecial("$ORIGIN"); |
| 55 | + }, |
| 56 | + .macos => { |
| 57 | + compile_step.linkFramework("SDL2_ttf"); |
| 58 | + compile_step.root_module.addRPathSpecial("@executable_path"); |
| 59 | + }, |
| 60 | + else => {}, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +pub fn link_SDL2_image(compile_step: *std.Build.Step.Compile) void { |
| 65 | + switch (compile_step.rootModuleTarget().os.tag) { |
| 66 | + .windows => { |
| 67 | + compile_step.linkSystemLibrary("SDL2_image"); |
| 68 | + }, |
| 69 | + .linux => { |
| 70 | + compile_step.linkSystemLibrary("SDL2_image"); |
| 71 | + compile_step.root_module.addRPathSpecial("$ORIGIN"); |
| 72 | + }, |
| 73 | + .macos => { |
| 74 | + compile_step.linkFramework("SDL2_image"); |
| 75 | + compile_step.root_module.addRPathSpecial("@executable_path"); |
| 76 | + }, |
| 77 | + else => {}, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +pub fn link_SDL3(compile_step: *std.Build.Step.Compile) void { |
| 82 | + switch (compile_step.rootModuleTarget().os.tag) { |
| 83 | + .windows => { |
| 84 | + compile_step.linkSystemLibrary("SDL3"); |
| 85 | + }, |
| 86 | + .linux => { |
| 87 | + compile_step.linkSystemLibrary("SDL3"); |
| 88 | + compile_step.root_module.addRPathSpecial("$ORIGIN"); |
| 89 | + }, |
| 90 | + .macos => { |
| 91 | + compile_step.linkFramework("SDL3"); |
| 92 | + compile_step.root_module.addRPathSpecial("@executable_path"); |
| 93 | + }, |
| 94 | + else => {}, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +pub fn testVersionCheckSDL2(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Step { |
| 99 | + const test_sdl2_version_check = b.addTest(.{ |
| 100 | + .name = "sdl2-version-check", |
| 101 | + .root_source_file = b.dependency("zsdl", .{}).path("src/sdl2_version_check.zig"), |
| 102 | + .target = target, |
| 103 | + .optimize = .ReleaseSafe, |
| 104 | + }); |
| 105 | + |
| 106 | + link_SDL2(test_sdl2_version_check); |
| 107 | + |
| 108 | + prebuilt.addLibraryPathsTo(test_sdl2_version_check); |
| 109 | + |
| 110 | + const version_check_run = b.addRunArtifact(test_sdl2_version_check); |
| 111 | + |
| 112 | + if (target.result.os.tag == .windows) { |
| 113 | + version_check_run.setCwd(.{ |
| 114 | + .cwd_relative = b.getInstallPath(.bin, ""), |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + version_check_run.step.dependOn(&test_sdl2_version_check.step); |
| 119 | + |
| 120 | + if (prebuilt.install_SDL2(b, target.result, .bin)) |install_sdl2_step| { |
| 121 | + version_check_run.step.dependOn(install_sdl2_step); |
| 122 | + } |
| 123 | + |
| 124 | + return &version_check_run.step; |
| 125 | +} |
| 126 | + |
| 127 | +pub const prebuilt = struct { |
| 128 | + pub fn addLibraryPathsTo(compile_step: *std.Build.Step.Compile) void { |
| 129 | + const b = compile_step.step.owner; |
| 130 | + const target = compile_step.rootModuleTarget(); |
| 131 | + switch (target.os.tag) { |
| 132 | + .windows => { |
| 133 | + if (target.cpu.arch.isX86()) { |
| 134 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| { |
| 135 | + compile_step.addLibraryPath(sdl2_prebuilt.path("lib")); |
| 136 | + } |
| 137 | + } |
| 138 | + }, |
| 139 | + .linux => { |
| 140 | + if (target.cpu.arch.isX86()) { |
| 141 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| { |
| 142 | + compile_step.addLibraryPath(sdl2_prebuilt.path("lib")); |
| 143 | + } |
| 144 | + } |
| 145 | + }, |
| 146 | + .macos => { |
| 147 | + if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| { |
| 148 | + compile_step.addFrameworkPath(sdl2_prebuilt.path("Frameworks")); |
| 149 | + } |
| 150 | + }, |
| 151 | + else => {}, |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + pub fn install_SDL2( |
| 156 | + b: *std.Build, |
| 157 | + target: std.Target, |
| 158 | + install_dir: std.Build.InstallDir, |
| 159 | + ) ?*std.Build.Step { |
| 160 | + switch (target.os.tag) { |
| 161 | + .windows => { |
| 162 | + if (target.cpu.arch.isX86()) { |
| 163 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| { |
| 164 | + return &b.addInstallFileWithDir( |
| 165 | + sdl2_prebuilt.path("bin/SDL2.dll"), |
| 166 | + install_dir, |
| 167 | + "SDL2.dll", |
| 168 | + ).step; |
| 169 | + } |
| 170 | + } |
| 171 | + }, |
| 172 | + .linux => { |
| 173 | + if (target.cpu.arch.isX86()) { |
| 174 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| { |
| 175 | + return &b.addInstallFileWithDir( |
| 176 | + sdl2_prebuilt.path("lib/libSDL2.so"), |
| 177 | + install_dir, |
| 178 | + "libSDL2.so", |
| 179 | + ).step; |
| 180 | + } |
| 181 | + } |
| 182 | + }, |
| 183 | + .macos => { |
| 184 | + if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| { |
| 185 | + return &b.addInstallDirectory(.{ |
| 186 | + .source_dir = sdl2_prebuilt.path("Frameworks/SDL2.framework"), |
| 187 | + .install_dir = install_dir, |
| 188 | + .install_subdir = "SDL2.framework", |
| 189 | + }).step; |
| 190 | + } |
| 191 | + }, |
| 192 | + else => {}, |
| 193 | + } |
| 194 | + return null; |
| 195 | + } |
| 196 | + |
| 197 | + pub fn install_SDL2_ttf( |
| 198 | + b: *std.Build, |
| 199 | + target: std.Target, |
| 200 | + install_dir: std.Build.InstallDir, |
| 201 | + ) ?*std.Build.Step { |
| 202 | + switch (target.os.tag) { |
| 203 | + .windows => { |
| 204 | + if (target.cpu.arch.isX86()) { |
| 205 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| { |
| 206 | + return &b.addInstallFileWithDir( |
| 207 | + sdl2_prebuilt.path("bin/SDL2_ttf.dll"), |
| 208 | + install_dir, |
| 209 | + "SDL2_ttf.dll", |
| 210 | + ).step; |
| 211 | + } |
| 212 | + } |
| 213 | + }, |
| 214 | + .linux => { |
| 215 | + if (target.cpu.arch.isX86()) { |
| 216 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| { |
| 217 | + return &b.addInstallFileWithDir( |
| 218 | + sdl2_prebuilt.path("lib/libSDL2_ttf.so"), |
| 219 | + install_dir, |
| 220 | + "libSDL2_ttf.so", |
| 221 | + ).step; |
| 222 | + } |
| 223 | + } |
| 224 | + }, |
| 225 | + .macos => { |
| 226 | + if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| { |
| 227 | + return &b.addInstallDirectory(.{ |
| 228 | + .source_dir = sdl2_prebuilt.path("Frameworks/SDL2_ttf.framework"), |
| 229 | + .install_dir = install_dir, |
| 230 | + .install_subdir = "SDL2_ttf.framework", |
| 231 | + }).step; |
| 232 | + } |
| 233 | + }, |
| 234 | + else => {}, |
| 235 | + } |
| 236 | + return null; |
| 237 | + } |
| 238 | + |
| 239 | + pub fn install_SDL2_image( |
| 240 | + b: *std.Build, |
| 241 | + target: std.Target, |
| 242 | + install_dir: std.Build.InstallDir, |
| 243 | + ) ?*std.Build.Step { |
| 244 | + switch (target.os.tag) { |
| 245 | + .windows => { |
| 246 | + if (target.cpu.arch.isX86()) { |
| 247 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| { |
| 248 | + return &b.addInstallFileWithDir( |
| 249 | + sdl2_prebuilt.path("bin/SDL2_image.dll"), |
| 250 | + install_dir, |
| 251 | + "SDL2_image.dll", |
| 252 | + ).step; |
| 253 | + } |
| 254 | + } |
| 255 | + }, |
| 256 | + .linux => { |
| 257 | + if (target.cpu.arch.isX86()) { |
| 258 | + if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| { |
| 259 | + return &b.addInstallFileWithDir( |
| 260 | + sdl2_prebuilt.path("lib/libSDL2_image.so"), |
| 261 | + install_dir, |
| 262 | + "libSDL2_image.so", |
| 263 | + ).step; |
| 264 | + } |
| 265 | + } |
| 266 | + }, |
| 267 | + .macos => { |
| 268 | + if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| { |
| 269 | + return &b.addInstallDirectory(.{ |
| 270 | + .source_dir = sdl2_prebuilt.path("Frameworks/SDL2_image.framework"), |
| 271 | + .install_dir = install_dir, |
| 272 | + .install_subdir = "SDL2_image.framework", |
| 273 | + }).step; |
| 274 | + } |
| 275 | + }, |
| 276 | + else => {}, |
| 277 | + } |
| 278 | + return null; |
| 279 | + } |
| 280 | +}; |
0 commit comments