Skip to content

Commit

Permalink
Merge pull request #1923 from kabiroberai/kabir/cross-index
Browse files Browse the repository at this point in the history
Support background indexing when cross-compiling
  • Loading branch information
ahoppen authored Jan 21, 2025
2 parents ca55e04 + 6b533b3 commit e39321f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,15 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
if let configuration = options.swiftPMOrDefault.configuration {
arguments += ["-c", configuration.rawValue]
}
if let triple = options.swiftPMOrDefault.triple {
arguments += ["--triple", triple]
}
if let swiftSDKsDirectory = options.swiftPMOrDefault.swiftSDKsDirectory {
arguments += ["--swift-sdks-path", swiftSDKsDirectory]
}
if let swiftSDK = options.swiftPMOrDefault.swiftSDK {
arguments += ["--swift-sdk", swiftSDK]
}
arguments += options.swiftPMOrDefault.cCompilerFlags?.flatMap { ["-Xcc", $0] } ?? []
arguments += options.swiftPMOrDefault.cxxCompilerFlags?.flatMap { ["-Xcxx", $0] } ?? []
arguments += options.swiftPMOrDefault.swiftCompilerFlags?.flatMap { ["-Xswiftc", $0] } ?? []
Expand Down
28 changes: 28 additions & 0 deletions Sources/SKTestSupport/SkipUnless.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,34 @@ package actor SkipUnless {
}
}

package static func canSwiftPMCompileForIOS(
file: StaticString = #filePath,
line: UInt = #line
) async throws {
return try await shared.skipUnlessSupported(allowSkippingInCI: true, file: file, line: line) {
#if os(macOS)
let project = try await SwiftPMTestProject(files: [
"MyFile.swift": """
public func foo() {}
"""
])
do {
try await SwiftPMTestProject.build(
at: project.scratchDirectory,
extraArguments: [
"--swift-sdk", "arm64-apple-ios",
]
)
return .featureSupported
} catch {
return .featureUnsupported(skipMessage: "Cannot build for iOS: \(error)")
}
#else
return .featureUnsupported(skipMessage: "Cannot build for iOS outside macOS by default")
#endif
}
}

package static func canCompileForWasm(
file: StaticString = #filePath,
line: UInt = #line
Expand Down
47 changes: 47 additions & 0 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,53 @@ final class BackgroundIndexingTests: XCTestCase {
)
}

func testUseSwiftSDKFlagsDuringPreparation() async throws {
try await SkipUnless.canSwiftPMCompileForIOS()

var options = SourceKitLSPOptions.testDefault()
options.swiftPMOrDefault.swiftSDK = "arm64-apple-ios"
let project = try await SwiftPMTestProject(
files: [
"Lib/Lib.swift": """
#if os(iOS)
public func foo() -> Int { 1 }
#endif
""",
"Client/Client.swift": """
import Lib
func test() -> String {
return foo()
}
""",
],
manifest: """
let package = Package(
name: "MyLibrary",
targets: [
.target(name: "Lib"),
.target(name: "Client", dependencies: ["Lib"]),
]
)
""",
options: options,
enableBackgroundIndexing: true
)

// Check that we get an error about the return type of `foo` (`Int`) not being convertible to the return type of
// `test` (`String`), which indicates that `Lib` had `foo` and was thus compiled for iOS
let (uri, _) = try project.openDocument("Client.swift")
let diagnostics = try await project.testClient.send(
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
)
XCTAssert(
(diagnostics.fullReport?.items ?? []).contains(where: {
$0.message == "Cannot convert return expression of type 'Int' to return type 'String'"
}),
"Did not get expected diagnostic: \(diagnostics)"
)
}

func testLibraryUsedByExecutableTargetAndPackagePlugin() async throws {
try await SkipUnless.swiftPMStoresModulesForTargetAndHostInSeparateFolders()
let project = try await SwiftPMTestProject(
Expand Down

0 comments on commit e39321f

Please sign in to comment.