| author | |
| committer | |
| log | 5d6521d2814e6768536103c233d2e283051b9f1e |
| tree | bccd4d252a4c9849ae31e76258d6e7d83a35f78b |
| parent | 15fd7cd154316e7eb3bf254e249072e562fdbe2c |
| signature |
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 }6 files changed, 60 insertions(+), 21 deletions(-)
lib/libc/darwin/SDKSettings.json created+1| ... | ... | @@ -0,0 +1 @@ |
| 1 | {"MinimalDisplayName":"14.0"} |
src/Compilation.zig+7| ... | ... | @@ -1554,6 +1554,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1554 | 1554 | .use_lld = use_lld, |
| 1555 | 1555 | .use_llvm = use_llvm, |
| 1556 | 1556 | .use_lib_llvm = use_lib_llvm, |
| 1557 | .libc_provider = libc_dirs.provider, | |
| 1557 | 1558 | .link_libc = link_libc, |
| 1558 | 1559 | .link_libcpp = link_libcpp, |
| 1559 | 1560 | .link_libunwind = link_libunwind, |
| ... | ... | @@ -5654,6 +5655,7 @@ const LibCDirs = struct { |
| 5654 | 5655 | libc_installation: ?*const LibCInstallation, |
| 5655 | 5656 | libc_framework_dir_list: []const []const u8, |
| 5656 | 5657 | sysroot: ?[]const u8, |
| 5658 | provider: link.LibCProvider, | |
| 5657 | 5659 | }; |
| 5658 | 5660 | |
| 5659 | 5661 | fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) !LibCDirs { |
| ... | ... | @@ -5669,6 +5671,7 @@ fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) |
| 5669 | 5671 | .libc_installation = null, |
| 5670 | 5672 | .libc_framework_dir_list = &.{}, |
| 5671 | 5673 | .sysroot = null, |
| 5674 | .provider = .vendored, | |
| 5672 | 5675 | }; |
| 5673 | 5676 | } |
| 5674 | 5677 | |
| ... | ... | @@ -5686,6 +5689,7 @@ pub fn detectLibCIncludeDirs( |
| 5686 | 5689 | .libc_installation = null, |
| 5687 | 5690 | .libc_framework_dir_list = &.{}, |
| 5688 | 5691 | .sysroot = null, |
| 5692 | .provider = .none, | |
| 5689 | 5693 | }; |
| 5690 | 5694 | } |
| 5691 | 5695 | |
| ... | ... | @@ -5743,6 +5747,7 @@ pub fn detectLibCIncludeDirs( |
| 5743 | 5747 | .libc_installation = null, |
| 5744 | 5748 | .libc_framework_dir_list = &.{}, |
| 5745 | 5749 | .sysroot = null, |
| 5750 | .provider = .none, | |
| 5746 | 5751 | }; |
| 5747 | 5752 | } |
| 5748 | 5753 | |
| ... | ... | @@ -5797,6 +5802,7 @@ fn detectLibCFromLibCInstallation(arena: Allocator, target: Target, lci: *const |
| 5797 | 5802 | .libc_installation = lci, |
| 5798 | 5803 | .libc_framework_dir_list = framework_list.items, |
| 5799 | 5804 | .sysroot = sysroot, |
| 5805 | .provider = if (sysroot == null) .installation else .sysroot, | |
| 5800 | 5806 | }; |
| 5801 | 5807 | } |
| 5802 | 5808 | |
| ... | ... | @@ -5858,6 +5864,7 @@ fn detectLibCFromBuilding( |
| 5858 | 5864 | .libc_installation = null, |
| 5859 | 5865 | .libc_framework_dir_list = &.{}, |
| 5860 | 5866 | .sysroot = null, |
| 5867 | .provider = .vendored, | |
| 5861 | 5868 | }; |
| 5862 | 5869 | } |
| 5863 | 5870 |
src/link.zig+8| ... | ... | @@ -134,6 +134,7 @@ pub const Options = struct { |
| 134 | 134 | /// Otherwise (depending on `use_lld`) this link code directly outputs and updates the final binary. |
| 135 | 135 | use_llvm: bool, |
| 136 | 136 | use_lib_llvm: bool, |
| 137 | libc_provider: LibCProvider, | |
| 137 | 138 | link_libc: bool, |
| 138 | 139 | link_libcpp: bool, |
| 139 | 140 | link_libunwind: bool, |
| ... | ... | @@ -282,6 +283,13 @@ pub const HashStyle = enum { sysv, gnu, both }; |
| 282 | 283 | |
| 283 | 284 | pub const CompressDebugSections = enum { none, zlib }; |
| 284 | 285 | |
| 286 | pub const LibCProvider = enum { | |
| 287 | none, | |
| 288 | installation, | |
| 289 | sysroot, | |
| 290 | vendored, | |
| 291 | }; | |
| 292 | ||
| 285 | 293 | pub const File = struct { |
| 286 | 294 | tag: Tag, |
| 287 | 295 | options: Options, |
src/link/MachO.zig+13-11| ... | ... | @@ -558,10 +558,7 @@ pub fn flushModule(self: *MachO, comp: *Compilation, prog_node: *std.Progress.No |
| 558 | 558 | }); |
| 559 | 559 | { |
| 560 | 560 | const platform = Platform.fromTarget(self.base.options.target); |
| 561 | const sdk_version: ?std.SemanticVersion = if (self.base.options.sysroot) |path| | |
| 562 | load_commands.inferSdkVersionFromSdkPath(path) | |
| 563 | else | |
| 564 | null; | |
| 561 | const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); | |
| 565 | 562 | if (platform.isBuildVersionCompatible()) { |
| 566 | 563 | try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); |
| 567 | 564 | } else if (platform.isVersionMinCompatible()) { |
| ... | ... | @@ -647,11 +644,6 @@ pub fn resolveLibSystem( |
| 647 | 644 | var checked_paths = std.ArrayList([]const u8).init(tmp_arena); |
| 648 | 645 | |
| 649 | 646 | success: { |
| 650 | if (self.base.options.sysroot) |root| { | |
| 651 | const dir = try fs.path.join(tmp_arena, &[_][]const u8{ root, "usr", "lib" }); | |
| 652 | if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; | |
| 653 | } | |
| 654 | ||
| 655 | 647 | for (search_dirs) |dir| if (try accessLibPath( |
| 656 | 648 | tmp_arena, |
| 657 | 649 | &test_path, |
| ... | ... | @@ -660,8 +652,18 @@ pub fn resolveLibSystem( |
| 660 | 652 | "libSystem", |
| 661 | 653 | )) break :success; |
| 662 | 654 | |
| 663 | const dir = try comp.zig_lib_directory.join(tmp_arena, &[_][]const u8{ "libc", "darwin" }); | |
| 664 | if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; | |
| 655 | switch (self.base.options.libc_provider) { | |
| 656 | .none => unreachable, | |
| 657 | .installation => unreachable, | |
| 658 | .sysroot => { | |
| 659 | const dir = try fs.path.join(tmp_arena, &[_][]const u8{ self.base.options.sysroot.?, "usr", "lib" }); | |
| 660 | if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; | |
| 661 | }, | |
| 662 | .vendored => { | |
| 663 | const dir = try comp.zig_lib_directory.join(tmp_arena, &[_][]const u8{ "libc", "darwin" }); | |
| 664 | if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; | |
| 665 | }, | |
| 666 | } | |
| 665 | 667 | |
| 666 | 668 | try self.reportMissingLibraryError(checked_paths.items, "unable to find libSystem system library", .{}); |
| 667 | 669 | return; |
src/link/MachO/load_commands.zig+29-2| ... | ... | @@ -467,8 +467,34 @@ pub inline fn appleVersionToSemanticVersion(version: u32) std.SemanticVersion { |
| 467 | 467 | }; |
| 468 | 468 | } |
| 469 | 469 | |
| 470 | pub fn inferSdkVersionFromSdkPath(path: []const u8) ?std.SemanticVersion { | |
| 471 | const stem = std.fs.path.stem(path); | |
| 470 | fn readSdkVersionString(arena: Allocator, dir: []const u8) ![]const u8 { | |
| 471 | const sdk_path = try std.fs.path.join(arena, &.{ dir, "SDKSettings.json" }); | |
| 472 | const contents = try std.fs.cwd().readFileAlloc(arena, sdk_path, std.math.maxInt(u16)); | |
| 473 | const parsed = try std.json.parseFromSlice(std.json.Value, arena, contents, .{}); | |
| 474 | if (parsed.value.object.get("MinimalDisplayName")) |ver| return ver.string; | |
| 475 | return error.SdkVersionFailure; | |
| 476 | } | |
| 477 | ||
| 478 | pub fn inferSdkVersion(comp: *const Compilation) ?std.SemanticVersion { | |
| 479 | var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa); | |
| 480 | defer arena_allocator.deinit(); | |
| 481 | const arena = arena_allocator.allocator(); | |
| 482 | const options = comp.bin_file.options; | |
| 483 | ||
| 484 | const sdk_dir = switch (options.libc_provider) { | |
| 485 | .none => unreachable, | |
| 486 | .installation => unreachable, | |
| 487 | .sysroot => options.sysroot.?, | |
| 488 | .vendored => std.fs.path.join(arena, &.{ comp.zig_lib_directory.path.?, "libc", "darwin" }) catch return null, | |
| 489 | }; | |
| 490 | ||
| 491 | // prefer meta information if available | |
| 492 | if (readSdkVersionString(arena, sdk_dir)) |ver| { | |
| 493 | return parseSdkVersion(ver); | |
| 494 | } else |_| {} | |
| 495 | ||
| 496 | // infer from pathname | |
| 497 | const stem = std.fs.path.stem(sdk_dir); | |
| 472 | 498 | const start = for (stem, 0..) |c, i| { |
| 473 | 499 | if (std.ascii.isDigit(c)) break i; |
| 474 | 500 | } else stem.len; |
| ... | ... | @@ -532,3 +558,4 @@ const mem = std.mem; |
| 532 | 558 | const Allocator = mem.Allocator; |
| 533 | 559 | const Dylib = @import("Dylib.zig"); |
| 534 | 560 | const MachO = @import("../MachO.zig"); |
| 561 | const Compilation = @import("../../Compilation.zig"); |
src/link/MachO/zld.zig+2-8| ... | ... | @@ -241,10 +241,7 @@ pub fn linkWithZld( |
| 241 | 241 | try argv.append(@tagName(platform.os_tag)); |
| 242 | 242 | try argv.append(try std.fmt.allocPrint(arena, "{}", .{platform.version})); |
| 243 | 243 | |
| 244 | const sdk_version: ?std.SemanticVersion = if (options.sysroot) |path| | |
| 245 | load_commands.inferSdkVersionFromSdkPath(path) | |
| 246 | else | |
| 247 | null; | |
| 244 | const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); | |
| 248 | 245 | if (sdk_version) |ver| { |
| 249 | 246 | try argv.append(try std.fmt.allocPrint(arena, "{d}.{d}", .{ ver.major, ver.minor })); |
| 250 | 247 | } else { |
| ... | ... | @@ -591,10 +588,7 @@ pub fn linkWithZld( |
| 591 | 588 | }); |
| 592 | 589 | { |
| 593 | 590 | const platform = Platform.fromTarget(macho_file.base.options.target); |
| 594 | const sdk_version: ?std.SemanticVersion = if (macho_file.base.options.sysroot) |path| | |
| 595 | load_commands.inferSdkVersionFromSdkPath(path) | |
| 596 | else | |
| 597 | null; | |
| 591 | const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); | |
| 598 | 592 | if (platform.isBuildVersionCompatible()) { |
| 599 | 593 | try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); |
| 600 | 594 | } else { |