diff options
| author | Jacob Banta <[email protected]> | 2026-01-19 19:09:52 -0500 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-20 18:26:19 -0800 |
| commit | b109775ab8a9b6eb0a7f9cf1de3b1727e644eec8 (patch) | |
| tree | 4841308685454f5005bceef37ae42d58a8ba6a4e | |
| parent | change TIGR import to use Zig build system (diff) | |
| download | zigr-b109775ab8a9b6eb0a7f9cf1de3b1727e644eec8.tar.xz zigr-b109775ab8a9b6eb0a7f9cf1de3b1727e644eec8.zip | |
future proofing
| -rwxr-xr-x | bindgen.sh | 5 | ||||
| -rw-r--r-- | bindings.patch | 38 | ||||
| -rw-r--r-- | build.zig | 37 |
3 files changed, 63 insertions, 17 deletions
diff --git a/bindgen.sh b/bindgen.sh deleted file mode 100755 index 3c6c1f4..0000000 --- a/bindgen.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash - -git submodule update --init --recursive - -zig translate-c libs/tigr/tigr.h > src/c.zig diff --git a/bindings.patch b/bindings.patch new file mode 100644 index 0000000..1a831da --- /dev/null +++ b/bindings.patch @@ -0,0 +1,38 @@ +diff --git b/src/c.zig a/src/c.zig +index 19694cd..dfb9b5f 100644 +--- b/src/c.zig ++++ a/src/c.zig +@@ -54,23 +54,8 @@ pub const __builtin_assume = @import("std").zig.c_builtins.__builtin_assume; + pub const __builtin_unreachable = @import("std").zig.c_builtins.__builtin_unreachable; + pub const __builtin_constant_p = @import("std").zig.c_builtins.__builtin_constant_p; + pub const __builtin_mul_overflow = @import("std").zig.c_builtins.__builtin_mul_overflow; +-pub const TPixel = extern struct { +- r: u8 = @import("std").mem.zeroes(u8), +- g: u8 = @import("std").mem.zeroes(u8), +- b: u8 = @import("std").mem.zeroes(u8), +- a: u8 = @import("std").mem.zeroes(u8), +-}; +-pub const struct_Tigr = extern struct { +- w: c_int = @import("std").mem.zeroes(c_int), +- h: c_int = @import("std").mem.zeroes(c_int), +- cx: c_int = @import("std").mem.zeroes(c_int), +- cy: c_int = @import("std").mem.zeroes(c_int), +- cw: c_int = @import("std").mem.zeroes(c_int), +- ch: c_int = @import("std").mem.zeroes(c_int), +- pix: [*c]TPixel = @import("std").mem.zeroes([*c]TPixel), +- handle: ?*anyopaque = @import("std").mem.zeroes(?*anyopaque), +- blitMode: c_int = @import("std").mem.zeroes(c_int), +-}; ++pub const TPixel = @import("main.zig").Pixel; ++pub const struct_Tigr = @import("main.zig").Tigr; + pub const Tigr = struct_Tigr; + pub extern fn tigrWindow(w: c_int, h: c_int, title: [*c]const u8, flags: c_int) [*c]Tigr; + pub extern fn tigrBitmap(w: c_int, h: c_int) [*c]Tigr; +@@ -231,7 +216,6 @@ pub extern fn tigrScrollWheel(bmp: [*c]Tigr, x: [*c]f32, y: [*c]f32) void; + pub extern fn tigrKeyDown(bmp: [*c]Tigr, key: c_int) c_int; + pub extern fn tigrKeyHeld(bmp: [*c]Tigr, key: c_int) c_int; + pub extern fn tigrReadChar(bmp: [*c]Tigr) c_int; +-pub extern fn tigrShowKeyboard(show: c_int) void; + pub extern fn tigrLoadImage(fileName: [*c]const u8) [*c]Tigr; + pub extern fn tigrLoadImageMem(data: ?*const anyopaque, length: c_int) [*c]Tigr; + pub extern fn tigrSaveImage(fileName: [*c]const u8, bmp: [*c]Tigr) c_int; @@ -1,14 +1,5 @@ const std = @import("std"); -fn apply(b: *std.Build, lib: *std.Build.Step.Compile) void { - lib.root_module.link_libc = true; - lib.root_module.addIncludePath(b.dependency("tigr", .{}).path("")); - lib.root_module.addCSourceFile(.{ .file = b.dependency("tigr", .{}).path("tigr.c") }); - lib.root_module.linkSystemLibrary("X11", .{}); - lib.root_module.linkSystemLibrary("GL", .{}); - lib.root_module.linkSystemLibrary("GLU", .{}); -} - pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -19,19 +10,25 @@ pub fn build(b: *std.Build) void { .root_source_file = b.path("src/main.zig"), .sanitize_c = .off, }); + zigr_mod.link_libc = true; + zigr_mod.addIncludePath(b.dependency("tigr", .{}).path("")); + zigr_mod.addCSourceFile(.{ .file = b.dependency("tigr", .{}).path("tigr.c") }); + zigr_mod.linkSystemLibrary("X11", .{}); + zigr_mod.linkSystemLibrary("GL", .{}); + zigr_mod.linkSystemLibrary("GLU", .{}); + const lib = b.addLibrary(.{ .linkage = .static, .name = "zigr", .root_module = zigr_mod, }); - apply(b, lib); b.installArtifact(lib); const tests = b.addTest(.{ .root_module = zigr_mod }); const run_tests = b.addRunArtifact(tests); - const test_step = b.step("test", "Runs all tests"); + const test_step = b.step("test", "Makes sure everything compiles"); test_step.dependOn(&run_tests.step); const hello_example = b.addExecutable(.{ @@ -44,6 +41,22 @@ pub fn build(b: *std.Build) void { }), }); - const examples_step = b.step("examples", "Builds examples"); + const examples_step = b.step("example", "Builds the example"); examples_step.dependOn(&b.addInstallArtifact(hello_example, .{}).step); + + // This command takes the header file from the zig cache, + // and translates it to zig. + const bindgen_cmd = b.addSystemCommand(&.{ "zig", "translate-c" }); + bindgen_cmd.addFileArg(b.dependency("tigr", .{}).path("tigr.h")); + const bindgen_output = bindgen_cmd.captureStdOut(); + // This command takes the output from the previous command, + // and writes it to `src/c.zig`. + const bindgen_copy = b.addSystemCommand(&.{"cp"}); + bindgen_copy.addFileArg(bindgen_output); + bindgen_copy.addFileArg(b.path("src/c.zig")); + + // the extra step should be as simple as running `git apply bindings.patch`, + // but you should run it with `--check` to make sure it still works with the latest versions. + const bindgen_step = b.step("bindgen", "Generates the zig bindings for the latest version of tigr. Takes some manual changes to get working."); + bindgen_step.dependOn(&bindgen_copy.step); } |