Skip to content

Commit

Permalink
[5.9] Handle macOS CLT --show-sdk-platform-path issues (#6991)
Browse files Browse the repository at this point in the history
We have been seeing this issue for a while when using SwiftPM from the
CommandLineTools package:

```
❯ /usr/bin/xcrun --sdk macosx --show-sdk-platform-path
xcrun: error: unable to lookup item 'PlatformPath' from command line tools installation
xcrun: error: unable to lookup item 'PlatformPath' in SDK '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk'
```

This changes SwiftPM to handle this gracefully during builds (a warning
gets emitted) and to fail properly when running `swift test`.

rdar://107479428

(cherry picked from commit 3ab568e)
  • Loading branch information
neonichu authored Oct 12, 2023
1 parent 9c432d2 commit 732d540
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Sources/Commands/SwiftTestTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public struct SwiftTestTool: SwiftCommand {

// validate XCTest available on darwin based systems
let toolchain = try swiftTool.getDestinationToolchain()
if toolchain.triple.isDarwin() && toolchain.xctestPath == nil {
let isHostTestingAvailable = try swiftTool.getHostToolchain().destination.supportsTesting
if toolchain.triple.isDarwin() && toolchain.xctestPath == nil || !isHostTestingAvailable {
throw TestError.xctestNotAvailable
}
} catch {
Expand Down
2 changes: 1 addition & 1 deletion Sources/CoreCommands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ public final class SwiftTool {
private lazy var _hostToolchain: Result<UserToolchain, Swift.Error> = {
return Result(catching: {
try UserToolchain(destination: Destination.hostDestination(
originalWorkingDirectory: self.originalWorkingDirectory))
originalWorkingDirectory: self.originalWorkingDirectory, observabilityScope: self.observabilityScope))
})
}()

Expand Down
32 changes: 24 additions & 8 deletions Sources/PackageModel/Destination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public struct Destination: Equatable {
/// The architectures to build for. We build for host architecture if this is empty.
public var architectures: [String]? = nil

/// Whether or not the receiver supports testing.
public let supportsTesting: Bool

/// Root directory path of the SDK used to compile for the destination.
@available(*, deprecated, message: "use `sdkRootDir` instead")
public var sdk: AbsolutePath? {
Expand Down Expand Up @@ -402,12 +405,14 @@ public struct Destination: Equatable {
hostTriple: Triple? = nil,
targetTriple: Triple? = nil,
toolset: Toolset,
pathsConfiguration: PathsConfiguration
pathsConfiguration: PathsConfiguration,
supportsTesting: Bool = true
) {
self.hostTriple = hostTriple
self.targetTriple = targetTriple
self.toolset = toolset
self.pathsConfiguration = pathsConfiguration
self.supportsTesting = supportsTesting
}

/// Returns the bin directory for the host.
Expand All @@ -428,7 +433,8 @@ public struct Destination: Equatable {
public static func hostDestination(
_ binDir: AbsolutePath? = nil,
originalWorkingDirectory: AbsolutePath? = nil,
environment: [String: String] = ProcessEnv.vars
environment: [String: String] = ProcessEnv.vars,
observabilityScope: ObservabilityScope? = nil
) throws -> Destination {
let originalWorkingDirectory = originalWorkingDirectory ?? localFileSystem.currentWorkingDirectory
// Select the correct binDir.
Expand Down Expand Up @@ -463,14 +469,23 @@ public struct Destination: Equatable {
#endif

// Compute common arguments for clang and swift.
let supportsTesting: Bool
var extraCCFlags: [String] = []
var extraSwiftCFlags: [String] = []
#if os(macOS)
let sdkPaths = try Destination.sdkPlatformFrameworkPaths(environment: environment)
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
do {
let sdkPaths = try Destination.sdkPlatformFrameworkPaths(environment: environment)
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
supportsTesting = true
} catch {
supportsTesting = false
observabilityScope?.emit(warning: "could not determine XCTest paths: \(error)")
}
#else
supportsTesting = true
#endif

#if !os(Windows)
Expand All @@ -485,7 +500,8 @@ public struct Destination: Equatable {
],
rootPaths: [binDir]
),
pathsConfiguration: .init(sdkRootPath: sdkPath)
pathsConfiguration: .init(sdkRootPath: sdkPath),
supportsTesting: supportsTesting
)
}

Expand Down

0 comments on commit 732d540

Please sign in to comment.