From f9e098c44234821231f7184dacf465aaf907bb99 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Sat, 24 Jul 2021 13:38:43 -0700 Subject: [PATCH] glfw: add SDK_PATH for easier testing of changes to SDKs (#6) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change makes it easier to test SDKs locally, e.g. if you have: ``` % tree projects/ projects ├── mach │   └── glfw └── sdk-macos-11.3 ``` In the `projects/mach/glfw` directory it is now easy to test changes to `sdk-macos-11.3` using: ```sh SDK_PATH="$(pwd)/../.." zig build test -Dtarget=aarch64-macos ``` And it'll use your local SDK, instead of the one in the appdata dir. Signed-off-by: Stephen Gutekanst --- glfw/build.zig | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/glfw/build.zig b/glfw/build.zig index a0ca24b0..c97c26bd 100644 --- a/glfw/build.zig +++ b/glfw/build.zig @@ -218,8 +218,25 @@ fn includeSdkMacOS(b: *Builder, step: *std.build.LibExeObjStep) void { /// 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 }); + // Find the directory where the SDK should be located. We'll consider two locations: + // + // 1. $SDK_PATH/ (if set, e.g. for testing changes to SDKs easily) + // 2. / (default) + // + // Where `` is the name of the SDK, e.g. `sdk-macos-11.3`. + var sdk_root_dir: []const u8 = if (std.process.getEnvVarOwned(allocator, "SDK_PATH")) |sdk_path| { + defer allocator.free(sdk_path); + return try std.fs.path.join(allocator, &.{ sdk_path, name }); + } else |err| switch (err) { + error.EnvironmentVariableNotFound => { + const app_data_dir = try std.fs.getAppDataDir(allocator, "mach"); + defer allocator.free(app_data_dir); + return try std.fs.path.join(allocator, &.{ app_data_dir, name }); + }, + else => |e| return e, + }; + + // If the SDK exists, return it. Otherwise, clone it. if (std.fs.openDirAbsolute(sdk_root_dir, .{})) { return sdk_root_dir; } else |err| return switch (err) {