| author | |
| committer | |
| log | 9357973912798d3f64938762ea8993ff3a083ac0 |
| tree | 96904629a22bb99a16b427857569696d8612c276 |
| parent | 50f2d7958226efaa9cd441998877c125cd01533b |
| signature |
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 path5 files changed, 43 insertions(+), 33 deletions(-)
src/Compilation.zig+8-7| ... | ... | @@ -1554,10 +1554,10 @@ 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, | |
| 1558 | 1557 | .link_libc = link_libc, |
| 1559 | 1558 | .link_libcpp = link_libcpp, |
| 1560 | 1559 | .link_libunwind = link_libunwind, |
| 1560 | .darwinSdkLayout = libc_dirs.darwinSdkLayout, | |
| 1561 | 1561 | .objects = options.link_objects, |
| 1562 | 1562 | .frameworks = options.frameworks, |
| 1563 | 1563 | .framework_dirs = options.framework_dirs, |
| ... | ... | @@ -5287,6 +5287,7 @@ fn detectWin32ResourceIncludeDirs(arena: Allocator, options: InitOptions) !LibCD |
| 5287 | 5287 | .libc_installation = null, |
| 5288 | 5288 | .libc_framework_dir_list = &.{}, |
| 5289 | 5289 | .sysroot = null, |
| 5290 | .darwinSdkLayout = .none, | |
| 5290 | 5291 | }, |
| 5291 | 5292 | } |
| 5292 | 5293 | } |
| ... | ... | @@ -5655,7 +5656,7 @@ const LibCDirs = struct { |
| 5655 | 5656 | libc_installation: ?*const LibCInstallation, |
| 5656 | 5657 | libc_framework_dir_list: []const []const u8, |
| 5657 | 5658 | sysroot: ?[]const u8, |
| 5658 | provider: link.LibCProvider, | |
| 5659 | darwinSdkLayout: link.DarwinSdkLayout, | |
| 5659 | 5660 | }; |
| 5660 | 5661 | |
| 5661 | 5662 | fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) !LibCDirs { |
| ... | ... | @@ -5671,7 +5672,7 @@ fn getZigShippedLibCIncludeDirsDarwin(arena: Allocator, zig_lib_dir: []const u8) |
| 5671 | 5672 | .libc_installation = null, |
| 5672 | 5673 | .libc_framework_dir_list = &.{}, |
| 5673 | 5674 | .sysroot = null, |
| 5674 | .provider = .vendored, | |
| 5675 | .darwinSdkLayout = .vendored, | |
| 5675 | 5676 | }; |
| 5676 | 5677 | } |
| 5677 | 5678 | |
| ... | ... | @@ -5689,7 +5690,7 @@ pub fn detectLibCIncludeDirs( |
| 5689 | 5690 | .libc_installation = null, |
| 5690 | 5691 | .libc_framework_dir_list = &.{}, |
| 5691 | 5692 | .sysroot = null, |
| 5692 | .provider = .none, | |
| 5693 | .darwinSdkLayout = .none, | |
| 5693 | 5694 | }; |
| 5694 | 5695 | } |
| 5695 | 5696 | |
| ... | ... | @@ -5747,7 +5748,7 @@ pub fn detectLibCIncludeDirs( |
| 5747 | 5748 | .libc_installation = null, |
| 5748 | 5749 | .libc_framework_dir_list = &.{}, |
| 5749 | 5750 | .sysroot = null, |
| 5750 | .provider = .none, | |
| 5751 | .darwinSdkLayout = .none, | |
| 5751 | 5752 | }; |
| 5752 | 5753 | } |
| 5753 | 5754 | |
| ... | ... | @@ -5802,7 +5803,7 @@ fn detectLibCFromLibCInstallation(arena: Allocator, target: Target, lci: *const |
| 5802 | 5803 | .libc_installation = lci, |
| 5803 | 5804 | .libc_framework_dir_list = framework_list.items, |
| 5804 | 5805 | .sysroot = sysroot, |
| 5805 | .provider = if (sysroot == null) .installation else .sysroot, | |
| 5806 | .darwinSdkLayout = if (sysroot == null) .none else .sdk, | |
| 5806 | 5807 | }; |
| 5807 | 5808 | } |
| 5808 | 5809 | |
| ... | ... | @@ -5864,7 +5865,7 @@ fn detectLibCFromBuilding( |
| 5864 | 5865 | .libc_installation = null, |
| 5865 | 5866 | .libc_framework_dir_list = &.{}, |
| 5866 | 5867 | .sysroot = null, |
| 5867 | .provider = .vendored, | |
| 5868 | .darwinSdkLayout = .vendored, | |
| 5868 | 5869 | }; |
| 5869 | 5870 | } |
| 5870 | 5871 |
src/link.zig+7-4| ... | ... | @@ -134,10 +134,10 @@ 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, | |
| 138 | 137 | link_libc: bool, |
| 139 | 138 | link_libcpp: bool, |
| 140 | 139 | link_libunwind: bool, |
| 140 | darwinSdkLayout: DarwinSdkLayout, | |
| 141 | 141 | function_sections: bool, |
| 142 | 142 | no_builtin: bool, |
| 143 | 143 | eh_frame_hdr: bool, |
| ... | ... | @@ -283,10 +283,13 @@ pub const HashStyle = enum { sysv, gnu, both }; |
| 283 | 283 | |
| 284 | 284 | pub const CompressDebugSections = enum { none, zlib }; |
| 285 | 285 | |
| 286 | pub const LibCProvider = enum { | |
| 286 | /// The filesystem layout of darwin SDK elements. | |
| 287 | pub const DarwinSdkLayout = enum { | |
| 288 | /// Does not apply to the target. | |
| 287 | 289 | none, |
| 288 | installation, | |
| 289 | sysroot, | |
| 290 | /// macOS SDK layout: TOP { /usr/include, /usr/lib, /System/Library/Frameworks }. | |
| 291 | sdk, | |
| 292 | /// Shipped libc layout: TOP { /lib/libc/include, /lib/libc/darwin, <NONE> }. | |
| 290 | 293 | vendored, |
| 291 | 294 | }; |
| 292 | 295 |
src/link/MachO.zig+3-4| ... | ... | @@ -558,7 +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 = load_commands.inferSdkVersion(comp); | |
| 561 | const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp); | |
| 562 | 562 | if (platform.isBuildVersionCompatible()) { |
| 563 | 563 | try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); |
| 564 | 564 | } else if (platform.isVersionMinCompatible()) { |
| ... | ... | @@ -652,10 +652,9 @@ pub fn resolveLibSystem( |
| 652 | 652 | "libSystem", |
| 653 | 653 | )) break :success; |
| 654 | 654 | |
| 655 | switch (self.base.options.libc_provider) { | |
| 655 | switch (self.base.options.darwinSdkLayout) { | |
| 656 | 656 | .none => unreachable, |
| 657 | .installation => unreachable, | |
| 658 | .sysroot => { | |
| 657 | .sdk => { | |
| 659 | 658 | const dir = try fs.path.join(tmp_arena, &[_][]const u8{ self.base.options.sysroot.?, "usr", "lib" }); |
| 660 | 659 | if (try accessLibPath(tmp_arena, &test_path, &checked_paths, dir, "libSystem")) break :success; |
| 661 | 660 | }, |
src/link/MachO/load_commands.zig+23-16| ... | ... | @@ -467,31 +467,27 @@ pub inline fn appleVersionToSemanticVersion(version: u32) std.SemanticVersion { |
| 467 | 467 | }; |
| 468 | 468 | } |
| 469 | 469 | |
| 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); | |
| 470 | pub fn inferSdkVersion(gpa: Allocator, comp: *const Compilation) ?std.SemanticVersion { | |
| 471 | var arena_allocator = std.heap.ArenaAllocator.init(gpa); | |
| 480 | 472 | defer arena_allocator.deinit(); |
| 481 | 473 | const arena = arena_allocator.allocator(); |
| 474 | ||
| 482 | 475 | const options = comp.bin_file.options; |
| 483 | 476 | |
| 484 | const sdk_dir = switch (options.libc_provider) { | |
| 477 | const sdk_dir = switch (options.darwinSdkLayout) { | |
| 485 | 478 | .none => unreachable, |
| 486 | .installation => unreachable, | |
| 487 | .sysroot => options.sysroot.?, | |
| 479 | .sdk => options.sysroot.?, | |
| 488 | 480 | .vendored => std.fs.path.join(arena, &.{ comp.zig_lib_directory.path.?, "libc", "darwin" }) catch return null, |
| 489 | 481 | }; |
| 490 | 482 | |
| 491 | // prefer meta information if available | |
| 492 | if (readSdkVersionString(arena, sdk_dir)) |ver| { | |
| 483 | if (readSdkVersionFromSettings(arena, sdk_dir)) |ver| { | |
| 493 | 484 | return parseSdkVersion(ver); |
| 494 | } else |_| {} | |
| 485 | } else |_| { | |
| 486 | if (options.darwinSdkLayout == .vendored) { | |
| 487 | // vendored layout does not have versioned pathname | |
| 488 | return null; | |
| 489 | } | |
| 490 | } | |
| 495 | 491 | |
| 496 | 492 | // infer from pathname |
| 497 | 493 | const stem = std.fs.path.stem(sdk_dir); |
| ... | ... | @@ -505,6 +501,17 @@ pub fn inferSdkVersion(comp: *const Compilation) ?std.SemanticVersion { |
| 505 | 501 | return parseSdkVersion(stem[start..end]); |
| 506 | 502 | } |
| 507 | 503 | |
| 504 | // Official Apple SDKs ship with a `SDKSettings.json` located at the top of SDK fs layout. | |
| 505 | // Use property `MinimalDisplayName` to determine version. | |
| 506 | // The file/property is also available with vendored libc. | |
| 507 | fn readSdkVersionFromSettings(arena: Allocator, dir: []const u8) ![]const u8 { | |
| 508 | const sdk_path = try std.fs.path.join(arena, &.{ dir, "SDKSettings.json" }); | |
| 509 | const contents = try std.fs.cwd().readFileAlloc(arena, sdk_path, std.math.maxInt(u16)); | |
| 510 | const parsed = try std.json.parseFromSlice(std.json.Value, arena, contents, .{}); | |
| 511 | if (parsed.value.object.get("MinimalDisplayName")) |ver| return ver.string; | |
| 512 | return error.SdkVersionFailure; | |
| 513 | } | |
| 514 | ||
| 508 | 515 | // Versions reported by Apple aren't exactly semantically valid as they usually omit |
| 509 | 516 | // the patch component, so we parse SDK value by hand. |
| 510 | 517 | fn parseSdkVersion(raw: []const u8) ?std.SemanticVersion { |
src/link/MachO/zld.zig+2-2| ... | ... | @@ -241,7 +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 = load_commands.inferSdkVersion(comp); | |
| 244 | const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp); | |
| 245 | 245 | if (sdk_version) |ver| { |
| 246 | 246 | try argv.append(try std.fmt.allocPrint(arena, "{d}.{d}", .{ ver.major, ver.minor })); |
| 247 | 247 | } else { |
| ... | ... | @@ -588,7 +588,7 @@ pub fn linkWithZld( |
| 588 | 588 | }); |
| 589 | 589 | { |
| 590 | 590 | const platform = Platform.fromTarget(macho_file.base.options.target); |
| 591 | const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(comp); | |
| 591 | const sdk_version: ?std.SemanticVersion = load_commands.inferSdkVersion(arena, comp); | |
| 592 | 592 | if (platform.isBuildVersionCompatible()) { |
| 593 | 593 | try load_commands.writeBuildVersionLC(platform, sdk_version, lc_writer); |
| 594 | 594 | } else { |