summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-16 22:57:31 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit88c04dd6c392c6d8fc3a02408506e85d86f476d5 (patch)
tree4462fcb19dce110c24595f000bd8303c69288689 /src/host
parent1cb36ee39a8bbc2cf61c28003670d760841d6d3e (diff)
downloadsketchbook-88c04dd6c392c6d8fc3a02408506e85d86f476d5.tar.gz
sketchbook-88c04dd6c392c6d8fc3a02408506e85d86f476d5.zip
Refuse a sketch library built by another compiler, backend or mode
abi.hash now covers the zig version, codegen backend and optimize mode: Zig's own calling convention (Host.VTable, every Allocator) differs between them, so a Debug host loading a ReleaseFast or LLVM library faulted on its first draw. Libraries export sketch_build, and the host's refusal says what differs and what to run instead.
Diffstat (limited to 'src/host')
-rw-r--r--src/host/library.zig20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/host/library.zig b/src/host/library.zig
index 6958eec..f8bc4e8 100644
--- a/src/host/library.zig
+++ b/src/host/library.zig
@@ -13,7 +13,8 @@ pub const Library = struct {
object: *c.SDL_SharedObject,
sketch: abi.Sketch,
- pub fn open(path: [:0]const u8) !Library {
+ /// Loads the library at `path`; `source` names it in messages.
+ pub fn open(path: [:0]const u8, source: []const u8) !Library {
const object = c.SDL_LoadObject(path.ptr) orelse return error.LoadLibrary;
errdefer c.SDL_UnloadObject(object);
var sketch: abi.Sketch = undefined;
@@ -22,7 +23,20 @@ pub const Library = struct {
const address = c.SDL_LoadFunction(object, symbol) orelse return error.MissingEntryPoint;
@field(sketch, name) = @ptrCast(address);
}
- if (sketch.info().abi != abi.hash) return error.AbiMismatch;
+ if (sketch.info().abi != abi.hash) {
+ const address = c.SDL_LoadFunction(object, "sketch_build") orelse {
+ std.log.err("{s}: built by an older sketchbook: rebuild it", .{source});
+ return error.AbiMismatch;
+ };
+ const build = std.mem.span(@as([*:0]const u8, @ptrFromInt(@intFromPtr(address))));
+ if (std.mem.eql(u8, build, abi.build)) {
+ std.log.err("{s}: built against other abi types: restart the host to load it", .{source});
+ } else {
+ const mode = build[std.mem.lastIndexOfScalar(u8, build, ' ').? + 1 ..];
+ std.log.err("{s}: built by zig {s}, this host by zig {s}; Zig calls cross the boundary, so both need the same compiler, backend and mode: build the host like the library (`zig build run -Doptimize={s}`) or the library like the host", .{ source, build, abi.build, mode });
+ }
+ return error.AbiMismatch;
+ }
return .{ .object = object, .sketch = sketch };
}
@@ -33,7 +47,7 @@ pub const Library = struct {
const cwd = std.Io.Dir.cwd();
try cwd.copyFile(source, cwd, copy, io, .{});
defer cwd.deleteFile(io, copy) catch {};
- return open(copy);
+ return open(copy, source);
}
pub fn close(self: *Library) void {