File: BuildConfiguration.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 (138 lines) | stat: -rw-r--r-- 6,976 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
//===----------------------------------------------------------------------===//
//
// 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 SWBProtocol
import SWBUtil
public import SWBMacro

public final class BuildConfiguration: ProjectModelItem, Encodable, Sendable
{
    public let name: String

    /// The (cached) parsed build settings table.
    public var buildSettings: MacroValueAssignmentTable { return buildSettingsCache.getValue(self) }
    private let buildSettingsCache = LazyCache { (buildConfiguration: BuildConfiguration) -> MacroValueAssignmentTable in
        return BuildConfiguration.createTableFromUserSettings(buildConfiguration.buildSettingsData, namespace: buildConfiguration.namespace)
    }
    private let buildSettingsData: [String: PropertyListItem]

    /// The namespace to use for lazily parsing build settings.
    private let namespace: MacroNamespace

    public let baseConfigurationFileReferenceGUID: String?

    public func baseConfigurationFileReference(_ workspace: Workspace) -> FileReference? {
        if let baseConfigurationFileReferenceGUID {
            return workspace.lookupReference(for: baseConfigurationFileReferenceGUID) as? FileReference
        } else {
            return nil
        }
    }

    public let impartedBuildProperties: ImpartedBuildProperties

    private enum CodingKeys : String, CodingKey {
        case name
        case namespace
        case baseConfigurationFileReferenceGUID
    }

    /// Helper method to convert MacroBindingSource array to plist dictionary.
    static func convertMacroBindingSourceToPlistDictionary(
        _ buildSettings: [SWBProtocol.BuildConfiguration.MacroBindingSource]
    ) -> [String: PropertyListItem] {
        // FIXME: Change the base representation once the legacy implementation dies.
        var settings = [String: PropertyListItem]()
        for setting in buildSettings {
            switch setting.value {
            case .string(let value):
                settings[setting.key] = .plString(value)
            case .stringList(let value):
                settings[setting.key] = .plArray(value.map{ .plString($0) })
            }
        }
        return settings
    }

    init(_ model: SWBProtocol.BuildConfiguration, _ pifLoader: PIFLoader) {
        self.name = model.name
        self.namespace = pifLoader.userNamespace
        self.buildSettingsData = BuildConfiguration.convertMacroBindingSourceToPlistDictionary(model.buildSettings)
        self.baseConfigurationFileReferenceGUID = model.baseConfigurationFileReferenceGUID
        self.impartedBuildProperties = ImpartedBuildProperties(model.impartedBuildProperties, pifLoader)
    }

    @_spi(Testing) public init(fromDictionary pifDict: ProjectModelItemPIF, withPIFLoader pifLoader: PIFLoader) throws {
        // name is required.
        name = try Self.parseValueForKeyAsString(PIFKey_name, pifDict: pifDict)

        // buildSettings is required.
        guard case let .plDict(settingsDict)? = pifDict[PIFKey_BuildConfiguration_buildSettings] else {
            throw PIFParsingError.missingRequiredKey(keyName: PIFKey_BuildConfiguration_buildSettings, objectType: Self.self)
        }

        // Create the macro assignment table.
        namespace = pifLoader.userNamespace
        buildSettingsData = settingsDict

        // baseConfigurationFileReference is optional.
        // The raw value here is the GUID of the file reference, but since that reference must be in the same project, and the project's group tree is loaded before any of the project's build configurations or targets, we can safely resolve the GUIDs to actual objects here.
        baseConfigurationFileReferenceGUID = try Self.parseOptionalValueForKeyAsString(PIFKey_BuildConfiguration_baseConfigurationFileReference, pifDict: pifDict)

        // The imparted build properties are optional.
        impartedBuildProperties = try Self.parseOptionalValueForKeyAsProjectModelItem(PIFKey_impartedBuildProperties, pifDict: pifDict, pifLoader: pifLoader, construct: { try ImpartedBuildProperties(fromDictionary: $0, withPIFLoader: pifLoader) }) ?? ImpartedBuildProperties(fromDictionary: [PIFKey_BuildConfiguration_buildSettings: .plDict([:])], withPIFLoader: pifLoader)
    }

    /// Create a macro value assignment table from a user provided dictionary of settings.
    //
    // FIXME: Figure out where this should really live, and how it should be factored. See also , e.g., MacroNamespace.parseTable.
    //
    // FIXME: We need to ensure that diagnostics from here are surfaced.
    static func createTableFromUserSettings(_ settings: [String: PropertyListItem], namespace: MacroNamespace) -> MacroValueAssignmentTable {
        // Iterate over the keys and values in the dictionary.
        var table = MacroValueAssignmentTable(namespace: namespace)
        for (key, value) in settings.sorted(byKey: <) {
            // Ask the MacroConfigFileParser to separate the macro name and any conditions.
            let (name, conditions) = MacroConfigFileParser.parseMacroNameAndConditionSet(key)

            // We might not get a macro name if the macro was malformed (for example in the case of environment variables).  We ignore such entries.
            guard let macroName = name else { continue }

            // If we did find a macro name, we look up the macro.
            let macro = namespace.lookupOrDeclareMacro(UserDefinedMacroDeclaration.self, macroName)

            // We also construct a condition set if we have conditions.
            var conditionSet: MacroConditionSet?
            if let conditions {
                conditionSet = MacroConditionSet(conditions: conditions.map{ MacroCondition(parameter: namespace.declareConditionParameter($0.0), valuePattern: $0.1) })
            }

            // Parse the value in a manner consistent with the macro definition.
            let expr: MacroExpression
            switch value {
            case .plString(let stringValue):
                expr = namespace.parseForMacro(macro, value: stringValue)
            case .plArray(let contents):
                let asStringList = contents.map{ item -> String in
                    guard case let .plString(string) = item else { fatalError("unexpected build configuration data") }
                    return string
                }
                expr = namespace.parseForMacro(macro, value: asStringList)
            default:
                fatalError("unexpected build configuration data")
            }

            table.push(macro, expr, conditions: conditionSet)
        }
        return table
    }
}