Hey all,
I am trying to use termbox2 in zig by updating these wrapper bindings here: https://sr.ht/\~kolunmi/termbox2-zig/ . Ideally I would like to use zig 0.16. My current build.zig looks like:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Build termbox-zig
// - taken and updated from https://git.sr.ht/~kolunmi/termbox2-zig
const ztb_opts = b.addOptions();
ztb_opts.addOption(i32, "attr_w", b.option(i32, "attr_w", "integer width of fg and bg attributes (16, 32, 64)") orelse 64);
ztb_opts.addOption(bool, "egc", b.option(bool, "egc", "enable extended grapheme cluster support") orelse false);
ztb_opts.addOption(usize, "printf_buf", b.option(usize, "printf_buf", "buffer size for printf operations") orelse 0);
ztb_opts.addOption(usize, "read_buf", b.option(usize, "read_buf", "buffer size for tty reads") orelse 0);
const ztb_opts_mod = ztb_opts.createModule();
const ztb_mod = b.addModule("ztb", .{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/ztb.zig"),
.link_libc = true,
});
ztb_mod.addImport("ztb_opts", ztb_opts_mod);
ztb_mod.addIncludePath(b.path("include"));
const lib = b.addLibrary(.{
.name = "ztb",
.linkage = .static,
.root_module = ztb_mod,
});
b.installArtifact(lib);
// small demo
// - taken and adapted from https://github.com/karintomania/zig-termbox2-wrapper
const demo_run_step = b.step("demo", "Run src/demo.zig");
const demo = b.addExecutable(.{
.name = "demo",
.root_module = b.addModule("demo-example", .{
.root_source_file = b.path("src/demo.zig"),
.target = target,
.optimize = optimize,
}),
});
demo.root_module.addImport("ztb", ztb_mod);
b.installArtifact(demo);
const demo_run = b.addRunArtifact(demo);
demo_run_step.dependOn(&demo_run.step);
}
When I try to build with zig version: 0.16.0-dev.3133+5ec8e45f3, I encounter the following error:
install
└─ install demo
└─ compile exe demo Debug native 4 errors
src/ztb.zig:5:11: error: C import failed
const c = @cImport({
^~~~~~~~
referenced by:
init: src/ztb.zig:340:16
thread 57358 panic: integer overflow
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/zig/ErrorBundle.zig:218:27: 0x130470e in renderErrorMessage (std.zig)
src.data.line + 1,
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/zig/ErrorBundle.zig:187:31: 0x1303851 in renderToTerminal (std.zig)
try renderErrorMessage(eb, options, err_msg, t, "error", .red, 0);
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/compiler/build_runner.zig:1494:58: 0x12ffafe in printErrorMessages (build_runner.zig)
try failing_step.result_error_bundle.renderToTerminal(options, stderr);
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/compiler/build_runner.zig:1388:27: 0x1472f13 in makeStep (build_runner.zig)
printErrorMessages(gpa, s, .{}, stderr.terminal(), run.error_style, run.multiline_errors) catch {};
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/Io.zig:1238:17: 0x14727a5 in start (std.zig)
_ = @as(Cancelable!void, @call(.auto, function, args_casted.*)) catch {};
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/Io/Threaded.zig:552:22: 0x122c3a5 in start (std.zig)
task.func(task.contextPointer());
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/Io/Threaded.zig:1794:29: 0x122a584 in worker (std.zig)
runnable.startFn(runnable, &thread, t);
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/Thread.zig:422:13: 0x122a265 in callFn__anon_27500 (std.zig)
@call(.auto, f, args);
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/Thread.zig:1431:30: 0x122a020 in entryFn (std.zig)
return callFn(f, self.fn_args);
^
/nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib/std/os/linux/x86_64.zig:105:5: 0x122a185 in clone (std.zig)
asm volatile (
^
error: the following build command terminated with signal ABRT:
.zig-cache/o/d05174c32b08d5612517aba18dcb9d6c/build /nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/bin/zig /nix/store/lv1fqkbld8qqrc81ghbl42z7xmh6m2a4-zig-0.16.0-dev.3133+5ec8e45f3/lib /home/inbelic/ztb .zig-cache /home/inbelic/.cache/zig --seed 0x111388f3 -Z4307e7f26bd20456
which means I can’t see the actual diagnostic error to try and resolve it.
Building with 0.15.1 builds fine and the demo.zig runs as expected. Full source is viewable here: ssh inbelic.dev -p 23231 under the ztb repo.
I don’t currently build the compiler from source to just hackily prevent the overflow, hoping for some leads before going down that route.
Thanks!