12
12
13
13
/// Namespace for build settings.
14
14
public enum BuildSettings {
15
-
16
15
/// Build settings declarations.
17
16
public struct Declaration : Hashable , Codable {
18
17
// 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 " )
20
20
public static let OTHER_SWIFT_FLAGS : Declaration = . init( " OTHER_SWIFT_FLAGS " )
21
21
public static let SWIFT_VERSION : Declaration = . init( " SWIFT_VERSION " )
22
22
@@ -48,18 +48,23 @@ public enum BuildSettings {
48
48
/// The condition associated with this assignment.
49
49
public var conditions : [ PackageCondition ] {
50
50
get {
51
- return _conditions. map { $0 . underlying }
51
+ self . _conditions. map ( \ . underlying)
52
52
}
53
53
set {
54
- _conditions = newValue. map { PackageConditionWrapper ( $0) }
54
+ self . _conditions = newValue. map { PackageConditionWrapper ( $0) }
55
55
}
56
56
}
57
57
58
58
private var _conditions : [ PackageConditionWrapper ]
59
59
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 ) {
61
65
self . _conditions = [ ]
62
66
self . values = [ ]
67
+ self . default = `default`
63
68
}
64
69
}
65
70
@@ -68,13 +73,13 @@ public enum BuildSettings {
68
73
public private( set) var assignments : [ Declaration : [ Assignment ] ]
69
74
70
75
public init ( ) {
71
- assignments = [ : ]
76
+ self . assignments = [ : ]
72
77
}
73
78
74
79
/// 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 ) {
76
81
// FIXME: We should check for duplicate assignments.
77
- assignments [ decl, default: [ ] ] . append ( assignment)
82
+ self . assignments [ decl, default: [ ] ] . append ( assignment)
78
83
}
79
84
}
80
85
@@ -101,12 +106,18 @@ public enum BuildSettings {
101
106
}
102
107
103
108
// Add values from each assignment if it satisfies the build environment.
104
- let values = assignments
109
+ let allViableAssignments = assignments
105
110
. lazy
106
111
. filter { $0. conditions. allSatisfy { $0. satisfies ( self . environment) } }
107
- . flatMap { $0. values }
108
112
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)
110
121
}
111
122
}
112
123
}
0 commit comments