Skip to content

Support custom host SDK (aka. SDKROOT) #266

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 7 commits into from

Conversation

stevapple
Copy link
Contributor

This PR differentiates host SDK and target SDK, and adds support for setting custom host SDK through "Advanced" configuration.

@stevapple
Copy link
Contributor Author

@adam-fowler Are you able to reproduce the CI error? I didn't think this has touched the behavior if no custom SDK is set, and cannot reproduce in my environments (Windows/5.6.1 & macOS/5.6) either.

@adam-fowler
Copy link
Contributor

I can't see why this should fail during CI. Running tests locally works fine. Can you merge in latest changes as it is very hard to see which changes are specific to this PR.

@stevapple
Copy link
Contributor Author

@adam-fowler Do you have time to review this now?

@adam-fowler
Copy link
Contributor

So as I understand it you have added a new setting hostSDK, which is used as the SDKROOT environment variable when running many of the swift commands. At the same time a couple of the swift package commands (describe, unedit) have had the runtime and compiler environment removed from them, as it is unnecessary. Any reason you didn't do this for edit.

With this PR plus the two prior PRs we now have settings
SDK: Adds --sdk parameter to swift calls
runtimePath: Added to Path env var when compiling and running
hostSDK: Set SDKROOT environment variable when compiling

@adam-fowler
Copy link
Contributor

What is the point of setting SDKROOT env variable and also including --sdk command line parameter?

@stevapple
Copy link
Contributor Author

At the same time a couple of the swift package commands (describe, unedit) have had the runtime and compiler environment removed from them, as it is unnecessary. Any reason you didn't do this for edit.

Could you point out where I may be missing? It feels like any swift-package command won’t be affected by custom SDK flags.

I intentionally want to make:

  • SDK (and the upcoming "Target") applied to Build Tasks and Run Script Task (and possible debugging on Windows)
  • host SDK applied to all tasks except for debugging
  • runtime path applied everywhere

@stevapple
Copy link
Contributor Author

What is the point of setting SDKROOT env variable and also including --sdk command line parameter?

SDKROOT is used to override host SDK, and --sdk is specified for the target. SwiftPM needs to run the manifest on the host, that’s where host SDK should be explicitly used. (It makes sense, but the use case is rare).

BTW SDKROOT only works on macOS and Windows, that’s something that needs to be documented.

@adam-fowler
Copy link
Contributor

