Skip to content

Commit eb3abcd

Browse files
committed
[PackageModel] Add a way to specify a default setting assignment
This setting is going to be used if there are no non-default assignments that match build environment conditions.
1 parent a2e0e71 commit eb3abcd

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Sources/PackageModel/BuildSettings.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
/// Namespace for build settings.
1414
public enum BuildSettings {
15-
1615
/// Build settings declarations.
1716
public struct Declaration: Hashable, Codable {
1817
// Swift.
19-
public static let SWIFT_ACTIVE_COMPILATION_CONDITIONS: Declaration = .init("SWIFT_ACTIVE_COMPILATION_CONDITIONS")
18+
public static let SWIFT_ACTIVE_COMPILATION_CONDITIONS: Declaration =
19+
.init("SWIFT_ACTIVE_COMPILATION_CONDITIONS")
2020
public static let OTHER_SWIFT_FLAGS: Declaration = .init("OTHER_SWIFT_FLAGS")
2121
public static let SWIFT_VERSION: Declaration = .init("SWIFT_VERSION")
2222

@@ -48,18 +48,23 @@ public enum BuildSettings {
4848
/// The condition associated with this assignment.
4949
public var conditions: [PackageCondition] {
5050
get {
51-
return _conditions.map { $0.underlying }
51+
self._conditions.map(\.underlying)
5252
}
5353
set {
54-
_conditions = newValue.map { PackageConditionWrapper($0) }
54+
self._conditions = newValue.map { PackageConditionWrapper($0) }
5555
}
5656
}
5757

5858
private var _conditions: [PackageConditionWrapper]
5959

60-
public init() {
60+
/// Indicates whether this assignment represents a default
61+
/// that should be used only if no other assignments match.
62+
public let `default`: Bool
63+
64+
public init(default: Bool = false) {
6165
self._conditions = []
6266
self.values = []
67+
self.default = `default`
6368
}
6469
}
6570

@@ -68,13 +73,13 @@ public enum BuildSettings {
6873
public private(set) var assignments: [Declaration: [Assignment]]
6974

7075
public init() {
71-
assignments = [:]
76+
self.assignments = [:]
7277
}
7378

7479
/// Add the given assignment to the table.
75-
mutating public func add(_ assignment: Assignment, for decl: Declaration) {
80+
public mutating func add(_ assignment: Assignment, for decl: Declaration) {
7681
// FIXME: We should check for duplicate assignments.
77-
assignments[decl, default: []].append(assignment)
82+
self.assignments[decl, default: []].append(assignment)
7883
}
7984
}
8085

@@ -101,12 +106,18 @@ public enum BuildSettings {
101106
}
102107

103108
// Add values from each assignment if it satisfies the build environment.
104-
let values = assignments
109+
let allViableAssignments = assignments
105110
.lazy
106111
.filter { $0.conditions.allSatisfy { $0.satisfies(self.environment) } }
107-
.flatMap { $0.values }
108112

109-
return Array(values)
113+
let nonDefaultAssignments = allViableAssignments.filter { !$0.default }
114+
115+
// If there are no non-default assignments, let's fallback to defaults.
116+
if nonDefaultAssignments.isEmpty {
117+
return allViableAssignments.filter(\.default).flatMap(\.values)
118+
}
119+
120+
return nonDefaultAssignments.flatMap(\.values)
110121
}
111122
}
112123
}

0 commit comments

Comments
 (0)