File: BuildPhases.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (433 lines) | stat: -rw-r--r-- 18,715 bytes parent folder | download
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation
import SWBProtocol

extension ProjectModel {
    /// All possible build phases in a target.
    public enum BuildPhase: Sendable, Hashable {
        case headers(HeadersBuildPhase)
        case sources(SourcesBuildPhase)
        case frameworks(FrameworksBuildPhase)
        case copyBundleResources(CopyBundleResourcesBuildPhase)
        case copyFiles(CopyFilesBuildPhase)
        case shellScript(ShellScriptBuildPhase)

        var common: BuildPhaseCommon {
            switch self {
            case .headers(let phase): return phase.common
            case .sources(let phase): return phase.common
            case .frameworks(let phase): return phase.common
            case .copyBundleResources(let phase): return phase.common
            case .copyFiles(let phase): return phase.common
            case .shellScript(let phase): return phase.common
            }
        }
    }

    public struct BuildPhaseCommon: Sendable, Hashable {
        public var id: GUID
        public fileprivate(set) var files: [BuildFile]

        public init(id: GUID, files: [BuildFile] = []) {
            self.id = id
            self.files = files
        }

        private var nextBuildFileId: GUID {
            return "\(self.id.value)::\(self.files.count)"
        }

        // MARK: - BuildFile

        @discardableResult public mutating func addBuildFile(_ create: CreateFn<BuildFile>) -> BuildFile {
            let buildFile = create(nextBuildFileId)
            self.files.append(buildFile)
            return buildFile
        }

        public subscript(file tag: Tag<BuildFile>) -> BuildFile {
            get { files[id: tag.value] }
            set { files[id: tag.value] = newValue }
        }

    }

    /// A "headers" build phase, i.e. one that copies headers into a directory of the product, after suitable processing.
    public struct HeadersBuildPhase: Sendable, Hashable, Common {
        public var common: BuildPhaseCommon

        public init(id: GUID, files: [BuildFile] = []) {
            self.common = BuildPhaseCommon(id: id, files: files)
        }
    }

    /// A "sources" build phase, i.e. one that compiles sources and provides them to be linked into the executable code of the product.
    public struct SourcesBuildPhase: Sendable, Hashable, Common {
        public var common: BuildPhaseCommon

        public init(id: GUID, files: [BuildFile] = []) {
            self.common = BuildPhaseCommon(id: id, files: files)
        }
    }

    /// A "frameworks" build phase, i.e. one that links compiled code and libraries into the executable of the product.
    public struct FrameworksBuildPhase: Sendable, Hashable, Common {
        public var common: BuildPhaseCommon

        public init(id: GUID, files: [BuildFile] = []) {
            self.common = BuildPhaseCommon(id: id, files: files)
        }
    }

    public struct CopyBundleResourcesBuildPhase: Sendable, Hashable, Common {
        public var common: BuildPhaseCommon

        public init(id: GUID) {
            self.common = BuildPhaseCommon(id: id)
        }
    }

    /// A "copy files" build phase, i.e. one that copies files to an arbitrary location relative to the product.
    public struct CopyFilesBuildPhase: Sendable, Hashable, Common {
        public enum DestinationSubfolder: Sendable, Hashable, Codable {
            case absolute
            case builtProductsDir
            case buildSetting(String)

            public static let wrapper = DestinationSubfolder.buildSetting("$(WRAPPER_NAME)")
            public static let resources = DestinationSubfolder.buildSetting("$(UNLOCALIZED_RESOURCES_FOLDER_PATH)")
            public static let frameworks = DestinationSubfolder.buildSetting("$(FRAMEWORKS_FOLDER_PATH)")
            public static let sharedFrameworks = DestinationSubfolder.buildSetting("$(SHARED_FRAMEWORKS_FOLDER_PATH)")
            public static let sharedSupport = DestinationSubfolder.buildSetting("$(SHARED_SUPPORT_FOLDER_PATH)")
            public static let plugins = DestinationSubfolder.buildSetting("$(PLUGINS_FOLDER_PATH)")
            public static let javaResources = DestinationSubfolder.buildSetting("$(JAVA_FOLDER_PATH)")

            public var pathString: String {
                switch self {
                case .absolute:
                    return "<absolute>"
                case .builtProductsDir:
                    return "<builtProductsDir>"
                case .buildSetting(let value):
                    return value
                }
            }

            public init(from decoder: any Decoder) throws {
                let container = try decoder.singleValueContainer()
                let str = try container.decode(String.self)
                switch str {
                case "<absolute>": self = .absolute
                case "<builtProductsDir>": self = .builtProductsDir
                default: self = .buildSetting(str)
                }
            }

