Skip to content

Commit e39321f

Browse files
authored
Merge pull request #1923 from kabiroberai/kabir/cross-index
Support background indexing when cross-compiling
2 parents ca55e04 + 6b533b3 commit e39321f

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
@@ -616,6 +616,15 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
616616
if let configuration = options.swiftPMOrDefault.configuration {
617617
arguments += ["-c", configuration.rawValue]
618618
}
619+
if let triple = options.swiftPMOrDefault.triple {
620+
arguments += ["--triple", triple]
621+
}
622+
if let swiftSDKsDirectory = options.swiftPMOrDefault.swiftSDKsDirectory {
623+
arguments += ["--swift-sdks-path", swiftSDKsDirectory]
624+
}
625+
if let swiftSDK = options.swiftPMOrDefault.swiftSDK {
626+
arguments += ["--swift-sdk", swiftSDK]
627+
}
619628
arguments += options.swiftPMOrDefault.cCompilerFlags?.flatMap { ["-Xcc", $0] } ?? []
620629
arguments += options.swiftPMOrDefault.cxxCompilerFlags?.flatMap { ["-Xcxx", $0] } ?? []
621630
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
@@ -478,6 +478,34 @@ package actor SkipUnless {
478478
}
479479
}
480480

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

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,53 @@ final class BackgroundIndexingTests: XCTestCase {
10181018
)
10191019
}
10201020

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

0 commit comments

Comments
 (0)