|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-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 | +import class Basics.ObservabilityScope |
| 16 | +import struct TSCBasic.AbsolutePath |
| 17 | +import protocol TSCBasic.FileSystem |
| 18 | +import struct TSCBasic.RelativePath |
| 19 | +import struct TSCBasic.StringError |
| 20 | +import struct TSCUtility.Version |
| 21 | + |
| 22 | +/// A set of paths and flags for tools used for building Swift packages. This type unifies pre-existing assorted ways |
| 23 | +/// to specify these properties across SwiftPM codebase. |
| 24 | +public struct Toolset { |
| 25 | + public enum KnownTool: String, Hashable, CaseIterable { |
| 26 | + case swiftCompiler |
| 27 | + case cCompiler |
| 28 | + case cxxCompiler |
| 29 | + case linker |
| 30 | + case librarian |
| 31 | + case debugger |
| 32 | + case testRunner |
| 33 | + } |
| 34 | + |
| 35 | + /// Properties of a known tool in a ``Toolset``. |
| 36 | + public struct ToolProperties: Equatable { |
| 37 | + /// Absolute path to the tool on the filesystem. If absent, implies a default tool is used. |
| 38 | + public fileprivate(set) var path: AbsolutePath? |
| 39 | + |
| 40 | + /// Command-line options to be passed to the tool when it's invoked. |
| 41 | + public fileprivate(set) var extraCLIOptions: [String]? |
| 42 | + } |
| 43 | + |
| 44 | + /// A dictionary of known tools in this toolset. |
| 45 | + public fileprivate(set) var knownTools: [KnownTool: ToolProperties] |
| 46 | +} |
| 47 | + |
| 48 | +extension Toolset { |
| 49 | + /// Initialize a toolset from an encoded file on a file system. |
| 50 | + /// - Parameters: |
| 51 | + /// - path: absolute path on the `fileSystem`. |
| 52 | + /// - fileSystem: file system from which the toolset should be read. |
| 53 | + /// - observability: an instance of `ObservabilityScope` to log warnings about unknown or invalid tools. |
| 54 | + public init(from toolsetPath: AbsolutePath, at fileSystem: FileSystem, _ observability: ObservabilityScope) throws { |
| 55 | + let decoder = JSONDecoder() |
| 56 | + let decoded = try decoder.decode(path: toolsetPath, fileSystem: fileSystem, as: DecodedToolset.self) |
| 57 | + guard decoded.schemaVersion == Version(1, 0, 0) else { |
| 58 | + throw StringError( |
| 59 | + "Unsupported `schemaVersion` \(decoded.schemaVersion) in toolset configuration at \(toolsetPath)" |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + var knownTools = [KnownTool: ToolProperties]() |
| 64 | + var hasEmptyToolConfiguration = false |
| 65 | + for (tool, properties) in decoded.tools { |
| 66 | + guard let knownTool = KnownTool(rawValue: tool) else { |
| 67 | + observability.emit(warning: "Unknown tool `\(tool)` in toolset configuration at `\(toolsetPath)`") |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + let toolPath: AbsolutePath? |
| 72 | + if let path = properties.path { |
| 73 | + if let absolutePath = try? AbsolutePath(validating: path) { |
| 74 | + toolPath = absolutePath |
| 75 | + } else { |
| 76 | + let rootPath = decoded.rootPath ?? toolsetPath.parentDirectory |
| 77 | + toolPath = rootPath.appending(RelativePath(path)) |
| 78 | + } |
| 79 | + } else { |
| 80 | + toolPath = nil |
| 81 | + } |
| 82 | + |
| 83 | + guard toolPath != nil || !(properties.extraCLIOptions?.isEmpty ?? true) else { |
| 84 | + // don't keep track of a tool with no path and CLI options specified. |
| 85 | + observability.emit(error: |
| 86 | + """ |
| 87 | + Tool `\(knownTool.rawValue) in toolset configuration at `\(toolsetPath)` has neither `path` nor \ |
| 88 | + `extraCLIOptions` properties specified with valid values, skipping it. |
| 89 | + """) |
| 90 | + hasEmptyToolConfiguration = true |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + knownTools[knownTool] = ToolProperties( |
| 95 | + path: toolPath, |
| 96 | + extraCLIOptions: properties.extraCLIOptions |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + guard !hasEmptyToolConfiguration else { |
| 101 | + throw StringError("Toolset configuration at `\(toolsetPath)` has at least one tool with no properties.") |
| 102 | + } |
| 103 | + |
| 104 | + self.init(knownTools: knownTools) |
| 105 | + } |
| 106 | + |
| 107 | + /// Merges toolsets together into a single configuration. Tools passed in a new toolset will shadow tools with |
| 108 | + /// same names from previous toolsets. When no `path` is specified for a new tool, its `extraCLIOptions` are |
| 109 | + /// appended to `extraCLIOptions` of a tool from a previous toolset, which allows augmenting existing tools instead |
| 110 | + /// of replacing them. |
| 111 | + /// - Parameter newToolset: new toolset to merge into the existing `self` toolset. |
| 112 | + public mutating func merge(with newToolset: Toolset) { |
| 113 | + for (newTool, newProperties) in newToolset.knownTools { |
| 114 | + if newProperties.path != nil { |
| 115 | + // if `newTool` has `path` specified, it overrides the existing tool completely. |
| 116 | + knownTools[newTool] = newProperties |
| 117 | + } else if let newExtraCLIOptions = newProperties.extraCLIOptions, !newExtraCLIOptions.isEmpty { |
| 118 | + // if `newTool` has no `path` specified, `newExtraCLIOptions` are appended to the existing tool. |
| 119 | + if var existingTool = knownTools[newTool] { |
| 120 | + // either update the existing tool and store it back... |
| 121 | + if existingTool.extraCLIOptions == nil { |
| 122 | + existingTool.extraCLIOptions = newExtraCLIOptions |
| 123 | + } else { |
| 124 | + existingTool.extraCLIOptions?.append(contentsOf: newExtraCLIOptions) |
| 125 | + } |
| 126 | + knownTools[newTool] = existingTool |
| 127 | + } else { |
| 128 | + // ...or store a new tool if no existing tool is found. |
| 129 | + knownTools[newTool] = newProperties |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// A raw decoding of toolset configuration stored on disk. |
| 137 | +private struct DecodedToolset { |
| 138 | + /// Version of a toolset schema used for decoding a toolset file. |
| 139 | + let schemaVersion: Version |
| 140 | + |
| 141 | + /// Root path of the toolset, if present. When filling in ``Toolset.ToolProperties/path``, if a raw path string in |
| 142 | + /// ``DecodedToolset`` is inferred to be relative, it's resolved as absolute path relatively to `rootPath`. |
| 143 | + let rootPath: AbsolutePath? |
| 144 | + |
| 145 | + /// Dictionary of raw tools that haven't been validated yet to match ``Toolset.KnownTool``. |
| 146 | + var tools: [String: ToolProperties] |
| 147 | + |
| 148 | + /// Properties of a tool in a ``DecodedToolset``. |
| 149 | + public struct ToolProperties { |
| 150 | + /// Either a relative or an absolute path to the tool on the filesystem. |
| 151 | + let path: String? |
| 152 | + |
| 153 | + /// Command-line options to be passed to the tool when it's invoked. |
| 154 | + let extraCLIOptions: [String]? |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +extension DecodedToolset.ToolProperties: Decodable {} |
| 159 | + |
| 160 | +extension DecodedToolset: Decodable { |
| 161 | + /// Custom decoding keys that allow decoding tools with arbitrary names, |
| 162 | + enum CodingKeys: Equatable { |
| 163 | + case schemaVersion |
| 164 | + case rootPath |
| 165 | + case tool(String) |
| 166 | + } |
| 167 | + |
| 168 | + public init(from decoder: Decoder) throws { |
| 169 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 170 | + |
| 171 | + self.schemaVersion = try Version( |
| 172 | + versionString: container.decode(String.self, forKey: .schemaVersion), |
| 173 | + usesLenientParsing: true |
| 174 | + ) |
| 175 | + self.rootPath = try container.decodeIfPresent(AbsolutePath.self, forKey: .rootPath) |
| 176 | + |
| 177 | + self.tools = [String: DecodedToolset.ToolProperties]() |
| 178 | + for key in container.allKeys { |
| 179 | + switch key { |
| 180 | + case .rootPath, .schemaVersion: |
| 181 | + // These keys were already decoded before entering this loop, skipping. |
| 182 | + continue |
| 183 | + case .tool(let tool): |
| 184 | + self.tools[tool] = try container.decode(DecodedToolset.ToolProperties.self, forKey: key) |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +/// Custom `CodingKey` implementation for `DecodedToolset`, which allows us to resiliently decode unknown tools and emit |
| 191 | +/// multiple diagnostic messages about them separately from the decoding process, instead of emitting a single error |
| 192 | +/// that will disrupt whole decoding at once. |
| 193 | +extension DecodedToolset.CodingKeys: CodingKey { |
| 194 | + var stringValue: String { |
| 195 | + switch self { |
| 196 | + case .schemaVersion: |
| 197 | + return "schemaVersion" |
| 198 | + case .rootPath: |
| 199 | + return "rootPath" |
| 200 | + case .tool(let toolName): |
| 201 | + return toolName |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + init?(stringValue: String) { |
| 206 | + switch stringValue { |
| 207 | + case "schemaVersion": |
| 208 | + self = .schemaVersion |
| 209 | + case "rootPath": |
| 210 | + self = .rootPath |
| 211 | + default: |
| 212 | + self = .tool(stringValue) |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + var intValue: Int? { nil } |
| 217 | + |
| 218 | + init?(intValue: Int) { |
| 219 | + nil |
| 220 | + } |
| 221 | +} |
0 commit comments