Skip to content

Commit 7b44829

Browse files
authored
Merge pull request #828 from owenv/owenv/conditional-api
Improve project model build settings API for platform-conditional settings
2 parents 221f009 + c5f547a commit 7b44829

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

Sources/SwiftBuild/ProjectModel/BuildSettings.swift

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ extension ProjectModel {
137137
case SWIFT_WARNINGS_AS_ERRORS_GROUPS
138138
}
139139

140+
@available(*, deprecated, message: "Use subscripts to set platform-specific SingleValueSetting/MultipleValueSettings instead")
140141
public enum Declaration: String, Hashable, CaseIterable, Sendable {
141142
case ARCHS
142143
case GCC_PREPROCESSOR_DEFINITIONS
@@ -186,8 +187,6 @@ extension ProjectModel {
186187
}
187188
}
188189

189-
public var platformSpecificSettings = [Platform: [Declaration: [String]]]()
190-
191190
public init() {
192191
var settings: [Declaration: [String]] = [:]
193192
for declaration in Declaration.allCases {
@@ -201,6 +200,13 @@ extension ProjectModel {
201200

202201
private(set) var singleValueSettings: OrderedDictionary<String, String> = [:]
203202
private(set) var multipleValueSettings: OrderedDictionary<String, [String]> = [:]
203+
private(set) var singleValuePlatformSpecificSettings = [Platform: OrderedDictionary<String, String>]()
204+
private(set) var multipleValuePlatformSpecificSettings = [Platform: OrderedDictionary<String, [String]>]()
205+
206+
// Kept for API compatibility
207+
@available(*, deprecated, message: "Use subscripts to set platform-specific settings instead")
208+
public var platformSpecificSettings = [Platform: [Declaration: [String]]]()
209+
204210

205211
public subscript(_ setting: SingleValueSetting) -> String? {
206212
get { singleValueSettings[setting.rawValue] }
@@ -221,6 +227,16 @@ extension ProjectModel {
221227
get { multipleValueSettings[setting] }
222228
set { multipleValueSettings[setting] = newValue }
223229
}
230+
231+
public subscript(_ setting: SingleValueSetting, platform: Platform) -> String? {
232+
get { singleValuePlatformSpecificSettings[platform]?[setting.rawValue] }
233+
set { singleValuePlatformSpecificSettings[platform, default: .init()][setting.rawValue] = newValue }
234+
}
235+
236+
public subscript(_ setting: MultipleValueSetting, platform: Platform) -> [String]? {
237+
get { multipleValuePlatformSpecificSettings[platform]?[setting.rawValue] }
238+
set { multipleValuePlatformSpecificSettings[platform, default: .init()][setting.rawValue] = newValue }
239+
}
224240
}
225241
}
226242

@@ -325,6 +341,23 @@ extension ProjectModel.BuildSettings: Codable {
325341
self.platformSpecificSettings[platform, default: [:]][declaration] = value
326342
}
327343
}
344+
let declarationValues = Set(Declaration.allCases.map(\.rawValue))
345+
for key in SingleValueSetting.allCases {
346+
if declarationValues.contains(key.rawValue) {
347+
continue
348+
}
349+
if let value = try container.decodeIfPresent(String.self, forKey: StringKey("\(key.rawValue)[\(condition)]")) {
350+
self[key, platform] = value
351+
}
352+
}
353+
for key in MultipleValueSetting.allCases {
354+
if declarationValues.contains(key.rawValue) {
355+
continue
356+
}
357+
if let value = try container.decodeIfPresent([String].self, forKey: StringKey("\(key.rawValue)[\(condition)]")) {
358+
self[key, platform] = value
359+
}
360+
}
328361
}
329362
}
330363
}
@@ -351,5 +384,21 @@ extension ProjectModel.BuildSettings: Codable {
351384
}
352385
}
353386
}
387+
388+
for (platform, table) in singleValuePlatformSpecificSettings {
389+
for condition in platform.asConditionStrings {
390+
for (key, value) in table {
391+
try container.encode(value, forKey: StringKey("\(key)[\(condition)]"))
392+
}
393+
}
394+
}
395+
396+
for (platform, table) in multipleValuePlatformSpecificSettings {
397+
for condition in platform.asConditionStrings {
398+
for (key, value) in table {
399+
try container.encode(value, forKey: StringKey("\(key)[\(condition)]"))
400+
}
401+
}
402+
}
354403
}
355404
}

Tests/SwiftBuildTests/ProjectModel/BuildSettingsTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ fileprivate struct BuildSettingsTests {
2525
try testCodable(obj) { $0[.BUILT_PRODUCTS_DIR] = "/tmp" }
2626
try testCodable(obj) { $0[.HEADER_SEARCH_PATHS] = ["/foo", "/bar"] }
2727
try testCodable(obj) { $0.platformSpecificSettings[.macOS, default: [:]][.FRAMEWORK_SEARCH_PATHS] = ["/baz", "/qux"] }
28+
try testCodable(obj) { $0[.CLANG_ENABLE_MODULES, .macOS] = "NO" }
29+
try testCodable(obj) { $0[.SWIFT_MODULE_ALIASES, .macOS] = ["A=B", "C=D"] }
2830
}
2931

3032
@Test func unknownBuildSettings() throws {

0 commit comments

Comments
 (0)