Skip to content

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

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

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,26 @@ extension Driver {
try commandLine.appendAll(.F, .Fsystem, from: &parsedOptions)
try commandLine.appendAll(.vfsoverlay, from: &parsedOptions)

if targetTriple.environment == .android {
if let ndk = env["ANDROID_NDK_ROOT"] {
var sysroot: AbsolutePath =
try AbsolutePath(validating: ndk)
.appending(components: "toolchains", "llvm", "prebuilt")
#if arch(x86_64)
#if os(Windows)
.appending(component: "windows-x86_64")
#elseif os(Linux)
.appending(component: "linux-x86_64")
#else
.appending(component: "darwin-x86_64")
#endif
#endif
.appending(component: "sysroot")
commandLine.appendFlag("-sysroot")
commandLine.appendFlag(sysroot.pathString)
}
}

if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
appendXccFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
}
Expand Down
45 changes: 37 additions & 8 deletions Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,25 @@ extension GenericUnixToolchain {
}
}

if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
for libpath in targetInfo.runtimeLibraryImportPaths {
commandLine.appendFlag(.L)
commandLine.appendPath(VirtualPath.lookup(libpath.path))
}
}

if !isEmbeddedEnabled && !parsedOptions.hasArgument(.nostartfiles) {
let swiftrtPath = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
.appending(
components: targetTriple.platformName() ?? "",
String(majorArchitectureName(for: targetTriple)),
"swiftrt.o"
)
commandLine.appendPath(swiftrtPath)
let rsrc: VirtualPath
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
Member

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
Member 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
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that it's best to push swiftlang/swift#72409 over the line to avoid this particular workaround for SDK-relative logic here.

} else {
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
}
let platform: String = targetTriple.platformName() ?? ""
let architecture: String = majorArchitectureName(for: targetTriple)
commandLine.appendPath(rsrc.appending(components: platform, architecture, "swiftrt.o"))
}

// If we are linking statically, we need to add all
Expand Down Expand Up @@ -194,7 +205,25 @@ extension GenericUnixToolchain {
commandLine.appendPath(try VirtualPath(path: opt.argument.asSingle))
}

if let path = targetInfo.sdkPath?.path {
if targetTriple.environment == .android {
if let ndk = env["ANDROID_NDK_ROOT"] {
var sysroot: AbsolutePath =
try AbsolutePath(validating: ndk)
.appending(components: "toolchains", "llvm", "prebuilt")
#if arch(x86_64)
#if os(Windows)
.appending(component: "windows-x86_64")
#elseif os(Linux)
.appending(component: "linux-x86_64")
#else
.appending(component: "darwin-x86_64")
#endif
#endif
.appending(component: "sysroot")
commandLine.appendFlag("--sysroot")
commandLine.appendPath(sysroot)
}
} else if let path = targetInfo.sdkPath?.path {
commandLine.appendFlag("--sysroot")
commandLine.appendPath(VirtualPath.lookup(path))
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftOptions/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ extension Option {
public static let swiftVersion: Option = Option("-swift-version", .separate, attributes: [.frontend, .moduleInterface], metaVar: "<vers>", helpText: "Interpret input according to a specific Swift language version number")
public static let switchCheckingInvocationThresholdEQ: Option = Option("-switch-checking-invocation-threshold=", .joined, attributes: [.helpHidden, .frontend, .noDriver])
public static let symbolGraphMinimumAccessLevel: Option = Option("-symbol-graph-minimum-access-level", .separate, attributes: [.helpHidden, .frontend, .noInteractive, .supplementaryOutput], metaVar: "<level>", helpText: "Include symbols with this access level or more when emitting a symbol graph")
public static let sysroot: Option = Option("-sysroot", .separate, attributes: [.frontend, .argumentIsPath], metaVar: "<sysroot>", helpText: "Native Platform sysroot")
public static let S: Option = Option("-S", .flag, alias: Option.emitAssembly, attributes: [.frontend, .noInteractive], group: .modes)
public static let tabWidth: Option = Option("-tab-width", .separate, attributes: [.noInteractive, .noBatch, .indent], metaVar: "<n>", helpText: "Width of tab character.", group: .codeFormatting)
public static let targetCpu: Option = Option("-target-cpu", .separate, attributes: [.frontend, .moduleInterface], helpText: "Generate code for a particular CPU variant")
Expand Down Expand Up @@ -1651,6 +1652,7 @@ extension Option {
Option.swiftVersion,
Option.switchCheckingInvocationThresholdEQ,
Option.symbolGraphMinimumAccessLevel,
Option.sysroot,
Option.S,
Option.tabWidth,
Option.targetCpu,
Expand Down