Skip to content

Extend xcrun SDK resolution to all Apple platforms for NativeAOT linker#126893

Open
Copilot wants to merge 3 commits intomainfrom
copilot/fix-nativeaot-build-issue
Open

Extend xcrun SDK resolution to all Apple platforms for NativeAOT linker#126893
Copilot wants to merge 3 commits intomainfrom
copilot/fix-nativeaot-build-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

Description

NativeAOT builds fail intermittently on macOS-15 arm64 because Swift auto-linked libraries (swift_Builtin_float, swift_errno, etc.) from libSystem.Security.Cryptography.Native.Apple.a cannot be found. The linker targets hardcode -L/usr/lib/swift, but on macOS 15+ these shims may only exist under the Xcode SDK path.

iOS-like platforms are unaffected because they already resolve the SDK via xcrun --show-sdk-path and pass -isysroot which makes Apple's ld remap all -L paths relative to the sysroot. macOS desktop targets (_targetOS == 'osx') previously didn't participate in this SDK resolution, so the hardcoded /usr/lib/swift path was the only search location.

Changes:

  • Set _AppleSdkName to macosx for osx targets, matching how iOS-like platforms already define their SDK names
  • Broaden the existing Xcrun property initialization, command -v check, and xcrun --show-sdk-path conditions from _IsiOSLikePlatform to _IsApplePlatform so macOS also resolves its SDK path into SysRoot
  • The existing -isysroot "$(SysRoot)" linker argument (already conditioned on _IsApplePlatform) then automatically remaps -L/usr/lib/swift to <SDK>/usr/lib/swift
  • Error conditions for missing xcrun and missing SDK remain scoped to _IsiOSLikePlatform so macOS builds degrade gracefully if xcrun is unavailable (e.g., cross-compilation from non-macOS hosts where SysRoot would be set manually)

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-infrastructure-libraries
See info in area-owners.md if you want to be subscribed.

On macOS 15+, Swift stdlib shims may only exist under the Xcode SDK
path and not in /usr/lib/swift. This causes NativeAOT builds to fail
intermittently when linking libSystem.Security.Cryptography.Native.Apple.a.

Resolve the macOS SDK path via `xcrun --show-sdk-path` and add it as
an additional -L linker search path. iOS-like platforms are unaffected
since they already pass -isysroot which remaps -L paths automatically.

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/1ea30f49-d15d-45c6-be1d-9572f6f8bf28

Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 14, 2026 18:11
Copilot AI changed the title [WIP] Fix random NativeAOT build failures on arm64 macOS-15 Add macOS SDK Swift library path to NativeAOT linker arguments Apr 14, 2026
Copilot AI requested a review from agocke April 14, 2026 18:13
Comment on lines +131 to +137
<!-- Resolve macOS SDK path for Swift library lookup. On macOS 15+, Swift stdlib shims
may only exist under the SDK (e.g. <Xcode SDK>/usr/lib/swift) and not in /usr/lib/swift.
iOS-like platforms already get this through -isysroot which remaps -L paths. -->
<Exec Command="xcrun --show-sdk-path" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMsBuild="true"
Condition="'$(_targetOS)' == 'osx' and '$(_HasDefaultSysRoot)' != 'true'">
<Output TaskParameter="ConsoleOutput" PropertyName="_MacOSXSdkPath" />
</Exec>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just change the condition for the `xcrun --show-sdk-path above to also work for macos instead of only iOS-like platforms?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot this suggestion seems good, let's make that change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in e858ef0. Extended the existing xcrun conditions from _IsiOSLikePlatform to _IsApplePlatform and added _AppleSdkName=macosx for osx targets. This sets SysRoot for macOS so the existing -isysroot on the linker command handles the SDK path remapping automatically. Error conditions remain iOS-only so macOS builds degrade gracefully if xcrun isn't available.

@agocke
Copy link
Copy Markdown
Member

agocke commented Apr 15, 2026

OK, after going back and forth with copilot, the operating theory is that this is a difference in xcode-select and whether or not the machine has been configured for full command line tools, vs full xcode config.

I'm not sure that's right, but I think it's plausible. So this fix seems reasonable.

@agocke agocke marked this pull request as ready for review April 15, 2026 04:57
Copilot AI review requested due to automatic review settings April 15, 2026 04:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the NativeAOT Unix MSBuild targets to improve macOS 15+ linking reliability by adding an additional Swift library search path derived from the Xcode macOS SDK, addressing missing auto-linked Swift shim libraries during linking.

Changes:

  • Resolves the macOS SDK path for osx targets by invoking xcrun --show-sdk-path (best-effort).
  • Adds -L<macOS SDK>/usr/lib/swift to NativeAOT linker arguments for macOS, while keeping the existing /usr/lib/swift fallback.

<!-- Resolve macOS SDK path for Swift library lookup. On macOS 15+, Swift stdlib shims
may only exist under the SDK (e.g. <Xcode SDK>/usr/lib/swift) and not in /usr/lib/swift.
iOS-like platforms already get this through -isysroot which remaps -L paths. -->
<Exec Command="xcrun --show-sdk-path" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMsBuild="true"
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new SDK path resolution uses xcrun --show-sdk-path without specifying a macOS SDK, and it hardcodes xcrun instead of using the existing $(Xcrun) indirection. xcrun will honor SDKROOT/developer environment and can return a non-macOS SDK path, and hardcoding bypasses any user override of Xcrun. Consider setting Xcrun for all Apple targets (not just iOS-like) and invoking "$(Xcrun)" --sdk macosx --show-sdk-path here so the resolved path is deterministic for osx builds.

Suggested change
<Exec Command="xcrun --show-sdk-path" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMsBuild="true"
<Exec Command="&quot;$(Xcrun)&quot; --sdk macosx --show-sdk-path" IgnoreExitCode="true" StandardOutputImportance="Low" ConsoleToMsBuild="true"

Copilot uses AI. Check for mistakes.
…of separate macOS block

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/1cf65e66-4b7b-409c-bd4c-58c477560e6f

Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
Copilot AI changed the title Add macOS SDK Swift library path to NativeAOT linker arguments Extend xcrun SDK resolution to all Apple platforms for NativeAOT linker Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

NativeAOT builds fail randomly on arm64 macOS-15 with 'Could not find or use auto-linked library'

4 participants