            public func encode(to encoder: any Encoder) throws {
                var container = encoder.singleValueContainer()
                try container.encode(self.pathString)
            }
        }

        public var common: BuildPhaseCommon
        public var destinationSubfolder: CopyFilesBuildPhase.DestinationSubfolder
        public var destinationSubpath: String = ""
        public var runOnlyForDeploymentPostprocessing: Bool = false

        public init(common: BuildPhaseCommon, destinationSubfolder: CopyFilesBuildPhase.DestinationSubfolder, destinationSubpath: String = "", runOnlyForDeploymentPostprocessing: Bool = false) {
            self.common = common
            self.destinationSubfolder = destinationSubfolder
            self.destinationSubpath = destinationSubpath
            self.runOnlyForDeploymentPostprocessing = runOnlyForDeploymentPostprocessing
        }
    }

    /// A "shell script" build phase, i.e. one that runs a custom shell script.
    public struct ShellScriptBuildPhase: Sendable, Hashable, Common {
        public var common: BuildPhaseCommon
        public var name: String
        public var scriptContents: String
        public var shellPath: String
        public var inputPaths: [String]
        public var outputPaths: [String]
        public var emitEnvironment: Bool
        public var alwaysOutOfDate: Bool
        public var runOnlyForDeploymentPostprocessing: Bool
        public var originalObjectID: String
        public var sandboxingOverride: SandboxingOverride
        public var alwaysRunForInstallHdrs: Bool

        public init(
            id: GUID,
            name: String,
            scriptContents: String,
            shellPath: String,
            inputPaths: [String],
            outputPaths: [String],
            emitEnvironment: Bool,
            alwaysOutOfDate: Bool,
            runOnlyForDeploymentPostprocessing: Bool = false,
            originalObjectID: String,
            sandboxingOverride: SandboxingOverride = .basedOnBuildSetting,
            alwaysRunForInstallHdrs: Bool = false
        ) {
            self.common = BuildPhaseCommon(id: id)
            self.name = name
            self.scriptContents = scriptContents
            self.shellPath = shellPath
            self.inputPaths = inputPaths
            self.outputPaths = outputPaths
            self.emitEnvironment = emitEnvironment
            self.alwaysOutOfDate = alwaysOutOfDate
            self.runOnlyForDeploymentPostprocessing = runOnlyForDeploymentPostprocessing
            self.originalObjectID = originalObjectID
            self.sandboxingOverride = sandboxingOverride
            self.alwaysRunForInstallHdrs = alwaysRunForInstallHdrs
        }
    }
}



extension ProjectModel.BuildPhase: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let type = try container.decode(String.self, forKey: .type)
        switch type {
        case "com.apple.buildphase.headers":
            self = .headers(try .init(from: decoder))
        case "com.apple.buildphase.sources":
            self = .sources(try .init(from: decoder))
        case "com.apple.buildphase.frameworks":
            self = .frameworks(try .init(from: decoder))
        case "com.apple.buildphase.resources":
            self = .copyBundleResources(try .init(from: decoder))
        case "com.apple.buildphase.copy-files":
            self = .copyFiles(try .init(from: decoder))
        case "com.apple.buildphase.shell-script":
            self = .shellScript(try .init(from: decoder))
        default:
            throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Unknown value")
        }
    }

    public func encode(to encoder: any Encoder) throws {
        switch self {
        case .headers(let buildPhase): try buildPhase.encode(to: encoder)
        case .sources(let buildPhase): try buildPhase.encode(to: encoder)
        case .frameworks(let buildPhase): try buildPhase.encode(to: encoder)
        case .copyBundleResources(let buildPhase): try buildPhase.encode(to: encoder)
        case .copyFiles(let buildPhase): try buildPhase.encode(to: encoder)
        case .shellScript(let buildPhase): try buildPhase.encode(to: encoder)
        }
    }

    enum CodingKeys: String, CodingKey {
        case type
    }
}

extension ProjectModel.BuildPhaseCommon: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        self.files = try container.decode([ProjectModel.BuildFile].self, forKey: .buildFiles)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.files, forKey: .buildFiles)
    }

    enum CodingKeys: String, CodingKey {
        case guid
        case buildFiles
    }
}

extension ProjectModel.HeadersBuildPhase: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        let files = try container.decode([ProjectModel.BuildFile].self, forKey: .buildFiles)
        self.common = .init(id: id, files: files)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("com.apple.buildphase.headers", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.files, forKey: .buildFiles)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case buildFiles
    }
}

extension ProjectModel.SourcesBuildPhase: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        let files = try container.decode([ProjectModel.BuildFile].self, forKey: .buildFiles)
        self.common = .init(id: id, files: files)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("com.apple.buildphase.sources", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.files, forKey: .buildFiles)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case buildFiles
    }
}

