Skip to content
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

Add a swift-corelibs-foundation specific workaround #7425

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import Basics
import PackageGraph
import PackageLoading
import PackageModel
import struct PackageGraph.ModulesGraph
Expand All @@ -23,6 +24,9 @@ import enum TSCBasic.ProcessEnv

/// Target description for a Clang target i.e. C language family target.
package final class ClangTargetBuildDescription {
/// The package this target belongs to.
package let package: ResolvedPackage

/// The target described by this target.
package let target: ResolvedTarget

Expand Down Expand Up @@ -109,6 +113,7 @@ package final class ClangTargetBuildDescription {

/// Create a new target description with target and build parameters.
init(
package: ResolvedPackage,
target: ResolvedTarget,
toolsVersion: ToolsVersion,
additionalFileRules: [FileRuleDescription] = [],
Expand All @@ -122,6 +127,7 @@ package final class ClangTargetBuildDescription {
throw InternalError("underlying target type mismatch \(target)")
}

self.package = package
self.clangTarget = clangTarget
self.fileSystem = fileSystem
self.target = target
Expand Down Expand Up @@ -305,6 +311,18 @@ package final class ClangTargetBuildDescription {
args += ["-I", includeSearchPath.pathString]
}

// FIXME: Remove this once it becomes possible to express this dependency in a package manifest.
//
// On Linux/Android swift-corelibs-foundation depends on dispatch library which is
// currently shipped with the Swift toolchain.
if (triple.isLinux() || triple.isAndroid()) && self.package.id == .plain("swift-corelibs-foundation") {
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
let swiftCompilerPath = self.buildParameters.toolchain.swiftCompilerPath
let toolchainResourcesPath = swiftCompilerPath.parentDirectory
.parentDirectory
.appending(components: ["lib", "swift"])
args += ["-I", toolchainResourcesPath.pathString]
}

return args
}

Expand Down
5 changes: 5 additions & 0 deletions Sources/Build/BuildPlan/BuildPlan.swift
Expand Up @@ -394,8 +394,13 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
)
)
case is ClangTarget:
guard let package = graph.package(for: target) else {
throw InternalError("package not found for \(target)")
}

targetMap[target.id] = try .clang(
ClangTargetBuildDescription(
package: package,
target: target,
toolsVersion: toolsVersion,
additionalFileRules: additionalFileRules,
Expand Down
6 changes: 4 additions & 2 deletions Sources/SPMTestSupport/MockBuildTestHelper.swift
Expand Up @@ -30,7 +30,7 @@ package struct MockToolchain: PackageModel.Toolchain {
package let swiftCompilerPath = AbsolutePath("/fake/path/to/swiftc")
package let includeSearchPaths = [AbsolutePath]()
package let librarySearchPaths = [AbsolutePath]()
package let swiftResourcesPath: AbsolutePath? = nil
package let swiftResourcesPath: AbsolutePath?
package let swiftStaticResourcesPath: AbsolutePath? = nil
package let isSwiftDevelopmentToolchain = false
package let sdkRootPath: AbsolutePath? = nil
Expand All @@ -51,7 +51,9 @@ package struct MockToolchain: PackageModel.Toolchain {
#endif
}

package init() {}
package init(swiftResourcesPath: AbsolutePath? = nil) {
self.swiftResourcesPath = swiftResourcesPath
}
}

extension Basics.Triple {
Expand Down
56 changes: 52 additions & 4 deletions Tests/BuildTests/ClangTargetBuildDescriptionTests.swift
Expand Up @@ -14,15 +14,37 @@ import Basics
@testable import Build
import PackageGraph
import PackageModel
import SPMBuildCore
import SPMTestSupport
import XCTest

final class ClangTargetBuildDescriptionTests: XCTestCase {
func testClangIndexStorePath() throws {
let targetDescription = try makeTargetBuildDescription()
let targetDescription = try makeTargetBuildDescription("test")
XCTAssertTrue(try targetDescription.basicArguments().contains("-index-store-path"))
}

func testSwiftCorelibsFoundationIncludeWorkaround() throws {
let toolchain = MockToolchain(swiftResourcesPath: AbsolutePath("/fake/path/lib/swift"))

let macosParameters = mockBuildParameters(toolchain: toolchain, targetTriple: .macOS)
let linuxParameters = mockBuildParameters(toolchain: toolchain, targetTriple: .arm64Linux)
let androidParameters = mockBuildParameters(toolchain: toolchain, targetTriple: .arm64Android)

let macDescription = try makeTargetBuildDescription("swift-corelibs-foundation",
buildParameters: macosParameters)
XCTAssertFalse(try macDescription.basicArguments().contains("\(macosParameters.toolchain.swiftResourcesPath!)"))

let linuxDescription = try makeTargetBuildDescription("swift-corelibs-foundation",
buildParameters: linuxParameters)
print(try linuxDescription.basicArguments())
XCTAssertTrue(try linuxDescription.basicArguments().contains("\(linuxParameters.toolchain.swiftResourcesPath!)"))

let androidDescription = try makeTargetBuildDescription("swift-corelibs-foundation",
buildParameters: androidParameters)
XCTAssertTrue(try androidDescription.basicArguments().contains("\(androidParameters.toolchain.swiftResourcesPath!)"))
}

private func makeClangTarget() throws -> ClangTarget {
try ClangTarget(
name: "dummy",
Expand All @@ -47,12 +69,38 @@ final class ClangTargetBuildDescriptionTests: XCTestCase {
)
}

private func makeTargetBuildDescription() throws -> ClangTargetBuildDescription {
private func makeTargetBuildDescription(_ packageName: String,
buildParameters: BuildParameters? = nil) throws -> ClangTargetBuildDescription {
let observability = ObservabilitySystem.makeForTesting(verbose: false)

let manifest = Manifest.createRootManifest(
displayName: "dummy",
toolsVersion: .v5,
targets: [try TargetDescription(name: "dummy")]
)

let target = try makeResolvedTarget()

let package = Package(identity: .plain(packageName),
manifest: manifest,
path: .root,
targets: [target.underlying],
products: [],
targetSearchPath: .root,
testTargetSearchPath: .root)

return try ClangTargetBuildDescription(
target: try makeResolvedTarget(),
package: .init(underlying: package,
defaultLocalization: nil,
supportedPlatforms: [],
dependencies: [],
targets: [target],
products: [],
registryMetadata: nil,
platformVersionProvider: .init(implementation: .minimumDeploymentTargetDefault)),
target: target,
xedin marked this conversation as resolved.
Show resolved Hide resolved
toolsVersion: .current,
buildParameters: mockBuildParameters(
buildParameters: buildParameters ?? mockBuildParameters(
toolchain: try UserToolchain.default,
indexStoreMode: .on
),
Expand Down