|
| 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 PackageModel |
| 17 | + |
| 18 | +import struct TSCBasic.AbsolutePath |
| 19 | + |
| 20 | +struct SetConfiguration: ConfigurationCommand { |
| 21 | + static let configuration = CommandConfiguration( |
| 22 | + commandName: "set", |
| 23 | + abstract: """ |
| 24 | + Sets configuration options for installed cross-compilation destinations. |
| 25 | + """ |
| 26 | + ) |
| 27 | + |
| 28 | + @OptionGroup(visibility: .hidden) |
| 29 | + var locations: LocationOptions |
| 30 | + |
| 31 | + @Option(help: "A path to a directory containing the SDK root.") |
| 32 | + var sdkRootPath: String? = nil |
| 33 | + |
| 34 | + @Option(help: "A path to a directory containing Swift resources for dynamic linking.") |
| 35 | + var swiftResourcesPath: String? = nil |
| 36 | + |
| 37 | + @Option(help: "A path to a directory containing Swift resources for static linking.") |
| 38 | + var swiftStaticResourcesPath: String? = nil |
| 39 | + |
| 40 | + @Option( |
| 41 | + parsing: .singleValue, |
| 42 | + help: """ |
| 43 | + A path to a directory containing headers. Multiple paths can be specified by providing this option multiple \ |
| 44 | + times to the command. |
| 45 | + """ |
| 46 | + ) |
| 47 | + var includeSearchPath: [String] = [] |
| 48 | + |
| 49 | + @Option( |
| 50 | + parsing: .singleValue, |
| 51 | + help: """ |
| 52 | + "A path to a directory containing libraries. Multiple paths can be specified by providing this option multiple \ |
| 53 | + times to the command. |
| 54 | + """ |
| 55 | + ) |
| 56 | + var librarySearchPath: [String] = [] |
| 57 | + |
| 58 | + @Option( |
| 59 | + parsing: .singleValue, |
| 60 | + help: """ |
| 61 | + "A path to a toolset file. Multiple paths can be specified by providing this option multiple times to the command. |
| 62 | + """ |
| 63 | + ) |
| 64 | + var toolsetPath: [String] = [] |
| 65 | + |
| 66 | + @Argument( |
| 67 | + help: """ |
| 68 | + An identifier of an already installed destination. Use the `list` subcommand to see all available \ |
| 69 | + identifiers. |
| 70 | + """ |
| 71 | + ) |
| 72 | + var destinationID: String |
| 73 | + |
| 74 | + @Argument(help: "The run-time triple of the destination to configure.") |
| 75 | + var runTimeTriple: String |
| 76 | + |
| 77 | + func run( |
| 78 | + buildTimeTriple: Triple, |
| 79 | + runTimeTriple: Triple, |
| 80 | + _ destination: Destination, |
| 81 | + _ configurationStore: DestinationConfigurationStore, |
| 82 | + _ destinationsDirectory: AbsolutePath, |
| 83 | + _ observabilityScope: ObservabilityScope |
| 84 | + ) throws { |
| 85 | + var configuration = destination.pathsConfiguration |
| 86 | + var updatedProperties = [String]() |
| 87 | + |
| 88 | + let currentWorkingDirectory = fileSystem.currentWorkingDirectory |
| 89 | + |
| 90 | + if let sdkRootPath = sdkRootPath { |
| 91 | + configuration.sdkRootPath = try AbsolutePath(validating: sdkRootPath, relativeTo: currentWorkingDirectory) |
| 92 | + updatedProperties.append(CodingKeys.sdkRootPath.stringValue) |
| 93 | + } |
| 94 | + |
| 95 | + if let swiftResourcesPath = swiftResourcesPath { |
| 96 | + configuration.swiftResourcesPath = |
| 97 | + try AbsolutePath(validating: swiftResourcesPath, relativeTo: currentWorkingDirectory) |
| 98 | + updatedProperties.append(CodingKeys.swiftResourcesPath.stringValue) |
| 99 | + } |
| 100 | + |
| 101 | + if let swiftStaticResourcesPath = swiftStaticResourcesPath { |
| 102 | + configuration.swiftResourcesPath = |
| 103 | + try AbsolutePath(validating: swiftStaticResourcesPath, relativeTo: currentWorkingDirectory) |
| 104 | + updatedProperties.append(CodingKeys.swiftStaticResourcesPath.stringValue) |
| 105 | + } |
| 106 | + |
| 107 | + if !includeSearchPath.isEmpty { |
| 108 | + configuration.includeSearchPaths = |
| 109 | + try includeSearchPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) } |
| 110 | + updatedProperties.append(CodingKeys.includeSearchPath.stringValue) |
| 111 | + } |
| 112 | + |
| 113 | + if !librarySearchPath.isEmpty { |
| 114 | + configuration.librarySearchPaths = |
| 115 | + try librarySearchPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) } |
| 116 | + updatedProperties.append(CodingKeys.librarySearchPath.stringValue) |
| 117 | + } |
| 118 | + |
| 119 | + if !toolsetPath.isEmpty { |
| 120 | + configuration.toolsetPaths = |
| 121 | + try toolsetPath.map { try AbsolutePath(validating: $0, relativeTo: currentWorkingDirectory) } |
| 122 | + updatedProperties.append(CodingKeys.toolsetPath.stringValue) |
| 123 | + } |
| 124 | + |
| 125 | + guard !updatedProperties.isEmpty else { |
| 126 | + observabilityScope.emit( |
| 127 | + error: """ |
| 128 | + No properties of destination `\(destinationID) for run-time triple `\(runTimeTriple)` were updated \ |
| 129 | + since none were specified. Pass `--help` flag to see the list of all available properties. |
| 130 | + """ |
| 131 | + ) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + var destination = destination |
| 136 | + destination.pathsConfiguration = configuration |
| 137 | + try configurationStore.updateConfiguration(destinationID: destinationID, destination: destination) |
| 138 | + |
| 139 | + observabilityScope.emit( |
| 140 | + info: """ |
| 141 | + These properties of destination `\(destinationID) for run-time triple \ |
| 142 | + `\(runTimeTriple)` were successfully updated: \(updatedProperties.joined(separator: ", ")). |
| 143 | + """ |
| 144 | + ) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +extension AbsolutePath { |
| 149 | + fileprivate init(validating string: String, relativeTo basePath: AbsolutePath?) throws { |
| 150 | + if let basePath = basePath { |
| 151 | + try self.init(validating: string, relativeTo: basePath) |
| 152 | + } else { |
| 153 | + try self.init(validating: string) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments