Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions Sources/SwiftBuild/ProjectModel/BuildSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ extension ProjectModel {
case SWIFT_WARNINGS_AS_ERRORS_GROUPS
}

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

public var platformSpecificSettings = [Platform: [Declaration: [String]]]()

public init() {
var settings: [Declaration: [String]] = [:]
for declaration in Declaration.allCases {
Expand All @@ -201,6 +200,13 @@ extension ProjectModel {

private(set) var singleValueSettings: OrderedDictionary<String, String> = [:]
private(set) var multipleValueSettings: OrderedDictionary<String, [String]> = [:]
private(set) var singleValuePlatformSpecificSettings = [Platform: OrderedDictionary<String, String>]()
private(set) var multipleValuePlatformSpecificSettings = [Platform: OrderedDictionary<String, [String]>]()

// Kept for API compatibility
@available(*, deprecated, message: "Use subscripts to set platform-specific settings instead")
public var platformSpecificSettings = [Platform: [Declaration: [String]]]()


public subscript(_ setting: SingleValueSetting) -> String? {
get { singleValueSettings[setting.rawValue] }
Expand All @@ -221,6 +227,16 @@ extension ProjectModel {
get { multipleValueSettings[setting] }
set { multipleValueSettings[setting] = newValue }
}

public subscript(_ setting: SingleValueSetting, platform: Platform) -> String? {
get { singleValuePlatformSpecificSettings[platform]?[setting.rawValue] }
set { singleValuePlatformSpecificSettings[platform, default: .init()][setting.rawValue] = newValue }
}

public subscript(_ setting: MultipleValueSetting, platform: Platform) -> [String]? {
get { multipleValuePlatformSpecificSettings[platform]?[setting.rawValue] }
set { multipleValuePlatformSpecificSettings[platform, default: .init()][setting.rawValue] = newValue }
}
}
}

Expand Down Expand Up @@ -325,6 +341,23 @@ extension ProjectModel.BuildSettings: Codable {
self.platformSpecificSettings[platform, default: [:]][declaration] = value
}
}
let declarationValues = Set(Declaration.allCases.map(\.rawValue))
for key in SingleValueSetting.allCases {
if declarationValues.contains(key.rawValue) {
continue
}
if let value = try container.decodeIfPresent(String.self, forKey: StringKey("\(key.rawValue)[\(condition)]")) {
self[key, platform] = value
}
}
for key in MultipleValueSetting.allCases {
if declarationValues.contains(key.rawValue) {
continue
}
if let value = try container.decodeIfPresent([String].self, forKey: StringKey("\(key.rawValue)[\(condition)]")) {
self[key, platform] = value
}
}
}
}
}
Expand All @@ -351,5 +384,21 @@ extension ProjectModel.BuildSettings: Codable {
}
}
}

for (platform, table) in singleValuePlatformSpecificSettings {
for condition in platform.asConditionStrings {
for (key, value) in table {
try container.encode(value, forKey: StringKey("\(key)[\(condition)]"))
}
}
}

for (platform, table) in multipleValuePlatformSpecificSettings {
for condition in platform.asConditionStrings {
for (key, value) in table {
try container.encode(value, forKey: StringKey("\(key)[\(condition)]"))
}
}
}
}
}
2 changes: 2 additions & 0 deletions Tests/SwiftBuildTests/ProjectModel/BuildSettingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ fileprivate struct BuildSettingsTests {
try testCodable(obj) { $0[.BUILT_PRODUCTS_DIR] = "/tmp" }
try testCodable(obj) { $0[.HEADER_SEARCH_PATHS] = ["/foo", "/bar"] }
try testCodable(obj) { $0.platformSpecificSettings[.macOS, default: [:]][.FRAMEWORK_SEARCH_PATHS] = ["/baz", "/qux"] }
try testCodable(obj) { $0[.CLANG_ENABLE_MODULES, .macOS] = "NO" }
try testCodable(obj) { $0[.SWIFT_MODULE_ALIASES, .macOS] = ["A=B", "C=D"] }
}

@Test func unknownBuildSettings() throws {
Expand Down
Loading