Skip to content

Commit

Permalink
apprt/gtk: Add version.runtimeAtLeast
Browse files Browse the repository at this point in the history
This will be used for version checks that are independent of the version
of GTK we built against.
  • Loading branch information
sin-ack committed Jan 7, 2025
1 parent 0065aae commit a115e84
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/apprt/gtk/version.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
Expand All @@ -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));
}
}

0 comments on commit a115e84

Please sign in to comment.