|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | + |
| 15 | +// FIXME: should not import this module |
| 16 | +import Build |
| 17 | +// FIXME: should be internal imports |
| 18 | +import PackageGraph |
| 19 | + |
| 20 | +public protocol BuildTarget { |
| 21 | + // FIXME: should not use `ResolvedTarget` in the public interface |
| 22 | + var target: ResolvedTarget { get } |
| 23 | + var sources: [URL] { get } |
| 24 | + |
| 25 | + func compileArguments() throws -> [String] |
| 26 | + } |
| 27 | + |
| 28 | +extension ClangTargetBuildDescription: BuildTarget { |
| 29 | + public var sources: [URL] { |
| 30 | + return (try? compilePaths().map { URL(fileURLWithPath: $0.source.pathString) }) ?? [] |
| 31 | + } |
| 32 | + |
| 33 | + public func compileArguments() throws -> [String] { |
| 34 | + return try self.basicArguments() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +private struct WrappedSwiftTargetBuildDescription: BuildTarget { |
| 39 | + private let description: SwiftTargetBuildDescription |
| 40 | + |
| 41 | + init(_ description: SwiftTargetBuildDescription) { |
| 42 | + self.description = description |
| 43 | + } |
| 44 | + |
| 45 | + var target: ResolvedTarget { |
| 46 | + return description.target |
| 47 | + } |
| 48 | + |
| 49 | + var sources: [URL] { |
| 50 | + return description.sources.map { URL(fileURLWithPath: $0.pathString) } |
| 51 | + } |
| 52 | + |
| 53 | + func compileArguments() throws -> [String] { |
| 54 | + return try description.compileArguments() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +public struct BuildDescription { |
| 59 | + private let buildPlan: BuildPlan |
| 60 | + |
| 61 | + // FIXME: should not use `BuildPlan` in the public interface |
| 62 | + public init(buildPlan: BuildPlan) { |
| 63 | + self.buildPlan = buildPlan |
| 64 | + } |
| 65 | + |
| 66 | + // FIXME: should not use `ResolvedTarget` in the public interface |
| 67 | + public func getBuildTarget(for target: ResolvedTarget) -> BuildTarget? { |
| 68 | + if let description = buildPlan.targetMap[target] { |
| 69 | + switch description { |
| 70 | + case .clang(let description): |
| 71 | + return description |
| 72 | + case .swift(let description): |
| 73 | + return WrappedSwiftTargetBuildDescription(description) |
| 74 | + } |
| 75 | + } else { |
| 76 | + if target.type == .plugin, let package = self.buildPlan.graph.package(for: target) { |
| 77 | + return PluginTargetBuildDescription(target: target, toolsVersion: package.manifest.toolsVersion) |
| 78 | + } |
| 79 | + return nil |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments