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

-sdk for Android (Take 3) #2633

Merged
merged 4 commits into from Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/Workspace/Destination.swift
Expand Up @@ -52,6 +52,23 @@ public struct Destination: Encodable, Equatable {
/// Additional flags to be passed when compiling with C++.
public let extraCPPFlags: [String]

/// Creates a compilation destination with the specified properties.
public init(
target: Triple? = nil,
sdk: AbsolutePath,
binDir: AbsolutePath,
extraCCFlags: [String] = [],
extraSwiftCFlags: [String] = [],
extraCPPFlags: [String] = []
) {
self.target = target
self.sdk = sdk
self.binDir = binDir
self.extraCCFlags = extraCCFlags
self.extraSwiftCFlags = extraSwiftCFlags
self.extraCPPFlags = extraCPPFlags
}

/// Returns the bin directory for the host.
///
/// - Parameter originalWorkingDirectory: The working directory when the program was launched.
Expand Down
15 changes: 10 additions & 5 deletions Sources/Workspace/UserToolchain.swift
Expand Up @@ -222,6 +222,13 @@ public final class UserToolchain: Toolchain {
throw InvalidToolchainDiagnostic("could not find swift-api-digester")
}

public static func deriveSwiftCFlags(triple: Triple, destination: Destination) -> [String] {
return (triple.isDarwin() || triple.isAndroid()
? ["-sdk", destination.sdk.pathString]
: [])
+ destination.extraSwiftCFlags
}

public init(destination: Destination, environment: [String: String] = ProcessEnv.vars) throws {
self.destination = destination
self.processEnvironment = environment
Expand All @@ -248,11 +255,9 @@ public final class UserToolchain: Toolchain {
#endif

// Use the triple from destination or compute the host triple using swiftc.
self.triple = destination.target ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
self.extraSwiftCFlags = (triple.isDarwin()
? ["-sdk", destination.sdk.pathString]
: [])
+ destination.extraSwiftCFlags
let triple = destination.target ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
self.triple = triple
self.extraSwiftCFlags = UserToolchain.deriveSwiftCFlags(triple: triple, destination: destination)

self.extraCCFlags = [
triple.isDarwin() ? "-isysroot" : "--sysroot", destination.sdk.pathString
Expand Down
18 changes: 18 additions & 0 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Expand Up @@ -16,6 +16,7 @@ import PackageModel
import PackageGraph
import SourceControl
import TSCUtility
import SPMBuildCore
import Workspace

import SPMTestSupport
Expand Down Expand Up @@ -4479,6 +4480,23 @@ final class WorkspaceTests: XCTestCase {
}
}
}

func testAndroidCompilerFlags() throws {
let target = try Triple("x86_64-unknown-linux-android")
let sdk = AbsolutePath("/some/path/to/an/SDK.sdk")
let toolchainPath = AbsolutePath("/some/path/to/a/toolchain.xctoolchain")

let destination = Destination(
target: target,
sdk: sdk,
binDir: toolchainPath.appending(components: "usr", "bin")
)

XCTAssertEqual(UserToolchain.deriveSwiftCFlags(triple: target, destination: destination), [
// Needed when cross‐compiling for Android. 2020‐03‐01
"-sdk", sdk.pathString
])
}
}

extension PackageGraph {
Expand Down