Skip to content

Pass SDK to Compiler when Cross‐Compiling #2617

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ let package = Package(
dependencies: ["swift-build", "swift-package", "swift-test", "swift-run", "Commands", "Workspace", "SPMTestSupport"]),
.testTarget(
name: "WorkspaceTests",
dependencies: ["Workspace", "SPMTestSupport"]),
dependencies: ["Workspace", "SPMTestSupport", "SPMBuildCore"]),
.testTarget(
name: "FunctionalTests",
dependencies: ["swift-build", "swift-package", "swift-test", "PackageModel", "SPMTestSupport"]),
Expand Down
2 changes: 1 addition & 1 deletion Sources/SPMBuildCore/Triple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import TSCBasic
/// @see Destination.target
/// @see https://github.com/apple/swift-llvm/blob/stable/include/llvm/ADT/Triple.h
///
public struct Triple: Encodable {
public struct Triple: Encodable, Equatable {
public let tripleString: String

public let arch: Arch
Expand Down
17 changes: 17 additions & 0 deletions Sources/Workspace/Destination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ public struct Destination: Encodable {
/// 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
13 changes: 11 additions & 2 deletions Sources/Workspace/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,20 @@ 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()
let hostTriple: Triple = Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
let triple = destination.target ?? hostTriple
let crossCompiling = triple != hostTriple
self.triple = triple

var extraSwiftCFlags = (triple.isDarwin()
? ["-sdk", destination.sdk.pathString]
: [])
+ destination.extraSwiftCFlags
if crossCompiling,
!extraSwiftCFlags.contains("-sdk") {
extraSwiftCFlags += ["-sdk", destination.sdk.pathString]
}
self.extraSwiftCFlags = extraSwiftCFlags

self.extraCCFlags = [
triple.isDarwin() ? "-isysroot" : "--sysroot", destination.sdk.pathString
Expand Down
20 changes: 20 additions & 0 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
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,25 @@ final class WorkspaceTests: XCTestCase {
}
}
}

func testCrossCompilationDefaults() 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")
)
let toolchain = try UserToolchain(destination: destination)

#if !(os(Android) && arch(x86_64)) // ← Wouldn’t be cross‐compiling.
XCTAssertEqual(toolchain.extraSwiftCFlags, [
"-sdk", sdk.pathString
])
#endif
}
}

extension PackageGraph {
Expand Down