extension ProjectModel.FrameworksBuildPhase: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        let files = try container.decode([ProjectModel.BuildFile].self, forKey: .buildFiles)
        self.common = .init(id: id, files: files)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("com.apple.buildphase.frameworks", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.files, forKey: .buildFiles)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case buildFiles
    }
}

extension ProjectModel.CopyBundleResourcesBuildPhase: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        let files = try container.decode([ProjectModel.BuildFile].self, forKey: .buildFiles)
        self.common = .init(id: id, files: files)
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("com.apple.buildphase.resources", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.files, forKey: .buildFiles)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case buildFiles
    }
}

extension ProjectModel.CopyFilesBuildPhase: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.common = .init(id: try container.decode(ProjectModel.GUID.self, forKey: .guid))
        self.destinationSubfolder = try container.decode(DestinationSubfolder.self, forKey: .destinationSubfolder)
        self.files = try container.decode([ProjectModel.BuildFile].self, forKey: .buildFiles)
        self.destinationSubpath = try container.decode(String.self, forKey: .destinationSubpath)
        self.runOnlyForDeploymentPostprocessing = try container.decode(String.self, forKey: .runOnlyForDeploymentPostprocessing) == "true"
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("com.apple.buildphase.copy-files", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.files, forKey: .buildFiles)
        try container.encode(self.destinationSubfolder.pathString, forKey: .destinationSubfolder)
        try container.encode(self.destinationSubpath, forKey: .destinationSubpath)
        try container.encode(self.runOnlyForDeploymentPostprocessing ? "true" : "false", forKey: .runOnlyForDeploymentPostprocessing)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case buildFiles
        case destinationSubfolder
        case destinationSubpath
        case runOnlyForDeploymentPostprocessing
    }
}

extension ProjectModel.ShellScriptBuildPhase: Codable {
    public init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let id = try container.decode(ProjectModel.GUID.self, forKey: .guid)
        let files = try container.decode([ProjectModel.BuildFile].self, forKey: .buildFiles)
        self.common = .init(id: id, files: files)

        self.name = try container.decode(String.self, forKey: .name)
        self.shellPath = try container.decode(String.self, forKey: .shellPath)
        self.scriptContents = try container.decode(String.self, forKey: .scriptContents)
        self.inputPaths = try container.decode([String].self, forKey: .inputFilePaths)
        self.outputPaths = try container.decode([String].self, forKey: .outputFilePaths)
        self.emitEnvironment = try container.decode(String.self, forKey: .emitEnvironment) == "true"
        self.sandboxingOverride = try container.decode(ProjectModel.SandboxingOverride.self, forKey: .sandboxingOverride)
        self.alwaysOutOfDate = try container.decode(String.self, forKey: .alwaysOutOfDate) == "true"
        self.runOnlyForDeploymentPostprocessing = try container.decode(String.self, forKey: .runOnlyForDeploymentPostprocessing) == "true"
        self.originalObjectID = try container.decode(String.self, forKey: .originalObjectID)
        self.alwaysRunForInstallHdrs = try container.decode(String.self, forKey: .alwaysRunForInstallHdrs) == "true"
    }

    public func encode(to encoder: any Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode("com.apple.buildphase.shell-script", forKey: .type)
        try container.encode(self.id, forKey: .guid)
        try container.encode(self.name, forKey: .name)
        try container.encode(self.shellPath, forKey: .shellPath)
        try container.encode(self.scriptContents, forKey: .scriptContents)
        try container.encode(self.files, forKey: .buildFiles)
        try container.encode(self.inputPaths, forKey: .inputFilePaths)
        try container.encode(self.outputPaths, forKey: .outputFilePaths)
        try container.encode(self.emitEnvironment ? "true" : "false", forKey: .emitEnvironment)
        try container.encode(self.sandboxingOverride, forKey: .sandboxingOverride)
        try container.encode(self.alwaysOutOfDate ? "true" : "false", forKey: .alwaysOutOfDate)
        try container.encode(self.runOnlyForDeploymentPostprocessing ? "true" : "false", forKey: .runOnlyForDeploymentPostprocessing)
        try container.encode(self.originalObjectID, forKey: .originalObjectID)
        try container.encode(self.alwaysRunForInstallHdrs ? "true" : "false", forKey: .alwaysRunForInstallHdrs)
    }

    enum CodingKeys: String, CodingKey {
        case type
        case guid
        case name
        case shellPath
        case scriptContents
        case buildFiles
        case inputFilePaths
        case outputFilePaths
        case emitEnvironment
        case sandboxingOverride
        case alwaysOutOfDate
        case runOnlyForDeploymentPostprocessing
        case originalObjectID
        case alwaysRunForInstallHdrs
    }
}