From d6ae6756632ce424f1860c14f3d9501cf54c9d31 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Wed, 15 Jan 2025 21:40:26 -0500 Subject: [PATCH 1/2] Forward cross-compilation args when indexing --- Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift b/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift index 50a8e963a..202bb6f38 100644 --- a/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift +++ b/Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift @@ -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] } ?? [] From af08a4868ee58b97ad15b4f441a11fda8726f0d1 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Thu, 16 Jan 2025 12:27:15 -0500 Subject: [PATCH 2/2] Add cross-compilation regression test Feedback More cleanup Allow skipping canSwiftPMCompileForIOS on CI Format --- Sources/SKTestSupport/SkipUnless.swift | 28 +++++++++++ .../BackgroundIndexingTests.swift | 47 +++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/Sources/SKTestSupport/SkipUnless.swift b/Sources/SKTestSupport/SkipUnless.swift index 69a9deb5b..31cd7639b 100644 --- a/Sources/SKTestSupport/SkipUnless.swift +++ b/Sources/SKTestSupport/SkipUnless.swift @@ -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 diff --git a/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift b/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift index 83a9261e4..3960a1a23 100644 --- a/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift +++ b/Tests/SourceKitLSPTests/BackgroundIndexingTests.swift @@ -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(