|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 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 Basics |
| 14 | +import PackageModel |
| 15 | +import SwiftParser |
| 16 | +import SwiftSyntax |
| 17 | +import SwiftSyntaxBuilder |
| 18 | +import struct TSCUtility.Version |
| 19 | + |
| 20 | +/// Add a swift setting to a manifest's source code. |
| 21 | +public enum AddSwiftSetting { |
| 22 | + /// The set of argument labels that can occur after the "targets" |
| 23 | + /// argument in the Package initializers. |
| 24 | + private static let argumentLabelsAfterSwiftSettings: Set<String> = [ |
| 25 | + "linkerSettings", |
| 26 | + "plugins", |
| 27 | + ] |
| 28 | + |
| 29 | + public static func upcomingFeature( |
| 30 | + to target: String, |
| 31 | + name: String, |
| 32 | + manifest: SourceFileSyntax |
| 33 | + ) throws -> PackageEditResult { |
| 34 | + try self.addToTarget( |
| 35 | + target, |
| 36 | + name: "enableUpcomingFeature", |
| 37 | + value: name, |
| 38 | + firstIntroduced: .v5_8, |
| 39 | + manifest: manifest |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + public static func experimentalFeature( |
| 44 | + to target: String, |
| 45 | + name: String, |
| 46 | + manifest: SourceFileSyntax |
| 47 | + ) throws -> PackageEditResult { |
| 48 | + try self.addToTarget( |
| 49 | + target, |
| 50 | + name: "enableExperimentalFeature", |
| 51 | + value: name, |
| 52 | + firstIntroduced: .v5_8, |
| 53 | + manifest: manifest |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + public static func languageMode( |
| 58 | + to target: String, |
| 59 | + mode: SwiftLanguageVersion, |
| 60 | + manifest: SourceFileSyntax |
| 61 | + ) throws -> PackageEditResult { |
| 62 | + try self.addToTarget( |
| 63 | + target, |
| 64 | + name: "swiftLanguageMode", |
| 65 | + value: mode, |
| 66 | + firstIntroduced: .v6_0, |
| 67 | + manifest: manifest |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + public static func strictMemorySafety( |
| 72 | + to target: String, |
| 73 | + manifest: SourceFileSyntax |
| 74 | + ) throws -> PackageEditResult { |
| 75 | + try self.addToTarget( |
| 76 | + target, name: "strictMemorySafety", |
| 77 | + value: String?.none, |
| 78 | + firstIntroduced: .v6_2, |
| 79 | + manifest: manifest |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + private static func addToTarget( |
| 84 | + _ target: String, |
| 85 | + name: String, |
| 86 | + value: (some ManifestSyntaxRepresentable)?, |
| 87 | + firstIntroduced: ToolsVersion, |
| 88 | + manifest: SourceFileSyntax |
| 89 | + ) throws -> PackageEditResult { |
| 90 | + try manifest.checkManifestAtLeast(firstIntroduced) |
| 91 | + |
| 92 | + guard let packageCall = manifest.findCall(calleeName: "Package") else { |
| 93 | + throw ManifestEditError.cannotFindPackage |
| 94 | + } |
| 95 | + |
| 96 | + guard let targetsArgument = packageCall.findArgument(labeled: "targets"), |
| 97 | + let targetArray = targetsArgument.expression.findArrayArgument() |
| 98 | + else { |
| 99 | + throw ManifestEditError.cannotFindTargets |
| 100 | + } |
| 101 | + |
| 102 | + guard let targetCall = FunctionCallExprSyntax.findFirst(in: targetArray, matching: { |
| 103 | + if let nameArgument = $0.findArgument(labeled: "name"), |
| 104 | + let nameLiteral = nameArgument.expression.as(StringLiteralExprSyntax.self), |
| 105 | + nameLiteral.representedLiteralValue == target |
| 106 | + { |
| 107 | + return true |
| 108 | + } |
| 109 | + return false |
| 110 | + }) else { |
| 111 | + throw ManifestEditError.cannotFindTarget(targetName: target) |
| 112 | + } |
| 113 | + |
| 114 | + if let memberRef = targetCall.calledExpression.as(MemberAccessExprSyntax.self), |
| 115 | + memberRef.declName.baseName.text == "plugin" |
| 116 | + { |
| 117 | + throw ManifestEditError.cannotAddSettingsToPluginTarget |
| 118 | + } |
| 119 | + |
| 120 | + let newTargetCall = if let value { |
| 121 | + try targetCall.appendingToArrayArgument( |
| 122 | + label: "swiftSettings", |
| 123 | + trailingLabels: self.argumentLabelsAfterSwiftSettings, |
| 124 | + newElement: ".\(raw: name)(\(value.asSyntax()))" |
| 125 | + ) |
| 126 | + } else { |
| 127 | + try targetCall.appendingToArrayArgument( |
| 128 | + label: "swiftSettings", |
| 129 | + trailingLabels: self.argumentLabelsAfterSwiftSettings, |
| 130 | + newElement: ".\(raw: name)" |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + return PackageEditResult( |
| 135 | + manifestEdits: [ |
| 136 | + .replace(targetCall, with: newTargetCall.description), |
| 137 | + ] |
| 138 | + ) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +extension SwiftLanguageVersion: ManifestSyntaxRepresentable { |
| 143 | + func asSyntax() -> ExprSyntax { |
| 144 | + if !Self.supportedSwiftLanguageVersions.contains(self) { |
| 145 | + return ".version(\"\(raw: rawValue)\")" |
| 146 | + } |
| 147 | + |
| 148 | + if minor == 0 { |
| 149 | + return ".v\(raw: major)" |
| 150 | + } |
| 151 | + |
| 152 | + return ".v\(raw: major)_\(raw: minor)" |
| 153 | + } |
| 154 | +} |
0 commit comments