support other Zig versions in addition to Zig 2024.10

Support for Zig 0.12.1, 0.13.0, master, etc. is only incidental and is
not as thoroughly tested. mach-glfw only officially targets Zig 2024.10.
This commit is contained in:
Carl Åstholm 2024-10-09 23:19:52 +02:00
parent f31b5a5f60
commit 1338578255
9 changed files with 97 additions and 19 deletions

3
.gitignore vendored
View file

@ -16,3 +16,6 @@ zig-out/
/build/
/build-*/
/docgen_tmp/
# remove this after support for Zig 0.12.1 is dropped
zig-cache/

View file

@ -12,12 +12,10 @@ Perfected GLFW bindings for Zig
* Zero-fuss installation, cross-compilation at the flip of a switch, and broad platform support.
* 100% API coverage. Every function, type, constant, etc. has been exposed in a ziggified API.
See also: [What does a ziggified GLFW API offer?](https://machengine.org/pkg/mach-glfw/)
See also: [What does a ziggified GLFW API offer?](https://machengine.org/v0.4/pkg/mach-glfw/)
## Community maintained
The [Mach engine](https://machengine.org/) project no longer uses GLFW, and so this project is now community-maintained. Pull requests are welcome and will be reviewed. The project will still target [nominated Zig versions](https://machengine.org/about/zig-version/) but may not see regular updates as it is no longer a Mach project (see [hexops/mach#1166](https://github.com/hexops/mach/issues/1166)).
Note: [hexops/glfw]()
The [Mach engine](https://machengine.org/) project no longer uses GLFW, and so this project is now community-maintained. Pull requests are welcome and will be reviewed. The project will still target [nominated Zig versions](https://machengine.org/docs/nominated-zig/) (and may only incidentally work on other Zig versions) but may not see regular updates as it is no longer a Mach project (see [hexops/mach#1166](https://github.com/hexops/mach/issues/1166)).
Some old documentation is available at https://machengine.org/v0.4/pkg/mach-glfw/

View file

@ -43,10 +43,3 @@ pub fn build(b: *std.Build) !void {
}
}
}
comptime {
const supported_zig = std.SemanticVersion.parse("0.14.0-dev.1911+3bf89f55c") catch unreachable;
if (builtin.zig_version.order(supported_zig) != .eq) {
@compileError(std.fmt.comptimePrint("unsupported Zig version ({}). Required Zig version 2024.10.0-mach: https://machengine.org/docs/nominated-zig/#2024100-mach", .{builtin.zig_version}));
}
}

View file

@ -1677,7 +1677,7 @@ pub inline fn setInputMode(self: Window, mode: InputMode, value: anytype) void {
internal_debug.assertInitialized();
const T = @TypeOf(value);
std.debug.assert(switch (mode) {
.cursor => switch (@typeInfo(T)) {
.cursor => switch (@import("shims.zig").typeInfo(T)) {
.@"enum" => T == InputModeCursor,
.enum_literal => @hasField(InputModeCursor, @tagName(value)),
else => false,
@ -1687,7 +1687,7 @@ pub inline fn setInputMode(self: Window, mode: InputMode, value: anytype) void {
.lock_key_mods => T == bool,
.raw_mouse_motion => T == bool,
});
const int_value: c_int = switch (@typeInfo(T)) {
const int_value: c_int = switch (@import("shims.zig").typeInfo(T)) {
.@"enum",
.enum_literal,
=> @intFromEnum(@as(InputModeCursor, value)),
@ -2152,7 +2152,7 @@ pub inline fn setDropCallback(self: Window, comptime callback: ?fn (window: Wind
inline fn hint(h: Hint, value: anytype) void {
internal_debug.assertInitialized();
const value_type = @TypeOf(value);
const value_type_info: std.builtin.Type = @typeInfo(value_type);
const value_type_info: @import("shims.zig").std.builtin.Type = @import("shims.zig").typeInfo(value_type);
switch (value_type_info) {
.int, .comptime_int => {
@ -2173,7 +2173,7 @@ inline fn hint(h: Hint, value: anytype) void {
c.glfwWindowHintString(@intFromEnum(h), &value[0]);
},
.pointer => |pointer_info| {
const pointed_type = @typeInfo(pointer_info.child);
const pointed_type = @import("shims.zig").typeInfo(pointer_info.child);
switch (pointed_type) {
.array => |arr_type| {
if (arr_type.child != u8) {

View file

@ -17,7 +17,7 @@ pub const Hat = packed struct(u8) {
inline fn verifyIntType(comptime IntType: type) void {
comptime {
switch (@typeInfo(IntType)) {
switch (@import("shims.zig").typeInfo(IntType)) {
.int => {},
else => @compileError("Int was not of int type"),
}

View file

@ -303,7 +303,7 @@ pub const PlatformType = enum(c_int) {
///
/// @thread_safety This function must only be called from the main thread.
fn initHint(hint: InitHint, value: anytype) void {
switch (@typeInfo(@TypeOf(value))) {
switch (@import("shims.zig").typeInfo(@TypeOf(value))) {
.int, .comptime_int => {
c.glfwInitHint(@intFromEnum(hint), @as(c_int, @intCast(value)));
},

View file

@ -17,7 +17,7 @@ pub const Mods = packed struct(u8) {
inline fn verifyIntType(comptime IntType: type) void {
comptime {
switch (@typeInfo(IntType)) {
switch (@import("shims.zig").typeInfo(IntType)) {
.int => {},
else => @compileError("Int was not of int type"),
}

84
src/shims.zig Normal file
View file

@ -0,0 +1,84 @@
// Zig 0.14.0-dev changed the names of all 'std.builtin.Type' fields.
const old_std_builtin_type_field_names = @hasField(@import("std").builtin.Type, "Type");
pub const std = struct {
pub const builtin = struct {
pub const Type = if (old_std_builtin_type_field_names) union(enum) {
type: void,
void: void,
bool: void,
noreturn: void,
int: Int,
float: Float,
pointer: Pointer,
array: Array,
@"struct": Struct,
comptime_float: void,
comptime_int: void,
undefined: void,
null: void,
optional: Optional,
error_union: ErrorUnion,
error_set: ErrorSet,
@"enum": Enum,
@"union": Union,
@"fn": Fn,
@"opaque": Opaque,
frame: Frame,
@"anyframe": AnyFrame,
vector: Vector,
enum_literal: void,
pub const Int = @import("std").builtin.Type.Int;
pub const Float = @import("std").builtin.Type.Float;
pub const Pointer = @import("std").builtin.Type.Pointer;
pub const Array = @import("std").builtin.Type.Array;
pub const ContainerLayout = @import("std").builtin.Type.ContainerLayout;
pub const StructField = @import("std").builtin.Type.StructField;
pub const Struct = @import("std").builtin.Type.Struct;
pub const Optional = @import("std").builtin.Type.Optional;
pub const ErrorUnion = @import("std").builtin.Type.ErrorUnion;
pub const Error = @import("std").builtin.Type.Error;
pub const ErrorSet = @import("std").builtin.Type.ErrorSet;
pub const EnumField = @import("std").builtin.Type.EnumField;
pub const Enum = @import("std").builtin.Type.Enum;
pub const UnionField = @import("std").builtin.Type.UnionField;
pub const Union = @import("std").builtin.Type.Union;
pub const Fn = @import("std").builtin.Type.Fn;
pub const Opaque = @import("std").builtin.Type.Opaque;
pub const Frame = @import("std").builtin.Type.Frame;
pub const AnyFrame = @import("std").builtin.Type.AnyFrame;
pub const Vector = @import("std").builtin.Type.Vector;
pub const Declaration = @import("std").builtin.Type.Declaration;
} else @import("std").builtin.Type;
};
};
pub fn typeInfo(comptime T: type) std.builtin.Type {
return if (old_std_builtin_type_field_names) switch (@typeInfo(T)) {
.Type => .type,
.Void => .void,
.Bool => .bool,
.NoReturn => .noreturn,
.Int => |x| .{ .int = x },
.Float => |x| .{ .float = x },
.Pointer => |x| .{ .pointer = x },
.Array => |x| .{ .array = x },
.Struct => |x| .{ .@"struct" = x },
.ComptimeFloat => .comptime_float,
.ComptimeInt => .comptime_int,
.Undefined => .undefined,
.Null => .null,
.Optional => |x| .{ .optional = x },
.ErrorUnion => |x| .{ .error_union = x },
.ErrorSet => |x| .{ .error_set = x },
.Enum => |x| .{ .@"enum" = x },
.Union => |x| .{ .@"union" = x },
.Fn => |x| .{ .@"fn" = x },
.Opaque => |x| .{ .@"opaque" = x },
.Frame => |x| .{ .frame = x },
.AnyFrame => .@"anyframe",
.Vector => |x| .{ .vector = x },
.EnumLiteral => .enum_literal,
} else @typeInfo(T);
}

View file

@ -231,7 +231,7 @@ pub inline fn createWindowSurface(vk_instance: anytype, window: Window, vk_alloc
internal_debug.assertInitialized();
// zig-vulkan uses enums to represent opaque pointers:
// pub const Instance = enum(usize) { null_handle = 0, _ };
const instance: c.VkInstance = switch (@typeInfo(@TypeOf(vk_instance))) {
const instance: c.VkInstance = switch (@import("shims.zig").typeInfo(@TypeOf(vk_instance))) {
.@"enum" => @as(c.VkInstance, @ptrFromInt(@intFromEnum(vk_instance))),
else => @as(c.VkInstance, @ptrCast(vk_instance)),
};