Skip to content
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

SwiftDriver: initial work to properly handle android cross-compilation #1560

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

compnerd
Copy link
Collaborator

The intent here is to permit the Windows/macOS style cross-compilation for Android. This involves passing -sdk with the path to the "Swift SDK" which overlays the system's native SDK (NDK). The ANDROID_NDK_ROOT is a well-defined environment variable (setup by the SDK installer as well as a general expectation for Android development) that identifies the root of the installation of the NDK. This allows us to locate the native SDK root (--sysroot) for driving the linker driver amongst other paths.

if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
.appending(components: "usr", "lib", "swift")
.pathString)
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that if -resource-dir is not specified, this behavior is already the default, so the only reason to do this is to override -resource-dir. Any reason why you need that? I think this is a bad idea to silently override it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is not the default - this is going to try to prefer the resources in the SDK rather than the toolchain (which allows us to have platform specific resources).

Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, never mind, what I'm saying was true of the old C++ Driver, but is not true of this new swift-driver, after manually testing it on linux.

I think this is a genuine discrepancy between the two drivers that we should find the root cause and fix, rather than putting in partial workarounds like this.

@compnerd
Copy link
Collaborator Author

With this set of changes I am down to:

swiftc -target aarch64-unknown-linux-android28 -sdk "S:\Program Files\Swift\Platforms\Android.platform\Developer\SDKs\Android.sdk" -emit-executable -o hello hello.swift -Xclang-linker "-LS:\Program Files\Swift\Platforms\Android.platform\Developer\SDKs\Android.sdk\usr\lib\swift\android\aarch64" -Xclang-linker -resource-dir -Xclang-linker S:\b\android-ndk-r26b\toolchains\llvm\prebuilt\windows-x86_64\lib\clang\17

The bits that I would like to remove:

-Xclang-linker "-LS:\Program Files\Swift\Platforms\Android.platform\Developer\SDKs\Android.sdk\usr\lib\swift\android\aarch64"

This feels like we are failing to setup the library search path when we have a -sdk parameter for the layout like we do on Windows.

-Xclang-linker -resource-dir -Xclang-linker S:\b\android-ndk-r26b\toolchains\llvm\prebuilt\windows-x86_64\lib\clang\17

This is due to my builds missing clang_rt.builtins-... for Android and so we need to pull some files from the NDK.

Addressing those two issues would give us the following invocation:

swiftc -target aarch64-unknown-linux-android28 -sdk "S:\Program Files\Swift\Platforms\Android.platform\Developer\SDKs\Android.sdk" -emit-executable -o hello hello.swift

and with SDKROOT set to S:\Program Files\Swift\Platforms\Android.platform\Developer\SDKs\Android.sdk and ANDROID_NDK_ROOT set to S:\b\android-ndk-r26b, we should then be able to build code as:

swiftc -target aarch64-unknown-linux-android28 -emit-executable -o reduced reduced.swift

@finagolfin
Copy link
Contributor

The reason this entire approach doesn't already work is because of a regression in this new Swift Driver, see #1562. We should fix that for all platforms, not paper over it with these Android-specific changes.

The intent here is to permit the Windows/macOS style cross-compilation
for Android. This involves passing `-sdk` with the path to the "Swift
SDK" which overlays the system's native SDK (NDK). The
`ANDROID_NDK_ROOT` is a well-defined environment variable (setup by the
SDK installer as well as a general expectation for Android development)
that identifies the root of the installation of the NDK. This allows us
to locate the native SDK root (`--sysroot`) for driving the linker
driver amongst other paths.
@compnerd
Copy link
Collaborator Author

I'm not sure I'm following the "papering over" here. The idea here is to default the -sdk parameter for android targets using ANDROID_NDK_ROOT, and subsequently -sysroot as well. You can always explicitly specify the parameters. We do something similar for Windows where SDKROOT will be used to default the parameter for -sdk on Windows, and we query the registry for determining the path to the various system include directories.

@finagolfin
Copy link
Contributor

There are two aspects to this pull, having this new swift driver read environment variables like SDKROOT/ANDROID_NDK_ROOT and looking in those paths for the right build files.

I don't think we should hard-code such environment variables in this driver, but always "explicitly specify" them, as you say. As for finding build files, the reason that doesn't work already is #1562: take a look at my detailed writeup from a couple weeks ago for why.

@compnerd
Copy link
Collaborator Author

I disagree with you on that point. I would like the default value behavior for the targets where possible, much as it works on macOS. Fixing the behavior to work with the parameters however should be done.

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.

None yet

2 participants