Skip to content

Commit 9689188

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 412c92a commit 9689188

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

+20
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,26 @@ extension Driver {
182182
try commandLine.appendAll(.F, .Fsystem, from: &parsedOptions)
183183
try commandLine.appendAll(.vfsoverlay, from: &parsedOptions)
184184

185+
if targetTriple.environment == .android {
186+
if let ndk = env["ANDROID_NDK_ROOT"] {
187+
var sysroot: AbsolutePath =
188+
try AbsolutePath(validating: ndk)
189+
.appending(components: "toolchains", "llvm", "prebuilt")
190+
#if arch(x86_64)
191+
#if os(Windows)
192+
.appending(component: "windows-x86_64")
193+
#elseif os(Linux)
194+
.appending(component: "linux-x86_64")
195+
#else
196+
.appending(component: "darwin-x86_64")
197+
#endif
198+
#endif
199+
.appending(component: "sysroot")
200+
commandLine.appendFlag("-sysroot")
201+
commandLine.appendFlag(sysroot.pathString)
202+
}
203+
}
204+
185205
if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
186206
appendXccFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
187207
}

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

+37-8
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,25 @@ extension GenericUnixToolchain {
186186
}
187187
}
188188

189+
if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
190+
for libpath in targetInfo.runtimeLibraryImportPaths {
191+
commandLine.appendFlag(.L)
192+
commandLine.appendPath(VirtualPath.lookup(libpath.path))
193+
}
194+
}
195+
189196
if !isEmbeddedEnabled && !parsedOptions.hasArgument(.nostartfiles) {
190-
let swiftrtPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
191-
.appending(
192-
components: targetTriple.platformName() ?? "",
193-
String(majorArchitectureName(for: targetTriple)),
194-
"swiftrt.o"
195-
)
196-
commandLine.appendPath(swiftrtPath)
197+
let rsrc: VirtualPath
198+
if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
199+
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
200+
.appending(components: "usr", "lib", "swift")
201+
.pathString)
202+
} else {
203+
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
204+
}
205+
let platform: String = targetTriple.platformName() ?? ""
206+
let architecture: String = majorArchitectureName(for: targetTriple)
207+
commandLine.appendPath(rsrc.appending(components: platform, architecture, "swiftrt.o"))
197208
}
198209

199210
// If we are linking statically, we need to add all
@@ -233,7 +244,25 @@ extension GenericUnixToolchain {
233244
commandLine.appendPath(try VirtualPath(path: opt.argument.asSingle))
234245
}
235246

236-
if let path = targetInfo.sdkPath?.path {
247+
if targetTriple.environment == .android {
248+
if let ndk = env["ANDROID_NDK_ROOT"] {
249+
var sysroot: AbsolutePath =
250+
try AbsolutePath(validating: ndk)
251+
.appending(components: "toolchains", "llvm", "prebuilt")
252+
#if arch(x86_64)
253+
#if os(Windows)
254+
.appending(component: "windows-x86_64")
255+
#elseif os(Linux)
256+
.appending(component: "linux-x86_64")
257+
#else
258+
.appending(component: "darwin-x86_64")
259+
#endif
260+
#endif
261+
.appending(component: "sysroot")
262+
commandLine.appendFlag("--sysroot")
263+
commandLine.appendPath(sysroot)
264+
}
265+
} else if let path = targetInfo.sdkPath?.path {
237266
commandLine.appendFlag("--sysroot")
238267
commandLine.appendPath(VirtualPath.lookup(path))
239268
}

0 commit comments

Comments
 (0)