glfw: improve system-sdk version selection

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-06-25 01:12:07 -07:00
parent 4fdd12d93a
commit 07e35d8d4d

View file

@ -35,6 +35,7 @@ const Build = std.Build;
pub const Options = struct { pub const Options = struct {
pub const Sdk = struct { pub const Sdk = struct {
is_default: bool = false,
name: []const u8, name: []const u8,
git_addr: []const u8, git_addr: []const u8,
git_revision: []const u8, git_revision: []const u8,
@ -52,12 +53,13 @@ pub const Options = struct {
.os_tag = .macos, .os_tag = .macos,
.os_version = .{ .os_version = .{
.semver = .{ .semver = .{
.min = .{ .major = 13, .minor = 0, .patch = std.math.maxInt(u32) }, .min = .{ .major = 13, .minor = 0, .patch = 0 },
.max = .{ .major = 14, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) }, .max = .{ .major = 14, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) },
}, },
}, },
}, },
.{ .{
.is_default = true,
.name = "sdk-macos-12.0", .name = "sdk-macos-12.0",
.git_addr = "https://github.com/hexops/sdk-macos-12.0", .git_addr = "https://github.com/hexops/sdk-macos-12.0",
.git_revision = "14613b4917c7059dad8f3789f55bb13a2548f83d", .git_revision = "14613b4917c7059dad8f3789f55bb13a2548f83d",
@ -65,25 +67,28 @@ pub const Options = struct {
.os_tag = .macos, .os_tag = .macos,
.os_version = .{ .os_version = .{
.semver = .{ .semver = .{
.min = .{ .major = 12, .minor = 0, .patch = std.math.maxInt(u32) }, // Note: we force 11.0 compatibility here, in practice it works and the 11.3 SDK
// is missing AudioToolbox
.min = .{ .major = 11, .minor = 0, .patch = 0 },
.max = .{ .major = 12, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) }, .max = .{ .major = 12, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) },
}, },
}, },
}, },
// .{
// .name = "sdk-macos-11.3",
// .git_addr = "https://github.com/hexops/sdk-macos-11.3",
// .git_revision = "ccbaae84cc39469a6792108b24480a4806e09d59",
// .cpu_arch = &.{ .aarch64, .x86_64 },
// .os_tag = .macos,
// .os_version = .{
// .semver = .{
// .min = .{ .major = 11, .minor = 0, .patch = 0 },
// .max = .{ .major = 11, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) },
// },
// },
// },
.{ .{
.name = "sdk-macos-11.3", .is_default = true,
.git_addr = "https://github.com/hexops/sdk-macos-11.3",
.git_revision = "ccbaae84cc39469a6792108b24480a4806e09d59",
.cpu_arch = &.{ .aarch64, .x86_64 },
.os_tag = .macos,
.os_version = .{
.semver = .{
.min = .{ .major = 11, .minor = 0, .patch = std.math.maxInt(u32) },
.max = .{ .major = 11, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) },
},
},
},
.{
.name = "sdk-linux-x86_64", .name = "sdk-linux-x86_64",
.git_addr = "https://github.com/hexops/sdk-linux-x86_64", .git_addr = "https://github.com/hexops/sdk-linux-x86_64",
.git_revision = "311a0f18a2350c032a40b5917ae25c05cf500683s", .git_revision = "311a0f18a2350c032a40b5917ae25c05cf500683s",
@ -92,7 +97,7 @@ pub const Options = struct {
.os_version = .{ .os_version = .{
.linux = .{ .linux = .{
.range = .{ .range = .{
.min = .{ .major = 3, .minor = 16, .patch = std.math.maxInt(u32) }, .min = .{ .major = 3, .minor = 16, .patch = 0 },
.max = .{ .major = 6, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) }, .max = .{ .major = 6, .minor = std.math.maxInt(u32), .patch = std.math.maxInt(u32) },
}, },
.glibc = .{ .major = 0, .minor = 0, .patch = std.math.maxInt(u32) }, .glibc = .{ .major = 0, .minor = 0, .patch = std.math.maxInt(u32) },
@ -100,6 +105,7 @@ pub const Options = struct {
}, },
}, },
.{ .{
.is_default = true,
.name = "sdk-linux-aarch64", .name = "sdk-linux-aarch64",
.git_addr = "https://github.com/hexops/sdk-linux-aarch64", .git_addr = "https://github.com/hexops/sdk-linux-aarch64",
.git_revision = "cefd56ea2e97623d308e1897491a322fdca23d97", .git_revision = "cefd56ea2e97623d308e1897491a322fdca23d97",
@ -116,6 +122,7 @@ pub const Options = struct {
}, },
}, },
.{ .{
.is_default = true,
.name = "sdk-windows-x86_64", .name = "sdk-windows-x86_64",
.git_addr = "https://github.com/hexops/sdk-windows-x86_64", .git_addr = "https://github.com/hexops/sdk-windows-x86_64",
.git_revision = "13dcda7fe3f1aec0fc6130527226ad7ae0f4b792", .git_revision = "13dcda7fe3f1aec0fc6130527226ad7ae0f4b792",
@ -139,21 +146,37 @@ pub const Options = struct {
pub fn include(b: *Build, step: *std.build.CompileStep, options: Options) void { pub fn include(b: *Build, step: *std.build.CompileStep, options: Options) void {
const target = step.target_info.target; const target = step.target_info.target;
// var best_sdk: ?Options.Sdk = null; var best_sdk: ?Options.Sdk = null;
// Try to find an SDK that matches our minimum target version
for (options.sdk_list) |sdk| { for (options.sdk_list) |sdk| {
if (!std.mem.containsAtLeast(std.Target.Cpu.Arch, sdk.cpu_arch, 1, &.{target.cpu.arch})) if (!std.mem.containsAtLeast(std.Target.Cpu.Arch, sdk.cpu_arch, 1, &.{target.cpu.arch}))
continue; continue;
if (sdk.os_tag != target.os.tag) continue; if (sdk.os_tag != target.os.tag) continue;
const version_ok = switch (sdk.os_version) { const version_ok = switch (sdk.os_version) {
.semver => |vr| vr.includesVersion(target.os.version_range.semver.min) or vr.includesVersion(target.os.version_range.semver.max), .semver => |vr| vr.includesVersion(target.os.version_range.semver.min),
.linux => |vr| vr.includesVersion(target.os.version_range.linux.range.min) or vr.includesVersion(target.os.version_range.linux.range.max), .linux => |vr| vr.includesVersion(target.os.version_range.linux.range.min),
.windows => |vr| vr.includesVersion(target.os.version_range.windows.min) or vr.includesVersion(target.os.version_range.windows.max), .windows => |vr| vr.includesVersion(target.os.version_range.windows.min),
.none => false, .none => false,
}; };
if (!version_ok) continue; if (!version_ok) continue;
const sdk_root_dir = getSdkRoot(b.allocator, sdk) catch unreachable; best_sdk = sdk;
}
if (best_sdk == null) {
// We found no SDK matching our minimum target version, select the default one matching our os+arch then
for (options.sdk_list) |sdk| {
if (!std.mem.containsAtLeast(std.Target.Cpu.Arch, sdk.cpu_arch, 1, &.{target.cpu.arch}))
continue;
if (sdk.os_tag != target.os.tag) continue;
if (sdk.is_default) best_sdk = sdk;
}
}
const sdk_root_dir = getSdkRoot(b.allocator, best_sdk.?) catch unreachable;
if (options.set_sysroot) { if (options.set_sysroot) {
// We have no sysroot for Windows, but we still set one to prevent inclusion of other system // We have no sysroot for Windows, but we still set one to prevent inclusion of other system
// libs (if set_sysroot is set, don't want to accidentally depend on system libs.) // libs (if set_sysroot is set, don't want to accidentally depend on system libs.)
@ -198,7 +221,7 @@ pub fn include(b: *Build, step: *std.build.CompileStep, options: Options) void {
const sdk_root_libs = switch (target.cpu.arch) { const sdk_root_libs = switch (target.cpu.arch) {
.x86_64 => std.fs.path.join(b.allocator, &.{ sdk_root_dir, "root/usr/lib/x86_64-linux-gnu" }) catch unreachable, .x86_64 => std.fs.path.join(b.allocator, &.{ sdk_root_dir, "root/usr/lib/x86_64-linux-gnu" }) catch unreachable,
.aarch64 => std.fs.path.join(b.allocator, &.{ sdk_root_dir, "root/usr/lib/aarch64-linux-gnu" }) catch unreachable, .aarch64 => std.fs.path.join(b.allocator, &.{ sdk_root_dir, "root/usr/lib/aarch64-linux-gnu" }) catch unreachable,
else => break, else => unreachable,
}; };
defer { defer {
b.allocator.free(sdk_root_includes); b.allocator.free(sdk_root_includes);
@ -213,11 +236,6 @@ pub fn include(b: *Build, step: *std.build.CompileStep, options: Options) void {
}; };
} }
// TODO(error_handling)
std.debug.print("Unsupported Target!\n", .{});
unreachable;
}
var cached_sdk_roots: ?std.AutoHashMap(*const Options.Sdk, []const u8) = null; var cached_sdk_roots: ?std.AutoHashMap(*const Options.Sdk, []const u8) = null;
/// returns the SDK root path, determining it iff necessary. In a real application, this may be /// returns the SDK root path, determining it iff necessary. In a real application, this may be