Skip to content

Commit b933aea

Browse files
committed
SwiftDriver: initial work to properly handle android cross-compilation
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.
1 parent eab7910 commit b933aea

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

+30-8
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,17 @@ extension GenericUnixToolchain {
186186
}
187187

188188
if !isEmbeddedEnabled && !parsedOptions.hasArgument(.nostartfiles) {
189-
let swiftrtPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
190-
.appending(
191-
components: targetTriple.platformName() ?? "",
192-
String(majorArchitectureName(for: targetTriple)),
193-
"swiftrt.o"
194-
)
195-
commandLine.appendPath(swiftrtPath)
189+
let rsrc: VirtualPath
190+
if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
191+
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
192+
.appending(components: "usr", "lib", "swift")
193+
.pathString)
194+
} else {
195+
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
196+
}
197+
let platform: String = targetTriple.platformName() ?? ""
198+
let architecture: String = majorArchitectureName(for: targetTriple)
199+
commandLine.appendPath(rsrc.appending(components: platform, architecture, "swiftrt.o"))
196200
}
197201

198202
// If we are linking statically, we need to add all
@@ -232,7 +236,25 @@ extension GenericUnixToolchain {
232236
commandLine.appendPath(try VirtualPath(path: opt.argument.asSingle))
233237
}
234238

235-
if let path = targetInfo.sdkPath?.path {
239+
if targetTriple.environment == .android {
240+
if let ndk = env["ANDROID_NDK_ROOT"] {
241+
var sysroot: AbsolutePath =
242+
try AbsolutePath(validating: ndk)
243+
.appending(components: "toolchains", "llvm", "prebuilt")
244+
#if arch(x86_64)
245+
#if os(Windows)
246+
.appending(component: "windows-x86_64")
247+
#elseif os(Linux)
248+
.appending(component: "linux-x86_64")
249+
#else
250+
.appending(component: "darwin-x86_64")
251+
#endif
252+
#endif
253+
.appending(component: "sysroot")
254+
commandLine.appendFlag("--sysroot")
255+
commandLine.appendPath(sysroot)
256+
}
257+
} else if let path = targetInfo.sdkPath?.path {
236258
commandLine.appendFlag("--sysroot")
237259
commandLine.appendPath(VirtualPath.lookup(path))
238260
}

0 commit comments

Comments
 (0)