Skip to content

Commit

Permalink
fix build.zig for zig 0.12.0-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Jan 6, 2024
1 parent b1a3ec3 commit 8f87730
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const std = @import("std");
const builtin = @import("builtin");
const Build = std.Build;
const CompileStep = std.build.Step.Compile;
const Module = std.build.Module;
const CrossTarget = std.zig.CrossTarget;
const CompileStep = std.Build.Step.Compile;
const Module = std.Build.Module;
const ResolvedTarget = std.Build.ResolvedTarget;
const OptimizeMode = std.builtin.OptimizeMode;

pub const Backend = enum {
Expand All @@ -16,7 +16,7 @@ pub const Backend = enum {
};

pub const LibSokolOptions = struct {
target: CrossTarget,
resolved_target: ResolvedTarget,
optimize: OptimizeMode,
build_root: ?[]const u8 = null,
backend: Backend = .auto,
Expand All @@ -27,13 +27,13 @@ pub const LibSokolOptions = struct {

pub fn build(b: *Build) void {
const force_gl = b.option(bool, "gl", "Force GL backend") orelse false;
const target = b.standardTargetOptions(.{});
const resolved_target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const mod_sokol = b.addModule("sokol", .{ .source_file = .{ .path = "src/sokol/sokol.zig" } });
const mod_sokol = b.addModule("sokol", .{ .root_source_file = .{ .path = "src/sokol/sokol.zig" } });

const lib_sokol = buildLibSokol(b, .{
.target = target,
.resolved_target = resolved_target,
.optimize = optimize,
.backend = if (force_gl) .gl else .auto,
.enable_wayland = b.option(bool, "wayland", "Compile with wayland-support (default: false)") orelse false,
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn build(b: *Build) void {
};
inline for (examples) |example| {
buildExample(b, example, .{
.target = target,
.resolved_target = resolved_target,
.optimize = optimize,
.lib_sokol = lib_sokol,
.mod_sokol = mod_sokol,
Expand All @@ -79,25 +79,26 @@ pub fn build(b: *Build) void {
}

const ExampleOptions = struct {
target: CrossTarget,
resolved_target: ResolvedTarget,
optimize: OptimizeMode,
lib_sokol: *CompileStep,
mod_sokol: *Module,
};

// build sokol into a static library
pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
const is_wasm = options.target.getCpu().arch == .wasm32;
var resolved_target = options.resolved_target;
const is_wasm = resolved_target.result.cpu.arch == .wasm32;

// special case wasm, must compile as wasm32-emscripten, not wasm32-freestanding
var target = options.target;
if (is_wasm) {
target.os_tag = .emscripten;
resolved_target.result.os.tag = .emscripten;
}
const target = resolved_target.result;

const lib = b.addStaticLibrary(.{
.name = "sokol",
.target = target,
.target = resolved_target,
.optimize = options.optimize,
.link_libc = true,
});
Expand Down Expand Up @@ -128,13 +129,13 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
};
var _backend = options.backend;
if (_backend == .auto) {
if (lib.target.isDarwin()) {
if (target.isDarwin()) {
_backend = .metal;
} else if (lib.target.isWindows()) {
} else if (target.os.tag == .windows) {
_backend = .d3d11;
} else if (lib.target.getCpu().arch == .wasm32) {
} else if (target.cpu.arch == .wasm32) {
_backend = .gles3;
} else if (lib.target.getAbi() == .android) {
} else if (target.isAndroid()) {
_backend = .gles3;
} else {
_backend = .gl;
Expand All @@ -149,7 +150,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
else => unreachable,
};

if (lib.target.isDarwin()) {
if (target.isDarwin()) {
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = try std.fmt.allocPrint(b.allocator, "{s}/{s}", .{ sokol_path, csrc }) },
Expand All @@ -162,21 +163,21 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
lib.linkFramework("MetalKit");
lib.linkFramework("Metal");
}
if (lib.target.getOsTag() == .ios) {
if (target.os.tag == .ios) {
lib.linkFramework("UIKit");
lib.linkFramework("AVFoundation");
if (.gl == _backend) {
lib.linkFramework("OpenGLES");
lib.linkFramework("GLKit");
}
} else if (lib.target.getOsTag() == .macos) {
} else if (target.os.tag == .macos) {
lib.linkFramework("Cocoa");
lib.linkFramework("QuartzCore");
if (.gl == _backend) {
lib.linkFramework("OpenGL");
}
}
} else if (lib.target.getAbi() == .android) {
} else if (target.isAndroid()) {
if (.gles3 != _backend) {
@panic("For android targets, you must have backend set to GLES3");
}
Expand All @@ -190,7 +191,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
lib.linkSystemLibrary("EGL");
lib.linkSystemLibrary("android");
lib.linkSystemLibrary("log");
} else if (lib.target.isLinux()) {
} else if (target.os.tag == .linux) {
const egl_flag = if (options.force_egl) "-DSOKOL_FORCE_EGL " else "";
const x11_flag = if (!options.enable_x11) "-DSOKOL_DISABLE_X11 " else "";
const wayland_flag = if (!options.enable_wayland) "-DSOKOL_DISABLE_WAYLAND" else "";
Expand All @@ -217,20 +218,20 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
if (link_egl) {
lib.linkSystemLibrary("egl");
}
} else if (lib.target.isWindows()) {
} else if (target.os.tag == .windows) {
for (csources) |csrc| {
lib.addCSourceFile(.{
.file = .{ .path = try std.fmt.allocPrint(b.allocator, "{s}/{s}", .{ sokol_path, csrc }) },
.flags = &[_][]const u8{ "-DIMPL", backend_option },
});
}
lib.linkSystemLibraryName("kernel32");
lib.linkSystemLibraryName("user32");
lib.linkSystemLibraryName("gdi32");
lib.linkSystemLibraryName("ole32");
lib.linkSystemLibrary("kernel32");
lib.linkSystemLibrary("user32");
lib.linkSystemLibrary("gdi32");
lib.linkSystemLibrary("ole32");
if (.d3d11 == _backend) {
lib.linkSystemLibraryName("d3d11");
lib.linkSystemLibraryName("dxgi");
lib.linkSystemLibrary("d3d11");
lib.linkSystemLibrary("dxgi");
}
} else {
for (csources) |csrc| {
Expand All @@ -248,11 +249,11 @@ fn buildExample(b: *Build, comptime name: []const u8, options: ExampleOptions) v
const e = b.addExecutable(.{
.name = name,
.root_source_file = .{ .path = "src/examples/" ++ name ++ ".zig" },
.target = options.target,
.target = options.resolved_target,
.optimize = options.optimize,
});
e.linkLibrary(options.lib_sokol);
e.addModule("sokol", options.mod_sokol);
e.root_module.addImport("sokol", options.mod_sokol);
b.installArtifact(e);
const run = b.addRunArtifact(e);
b.step("run-" ++ name, "Run " ++ name).dependOn(&run.step);
Expand Down

0 comments on commit 8f87730

Please sign in to comment.