From a115e848c63ddd202def0482199d65b5935b73db Mon Sep 17 00:00:00 2001 From: sin-ack Date: Tue, 7 Jan 2025 19:12:53 +0000 Subject: [PATCH] apprt/gtk: Add version.runtimeAtLeast This will be used for version checks that are independent of the version of GTK we built against. --- src/apprt/gtk/version.zig | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/apprt/gtk/version.zig b/src/apprt/gtk/version.zig index af7ad12ea0..d8686fa28a 100644 --- a/src/apprt/gtk/version.zig +++ b/src/apprt/gtk/version.zig @@ -7,6 +7,11 @@ const c = @import("c.zig").c; /// in the headers. If it is run in a runtime context, it will /// check the actual version of the library we are linked against. /// +/// This function should be used in cases where the version check +/// would affect code generation, such as using symbols that are +/// only available beyond a certain version. For checks which only +/// depend on GTK's runtime behavior, use `runtimeAtLeast`. +/// /// This is inlined so that the comptime checks will disable the /// runtime checks if the comptime checks fail. pub inline fn atLeast( @@ -26,6 +31,20 @@ pub inline fn atLeast( // If we're in comptime then we can't check the runtime version. if (@inComptime()) return true; + return runtimeAtLeast(major, minor, micro); +} + +/// Verifies that the GTK version at runtime is at least the given +/// version. +/// +/// This function should be used in cases where the only the runtime +/// behavior is affected by the version check. For checks which would +/// affect code generation, use `atLeast`. +pub inline fn runtimeAtLeast( + comptime major: u16, + comptime minor: u16, + comptime micro: u16, +) bool { // We use the functions instead of the constants such as // c.GTK_MINOR_VERSION because the function gets the actual // runtime version. @@ -44,15 +63,18 @@ test "atLeast" { const std = @import("std"); const testing = std.testing; - try testing.expect(atLeast(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION)); + const funs = &.{ atLeast, runtimeAtLeast }; + inline for (funs) |fun| { + try testing.expect(fun(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION)); - try testing.expect(!atLeast(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION + 1)); - try testing.expect(!atLeast(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION + 1, c.GTK_MICRO_VERSION)); - try testing.expect(!atLeast(c.GTK_MAJOR_VERSION + 1, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION)); + try testing.expect(!fun(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION + 1)); + try testing.expect(!fun(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION + 1, c.GTK_MICRO_VERSION)); + try testing.expect(!fun(c.GTK_MAJOR_VERSION + 1, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION)); - try testing.expect(atLeast(c.GTK_MAJOR_VERSION - 1, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION)); - try testing.expect(atLeast(c.GTK_MAJOR_VERSION - 1, c.GTK_MINOR_VERSION + 1, c.GTK_MICRO_VERSION)); - try testing.expect(atLeast(c.GTK_MAJOR_VERSION - 1, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION + 1)); + try testing.expect(fun(c.GTK_MAJOR_VERSION - 1, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION)); + try testing.expect(fun(c.GTK_MAJOR_VERSION - 1, c.GTK_MINOR_VERSION + 1, c.GTK_MICRO_VERSION)); + try testing.expect(fun(c.GTK_MAJOR_VERSION - 1, c.GTK_MINOR_VERSION, c.GTK_MICRO_VERSION + 1)); - try testing.expect(atLeast(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION - 1, c.GTK_MICRO_VERSION + 1)); + try testing.expect(fun(c.GTK_MAJOR_VERSION, c.GTK_MINOR_VERSION - 1, c.GTK_MICRO_VERSION + 1)); + } }