`SwiftPM needs to run the manifest on the host, that’s where host SDK should be explicitly used.

Which commands are we talking about here?
Also any reason we don't use --sdk to point to the host SDK, in this situation?

@stevapple
Copy link
Contributor Author

stevapple commented Apr 27, 2022

Considering compiling a Swift package on macOS without Xcode (wish this could happen, but just imagine) for iOS.

We need to explicitly set the host SDK, or the manifest won’t compile. And we also need to set the target SDK, or the target cannot be supported. So finally it becomes:

SDKROOT=/path/to/macos/sdk swift build --triple arm64-apple-ios --sdk /path/to/ios/sdk

Maybe compiling for Android on Windows is easier to imagine:

SDKROOT=/path/to/windows/sdk swift build --triple aarch64-unknown-linux-android --sdk /path/to/android/sdk

@stevapple
Copy link
Contributor Author

stevapple commented Apr 27, 2022

Also any reason we don't use --sdk to point to the host SDK, in this situation?

Host SDK is never decided by CLI options. There’re only two sources: SDKROOT value > xcrun output

@adam-fowler
Copy link
Contributor

So what are the reasons you might want to make the "host" sdk configurable? If it is just to compile Package.swift I can't see what that would be.

@stevapple
Copy link
Contributor Author

If you don’t need to cross-compile, you’re recommended to set host SDK instead of custom SDK. The latter should be used explicitly for cross-compilation.

Also, setting SDKROOT is required for using an alternative Windows toolchain now. There is no second way.

@adam-fowler
Copy link
Contributor

If you don’t need to cross-compile, you’re recommended to set host SDK instead of custom SDK. The latter should be used explicitly for cross-compilation.

The "host" SDK is explicitly set already (when you install Xcode, and as I understand it SDKROOT is set when installing Swift on Windows, I may be wrong there).

Also, setting SDKROOT is required for using an alternative Windows toolchain now. There is no second way.

So does that mean you only use --sdk when doing cross compilation.

@stevapple
Copy link
Contributor Author

The "host" SDK is explicitly set already (when you install Xcode, and as I understand it SDKROOT is set when installing Swift on Windows, I may be wrong there).

You can have multiple macOS SDKs in parallel. macOS SDKs are standalone and you can extract older or newer SDKs from other versions of Xcode if you want.

So does that mean you only use --sdk when doing cross compilation.

At least when compiling to another triple (eg. a custom minimal OS version). I don’t think there should be a second usage.

Maybe we should hide it for now, and wait until custom target support?

@adam-fowler
Copy link
Contributor

So on macOS what do we gain from setting SDKROOT when we can already set the path to the swift compiler? The only thing I can think of is xctest.

@stevapple
Copy link
Contributor Author

So on macOS what do we gain from setting SDKROOT when we can already set the path to the swift compiler?

On macOS toolchain and SDK is not tied so closely. Using alternative toolchain doesn’t affect the SDK (thanks to ABI stability). You always need to set SDKROOT if you want to use an SDK outside your current Xcode installation.

@stevapple
Copy link
Contributor Author

The only thing I can think of is xctest.

XCTest on macOS is part of the toolchain. Because it is found using xcode-select, we have no option to override it yet.

@adam-fowler
Copy link
Contributor

You always need to set SDKROOT if you want to use an SDK outside your current Xcode installation.

But can't you use --sdk in that case?

@stevapple
Copy link
Contributor Author

  1. --sdk <sdk> should be used for cross-compilation;
  2. Manifest compiler won’t take this.

@stevapple
Copy link
Contributor Author

@adam-fowler Any review? Better get this in 0.5.0

@stevapple
Copy link
Contributor Author

BTW, plugins in SwiftPM 5.6+ will also stick to host SDK.

Copy link
Contributor

@adam-fowler adam-fowler left a comment

Choose a reason for hiding this comment

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

In general this looks fine. I'm not sure about the terminology host SDK. I don't remember seeing it anywhere else. Given people who are changing this will know it as SDKROOT call it that.

I'm not including this in 0.5.0 though. As I don't want to rush it in.

@@ -198,17 +198,22 @@ export class SwiftToolchain {
return path.join(stdout.trimEnd(), "usr", "bin");
}
case "win32": {
// look up runtime library directory for XCTest alternatively
const fallbackPath =
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this change it is unrelated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just moved here from L207-209

@stevapple
Copy link
Contributor Author

I'm not including this in 0.5.0 though. As I don't want to rush it in.

If you're not including this for 0.5.0, I'll split this pitch into two parts (fixing up target SDK & adding host SDK) and left the former merged first. The current behavior of custom target SDK has some mistakes that need to be addressed.

@stevapple
Copy link
Contributor Author

I'm not sure about the terminology host SDK.

This terminology is derived from "host toolchain" in SwiftPM source code. I don't know where SDKROOT is from but it seems to be a more ancient and platform-specific concept and I would tend to keep the SwiftPM naming.

@stevapple
Copy link
Contributor Author

Given that SwiftPM uses the terminology "destination" to describe the target, I'll update targetSDK into destinationSDK instead.

@adam-fowler
Copy link
Contributor

I'm not sure about the terminology host SDK.

This terminology is derived from "host toolchain" in SwiftPM source code. I don't know where SDKROOT is from but it seems to be a more ancient and platform-specific concept and I would tend to keep the SwiftPM naming.

host toolchain is a different thing from host sdk

@stevapple
Copy link
Contributor Author

host toolchain is a different thing from host sdk

There is a clear relationship between destination toolchain — destination SDK — destination target triple. Similarly the logic can be applied to host.

@adam-fowler
Copy link
Contributor

Now that #305 is merged, can we close this

@stevapple stevapple closed this May 16, 2022
@stevapple stevapple deleted the host-sdk branch August 1, 2022 20:23
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.

2 participants