swift: Fix compilation error when dependency adds the macOS SDK in the include dirs #13376
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
SwiftPM issue with more context: swiftlang/swift-package-manager#6439
Some dependencies can bring include paths pointing to older macOS SDK's. In this case, it was libffi pointing to SDK from 12.0. When the Foundation framework is imported in Swift, swiftc attempts to import the FFI module from the most recent version of the SDK, which causes a compilation error because of conflicting definitions between the two SDK versions.
It would error out like so:
I decided to do exactly what SwiftPM did: swiftlang/swift-package-manager#6772. Let's just naively look for .sdk paths in the compile args of our dependencies and replace them with the most recent one. I saw SwiftPM also patching linking arguments, but in my experience the include dirs are enough to get rid of the issue, and I couldn't replicate a scenario where linking would also be broken, so I'd rather not touch it without testing.
It's done in a very rudimentary way but works. Let me know if something fancier is needed, with proper regex or something. :)
I included a test which is confirmed to fail without the workaround added in this patch. This was not tested on anything else than macOS, but I don't expect it to make the situation worse on iOS and other
darwin
subplatforms.