From 15fd7cd154316e7eb3bf254e249072e562fdbe2c Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Fri, 15 Sep 2023 01:05:14 -0400 Subject: macos: vendored libc: combine headers: part 2 - update include dirs to use combined dir - use one libSystem.tbd (drop use of libSystem.VERSION.tbd) - update canBuildLibC to check for minimum os version only --- src/Compilation.zig | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 47d0125ffb..7cbb64e6c0 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -5656,31 +5656,14 @@ const LibCDirs = struct { sysroot: ?[]const u8, }; -fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8, target: Target) !LibCDirs { - const arch_name = @tagName(target.cpu.arch); - const os_name = try std.fmt.allocPrint(arena, "{s}.{d}", .{ - @tagName(target.os.tag), - target.os.version_range.semver.min.major, - }); +fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) !LibCDirs { const s = std.fs.path.sep_str; - const list = try arena.alloc([]const u8, 3); - + const list = try arena.alloc([]const u8, 1); list[0] = try std.fmt.allocPrint( - arena, - "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "{s}-{s}-none", - .{ zig_lib_dir, arch_name, os_name }, - ); - list[1] = try std.fmt.allocPrint( - arena, - "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-{s}-any", - .{ zig_lib_dir, os_name }, - ); - list[2] = try std.fmt.allocPrint( arena, "{s}" ++ s ++ "libc" ++ s ++ "include" ++ s ++ "any-macos-any", .{zig_lib_dir}, ); - return LibCDirs{ .libc_include_dir_list = list, .libc_installation = null, @@ -5823,7 +5806,7 @@ fn detectLibCFromBuilding( target: std.Target, ) !LibCDirs { if (target.isDarwin()) - return getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir, target); + return getZigShippedLibCIncludeDirsDarwin(arena, zig_lib_dir); const generic_name = target_util.libCGenericName(target); // Some architectures are handled by the same set of headers. -- cgit v1.2.3 From 5d6521d2814e6768536103c233d2e283051b9f1e Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Fri, 15 Sep 2023 01:05:14 -0400 Subject: macos: better SDK version detection SDK version detection: - read SDKSettings.json before inferral from SDK path - vendored libc: add SDKSettings.json for SDK version info resolveLibSystem: - adjust search order to { search_dirs, { sysroot or vendored }} - previous search order was { sysroot, search_dirs, vendored } --- lib/libc/darwin/SDKSettings.json | 1 + src/Compilation.zig | 7 +++++++ src/link.zig | 8 ++++++++ src/link/MachO.zig | 24 +++++++++++++----------- src/link/MachO/load_commands.zig | 31 +++++++++++++++++++++++++++++-- src/link/MachO/zld.zig | 10 ++-------- 6 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 lib/libc/darwin/SDKSettings.json (limited to 'src/Compilation.zig') diff --git a/lib/libc/darwin/SDKSettings.json b/lib/libc/darwin/SDKSettings.json new file mode 100644 index 0000000000..79599d4fe7 --- /dev/null +++ b/lib/libc/darwin/SDKSettings.json @@ -0,0 +1 @@ +{"MinimalDisplayName":"14.0"} diff --git a/src/Compilation.zig b/src/Compilation.zig index 7cbb64e6c0..e9f6be89fd 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1554,6 +1554,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .use_lld = use_lld, .use_llvm = use_llvm, .use_lib_llvm = use_lib_llvm, + .libc_provider = libc_dirs.provider, .link_libc = link_libc, .link_libcpp = link_libcpp, .link_libunwind = link_libunwind, @@ -5654,6 +5655,7 @@ const LibCDirs = struct { libc_installation: ?*const LibCInstallation, libc_framework_dir_list: []const []const u8, sysroot: ?[]const u8, + provider: link.LibCProvider, }; fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) !LibCDirs { @@ -5669,6 +5671,7 @@ fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, + .provider = .vendored, }; } @@ -5686,6 +5689,7 @@ pub fn detectLibCIncludeDirs( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, + .provider = .none, }; } @@ -5743,6 +5747,7 @@ pub fn detectLibCIncludeDirs( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, + .provider = .none, }; } @@ -5797,6 +5802,7 @@ fn detectLibCFromLibCInstallation(arena: Allocator, target: Target, lci: *const .libc_installation = lci, .libc_framework_dir_list = framework_list.items, .sysroot = sysroot, + .provider = if (sysroot == null) .installation else .sysroot, }; } @@ -5858,6 +5864,7 @@ fn detectLibCFromBuilding( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, + .provider = .vendored, }; } diff --git a/src/link.zig b/src/link.zig index fb71262f66..94b6517f0c 100644 --- a/src/link.zig +++ b/src/link.zig @@ -134,6 +134,7 @@ pub const Options = struct { /// Otherwise (depending on `use_lld`) this link code directly outputs and updates the final binary. use_llvm: bool, use_lib_llvm: bool, + libc_provider: LibCProvider, link_libc: bool, link_libcpp: bool, link_libunwind: bool, @@ -282,6 +283,13 @@ pub const HashStyle = enum { sysv, gnu, both }; pub const CompressDebugSections = enum { none, zlib }; +pub const LibCProvider = enum { + none, + installation, + sysroot, + vendored, +}; + pub const File = struct { tag: Tag, options: Options, diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 437768b96c..893c181f48 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -558,10 +558,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No }); { const platform = Platform.fromTarget(self.base.options.target); - const sdk_version: ?std.SemanticVersion = if (self.base.options.sysroot) |path| - load_commands.inferSdkVersionFromSdkPath(path) - else - null; + const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); if (platform.isBuildVersionCompatible()) { try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); } else if (platform.isVersionMinCompatible()) { @@ -647,11 +644,6 @@ pub fn resolveLibSystem( var checked_paths = std.ArrayList([]const u8).init(tmp_arena); success: { - if (self.base.options.sysroot) |root| { - const dir = try fs.path.join(tmp_arena, &[_][]const u8{ root, "usr", "lib" }); - if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; - } - for (search_dirs) |dir| if (try accessLibPath( tmp_arena, &test_path, @@ -660,8 +652,18 @@ pub fn resolveLibSystem( "libSystem", )) break :success; - const dir = try comp.zig_lib_directory.join(tmp_arena, &[_][]const u8{ "libc", "darwin" }); - if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; + switch (self.base.options.libc_provider) { + .none => unreachable, + .installation => unreachable, + .sysroot => { + const dir = try fs.path.join(tmp_arena, &[_][]const u8{ self.base.options.sysroot.?, "usr", "lib" }); + if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; + }, + .vendored => { + const dir = try comp.zig_lib_directory.join(tmp_arena, &[_][]const u8{ "libc", "darwin" }); + if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; + }, + } try self.reportMissingLibraryError(checked_paths.items, "unable to find libSystem system library", .{}); return; diff --git a/src/link/MachO/load_commands.zig b/src/link/MachO/load_commands.zig index cd1e015757..47cd0a54e3 100644 --- a/src/link/MachO/load_commands.zig +++ b/src/link/MachO/load_commands.zig @@ -467,8 +467,34 @@ pub inline fn appleVersionToSemanticVersion(version: u32) std.SemanticVersion { }; } -pub fn inferSdkVersionFromSdkPath(path: []const u8) ?std.SemanticVersion { - const stem = std.fs.path.stem(path); +fn readSdkVersionString(arena: Allocator, dir: []const u8) ![]const u8 { + const sdk_path = try std.fs.path.join(arena, &.{ dir, "SDKSettings.json" }); + const contents = try std.fs.cwd().readFileAlloc(arena, sdk_path, std.math.maxInt(u16)); + const parsed = try std.json.parseFromSlice(std.json.Value, arena, contents, .{}); + if (parsed.value.object.get("MinimalDisplayName")) |ver| return ver.string; + return error.SdkVersionFailure; +} + +pub fn inferSdkVersion(comp: *const Compilation) ?std.SemanticVersion { + var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); + defer arena_allocator.deinit(); + const arena = arena_allocator.allocator(); + const options = comp.bin_file.options; + + const sdk_dir = switch (options.libc_provider) { + .none => unreachable, + .installation => unreachable, + .sysroot => options.sysroot.?, + .vendored => std.fs.path.join(arena, &.{ comp.zig_lib_directory.path.?, "libc", "darwin" }) catch return null, + }; + + // prefer meta information if available + if (readSdkVersionString(arena, sdk_dir)) |ver| { + return parseSdkVersion(ver); + } else |_| {} + + // infer from pathname + const stem = std.fs.path.stem(sdk_dir); const start = for (stem, 0..) |c, i| { if (std.ascii.isDigit(c)) break i; } else stem.len; @@ -532,3 +558,4 @@ const mem = std.mem; const Allocator = mem.Allocator; const Dylib = @import("Dylib.zig"); const MachO = @import("../MachO.zig"); +const Compilation = @import("../../Compilation.zig"); diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig index d6fc1c9f03..ad9c59cb97 100644 --- a/src/link/MachO/zld.zig +++ b/src/link/MachO/zld.zig @@ -241,10 +241,7 @@ pub fn linkWithZld( try argv.append(@tagName(platform.os_tag)); try argv.append(try std.fmt.allocPrint(arena, "{}", .{platform.version})); - const sdk_version: ?std.SemanticVersion = if (options.sysroot) |path| - load_commands.inferSdkVersionFromSdkPath(path) - else - null; + const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); if (sdk_version) |ver| { try argv.append(try std.fmt.allocPrint(arena, "{d}.{d}", .{ ver.major, ver.minor })); } else { @@ -591,10 +588,7 @@ pub fn linkWithZld( }); { const platform = Platform.fromTarget(macho_file.base.options.target); - const sdk_version: ?std.SemanticVersion = if (macho_file.base.options.sysroot) |path| - load_commands.inferSdkVersionFromSdkPath(path) - else - null; + const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); if (platform.isBuildVersionCompatible()) { try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); } else { -- cgit v1.2.3 From 9357973912798d3f64938762ea8993ff3a083ac0 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Fri, 22 Sep 2023 14:10:24 -0400 Subject: kubkon review changes: 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit general: - rename `DarwinSdkLayout` → `DarwinSdkLayout` - drop `DarwinSdkLayout.installation` (not needed for darwin) - document struct inferSdkVersion: - use explicit allocator - avoid trying to infer SDK ver from vendored path --- src/Compilation.zig | 15 ++++++++------- src/link.zig | 11 +++++++---- src/link/MachO.zig | 7 +++---- src/link/MachO/load_commands.zig | 39 +++++++++++++++++++++++---------------- src/link/MachO/zld.zig | 4 ++-- 5 files changed, 43 insertions(+), 33 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index e9f6be89fd..65e433627f 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1554,10 +1554,10 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .use_lld = use_lld, .use_llvm = use_llvm, .use_lib_llvm = use_lib_llvm, - .libc_provider = libc_dirs.provider, .link_libc = link_libc, .link_libcpp = link_libcpp, .link_libunwind = link_libunwind, + .darwinSdkLayout = libc_dirs.darwinSdkLayout, .objects = options.link_objects, .frameworks = options.frameworks, .framework_dirs = options.framework_dirs, @@ -5287,6 +5287,7 @@ fn detectWin32ResourceIncludeDirs(arena: Allocator, options: InitOptions) !LibCD .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, + .darwinSdkLayout = .none, }, } } @@ -5655,7 +5656,7 @@ const LibCDirs = struct { libc_installation: ?*const LibCInstallation, libc_framework_dir_list: []const []const u8, sysroot: ?[]const u8, - provider: link.LibCProvider, + darwinSdkLayout: link.DarwinSdkLayout, }; fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) !LibCDirs { @@ -5671,7 +5672,7 @@ fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .provider = .vendored, + .darwinSdkLayout = .vendored, }; } @@ -5689,7 +5690,7 @@ pub fn detectLibCIncludeDirs( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .provider = .none, + .darwinSdkLayout = .none, }; } @@ -5747,7 +5748,7 @@ pub fn detectLibCIncludeDirs( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .provider = .none, + .darwinSdkLayout = .none, }; } @@ -5802,7 +5803,7 @@ fn detectLibCFromLibCInstallation(arena: Allocator, target: Target, lci: *const .libc_installation = lci, .libc_framework_dir_list = framework_list.items, .sysroot = sysroot, - .provider = if (sysroot == null) .installation else .sysroot, + .darwinSdkLayout = if (sysroot == null) .none else .sdk, }; } @@ -5864,7 +5865,7 @@ fn detectLibCFromBuilding( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .provider = .vendored, + .darwinSdkLayout = .vendored, }; } diff --git a/src/link.zig b/src/link.zig index 94b6517f0c..efd02e982e 100644 --- a/src/link.zig +++ b/src/link.zig @@ -134,10 +134,10 @@ pub const Options = struct { /// Otherwise (depending on `use_lld`) this link code directly outputs and updates the final binary. use_llvm: bool, use_lib_llvm: bool, - libc_provider: LibCProvider, link_libc: bool, link_libcpp: bool, link_libunwind: bool, + darwinSdkLayout: DarwinSdkLayout, function_sections: bool, no_builtin: bool, eh_frame_hdr: bool, @@ -283,10 +283,13 @@ pub const HashStyle = enum { sysv, gnu, both }; pub const CompressDebugSections = enum { none, zlib }; -pub const LibCProvider = enum { +/// The filesystem layout of darwin SDK elements. +pub const DarwinSdkLayout = enum { + /// Does not apply to the target. none, - installation, - sysroot, + /// macOS SDK layout: TOP { /usr/include, /usr/lib, /System/Library/Frameworks }. + sdk, + /// Shipped libc layout: TOP { /lib/libc/include, /lib/libc/darwin, }. vendored, }; diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 893c181f48..0110ff81ce 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -558,7 +558,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No }); { const platform = Platform.fromTarget(self.base.options.target); - const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); + const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp); if (platform.isBuildVersionCompatible()) { try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); } else if (platform.isVersionMinCompatible()) { @@ -652,10 +652,9 @@ pub fn resolveLibSystem( "libSystem", )) break :success; - switch (self.base.options.libc_provider) { + switch (self.base.options.darwinSdkLayout) { .none => unreachable, - .installation => unreachable, - .sysroot => { + .sdk => { const dir = try fs.path.join(tmp_arena, &[_][]const u8{ self.base.options.sysroot.?, "usr", "lib" }); if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; }, diff --git a/src/link/MachO/load_commands.zig b/src/link/MachO/load_commands.zig index 47cd0a54e3..a00aec46fa 100644 --- a/src/link/MachO/load_commands.zig +++ b/src/link/MachO/load_commands.zig @@ -467,31 +467,27 @@ pub inline fn appleVersionToSemanticVersion(version: u32) std.SemanticVersion { }; } -fn readSdkVersionString(arena: Allocator, dir: []const u8) ![]const u8 { - const sdk_path = try std.fs.path.join(arena, &.{ dir, "SDKSettings.json" }); - const contents = try std.fs.cwd().readFileAlloc(arena, sdk_path, std.math.maxInt(u16)); - const parsed = try std.json.parseFromSlice(std.json.Value, arena, contents, .{}); - if (parsed.value.object.get("MinimalDisplayName")) |ver| return ver.string; - return error.SdkVersionFailure; -} - -pub fn inferSdkVersion(comp: *const Compilation) ?std.SemanticVersion { - var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); +pub fn inferSdkVersion(gpa: Allocator, comp: *const Compilation) ?std.SemanticVersion { + var arena_allocator = std.heap.ArenaAllocator.init(gpa); defer arena_allocator.deinit(); const arena = arena_allocator.allocator(); + const options = comp.bin_file.options; - const sdk_dir = switch (options.libc_provider) { + const sdk_dir = switch (options.darwinSdkLayout) { .none => unreachable, - .installation => unreachable, - .sysroot => options.sysroot.?, + .sdk => options.sysroot.?, .vendored => std.fs.path.join(arena, &.{ comp.zig_lib_directory.path.?, "libc", "darwin" }) catch return null, }; - // prefer meta information if available - if (readSdkVersionString(arena, sdk_dir)) |ver| { + if (readSdkVersionFromSettings(arena, sdk_dir)) |ver| { return parseSdkVersion(ver); - } else |_| {} + } else |_| { + if (options.darwinSdkLayout == .vendored) { + // vendored layout does not have versioned pathname + return null; + } + } // infer from pathname const stem = std.fs.path.stem(sdk_dir); @@ -505,6 +501,17 @@ pub fn inferSdkVersion(comp: *const Compilation) ?std.SemanticVersion { return parseSdkVersion(stem[start..end]); } +// Official Apple SDKs ship with a `SDKSettings.json` located at the top of SDK fs layout. +// Use property `MinimalDisplayName` to determine version. +// The file/property is also available with vendored libc. +fn readSdkVersionFromSettings(arena: Allocator, dir: []const u8) ![]const u8 { + const sdk_path = try std.fs.path.join(arena, &.{ dir, "SDKSettings.json" }); + const contents = try std.fs.cwd().readFileAlloc(arena, sdk_path, std.math.maxInt(u16)); + const parsed = try std.json.parseFromSlice(std.json.Value, arena, contents, .{}); + if (parsed.value.object.get("MinimalDisplayName")) |ver| return ver.string; + return error.SdkVersionFailure; +} + // Versions reported by Apple aren't exactly semantically valid as they usually omit // the patch component, so we parse SDK value by hand. fn parseSdkVersion(raw: []const u8) ?std.SemanticVersion { diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig index ad9c59cb97..7768deecdf 100644 --- a/src/link/MachO/zld.zig +++ b/src/link/MachO/zld.zig @@ -241,7 +241,7 @@ pub fn linkWithZld( try argv.append(@tagName(platform.os_tag)); try argv.append(try std.fmt.allocPrint(arena, "{}", .{platform.version})); - const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); + const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp); if (sdk_version) |ver| { try argv.append(try std.fmt.allocPrint(arena, "{d}.{d}", .{ ver.major, ver.minor })); } else { @@ -588,7 +588,7 @@ pub fn linkWithZld( }); { const platform = Platform.fromTarget(macho_file.base.options.target); - const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); + const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp); if (platform.isBuildVersionCompatible()) { try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); } else { -- cgit v1.2.3 From ebd0776b28c50169936b58f8ba05be70e854fd35 Mon Sep 17 00:00:00 2001 From: Michael Dusan Date: Mon, 25 Sep 2023 15:51:57 -0400 Subject: kubkon review changes: 3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - make vendored settings failure unreachable - rename field `darwinSdkLayout` → `darwin_sdk_layout` - make `darwin_sdk_layout` optional --- src/Compilation.zig | 16 ++++++++-------- src/link.zig | 4 +--- src/link/MachO.zig | 3 +-- src/link/MachO/load_commands.zig | 11 ++++------- 4 files changed, 14 insertions(+), 20 deletions(-) (limited to 'src/Compilation.zig') diff --git a/src/Compilation.zig b/src/Compilation.zig index 65e433627f..395a399801 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1557,7 +1557,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { .link_libc = link_libc, .link_libcpp = link_libcpp, .link_libunwind = link_libunwind, - .darwinSdkLayout = libc_dirs.darwinSdkLayout, + .darwin_sdk_layout = libc_dirs.darwin_sdk_layout, .objects = options.link_objects, .frameworks = options.frameworks, .framework_dirs = options.framework_dirs, @@ -5287,7 +5287,7 @@ fn detectWin32ResourceIncludeDirs(arena: Allocator, options: InitOptions) !LibCD .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .darwinSdkLayout = .none, + .darwin_sdk_layout = null, }, } } @@ -5656,7 +5656,7 @@ const LibCDirs = struct { libc_installation: ?*const LibCInstallation, libc_framework_dir_list: []const []const u8, sysroot: ?[]const u8, - darwinSdkLayout: link.DarwinSdkLayout, + darwin_sdk_layout: ?link.DarwinSdkLayout, }; fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) !LibCDirs { @@ -5672,7 +5672,7 @@ fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .darwinSdkLayout = .vendored, + .darwin_sdk_layout = .vendored, }; } @@ -5690,7 +5690,7 @@ pub fn detectLibCIncludeDirs( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .darwinSdkLayout = .none, + .darwin_sdk_layout = null, }; } @@ -5748,7 +5748,7 @@ pub fn detectLibCIncludeDirs( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .darwinSdkLayout = .none, + .darwin_sdk_layout = null, }; } @@ -5803,7 +5803,7 @@ fn detectLibCFromLibCInstallation(arena: Allocator, target: Target, lci: *const .libc_installation = lci, .libc_framework_dir_list = framework_list.items, .sysroot = sysroot, - .darwinSdkLayout = if (sysroot == null) .none else .sdk, + .darwin_sdk_layout = if (sysroot == null) null else .sdk, }; } @@ -5865,7 +5865,7 @@ fn detectLibCFromBuilding( .libc_installation = null, .libc_framework_dir_list = &.{}, .sysroot = null, - .darwinSdkLayout = .vendored, + .darwin_sdk_layout = .vendored, }; } diff --git a/src/link.zig b/src/link.zig index efd02e982e..844ddb7b40 100644 --- a/src/link.zig +++ b/src/link.zig @@ -137,7 +137,7 @@ pub const Options = struct { link_libc: bool, link_libcpp: bool, link_libunwind: bool, - darwinSdkLayout: DarwinSdkLayout, + darwin_sdk_layout: ?DarwinSdkLayout, function_sections: bool, no_builtin: bool, eh_frame_hdr: bool, @@ -285,8 +285,6 @@ pub const CompressDebugSections = enum { none, zlib }; /// The filesystem layout of darwin SDK elements. pub const DarwinSdkLayout = enum { - /// Does not apply to the target. - none, /// macOS SDK layout: TOP { /usr/include, /usr/lib, /System/Library/Frameworks }. sdk, /// Shipped libc layout: TOP { /lib/libc/include, /lib/libc/darwin, }. diff --git a/src/link/MachO.zig b/src/link/MachO.zig index 0110ff81ce..81d6c5ead3 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -652,8 +652,7 @@ pub fn resolveLibSystem( "libSystem", )) break :success; - switch (self.base.options.darwinSdkLayout) { - .none => unreachable, + switch (self.base.options.darwin_sdk_layout.?) { .sdk => { const dir = try fs.path.join(tmp_arena, &[_][]const u8{ self.base.options.sysroot.?, "usr", "lib" }); if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; diff --git a/src/link/MachO/load_commands.zig b/src/link/MachO/load_commands.zig index a00aec46fa..3aca247d8a 100644 --- a/src/link/MachO/load_commands.zig +++ b/src/link/MachO/load_commands.zig @@ -474,19 +474,16 @@ pub fn inferSdkVersion(gpa: Allocator, comp: *const Compilation) ?std.SemanticVe const options = comp.bin_file.options; - const sdk_dir = switch (options.darwinSdkLayout) { - .none => unreachable, + const sdk_layout = options.darwin_sdk_layout.?; + const sdk_dir = switch (sdk_layout) { .sdk => options.sysroot.?, .vendored => std.fs.path.join(arena, &.{ comp.zig_lib_directory.path.?, "libc", "darwin" }) catch return null, }; - if (readSdkVersionFromSettings(arena, sdk_dir)) |ver| { return parseSdkVersion(ver); } else |_| { - if (options.darwinSdkLayout == .vendored) { - // vendored layout does not have versioned pathname - return null; - } + // We control vendored and reading settings should always succeed. + if (sdk_layout == .vendored) @panic("zig installation bug: unable to parse SDK version"); } // infer from pathname -- cgit v1.2.3