|
| 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 ArgumentParser |
| 14 | +import Basics |
| 15 | +import CoreCommands |
| 16 | +import Dispatch |
| 17 | +import PackageModel |
| 18 | + |
| 19 | +import var TSCBasic.stdoutStream |
| 20 | + |
| 21 | +struct ConfigureSwiftSDK: AsyncParsableCommand { |
| 22 | + static let configuration = CommandConfiguration( |
| 23 | + commandName: "configure", |
| 24 | + abstract: """ |
| 25 | + Manages configuration options for installed Swift SDKs. |
| 26 | + """ |
| 27 | + ) |
| 28 | + |
| 29 | + @OptionGroup(visibility: .hidden) |
| 30 | + var locations: LocationOptions |
| 31 | + |
| 32 | + @Option(help: "A path to a directory containing the SDK root.") |
| 33 | + var sdkRootPath: String? = nil |
| 34 | + |
| 35 | + @Option(help: "A path to a directory containing Swift resources for dynamic linking.") |
| 36 | + var swiftResourcesPath: String? = nil |
| 37 | + |
| 38 | + @Option(help: "A path to a directory containing Swift resources for static linking.") |
| 39 | + var swiftStaticResourcesPath: String? = nil |
| 40 | + |
| 41 | + @Option( |
| 42 | + parsing: .singleValue, |
| 43 | + help: """ |
| 44 | + A path to a directory containing headers. Multiple paths can be specified by providing this option multiple \ |
| 45 | + times to the command. |
| 46 | + """ |
| 47 | + ) |
| 48 | + var includeSearchPath: [String] = [] |
| 49 | + |
| 50 | + @Option( |
| 51 | + parsing: .singleValue, |
| 52 | + help: """ |
| 53 | + "A path to a directory containing libraries. Multiple paths can be specified by providing this option multiple \ |
| 54 | + times to the command. |
| 55 | + """ |
| 56 | + ) |
| 57 | + var librarySearchPath: [String] = [] |
| 58 | + |
| 59 | + @Option( |
| 60 | + parsing: .singleValue, |
| 61 | + help: """ |
| 62 | + "A path to a toolset file. Multiple paths can be specified by providing this option multiple times to the command. |
| 63 | + """ |
| 64 | + ) |
| 65 | + var toolsetPath: [String] = [] |
| 66 | + |
| 67 | + @Flag( |
| 68 | + name: .customLong("reset"), |
| 69 | + help: """ |
| 70 | + Resets configuration properties currently applied to a given Swift SDK and target triple. If no specific \ |
| 71 | + property is specified, all of them are reset for the Swift SDK. |
| 72 | + """ |
| 73 | + ) |
| 74 | + var shouldReset: Bool = false |
| 75 | + |
| 76 | + @Flag( |
| 77 | + name: .customLong("show-configuration"), |
| 78 | + help: """ |
| 79 | + Prints all configuration properties currently applied to a given Swift SDK and target triple. |
| 80 | + """ |
| 81 | + ) |
| 82 | + var shouldShowConfiguration: Bool = false |
| 83 | + |
| 84 | + @Argument( |
| 85 | + help: """ |
| 86 | + An identifier of an already installed Swift SDK. Use the `list` subcommand to see all available \ |
| 87 | + identifiers. |
| 88 | + """ |
| 89 | + ) |
| 90 | + var sdkID: String |
| 91 | + |
| 92 | + @Argument(help: "The target triple of the Swift SDK to configure.") |
| 93 | + var targetTriple: String |
| 94 | + |
| 95 | + /// The file system used by default by this command. |
| 96 | + private var fileSystem: FileSystem { localFileSystem } |
| 97 | + |
| 98 | + /// Parses Swift SDKs directory option if provided or uses the default path for Swift SDKs |
| 99 | + /// on the file system. A new directory at this path is created if one doesn't exist already. |
| 100 | + /// - Returns: existing or a newly created directory at the computed location. |
| 101 | + private func getOrCreateSwiftSDKsDirectory() throws -> AbsolutePath { |
| 102 | + var swiftSDKsDirectory = try fileSystem.getSharedSwiftSDKsDirectory( |
| 103 | + explicitDirectory: locations.swiftSDKsDirectory |
| 104 | + ) |
| 105 | + |
| 106 | + if !self.fileSystem.exists(swiftSDKsDirectory) { |
| 107 | + swiftSDKsDirectory = try self.fileSystem.getOrCreateSwiftPMSwiftSDKsDirectory() |
| 108 | + } |
| 109 | + |
| 110 | + return swiftSDKsDirectory |
| 111 | + } |
| 112 | + |
| 113 | + func run() async throws { |
| 114 | + let observabilityHandler = SwiftCommandObservabilityHandler(outputStream: stdoutStream, logLevel: .info) |
| 115 | + let observabilitySystem = ObservabilitySystem(observabilityHandler) |
| 116 | + let observabilityScope = observabilitySystem.topScope |
| 117 | + let swiftSDKsDirectory = try self.getOrCreateSwiftSDKsDirectory() |
| 118 | + |
| 119 | + let hostToolchain = try UserToolchain(swiftSDK: SwiftSDK.hostSwiftSDK()) |
| 120 | + let triple = try Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath) |
| 121 | + |
| 122 | + var commandError: Error? = nil |
| 123 | + do { |
| 124 | + let bundleStore = SwiftSDKBundleStore( |
| 125 | + swiftSDKsDirectory: swiftSDKsDirectory, |
| 126 | + fileSystem: self.fileSystem, |
| 127 | + observabilityScope: observabilityScope, |
| 128 | + outputHandler: { print($0) } |
| 129 | + ) |
| 130 | + let configurationStore = try SwiftSDKConfigurationStore( |
| 131 | + hostTimeTriple: triple, |
| 132 | + swiftSDKBundleStore: bundleStore |
| 133 | + ) |
| 134 | + let targetTriple = try Triple(self.targetTriple) |
| 135 | + |
| 136 | + guard let swiftSDK = try configurationStore.readConfiguration( |
| 137 | + sdkID: sdkID, |
| 138 | + targetTriple: targetTriple |
| 139 | + ) else { |
| 140 | + throw SwiftSDKError.swiftSDKNotFound( |
| 141 | + artifactID: sdkID, |
| 142 | + hostTriple: triple, |
| 143 | + targetTriple: targetTriple |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + if self.shouldShowConfiguration { |
| 148 | + print(swiftSDK.pathsConfiguration) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + var configuration = swiftSDK.pathsConfiguration |
| 153 | + if self.shouldReset { |
| 154 | + if try !configurationStore.resetConfiguration(sdkID: sdkID, targetTriple: targetTriple) { |
| 155 | + observabilityScope.emit( |
| 156 | + warning: "No configuration for Swift SDK `\(sdkID)`" |
| 157 | + ) |
| 158 | + } else { |
| 159 | + observabilityScope.emit( |
| 160 | + info: """ |
| 161 | + All configuration properties of Swift SDK `\(sdkID)` for target triple \ |
| 162 | + `\(targetTriple)` were successfully reset. |
| 163 | + """ |
| 164 | + ) |
| 165 | + } |
| 166 | + } else { |
| 167 | + var updatedProperties = [String]() |
| 168 | + |
| 169 | + let currentWorkingDirectory: AbsolutePath? = fileSystem.currentWorkingDirectory |
| 170 | + |
| 171 | + if let sdkRootPath { |
| 172 | + configuration.sdkRootPath = try AbsolutePath(validating: sdkRootPath, relativeTo: currentWorkingDirectory) |
| 173 | + updatedProperties.append(CodingKeys.sdkRootPath.stringValue) |
| 174 | + } |
| 175 | + |
| 176 | + if let swiftResourcesPath { |
| 177 | + configuration.swiftResourcesPath = |
| 178 | + try AbsolutePath(validating: swiftResourcesPath, relativeTo: currentWorkingDirectory) |
| 179 | + updatedProperties.append(CodingKeys.swiftResourcesPath.stringValue) |
| 180 | + } |
| 181 | + |
| 182 | + if let swiftStaticResourcesPath { |
| 183 | + configuration.swiftResourcesPath = |
| 184 | + try AbsolutePath(validating: swiftStaticResourcesPath, relativeTo: currentWorkingDirectory) |
| 185 | + updatedProperties.append(CodingKeys.swiftStaticResourcesPath.stringValue) |
| 186 | + } |
| 187 | + |
| 188 | + if !includeSearchPath.isEmpty { |
| 189 | + configuration.includeSearchPaths = |
| 190 | + try includeSearchPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) } |
| 191 | + updatedProperties.append(CodingKeys.includeSearchPath.stringValue) |
| 192 | + } |
| 193 | + |
| 194 | + if !librarySearchPath.isEmpty { |
| 195 | + configuration.librarySearchPaths = |
| 196 | + try librarySearchPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) } |
| 197 | + updatedProperties.append(CodingKeys.librarySearchPath.stringValue) |
| 198 | + } |
| 199 | + |
| 200 | + if !toolsetPath.isEmpty { |
| 201 | + configuration.toolsetPaths = |
| 202 | + try toolsetPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) } |
| 203 | + updatedProperties.append(CodingKeys.toolsetPath.stringValue) |
| 204 | + } |
| 205 | + |
| 206 | + guard !updatedProperties.isEmpty else { |
| 207 | + observabilityScope.emit( |
| 208 | + error: """ |
| 209 | + No properties of Swift SDK `\(sdkID)` for target triple `\(targetTriple)` were updated \ |
| 210 | + since none were specified. Pass `--help` flag to see the list of all available properties. |
| 211 | + """ |
| 212 | + ) |
| 213 | + return |
| 214 | + } |
| 215 | + |
| 216 | + var swiftSDK = swiftSDK |
| 217 | + swiftSDK.pathsConfiguration = configuration |
| 218 | + try configurationStore.updateConfiguration(sdkID: sdkID, swiftSDK: swiftSDK) |
| 219 | + |
| 220 | + observabilityScope.emit( |
| 221 | + info: """ |
| 222 | + These properties of Swift SDK `\(sdkID)` for target triple \ |
| 223 | + `\(targetTriple)` were successfully updated: \(updatedProperties.joined(separator: ", ")). |
| 224 | + """ |
| 225 | + ) |
| 226 | + } |
| 227 | + |
| 228 | + if observabilityScope.errorsReported { |
| 229 | + throw ExitCode.failure |
| 230 | + } |
| 231 | + } catch { |
| 232 | + commandError = error |
| 233 | + } |
| 234 | + |
| 235 | + // wait for all observability items to process |
| 236 | + observabilityHandler.wait(timeout: .now() + 5) |
| 237 | + |
| 238 | + if let commandError { |
| 239 | + throw commandError |
| 240 | + } |
| 241 | + } |
| 242 | +} |
| 243 | + |
| 244 | +extension AbsolutePath { |
| 245 | + fileprivate init(validating string: String, relativeTo basePath: AbsolutePath?) throws { |
| 246 | + if let basePath { |
| 247 | + try self.init(validating: string, relativeTo: basePath) |
| 248 | + } else { |
| 249 | + try self.init(validating: string) |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments