glfw: macOS cross compilation with automatic setup

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2021-07-10 01:01:51 -07:00
parent 91be49b733
commit bba27cf1aa
2 changed files with 61 additions and 13 deletions

View file

@ -19,9 +19,10 @@ fn thisDir() []const u8 {
}
pub const Options = struct {
GLES: bool = false,
OpenGL: bool = true,
Vulkan: bool = true,
Metal: bool = true,
OpenGL: bool = false,
GLES: bool = false,
};
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void {
@ -58,14 +59,10 @@ pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void
// General sources
"upstream/glfw/src/monitor.c",
// "upstream/glfw/src/null_init.c",
// "upstream/glfw/src/null_joystick.c",
"upstream/glfw/src/init.c",
"upstream/glfw/src/vulkan.c",
// "upstream/glfw/src/null_monitor.c",
"upstream/glfw/src/input.c",
"upstream/glfw/src/osmesa_context.c",
// "upstream/glfw/src/null_window.c",
"upstream/glfw/src/egl_context.c",
"upstream/glfw/src/context.c",
"upstream/glfw/src/window.c",
@ -111,12 +108,20 @@ fn linkGLFW(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void
switch (target.os.tag) {
.windows => {},
.macos => {
const sdk_root_dir = getSdkRoot(b.allocator, "sdk-macos-11.3") catch unreachable;
defer b.allocator.free(sdk_root_dir);
var sdk_root_frameworks = std.fs.path.join(b.allocator, &.{ sdk_root_dir, "root/System/Library/Frameworks" }) catch unreachable;
defer b.allocator.free(sdk_root_frameworks);
step.addFrameworkDir(sdk_root_frameworks);
var sdk_root_includes = std.fs.path.join(b.allocator, &.{ sdk_root_dir, "root/usr/include" }) catch unreachable;
defer b.allocator.free(sdk_root_includes);
step.addSystemIncludeDir(sdk_root_includes);
step.linkFramework("Cocoa");
step.linkFramework("IOKit");
step.linkFramework("CoreFoundation");
if (options.GLES) {
step.linkSystemLibrary("GLESv2");
}
if (options.OpenGL) {
step.linkFramework("OpenGL");
}
@ -125,4 +130,48 @@ fn linkGLFW(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void
// Assume Linux-like
},
}
}
}
// Caller owns returned memory.
fn getSdkRoot(allocator: *std.mem.Allocator, comptime name: []const u8) ![]const u8 {
const app_data_dir = try std.fs.getAppDataDir(allocator, "mach");
var sdk_root_dir = try std.fs.path.join(allocator, &.{ app_data_dir, name });
if (std.fs.openDirAbsolute(sdk_root_dir, .{})) {
return sdk_root_dir;
} else |err| return switch (err) {
error.FileNotFound => {
std.log.info("cloning required sdk..\ngit clone https://github.com/hexops/{s} '{s}'..\n", .{ name, sdk_root_dir });
if (std.mem.eql(u8, name, "sdk-macos-11.3")) {
if (!try confirmAppleSDKAgreement()) @panic("cannot continue");
}
try std.fs.cwd().makePath(app_data_dir);
const argv = &[_][]const u8{ "git", "clone", "https://github.com/hexops/" ++ name };
const child = try std.ChildProcess.init(argv, allocator);
child.cwd = app_data_dir;
child.stdin = std.io.getStdOut();
child.stderr = std.io.getStdErr();
child.stdout = std.io.getStdOut();
try child.spawn();
_ = try child.wait();
return sdk_root_dir;
},
else => err,
};
}
fn confirmAppleSDKAgreement() !bool {
if (std.mem.eql(u8, std.os.getenv("AGREE") orelse "", "true")) return true;
const stdin = std.io.getStdIn().reader();
const stdout = std.io.getStdOut().writer();
var buf: [10]u8 = undefined;
try stdout.print("This SDK is distributed under the terms of the Xcode and Apple SDKs agreement:\n", .{});
try stdout.print(" https://www.apple.com/legal/sla/docs/xcode.pdf\n", .{});
try stdout.print("\n", .{});
try stdout.print("Do you agree to those terms? [Y/n] ", .{});
if (try stdin.readUntilDelimiterOrEof(buf[0..], '\n')) |user_input| {
try stdout.print("\n", .{});
return std.mem.eql(u8, user_input, "y") or std.mem.eql(u8, user_input, "Y") or std.mem.eql(u8, user_input, "yes") or std.mem.eql(u8, user_input, "");
} else {
return false;
}
}

View file

@ -8,14 +8,13 @@ pub fn basicTest() void {
@panic("failed to init");
}
const window = c.glfwCreateWindow(640, 480, "GLFW example", null, null);
if (window == null)
{
if (window == null) {
c.glfwTerminate();
@panic("failed to create window");
}
var start = std.time.milliTimestamp();
while (std.time.milliTimestamp() < start+3000 and c.glfwWindowShouldClose(window) != c.GLFW_TRUE) {
while (std.time.milliTimestamp() < start + 3000 and c.glfwWindowShouldClose(window) != c.GLFW_TRUE) {
c.glfwPollEvents();
}