diff --git a/libs/glfw/build.zig b/libs/glfw/build.zig index dd231562..86768f85 100644 --- a/libs/glfw/build.zig +++ b/libs/glfw/build.zig @@ -1,3 +1,4 @@ +const builtin = @import("builtin"); const std = @import("std"); const Builder = std.build.Builder; @@ -68,7 +69,21 @@ pub const pkg = std.build.Pkg{ .source = .{ .path = thisDir() ++ "/src/main.zig" }, }; +// TODO(self-hosted): HACK: workaround https://github.com/ziglang/zig/issues/12483 +// +// Extracted from a build using stage1 from zig-cache/ (`cimport/c_darwin[_native].zig`) +// Then find+replace `= ?fn` -> `= ?*const fn` +fn cimportWorkaround() void { + const dest_dir = std.fs.cwd().openDir(thisDir() ++ "/src", .{}) catch unreachable; + const cn_path = thisDir() ++ "/src/cimport/" ++ if (builtin.os.tag == .macos) "c_darwin1.zig" else "c_normal_native.zig"; + const c_path = thisDir() ++ "/src/cimport/" ++ if (builtin.os.tag == .macos) "c_darwin2.zig" else "c_normal.zig"; + std.fs.cwd().copyFile(cn_path, dest_dir, thisDir() ++ "/src/c_native.zig", .{}) catch unreachable; + std.fs.cwd().copyFile(c_path, dest_dir, thisDir() ++ "/src/c.zig", .{}) catch unreachable; +} + pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void { + cimportWorkaround(); + const lib = buildLibrary(b, step.build_mode, step.target, options); step.linkLibrary(lib); addGLFWIncludes(step); diff --git a/libs/glfw/src/c.zig b/libs/glfw/src/c.zig deleted file mode 100644 index 10268859..00000000 --- a/libs/glfw/src/c.zig +++ /dev/null @@ -1,11 +0,0 @@ -pub const c = if (@import("builtin").zig_backend == .stage1 or !@import("builtin").target.isDarwin()) - @cImport({ - @cDefine("GLFW_INCLUDE_VULKAN", "1"); - @cInclude("GLFW/glfw3.h"); - }) -else - // TODO(self-hosted): HACK: workaround https://github.com/ziglang/zig/issues/12483 - // - // Extracted from a build using stage1 from zig-cache/ (`cimport.zig`) - // Then find+replace `= ?fn` -> `= ?*const fn` - @import("cimport2.zig"); diff --git a/libs/glfw/src/cimport2.zig b/libs/glfw/src/cimport/c_darwin.zig similarity index 99% rename from libs/glfw/src/cimport2.zig rename to libs/glfw/src/cimport/c_darwin.zig index 6fad1322..c43600ea 100644 --- a/libs/glfw/src/cimport2.zig +++ b/libs/glfw/src/cimport/c_darwin.zig @@ -1,3 +1,4 @@ +pub const c = @This(); pub const __builtin_bswap16 = @import("std").zig.c_builtins.__builtin_bswap16; pub const __builtin_bswap32 = @import("std").zig.c_builtins.__builtin_bswap32; pub const __builtin_bswap64 = @import("std").zig.c_builtins.__builtin_bswap64; diff --git a/libs/glfw/src/cimport1.zig b/libs/glfw/src/cimport/c_darwin_native.zig similarity index 99% rename from libs/glfw/src/cimport1.zig rename to libs/glfw/src/cimport/c_darwin_native.zig index 3d819d3d..21a910cb 100644 --- a/libs/glfw/src/cimport1.zig +++ b/libs/glfw/src/cimport/c_darwin_native.zig @@ -1,3 +1,6 @@ +pub fn import(comptime _: anytype) void { + return @This(); +} pub const __builtin_bswap16 = @import("std").zig.c_builtins.__builtin_bswap16; pub const __builtin_bswap32 = @import("std").zig.c_builtins.__builtin_bswap32; pub const __builtin_bswap64 = @import("std").zig.c_builtins.__builtin_bswap64; diff --git a/libs/glfw/src/cimport/c_normal.zig b/libs/glfw/src/cimport/c_normal.zig new file mode 100644 index 00000000..98063687 --- /dev/null +++ b/libs/glfw/src/cimport/c_normal.zig @@ -0,0 +1,4 @@ +pub const c = @cImport({ + @cDefine("GLFW_INCLUDE_VULKAN", "1"); + @cInclude("GLFW/glfw3.h"); +}); diff --git a/libs/glfw/src/cimport/c_normal_native.zig b/libs/glfw/src/cimport/c_normal_native.zig new file mode 100644 index 00000000..3d3662b2 --- /dev/null +++ b/libs/glfw/src/cimport/c_normal_native.zig @@ -0,0 +1,16 @@ +pub fn import(comptime options: anytype) type { + return @cImport({ + @cDefine("GLFW_INCLUDE_VULKAN", "1"); + @cInclude("GLFW/glfw3.h"); + if (options.win32) @cDefine("GLFW_EXPOSE_NATIVE_WIN32", "1"); + if (options.wgl) @cDefine("GLFW_EXPOSE_NATIVE_WGL", "1"); + if (options.cocoa) @cDefine("GLFW_EXPOSE_NATIVE_COCOA", "1"); + if (options.nsgl) @cDefine("GLFW_EXPOSE_NATIVE_NGSL", "1"); + if (options.x11) @cDefine("GLFW_EXPOSE_NATIVE_X11", "1"); + if (options.glx) @cDefine("GLFW_EXPOSE_NATIVE_GLX", "1"); + if (options.wayland) @cDefine("GLFW_EXPOSE_NATIVE_WAYLAND", "1"); + if (options.egl) @cDefine("GLFW_EXPOSE_NATIVE_EGL", "1"); + if (options.osmesa) @cDefine("GLFW_EXPOSE_NATIVE_OSMESA", "1"); + @cInclude("GLFW/glfw3native.h"); + }); +} diff --git a/libs/glfw/src/native.zig b/libs/glfw/src/native.zig index 75dd1f79..04dd6e40 100644 --- a/libs/glfw/src/native.zig +++ b/libs/glfw/src/native.zig @@ -40,28 +40,7 @@ pub const BackendOptions = struct { /// The chosen backends must match those the library was compiled for. Failure to do so /// will cause a link-time error. pub fn Native(comptime options: BackendOptions) type { - const native = if (@import("builtin").zig_backend == .stage1 or !@import("builtin").target.isDarwin()) - @cImport({ - @cDefine("GLFW_INCLUDE_VULKAN", "1"); - @cInclude("GLFW/glfw3.h"); - - if (options.win32) @cDefine("GLFW_EXPOSE_NATIVE_WIN32", "1"); - if (options.wgl) @cDefine("GLFW_EXPOSE_NATIVE_WGL", "1"); - if (options.cocoa) @cDefine("GLFW_EXPOSE_NATIVE_COCOA", "1"); - if (options.nsgl) @cDefine("GLFW_EXPOSE_NATIVE_NGSL", "1"); - if (options.x11) @cDefine("GLFW_EXPOSE_NATIVE_X11", "1"); - if (options.glx) @cDefine("GLFW_EXPOSE_NATIVE_GLX", "1"); - if (options.wayland) @cDefine("GLFW_EXPOSE_NATIVE_WAYLAND", "1"); - if (options.egl) @cDefine("GLFW_EXPOSE_NATIVE_EGL", "1"); - if (options.osmesa) @cDefine("GLFW_EXPOSE_NATIVE_OSMESA", "1"); - @cInclude("GLFW/glfw3native.h"); - }) - else - // TODO(self-hosted): HACK: workaround https://github.com/ziglang/zig/issues/12483 - // - // Extracted from a build using stage1 from zig-cache/ (`cimport.zig`) - // Then find+replace `= ?fn` -> `= ?*const fn` - @import("cimport1.zig"); + const native = @import("c_native.zig").import(options); return struct { /// Returns the adapter device name of the specified monitor.