Skip to content

Commit 63e050a

Browse files
Add optional log/assert hooks implemented in Zig
Also introduced a SokolBuildOptions struct to contain backendselection and flags for the Zig-side log and assert hooks (these are distinct)
1 parent 9f77a87 commit 63e050a

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

build.zig

+23-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ pub const Backend = enum {
1515
wgpu,
1616
};
1717

18+
pub const SokolBuildOpts = struct {
19+
zig_assert_hook: bool = false,
20+
zig_log_hook: bool = false,
21+
backend: Backend,
22+
};
23+
1824
// build sokol into a static library
19-
pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, backend: Backend, comptime prefix_path: []const u8) *LibExeObjStep {
20-
const lib = b.addStaticLibrary("sokol", null);
25+
pub fn buildSokol(
26+
b: *Builder,
27+
target: CrossTarget,
28+
mode: Mode,
29+
opts: SokolBuildOpts,
30+
comptime prefix_path: []const u8,
31+
) *LibExeObjStep {
32+
const lib = b.addStaticLibrary("sokol", "src/sokol_zig_hooks.zig");
2133
lib.setBuildMode(mode);
2234
lib.setTarget(target);
2335
lib.linkLibC();
@@ -31,7 +43,7 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, backend: Backend
3143
"sokol_debugtext.c",
3244
"sokol_shape.c",
3345
};
34-
var _backend = backend;
46+
var _backend = opts.backend;
3547
if (_backend == .auto) {
3648
if (lib.target.isDarwin()) { _backend = .metal; }
3749
else if (lib.target.isWindows()) { _backend = .d3d11; }
@@ -46,10 +58,14 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, backend: Backend
4658
.wgpu => "-DSOKOL_WGPU",
4759
else => unreachable,
4860
};
61+
const log_def = if (opts.zig_log_hook) "-DSOKOL_ZIG_LOG_HOOK" else "";
62+
const assert_def = if (opts.zig_assert_hook) "-DSOKOL_ZIG_ASSERT_HOOK" else "";
63+
4964
if (lib.target.isDarwin()) {
5065
inline for (csources) |csrc| {
51-
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{"-ObjC", "-DIMPL", backend_option});
66+
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{"-ObjC", "-DIMPL", backend_option, log_def, assert_def});
5267
}
68+
5369
lib.linkFramework("Cocoa");
5470
lib.linkFramework("QuartzCore");
5571
lib.linkFramework("AudioToolbox");
@@ -62,8 +78,9 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, backend: Backend
6278
}
6379
} else {
6480
inline for (csources) |csrc| {
65-
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{"-DIMPL", backend_option});
81+
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{"-DIMPL", backend_option, log_def, assert_def});
6682
}
83+
6784
if (lib.target.isLinux()) {
6885
lib.linkSystemLibrary("X11");
6986
lib.linkSystemLibrary("Xi");
@@ -102,7 +119,7 @@ pub fn build(b: *Builder) void {
102119

103120
const target = b.standardTargetOptions(.{});
104121
const mode = b.standardReleaseOptions();
105-
const sokol = buildSokol(b, target, mode, backend, "");
122+
const sokol = buildSokol(b, target, mode, .{.backend = backend}, "");
106123
const examples = .{
107124
"clear",
108125
"triangle",

src/sokol/c/sokol_defines.h

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
#define SOKOL_ZIG_BINDINGS
22
#define SOKOL_NO_ENTRY
3-
#if defined(_WIN32)
4-
#define SOKOL_WIN32_FORCE_MAIN
5-
#define SOKOL_LOG(msg) OutputDebugStringA(msg)
3+
4+
#if defined(SOKOL_ZIG_LOG_HOOK)
5+
void sokol_zig_log(const char *s);
6+
#define SOKOL_LOG sokol_zig_log
7+
#else
8+
#if defined(_WIN32)
9+
#define SOKOL_WIN32_FORCE_MAIN
10+
#define SOKOL_LOG(msg) OutputDebugStringA(msg)
11+
#endif
612
#endif
13+
14+
#if defined(SOKOL_ZIG_ASSERT_HOOK)
15+
void sokol_zig_assert(unsigned int c, const char *s);
16+
#define SOKOL_ASSERT(c) sokol_zig_assert(c, #c)
17+
#endif
18+
719
// FIXME: macOS Zig HACK without this, some C stdlib headers throw errors
820
#if defined(__APPLE__)
921
#include <TargetConditionals.h>

src/sokol_zig_hooks.zig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const std = @import("std");
2+
3+
export fn sokol_zig_log(s: [*:0]const u8) callconv(.C) void {
4+
std.log.info("{s}", .{s});
5+
}
6+
7+
export fn sokol_zig_assert(c: c_uint, s: [*:0]const u8) callconv(.C) void {
8+
if (c == 0) {
9+
std.log.err("Assertion: {s}", .{s});
10+
@panic(s[0..std.mem.len(s)]);
11+
}
12+
}

0 commit comments

Comments
 (0)