-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathBuildConfiguration.swift
315 lines (291 loc) · 11.1 KB
/
BuildConfiguration.swift
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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
//
//===----------------------------------------------------------------------===//
import SwiftSyntax
/// Describes the ordering of a sequence of bytes that make up a word of
/// storage for a particular architecture.
public enum Endianness: String {
/// Little endian, meaning that the least significant byte of a word is
/// stored at the lowest address.
case little
/// Big endian, meaning that the most significant byte of a word is stored
/// at the lowest address.
case big
}
/// Describes the requested version of a module.
public enum CanImportVersion {
/// Any version of the module will suffice.
case unversioned
/// Only modules with the given version or higher will match.
case version(VersionTuple)
/// Only modules where the underlying Clang module has the given version or
/// higher will match.
case underlyingVersion(VersionTuple)
}
enum BuildConfigurationError: Error, CustomStringConvertible {
case experimentalFeature(name: String)
var description: String {
switch self {
case .experimentalFeature(let name):
return "'name' is an experimental feature"
}
}
}
/// Captures information about the build configuration that can be
/// queried in a `#if` expression, including OS, compiler version,
/// enabled language features, and available modules.
///
/// Providing complete build configuration information effectively requires
/// a Swift compiler, because (for example) determining whether a module can
/// be imported is a complicated task only implemented in the Swift compiler.
/// Therefore, queries are permitted to throw an error to report when they
/// cannot answer a query, in which case this error will be reported to
/// the caller and the condition will be treated as being "false", so the
/// code covered by the condition will be inactive.
public protocol BuildConfiguration {
/// Determine whether a given custom build condition has been set.
///
/// Custom build conditions can be set by the `-D` command line option to
/// the Swift compiler. For example, `-DDEBUG` sets the custom condition
/// named `DEBUG`, which could be checked with, e.g.,
///
/// ```swift
/// #if DEBUG
/// // ...
/// #endif
/// ```
///
/// - Parameters:
/// - name: The name of the custom build condition being checked (e.g.,
/// `DEBUG`.
/// - Returns: Whether the custom condition is set.
func isCustomConditionSet(name: String) throws -> Bool
/// Determine whether the given feature is enabled.
///
/// Features are determined by the Swift compiler, language mode, and other
/// options such as `--enable-upcoming-feature`, and can be checked with
/// the `hasFeature` syntax, e.g.,
///
/// ```swift
/// #if hasFeature(VariadicGenerics)
/// // ...
/// #endif
/// ```
///
/// - Parameters:
/// - name: The name of the feature being checked.
/// - Returns: Whether the requested feature is available.
func hasFeature(name: String) throws -> Bool
/// Determine whether the given attribute is available.
///
/// Attributes are determined by the Swift compiler. They can be checked
/// with `hasAttribute` syntax, e.g.,
///
/// ```swift
/// #if hasAttribute(available)
/// // ...
/// #endif
/// ```
///
/// - Parameters:
/// - name: The name of the attribute being queried.
/// - Returns: Whether the requested attribute is supported.
func hasAttribute(name: String) throws -> Bool
/// Determine whether a module with the given import path can be imported,
/// with additional version information.
///
/// The availability of a module for import can be checked with `canImport`,
/// e.g.,
///
/// ```swift
/// #if canImport(UIKit)
/// // ...
/// #endif
/// ```
///
/// There is an experimental syntax for providing required module version
/// information, which will translate into the `version` argument.
///
/// - Parameters:
/// - importPath: A nonempty sequence of (token, identifier) pairs
/// describing the imported module, which was written in source as a
/// dotted sequence, e.g., `UIKit.UIViewController` will be passed in as
/// the import path array `[(token, "UIKit"), (token, "UIViewController")]`.
/// - version: The version restriction on the imported module. For the
/// normal `canImport(<import-path>)` syntax, this will always be
/// `CanImportVersion.unversioned`.
/// - Returns: Whether the module can be imported.
func canImport(importPath: [(TokenSyntax, String)], version: CanImportVersion) throws -> Bool
/// Determine whether the given name is the active target OS (e.g., Linux, iOS).
///
/// The target operating system can be queried with `os(<name>)`, e.g.,
///
/// ```swift
/// #if os(Linux)
/// // Linux-specific implementation
/// #endif
/// ```
///
/// - Parameters:
/// - name: The name of the operating system being queried, such as `Linux`,
/// `Windows`, `macOS`, etc.
/// - Returns: Whether the given operating system name is the target operating
/// system, i.e., the operating system for which code is being generated.
func isActiveTargetOS(name: String) throws -> Bool
/// Determine whether the given name is the active target architecture
/// (e.g., x86_64, arm64).
///
/// The target processor architecture can be queried with `arch(<name>)`, e.g.,
///
/// ```swift
/// #if arch(x86_64)
/// // 64-bit x86 Intel-specific code
/// #endif
/// ```
///
/// - Parameters:
/// - name: The name of the target architecture to check.
/// - Returns: Whether the given processor architecture is the target
/// architecture.
func isActiveTargetArchitecture(name: String) throws -> Bool
/// Determine whether the given name is the active target environment (e.g., simulator)
///
/// The target environment can be queried with `targetEnvironment(<name>)`,
/// e.g.,
///
/// ```swift
/// #if targetEnvironment(simulator)
/// // Simulator-specific code
/// #endif
/// ```
///
/// - Parameters:
/// - name: The name of the target environment to check.
/// - Returns: Whether the target platform is for a specific environment,
/// such as a simulator or emulator.
func isActiveTargetEnvironment(name: String) throws -> Bool
/// Determine whether the given name is the active target runtime (e.g., _ObjC vs. _Native)
///
/// The target runtime can only be queried by an experimental syntax
/// `_runtime(<name>)`, e.g.,
///
/// ```swift
/// #if _runtime(_ObjC)
/// // Code that depends on Swift being built for use with the Objective-C
/// // runtime, e.g., on Apple platforms.
/// #endif
/// ```
///
/// The only other runtime is "none", when Swift isn't tying into any other
/// specific runtime.
///
/// - Parameters:
/// - name: The name of the runtime.
/// - Returns: Whether the target runtime matches the given name.
func isActiveTargetRuntime(name: String) throws -> Bool
/// Determine whether the given name is the active target pointer authentication scheme (e.g., arm64e).
///
/// The target pointer authentication scheme describes how pointers are
/// signed, as a security mitigation. This scheme can only be queried by
/// an experimental syntax `_ptrath(<name>)`, e.g.,
///
/// ```swift
/// #if _ptrauth(arm64e)
/// // Special logic for arm64e pointer signing
/// #endif
/// ```
/// - Parameters:
/// - name: The name of the pointer authentication scheme to check.
/// - Returns: Whether the code generated for the target will use the given
/// pointer authentication scheme.
func isActiveTargetPointerAuthentication(name: String) throws -> Bool
/// Determine whether the given name is the active target object file format (e.g., ELF).
///
/// The target object file format can only be queried by an experimental
/// syntax `_objectFileFormat(<name>)`, e.g.,
///
/// ```swift
/// #if _objectFileFormat(ELF)
/// // Special logic for ELF object file formats
/// #endif
/// ```
/// - Parameters:
/// - name: The name of the object file format.
/// - Returns: Whether the target object file format matches the given name.
@_spi(ExperimentalLanguageFeatures)
func isActiveTargetObjectFileFormat(name: String) throws -> Bool
/// The bit width of a data pointer for the target architecture.
///
/// The target's pointer bit width (which also corresponds to the number of
/// bits in `Int`/`UInt`) can only be queried with the experimental syntax
/// `_pointerBitWidth(_<bitwidth>)`, e.g.,
///
/// ```swift
/// #if _pointerBitWidth(_32)
/// // 32-bit system
/// #endif
/// ```
var targetPointerBitWidth: Int { get }
/// The atomic bit widths that are natively supported by the target
/// architecture.
///
/// This lists all of the bit widths for which the target provides support
/// for atomic operations. It can be queried with
/// `_hasAtomicBitWidth(_<bitwidth>)`, e.g.
///
/// ```swift
/// #if _hasAtomicBitWidth(_64)
/// // 64-bit atomics are available
/// #endif
var targetAtomicBitWidths: [Int] { get }
/// The endianness of the target architecture.
///
/// The target's endianness can onyl be queried with the experimental syntax
/// `_endian(<name>)`, where `<name>` can be either "big" or "little", e.g.,
///
/// ```swift
/// #if _endian(little)
/// // Swap some bytes around for network byte order
/// #endif
/// ```
var endianness: Endianness { get }
/// The effective language version, which can be set by the user (e.g., 5.0).
///
/// The language version can be queried with the `swift` directive that checks
/// how the supported language version compares, as described by
/// [SE-0212](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0212-compiler-version-directive.md). For example:
///
/// ```swift
/// #if swift(>=5.5)
/// // Hooray, we can use tasks!
/// ```
var languageVersion: VersionTuple { get }
/// The version of the compiler (e.g., 5.9).
///
/// The compiler version can be queried with the `compiler` directive that
/// checks the specific version of the compiler being used to process the
/// code, e.g.,
///
/// ```swift
/// #if compiler(>=5.7)
/// // Hoorway, we can implicitly open existentials!
/// #endif
var compilerVersion: VersionTuple { get }
}
/// Default implementation of BuildConfiguration, to avoid a revlock with the
/// swift repo, and breaking clients with the new addition to the protocol.
extension BuildConfiguration {
/// FIXME: This should be @_spi(ExperimentalLanguageFeatures) but cannot due
/// to rdar://147943518, https://github.com/swiftlang/swift/issues/80313
public func isActiveTargetObjectFileFormat(name: String) throws -> Bool {
throw BuildConfigurationError.experimentalFeature(name: "_objectFileFormat")
}
}