Skip to content

[6.1] Support background indexing when cross-compiling #1939

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

Merged
merged 2 commits into from
Jan 21, 2025
Merged
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
9 changes: 9 additions & 0 deletions Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,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 @@ -473,6 +473,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 @@ -1012,6 +1012,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