From bba27cf1aa3ad3a0529b0bd9cadee215d507deee Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sat, 10 Jul 2021 01:01:51 -0700 Subject: [PATCH] glfw: macOS cross compilation with automatic setup Signed-off-by: Stephen Gutekanst --- glfw/build.zig | 69 ++++++++++++++++++++++++++++++++++++++++------- glfw/src/main.zig | 5 ++-- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/glfw/build.zig b/glfw/build.zig index 96179a5a..65554e89 100644 --- a/glfw/build.zig +++ b/glfw/build.zig @@ -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 }, } -} \ No newline at end of file +} + +// 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; + } +} diff --git a/glfw/src/main.zig b/glfw/src/main.zig index 13e879e5..6bb113b4 100644 --- a/glfw/src/main.zig +++ b/glfw/src/main.zig @@ -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(); }