Skip to content

Commit 2e2ef46

Browse files
authored
Merge pull request #1939 from kabiroberai/kabir/cross-index-6.1
[6.1] Support background indexing when cross-compiling
2 parents 32a2ff1 + af08a48 commit 2e2ef46

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,15 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
684684
if let configuration = options.swiftPMOrDefault.configuration {
685685
arguments += ["-c", configuration.rawValue]
686686
}
687+
if let triple = options.swiftPMOrDefault.triple {
688+
arguments += ["--triple", triple]
689+
}
690+
if let swiftSDKsDirectory = options.swiftPMOrDefault.swiftSDKsDirectory {
691+
arguments += ["--swift-sdks-path", swiftSDKsDirectory]
692+
}
693+
if let swiftSDK = options.swiftPMOrDefault.swiftSDK {
694+
arguments += ["--swift-sdk", swiftSDK]
695+
}
687696
arguments += options.swiftPMOrDefault.cCompilerFlags?.flatMap { ["-Xcc", $0] } ?? []
688697
arguments += options.swiftPMOrDefault.cxxCompilerFlags?.flatMap { ["-Xcxx", $0] } ?? []
689698
arguments += options.swiftPMOrDefault.swiftCompilerFlags?.flatMap { ["-Xswiftc", $0] } ?? []

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,34 @@ package actor SkipUnless {
473473
}
474474
}
475475

476+
package static func canSwiftPMCompileForIOS(
477+
file: StaticString = #filePath,
478+
line: UInt = #line
479+
) async throws {
480+
return try await shared.skipUnlessSupported(allowSkippingInCI: true, file: file, line: line) {
481+
#if os(macOS)
482+
let project = try await SwiftPMTestProject(files: [
483+
"MyFile.swift": """
484+
public func foo() {}
485+
"""
486+
])
487+
do {
488+
try await SwiftPMTestProject.build(
489+
at: project.scratchDirectory,
490+
extraArguments: [
491+
"--swift-sdk", "arm64-apple-ios",
492+
]
493+
)
494+
return .featureSupported
495+
} catch {
496+
return .featureUnsupported(skipMessage: "Cannot build for iOS: \(error)")
497+
}
498+
#else
499+
return .featureUnsupported(skipMessage: "Cannot build for iOS outside macOS by default")
500+
#endif
501+
}
502+
}
503+
476504
package static func canCompileForWasm(
477505
file: StaticString = #filePath,
478506
line: UInt = #line

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,53 @@ final class BackgroundIndexingTests: XCTestCase {
10121012
)
10131013
}
10141014

1015+
func testUseSwiftSDKFlagsDuringPreparation() async throws {
1016+
try await SkipUnless.canSwiftPMCompileForIOS()
1017+
1018+
var options = SourceKitLSPOptions.testDefault()
1019+
options.swiftPMOrDefault.swiftSDK = "arm64-apple-ios"
1020+
let project = try await SwiftPMTestProject(
1021+
files: [
1022+
"Lib/Lib.swift": """
1023+
#if os(iOS)
1024+
public func foo() -> Int { 1 }
1025+
#endif
1026+
""",
1027+
"Client/Client.swift": """
1028+
import Lib
1029+
1030+
func test() -> String {
1031+
return foo()
1032+
}
1033+
""",
1034+
],
1035+
manifest: """
1036+
let package = Package(
1037+
name: "MyLibrary",
1038+
targets: [
1039+
.target(name: "Lib"),
1040+
.target(name: "Client", dependencies: ["Lib"]),
1041+
]
1042+
)
1043+
""",
1044+
options: options,
1045+
enableBackgroundIndexing: true
1046+
)
1047+
1048+
// Check that we get an error about the return type of `foo` (`Int`) not being convertible to the return type of
1049+
// `test` (`String`), which indicates that `Lib` had `foo` and was thus compiled for iOS
1050+
let (uri, _) = try project.openDocument("Client.swift")
1051+
let diagnostics = try await project.testClient.send(
1052+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(uri))
1053+
)
1054+
XCTAssert(
1055+
(diagnostics.fullReport?.items ?? []).contains(where: {
1056+
$0.message == "Cannot convert return expression of type 'Int' to return type 'String'"
1057+
}),
1058+
"Did not get expected diagnostic: \(diagnostics)"
1059+
)
1060+
}
1061+
10151062
func testLibraryUsedByExecutableTargetAndPackagePlugin() async throws {
10161063
try await SkipUnless.swiftPMStoresModulesForTargetAndHostInSeparateFolders()
10171064
let project = try await SwiftPMTestProject(

0 commit comments

Comments
 (0)