apprt/gtk: add version helpers

This adds version helpers similar to the adwaita version helpers so that
build time and runtime version checks can be done.
This commit is contained in:
Mitchell Hashimoto
2024-09-19 20:12:28 -07:00
parent 38eb9071eb
commit a503e0250e
2 changed files with 50 additions and 0 deletions

View File

@ -85,6 +85,16 @@ quit_timer: union(enum) {
pub fn init(core_app: *CoreApp, opts: Options) !App { pub fn init(core_app: *CoreApp, opts: Options) !App {
_ = opts; _ = opts;
// Log our GTK version
log.info("GTK version build={d}.{d}.{d} runtime={d}.{d}.{d}", .{
c.GTK_MAJOR_VERSION,
c.GTK_MINOR_VERSION,
c.GTK_MICRO_VERSION,
c.gtk_get_major_version(),
c.gtk_get_minor_version(),
c.gtk_get_micro_version(),
});
// We need to export GDK_DEBUG to run on Wayland after GTK 4.14. // We need to export GDK_DEBUG to run on Wayland after GTK 4.14.
// Older versions of GTK do not support these values so it is safe // Older versions of GTK do not support these values so it is safe
// to always set this. Forwards versions are uncertain so we'll have to // to always set this. Forwards versions are uncertain so we'll have to

40
src/apprt/gtk/version.zig Normal file
View File

@ -0,0 +1,40 @@
const c = @import("c.zig").c;
/// Verifies that the GTK version is at least the given version.
///
/// This can be run in both a comptime and runtime context. If it
/// is run in a comptime context, it will only check the version
/// 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 is inlined so that the comptime checks will disable the
/// runtime checks if the comptime checks fail.
pub inline fn atLeast(
comptime major: u16,
comptime minor: u16,
comptime micro: u16,
) bool {
// If our header has lower versions than the given version,
// we can return false immediately. This prevents us from
// compiling against unknown symbols and makes runtime checks
// very slightly faster.
if (comptime c.GTK_MAJOR_VERSION < major or
c.GTK_MINOR_VERSION < minor or
c.GTK_MICRO_VERSION < micro) return false;
// If we're in comptime then we can't check the runtime version.
if (@inComptime()) return true;
// We use the functions instead of the constants such as
// c.GTK_MINOR_VERSION because the function gets the actual
// runtime version.
if (c.gtk_get_major_version() >= major) {
if (c.gtk_get_major_version() > major) return true;
if (c.gtk_get_minor_version() >= minor) {
if (c.gtk_get_minor_version() > minor) return true;
return c.gtk_get_micro_version() >= micro;
}
}
return false;
}