glfw: support compiling with stage2 (-fno-stage1) (#365)

This commit is contained in:
PiergiorgioZagaria 2022-06-24 19:12:45 +02:00 committed by GitHub
parent 19c7d0b0a6
commit 952ea5c3c5
7 changed files with 35 additions and 32 deletions

View file

@ -61,7 +61,7 @@ fn buildLibrary(b: *Builder, step: *std.build.LibExeObjStep, options: Options) *
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
const main_abs = std.fs.path.join(b.allocator, &.{ thisDir(), "src/main.zig" }) catch unreachable;
const main_abs = std.fs.path.join(b.allocator, &.{ (comptime thisDir()), "src/main.zig" }) catch unreachable;
const lib = b.addStaticLibrary("glfw", main_abs);
lib.setBuildMode(step.build_mode);
lib.setTarget(step.target);
@ -69,16 +69,16 @@ fn buildLibrary(b: *Builder, step: *std.build.LibExeObjStep, options: Options) *
// TODO(build-system): pass system SDK options through
system_sdk.include(b, step, .{});
const target = (std.zig.system.NativeTargetInfo.detect(b.allocator, step.target) catch unreachable).target;
const include_glfw_src = "-I" ++ thisDir() ++ "/upstream/glfw/src";
const include_glfw_src = "-I" ++ (comptime thisDir()) ++ "/upstream/glfw/src";
switch (target.os.tag) {
.windows => lib.addCSourceFiles(&.{
thisDir() ++ "/src/sources_all.c",
thisDir() ++ "/src/sources_windows.c",
(comptime thisDir()) ++ "/src/sources_all.c",
(comptime thisDir()) ++ "/src/sources_windows.c",
}, &.{ "-D_GLFW_WIN32", include_glfw_src }),
.macos => lib.addCSourceFiles(&.{
thisDir() ++ "/src/sources_all.c",
thisDir() ++ "/src/sources_macos.m",
thisDir() ++ "/src/sources_macos.c",
(comptime thisDir()) ++ "/src/sources_all.c",
(comptime thisDir()) ++ "/src/sources_macos.m",
(comptime thisDir()) ++ "/src/sources_macos.c",
}, &.{ "-D_GLFW_COCOA", include_glfw_src }),
else => {
// TODO(future): for now, Linux must be built with glibc, not musl:
@ -92,17 +92,17 @@ fn buildLibrary(b: *Builder, step: *std.build.LibExeObjStep, options: Options) *
var sources = std.ArrayList([]const u8).init(b.allocator);
var flags = std.ArrayList([]const u8).init(b.allocator);
sources.append(thisDir() ++ "/src/sources_all.c") catch unreachable;
sources.append(thisDir() ++ "/src/sources_linux.c") catch unreachable;
sources.append((comptime thisDir()) ++ "/src/sources_all.c") catch unreachable;
sources.append((comptime thisDir()) ++ "/src/sources_linux.c") catch unreachable;
if (options.x11) {
sources.append(thisDir() ++ "/src/sources_linux_x11.c") catch unreachable;
sources.append((comptime thisDir()) ++ "/src/sources_linux_x11.c") catch unreachable;
flags.append("-D_GLFW_X11") catch unreachable;
}
if (options.wayland) {
sources.append(thisDir() ++ "/src/sources_linux_wayland.c") catch unreachable;
sources.append((comptime thisDir()) ++ "/src/sources_linux_wayland.c") catch unreachable;
flags.append("-D_GLFW_WAYLAND") catch unreachable;
}
flags.append("-I" ++ thisDir() ++ "/upstream/glfw/src") catch unreachable;
flags.append("-I" ++ (comptime thisDir()) ++ "/upstream/glfw/src") catch unreachable;
lib.addCSourceFiles(sources.items, flags.items);
},
@ -117,7 +117,7 @@ fn ensureDependencySubmodule(allocator: std.mem.Allocator, path: []const u8) !vo
if (std.mem.eql(u8, no_ensure_submodules, "true")) return;
} else |_| {}
var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator);
child.cwd = thisDir();
child.cwd = (comptime thisDir());
child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut();
@ -129,11 +129,11 @@ fn thisDir() []const u8 {
}
fn linkGLFWDependencies(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
const include_dir = std.fs.path.join(b.allocator, &.{ thisDir(), "upstream/glfw/include" }) catch unreachable;
const include_dir = std.fs.path.join(b.allocator, &.{ (comptime thisDir()), "upstream/glfw/include" }) catch unreachable;
defer b.allocator.free(include_dir);
step.addIncludeDir(include_dir);
const vulkan_include_dir = std.fs.path.join(b.allocator, &.{ thisDir(), "upstream/vulkan_headers/include" }) catch unreachable;
const vulkan_include_dir = std.fs.path.join(b.allocator, &.{ (comptime thisDir()), "upstream/vulkan_headers/include" }) catch unreachable;
defer b.allocator.free(vulkan_include_dir);
step.addIncludeDir(vulkan_include_dir);

View file

@ -255,7 +255,7 @@ pub inline fn getName(self: Joystick) error{PlatformError}!?[:0]const u8 {
else => unreachable,
};
return if (name_opt) |name|
std.mem.span(name)
std.mem.span(@ptrCast([*:0]const u8, name))
else
null;
}
@ -297,7 +297,7 @@ pub inline fn getGUID(self: Joystick) error{PlatformError}!?[:0]const u8 {
else => unreachable,
};
return if (guid_opt) |guid|
std.mem.span(guid)
std.mem.span(@ptrCast([*:0]const u8, guid))
else
null;
}
@ -490,7 +490,7 @@ pub inline fn getGamepadName(self: Joystick) ?[:0]const u8 {
else => unreachable,
};
return if (name_opt) |name|
std.mem.span(name)
std.mem.span(@ptrCast([*:0]const u8, name))
else
null;
}
@ -603,7 +603,8 @@ test "setUserPointer_syntax" {
const joystick = glfw.Joystick{ .jid = .one };
// Must be called from joystick callback, we cannot test it.
_ = joystick.setUserPointer;
_ = joystick;
_ = setUserPointer;
}
test "getUserPointer_syntax" {
@ -614,7 +615,8 @@ test "getUserPointer_syntax" {
const joystick = glfw.Joystick{ .jid = .one };
// Must be called from joystick callback, we cannot test it.
_ = joystick.getUserPointer;
_ = joystick;
_ = getUserPointer;
}
test "setCallback" {

View file

@ -164,7 +164,7 @@ pub inline fn getContentScale(self: Monitor) error{PlatformError}!ContentScale {
/// see also: monitor_properties
pub inline fn getName(self: Monitor) [*:0]const u8 {
internal_debug.assertInitialized();
if (c.glfwGetMonitorName(self.handle)) |name| return name;
if (c.glfwGetMonitorName(self.handle)) |name| return @ptrCast([*:0]const u8, name);
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
else => unreachable,
@ -239,7 +239,7 @@ pub inline fn getVideoModes(self: Monitor, allocator: mem.Allocator) (mem.Alloca
const slice = try allocator.alloc(VideoMode, @intCast(u32, count));
var i: u32 = 0;
while (i < count) : (i += 1) {
slice[i] = VideoMode{ .handle = modes[i] };
slice[i] = VideoMode{ .handle = @ptrCast([*c]const c.GLFWvidmode, modes)[i] };
}
return slice;
}
@ -390,7 +390,7 @@ pub inline fn getAll(allocator: mem.Allocator) mem.Allocator.Error![]Monitor {
const slice = try allocator.alloc(Monitor, @intCast(u32, count));
var i: u32 = 0;
while (i < count) : (i += 1) {
slice[i] = Monitor{ .handle = monitors[i].? };
slice[i] = Monitor{ .handle = @ptrCast([*c]const ?*c.GLFWmonitor, monitors)[i].? };
}
return slice;
}

View file

@ -48,7 +48,7 @@ pub inline fn setClipboardString(value: [*:0]const u8) error{PlatformError}!void
/// see also: clipboard, glfwSetClipboardString
pub inline fn getClipboardString() error{ FormatUnavailable, PlatformError }![:0]const u8 {
internal_debug.assertInitialized();
if (c.glfwGetClipboardString(null)) |c_str| return std.mem.span(c_str);
if (c.glfwGetClipboardString(null)) |c_str| return std.mem.span(@ptrCast([*:0]const u8, c_str));
getError() catch |err| return switch (err) {
Error.NotInitialized => unreachable,
Error.FormatUnavailable, Error.PlatformError => |e| e,

View file

@ -223,7 +223,7 @@ pub const Key = enum(c_int) {
else => unreachable,
};
return if (name_opt) |name|
std.mem.span(name)
std.mem.span(@ptrCast([*:0]const u8, name))
else
null;
}

View file

@ -288,7 +288,7 @@ fn initHint(hint: InitHint, value: anytype) void {
///
/// thread_safety: This function may be called from any thread.
pub inline fn getVersionString() [:0]const u8 {
return std.mem.span(c.glfwGetVersionString());
return std.mem.span(@ptrCast([*:0]const u8, c.glfwGetVersionString()));
}
/// Returns the currently selected platform.

View file

@ -137,7 +137,7 @@ pub inline fn extensionSupported(extension: [:0]const u8) error{ NoCurrentContex
std.debug.assert(extension.len != 0);
std.debug.assert(extension[0] != 0);
const supported = c.glfwExtensionSupported(extension);
const supported = c.glfwExtensionSupported(extension.ptr);
getError() catch |err| return switch (err) {
Error.NoCurrentContext, Error.PlatformError => |e| e,
Error.NotInitialized => unreachable,
@ -147,12 +147,13 @@ pub inline fn extensionSupported(extension: [:0]const u8) error{ NoCurrentContex
return supported == c.GLFW_TRUE;
}
const builtin = @import("builtin");
/// Client API function pointer type.
///
/// Generic function pointer used for returning client API function pointers.
///
/// see also: context_glext, glfwGetProcAddress
pub const GLProc = fn () callconv(.C) void;
pub const GLProc = if (builtin.zig_backend == .stage1 or builtin.zig_backend == .other) fn () callconv(.C) void else *const fn () callconv(.C) void;
/// Returns the address of the specified function for the current context.
///
@ -197,7 +198,7 @@ test "makeContextCurrent" {
try glfw.init(.{});
defer glfw.terminate();
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
const window = Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
// return without fail, because most of our CI environments are headless / we cannot open
// windows on them.
std.debug.print("note: failed to create window: {}\n", .{err});
@ -222,7 +223,7 @@ test "swapInterval" {
try glfw.init(.{});
defer glfw.terminate();
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
const window = Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
// return without fail, because most of our CI environments are headless / we cannot open
// windows on them.
std.debug.print("note: failed to create window: {}\n", .{err});
@ -239,7 +240,7 @@ test "getProcAddress" {
try glfw.init(.{});
defer glfw.terminate();
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
const window = Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
// return without fail, because most of our CI environments are headless / we cannot open
// windows on them.
std.debug.print("note: failed to create window: {}\n", .{err});
@ -256,7 +257,7 @@ test "extensionSupported" {
try glfw.init(.{});
defer glfw.terminate();
const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
const window = Window.create(640, 480, "Hello, Zig!", null, null, .{}) catch |err| {
// return without fail, because most of our CI environments are headless / we cannot open
// windows on them.
std.debug.print("note: failed to create window: {}\n", .{err});