1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
//===--- KnownCommand.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
@preconcurrency import SwiftOptions
enum KnownCommand {
case clang, swiftc, swiftFrontend
}
extension PathProtocol {
var knownCommand: KnownCommand? {
switch fileName {
case "clang", "clang++", "c++", "cc", "clang-cache":
.clang
case "swiftc":
.swiftc
case "swift-frontend":
.swiftFrontend
default:
nil
}
}
}
extension Command.Flag {
/// If this spec is for a suboption, returns the command that it is for, or
/// `nil` if it is not for a suboption
var subOptionCommand: KnownCommand? {
switch self {
case .Xcc:
.clang
case .Xfrontend:
.swiftFrontend
default:
nil
}
}
}
extension Command.Flag {
struct Name: Hashable {
let rawValue: String
// Fileprivate because definitions should be added below.
fileprivate init(_ rawValue: String) {
self.rawValue = rawValue
}
}
fileprivate static func dash(_ name: String) -> Self {
dash(.init(name))
}
fileprivate static func doubleDash(_ name: String) -> Self {
doubleDash(.init(name))
}
fileprivate static func swiftc(_ opt: SwiftOptions.Option) -> Self {
let dashes = opt.spelling.prefix(while: { $0 == "-" }).count
guard let dash = Command.Flag.Dash(numDashes: dashes) else {
fatalError("Dash count not handled")
}
let name = String(opt.spelling.dropFirst(dashes))
return .init(dash: dash, name: .init(name))
}
}
extension SwiftOptions.Option {
fileprivate static let optionsWithJoinedEquals: Set<String> = {
// Make a note of all flags that are equal joined.
var result = Set<String>()
for opt in SwiftOptions.Option.allOptions {
switch opt.kind {
case .separate, .input, .flag, .remaining, .multiArg:
continue
case .joined, .joinedOrSeparate, .commaJoined:
if opt.spelling.hasSuffix("=") {
result.insert(String(opt.spelling.dropLast()))
}
}
}
return result
}()
fileprivate var spacingSpec: Command.FlagSpec.OptionSpacingSpec {
var spacing = Command.OptionSpacingSpec()
switch kind {
case .input, .remaining:
fatalError("Not handled")
case .flag:
break
case .joined, .commaJoined:
spacing.insert(.unspaced)
case .separate, .multiArg:
spacing.insert(.spaced)
case .joinedOrSeparate:
spacing.insert([.unspaced, .spaced])
}
if Self.optionsWithJoinedEquals.contains(spelling) {
spacing.insert(.equals)
}
return spacing
}
}
extension Command.FlagSpec {
fileprivate init(_ options: [SwiftOptions.Option]) {
self.init(options.map { .init(.swiftc($0), option: $0.spacingSpec) })
}
}
extension Command.Flag {
// Swift + Clang
static let D = dash("D")
static let I = dash("I")
static let target = dash("target")
// Clang
static let isystem = dash("isystem")
static let isysroot = dash("isysroot")
static let f = dash("f")
static let U = dash("U")
static let W = dash("W")
static let std = dash("std")
// Swift
static let cxxInteroperabilityMode =
swiftc(.cxxInteroperabilityMode)
static let enableExperimentalCxxInterop =
swiftc(.enableExperimentalCxxInterop)
static let enableExperimentalFeature =
swiftc(.enableExperimentalFeature)
static let enableLibraryEvolution =
swiftc(.enableLibraryEvolution)
static let experimentalSkipAllFunctionBodies =
swiftc(.experimentalSkipAllFunctionBodies)
static let experimentalSkipNonInlinableFunctionBodies =
swiftc(.experimentalSkipNonInlinableFunctionBodies)
static let experimentalSkipNonInlinableFunctionBodiesWithoutTypes =
swiftc(.experimentalSkipNonInlinableFunctionBodiesWithoutTypes)
static let F =
swiftc(.F)
static let Fsystem =
swiftc(.Fsystem)
static let moduleName =
swiftc(.moduleName)
static let moduleLinkName =
swiftc(.moduleLinkName)
static let nostdimport =
swiftc(.nostdimport)
static let O =
swiftc(.O)
static let Onone =
swiftc(.Onone)
static let packageName =
swiftc(.packageName)
static let parseAsLibrary =
swiftc(.parseAsLibrary)
static let parseStdlib =
swiftc(.parseStdlib)
static let runtimeCompatibilityVersion =
swiftc(.runtimeCompatibilityVersion)
static let sdk =
swiftc(.sdk)
static let swiftVersion =
swiftc(.swiftVersion)
static let wholeModuleOptimization =
swiftc(.wholeModuleOptimization)
static let wmo =
swiftc(.wmo)
static let Xcc =
swiftc(.Xcc)
static let Xfrontend =
swiftc(.Xfrontend)
static let Xllvm =
swiftc(.Xllvm)
}
extension KnownCommand {
private static let clangSpec = Command.FlagSpec([
.init(.I, option: .equals, .unspaced, .spaced),
.init(.D, option: .unspaced, .spaced),
.init(.U, option: .unspaced, .spaced),
.init(.W, option: .unspaced),
// This isn't an actual Clang flag, but it allows us to scoop up all the
// -f[...] flags.
// FIXME: We ought to see if we can get away with preserving unknown flags.
.init(.f, option: .unspaced),
// FIXME: Really we ought to map to Xcode's SDK
.init(.isystem, option: .unspaced, .spaced),
.init(.isysroot, option: .unspaced, .spaced),
.init(.std, option: .equals),
.init(.target, option: .spaced),
])
// FIXME: We currently only parse a small subset of the supported driver
// options. This is because:
//
// - It avoids including incompatible options (e.g options only suitable when
// emitting modules when we want to do a regular build).
// - It avoids including options that produce unnecessary outputs (e.g
// dependencies, object files), especially as they would be outputting into
// the Ninja build, which needs to be left untouched (maybe we could filter
// out options that have paths that point into the build dir?).
// - It avoids including options that do unnecessary work (e.g emitting debug
// info, code coverage).
// - It's quicker.
//
// This isn't great though, and we probably ought to revisit this, especially
// if the driver starts categorizing its options such that we can better
// reason about which we want to use. It should also be noted that we
// currently allow arbitrary options to be passed through -Xfrontend, we may
// want to reconsider that.
// NOTE: You can pass '--log-level debug' to see the options that are
// currently being missed.
private static let swiftOptions: [SwiftOptions.Option] = [
.cxxInteroperabilityMode,
.D,
.disableAutolinkingRuntimeCompatibilityDynamicReplacements,
.enableBuiltinModule,
.enableExperimentalCxxInterop,
.enableExperimentalFeature,
.enableLibraryEvolution,
.experimentalSkipAllFunctionBodies,
.experimentalSkipNonInlinableFunctionBodies,
.experimentalSkipNonInlinableFunctionBodiesWithoutTypes,
.F,
.Fsystem,
.I,
.nostdimport,
.O,
.Onone,
.moduleName,
.moduleLinkName,
.packageName,
.parseAsLibrary,
.parseStdlib,
.runtimeCompatibilityVersion,
.target,
.sdk,
.swiftVersion,
.wholeModuleOptimization,
.wmo,
.Xcc,
.Xfrontend,
.Xllvm,
]
private static let swiftcSpec = Command.FlagSpec(
swiftOptions.filter { !$0.attributes.contains(.noDriver) } + [
// Not currently listed as a driver option, but it used to be. Include
// for better compatibility.
.enableExperimentalCxxInterop
]
)
private static let swiftFrontendSpec = Command.FlagSpec(
swiftOptions.filter { $0.attributes.contains(.frontend) }
)
var flagSpec: Command.FlagSpec {
switch self {
case .clang:
Self.clangSpec
case .swiftc:
Self.swiftcSpec
case .swiftFrontend:
Self.swiftFrontendSpec
}
}
}
|