Skip to content

Pass SDK to Compiler when Cross‐Compiling #2617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

SDGGiesbrecht
Copy link
Contributor

This pull request makes SwiftPM pass -sdk [wherever] to swiftc when cross‐compiling. This used to be the case, but regressed since 5.1.3, breaking Android builds.

The implementation here makes two assumptions that I would like confirmation on:

  1. -sdk is necessary for all cross‐compilation, not just for Android. @compnerd
  2. -sdk is not necessary for Android to build itself. @buttaface

If either turns out to be incorrect, the precise conditions can be adjusted accordingly.

Here is the diff this enables to the Android build script. Notice how without this, the SDK must be specified double, once for SwiftPM and once for swiftc.

You can also see the results of compiling without the redundant flag before and after this change.

@jakepetroules
Copy link
Contributor

We should take the viewpoint that all compilation is cross compilation (this is essentially what Xcode does for Apple platforms). There's no need at all to detect and conditionalize this - just pass -sdk in all cases.

@SDGGiesbrecht
Copy link
Contributor Author

That is what I thought at first too. But then why was it deliberately removed in the past? The last commit to touch this particular code was a commit that deliberately removed -sdk outside Darwin. I must confess I don’t really understand the description of that commit, since what it seems to be saying is the opposite of what I observe in practice. But I don’t want to break anything by overapplying -sdk. @compnerd

@jakepetroules
Copy link
Contributor

It was removed for non-Darwin platforms previously; you'd have to ask @compnerd for more info there. But that seems orthogonal to attempting to conditionalize it as you're proposing in this patch.

@SDGGiesbrecht
Copy link
Contributor Author

SDGGiesbrecht commented Feb 26, 2020

But that seems orthogonal to attempting to conditionalize it

Without some sort of condition, this would be nothing but a revert of that commit (+ a new test). Unless that entire commit was a mistake, -sdk is invalid in some situation or other. @compnerd

@finagolfin
Copy link
Member

finagolfin commented Feb 26, 2020

-sdk is not necessary for Android to build itself.

If you mean when building SPM packages, like SPM itself, natively on an Android host, no, it's not necessary.

As for the issue of why it was dropped, I believe it has to do with -sdk not being precisely defined on non-Darwin platforms, see swiftlang/swift#26361 and associated forum thread.

@aciidgh
Copy link
Contributor

aciidgh commented Feb 27, 2020

@compnerd's commit might be outdated now since these days you are required to pass the -sdk when compiling for Windows. For example, this is how I was able to compile these examples using CMake:

cmake -G Ninja -DCMAKE_Swift_FLAGS="-sdk C:/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk -I C:/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/usr/lib/swift -L C:/Library/Developer/Platforms/Windows.platform/Developer/SDKs/Windows.sdk/usr/lib/swift/windows" -DBUILD_SHARED_LIBS=YES ..

@compnerd
Copy link
Member

At the point that the change was made, I believe that -sdk was actually causing problems. I modified swiftc at a latter point to honor -sdk on other targets. Its still not perfect, but, its better than previously. We now treat -sdk similar to --sysroot on clang. I would say that if you can test it, then its perfectly reasonable to revert the change and pass -sdk unconditionally.

@SDGGiesbrecht
Copy link
Contributor Author

SDGGiesbrecht commented Feb 27, 2020

Yes, I also need to pass -sdk when I use his Windows toolchain (with CMake). So should we just revert that commit instead then? I was really hoping @compnerd would have some answers for us. Edit: I see you answered while I was typing.

Do you see any harm in that @buttaface? What happens with your native Android toolchain right now if you compile a package with these extra flags: -Xswiftc -sdk -Xswiftc /. That should mimic what will happen if we revert that commit.

@finagolfin
Copy link
Member

What happens with your native Android toolchain right now if you compile a package with these extra flags: -Xswiftc -sdk -Xswiftc /

That's fine: it is what the SPM in my Swift 5.1.4 package for Android does, termux/termux-packages#4895, since it predates Saleem removing it. I messed with changing -sdk to the Termux sysroot last year but had some problems, so kept it at /. I will try patching it again to the Termux sysroot at some point, but that'll be external, so don't worry about it.

@SDGGiesbrecht
Copy link
Contributor Author

Okay then it sounds like we should just revert.

  • ✅ Darwin: Nothing changes.
  • ✅ Linux: It goes back to the way it was for years. (And the standard CI will catch it if anything changed since.)
  • ✅ Android: It demonstrably fixes cross‐compilation (see first comment), and native compilation demonstrably worked before the commit (see last comment).
  • ✅ Windows: The flag demonstrably works in CMake (see this comment) (SwiftPM has never been operable yet).

I’ll do it in a separate pull request.

SDGGiesbrecht added a commit to SDGGiesbrecht/swift-package-manager that referenced this pull request Feb 27, 2020
This reapplies -sdk universally.
See swiftlang#2617.
@SDGGiesbrecht SDGGiesbrecht deleted the sdg‐sdk branch March 3, 2020 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants