-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathCalculateXcodeConfigurationBuildSettings.swift
More file actions
246 lines (214 loc) · 8.33 KB
/
CalculateXcodeConfigurationBuildSettings.swift
File metadata and controls
246 lines (214 loc) · 8.33 KB
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
import PBXProj
import ToolCommon
extension Generator {
struct CalculateXcodeConfigurationBuildSettings {
private let callable: Callable
/// - Parameters:
/// - callable: The function that will be called in
/// `callAsFunction()`.
init(callable: @escaping Callable = Self.defaultCallable) {
self.callable = callable
}
/// Calculates the build settings for one of the target's Xcode
/// configurations.
func callAsFunction(
platformBuildSettings: [PlatformBuildSettings],
allConditionalFiles: Set<BazelPath>
) -> [BuildSetting] {
return callable(
/*platformBuildSettings:*/ platformBuildSettings,
/*allConditionalFiles:*/ allConditionalFiles
)
}
}
}
// MARK: - CalculateXcodeConfigurationBuildSettings.Callable
extension Generator.CalculateXcodeConfigurationBuildSettings {
typealias Callable = (
_ platformBuildSettings: [PlatformBuildSettings],
_ allConditionalFiles: Set<BazelPath>
) -> [BuildSetting]
static func defaultCallable(
platformBuildSettings: [PlatformBuildSettings],
allConditionalFiles: Set<BazelPath>
) -> [BuildSetting] {
var buildSettings: [BuildSetting] = []
var conditionalFiles: Set<BazelPath> = []
var excludedSourceFileNames: [String] = []
var platformedBuildSettings: [String: [(Platform, String)]] = [:]
var platformConditionalFiles: [(String, Set<BazelPath>)] = []
for platformBuildSettings in platformBuildSettings {
let platform = platformBuildSettings.platform
// Collect all build settings by key
for buildSetting in platformBuildSettings.buildSettings {
platformedBuildSettings[buildSetting.key, default: []]
.append((platform, buildSetting.value))
}
// Set per-platform conditional files
if !platformBuildSettings.conditionalFiles.isEmpty {
let key = """
\(platformBuildSettings.platform.rawValue.uppercased())_FILES
"""
conditionalFiles
.formUnion(platformBuildSettings.conditionalFiles)
platformConditionalFiles.append(
(key, platformBuildSettings.conditionalFiles)
)
buildSettings.append(
.init(
key: key,
value:
// Lots of code just to quote and `.pbxProjEscaped`
// the paths
platformBuildSettings.conditionalFiles
.map { $0.path.quoteIfNeeded }
// TODO: See if we can not sort, or sort earlier
.sorted()
.joined(separator: " ")
.pbxProjEscaped
)
)
let (
includeKey,
pbxProjEscapedIncludeKey
) = platform.conditionalizedBuildSettingKey(
"INCLUDED_SOURCE_FILE_NAMES"
)
buildSettings.append(
.init(
key: includeKey,
pbxProjEscapedKey: pbxProjEscapedIncludeKey,
value: #""$(\#(key))""#
)
)
excludedSourceFileNames.append(#""$(\#(key))""#)
}
}
// Exclude other configuration conditional files as well
excludedSourceFileNames.append(
contentsOf: allConditionalFiles
.subtracting(conditionalFiles)
.map { $0.path.pbxProjEscaped }
)
// Set configuration-wide conditional files
if !excludedSourceFileNames.isEmpty {
buildSettings.append(
.init(
key: "EXCLUDED_SOURCE_FILE_NAMES",
// This will always have 2 or more items, so format as an
// array
value: #"""
(
\#(
// TODO: See if we can not sort, or sort earlier
excludedSourceFileNames.sorted().joined(separator: ",\n\t\t\t\t\t")
),
)
"""#
)
)
buildSettings.append(
.init(
key: "INCLUDED_SOURCE_FILE_NAMES",
value: #""""#
)
)
}
let allPlatforms = Set(platformBuildSettings.map(\.platform))
let basePlatform = platformBuildSettings.first!.platform
for (key, platformAndValues) in platformedBuildSettings {
let isNonInheritableKey = nonInheritableKeys.contains(key)
var remainingPlatforms: Set<Platform>
let setBaseValue: Bool
if isNonInheritableKey {
remainingPlatforms = []
setBaseValue = true
} else {
remainingPlatforms = allPlatforms
// We only set a base value if none of the platforms want to
// inherit defaults (since they would inherit the base value
// instead of the default)
setBaseValue = remainingPlatforms
.subtracting(platformAndValues.map(\.0)).isEmpty
}
var remainingPlatformAndValues = ArraySlice(platformAndValues)
let baseValue: String?
if setBaseValue {
let (_, firstValue) = remainingPlatformAndValues.popFirst()!
baseValue = firstValue
// Set the base value to the first platform, which will be
// previously sorted, resulting in the most preferable default
buildSettings.append(.init(key: key, value: firstValue))
remainingPlatforms.remove(basePlatform)
} else {
baseValue = nil
// Not setting a base value will cause it to inherit defaults.
// We remove all `remainingPlatforms` because we don't need to
// explicitly set them to inherit defaults either.
remainingPlatforms.removeAll()
}
for (platform, value) in remainingPlatformAndValues {
remainingPlatforms.remove(platform)
guard value != baseValue else {
// Don't set redundant settings
continue
}
let (
conditionalizedKey,
pbxProjEscapedConditionalizedKey
) = platform.conditionalizedBuildSettingKey(key)
buildSettings.append(
.init(
key: conditionalizedKey,
pbxProjEscapedKey: pbxProjEscapedConditionalizedKey,
value: value
)
)
}
for platform in remainingPlatforms {
let (
conditionalizedKey,
pbxProjEscapedConditionalizedKey
) = platform.conditionalizedBuildSettingKey(key)
buildSettings.append(
.init(
key: conditionalizedKey,
pbxProjEscapedKey: pbxProjEscapedConditionalizedKey,
value: #""$(inherited)""#
)
)
}
}
return buildSettings
}
}
struct PlatformBuildSettings: Equatable {
let platform: Platform
let conditionalFiles: Set<BazelPath>
let buildSettings: [PlatformVariantBuildSetting]
}
private let nonInheritableKeys: Set<String> = [
"IPHONEOS_DEPLOYMENT_TARGET",
"MACOSX_DEPLOYMENT_TARGET",
"TVOS_DEPLOYMENT_TARGET",
"WATCHOS_DEPLOYMENT_TARGET",
]
private extension Platform {
func conditionalizedBuildSettingKey(
_ key: String
) -> (key: String, pbxProjEscapedKey: String) {
let conditionalizedKey = #"\#(key)[sdk=\#(rawValue)*]"#
return (
key: conditionalizedKey,
pbxProjEscapedKey: #""\#(conditionalizedKey)""#
)
}
}
extension String {
var quoteIfNeeded: String {
guard !contains(" ") else {
return #""\#(self)""#
}
return self
}
}