Skip to content

Commit

Permalink
build.zig: add an isPlatform() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Feb 24, 2025
1 parent 6d14eaa commit a0f7339
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ pub const SokolBackend = enum {
wgpu,
};

pub const TargetPlatform = enum {
android,
linux,
darwin, // macos and ios
macos,
ios,
windows,
web,
};

pub fn isPlatform(target: std.Target, platform: TargetPlatform) bool {
return switch (platform) {
.android => target.abi.isAndroid(),
.linux => target.os.tag == .linux,
.darwin => target.os.tag.isDarwin(),
.macos => target.os.tag == .macos,
.ios => target.os.tag == .ios,
.windows => target.os.tag == .windows,
.web => target.cpu.arch.isWasm(),
};
}

pub fn build(b: *Build) !void {
const opt_use_gl = b.option(bool, "gl", "Force OpenGL (default: false)") orelse false;
const opt_use_gles3 = b.option(bool, "gles3", "Force OpenGL ES3 (default: false)") orelse false;
Expand Down Expand Up @@ -94,7 +116,7 @@ const ExampleOptions = struct {
fn buildExample(b: *Build, comptime name: []const u8, options: ExampleOptions) !void {
const main_src = "src/examples/" ++ name ++ ".zig";
var run: ?*Build.Step.Run = null;
if (!options.target.result.cpu.arch.isWasm()) {
if (!isPlatform(options.target.result, .web)) {
// for native platforms, build into a regular executable
const example = b.addExecutable(.{
.name = name,
Expand Down Expand Up @@ -140,13 +162,13 @@ fn buildExample(b: *Build, comptime name: []const u8, options: ExampleOptions) !
pub fn resolveSokolBackend(backend: SokolBackend, target: std.Target) SokolBackend {
if (backend != .auto) {
return backend;
} else if (target.os.tag.isDarwin()) {
} else if (isPlatform(target, .darwin)) {
return .metal;
} else if (target.os.tag == .windows) {
} else if (isPlatform(target, .windows)) {
return .d3d11;
} else if (target.cpu.arch.isWasm()) {
} else if (isPlatform(target, .web)) {
return .gles3;
} else if (target.abi.isAndroid()) {
} else if (isPlatform(target, .android)) {
return .gles3;
} else {
return .gl;
Expand Down Expand Up @@ -194,7 +216,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*Build.Step.Compile {
// sokol is used as package manager dependency via 'dep_sokol.artifact("sokol_clib")'
b.installArtifact(lib);

if (options.target.result.cpu.arch.isWasm()) {
if (isPlatform(options.target.result, .web)) {
// make sure we're building for the wasm32-emscripten target, not wasm32-freestanding
if (lib.rootModuleTarget().os.tag != .emscripten) {
std.log.err("Please build with 'zig build -Dtarget=wasm32-emscripten", .{});
Expand Down Expand Up @@ -500,7 +522,7 @@ fn buildShaders(b: *Build, target: Build.ResolvedTarget) void {
}
const shdc_path = sokol_tools_bin_dir ++ optional_shdc.?;
const shdc_step = b.step("shaders", "Compile shaders (needs ../sokol-tools-bin)");
const glsl = if (target.result.os.tag.isDarwin()) "glsl410" else "glsl430";
const glsl = if (isPlatform(target.result, .darwin)) "glsl410" else "glsl430";
const slang = glsl ++ ":metal_macos:hlsl5:glsl300es:wgsl";
inline for (shaders) |shader| {
const cmd = b.addSystemCommand(&.{
Expand Down

0 comments on commit a0f7339

Please sign in to comment.