Skip to content

Commit 9e5f8b7

Browse files
authored
Remove unused Codable conformances on PackageModel types (#7687)
The conformances are unused and pull in an additional `TSCUtility.PolymorphicCodableProtocol` dependency. This change removes about 0.2-0.4 MB from final executables on macOS when comparing with `swift build -c release` to `main`. Also allows us to rename these types more easily to disambiguate between host/target triples and manifest targets/modules in our code. If there's an existing user of this JSON manifest serialization we're unaware of, they should rely on `swift package describe --type json` instead, which has stable, consistent, and tested output.
1 parent 7746e69 commit 9e5f8b7

13 files changed

+25
-357
lines changed

Sources/PackageModel/BuildConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// The configuration of the build environment.
14-
public enum BuildConfiguration: String, CaseIterable, Codable, Sendable {
14+
public enum BuildConfiguration: String, CaseIterable, Encodable, Sendable {
1515
case debug
1616
case release
1717

Sources/PackageModel/BuildEnvironment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// A build environment with which to evaluate conditions.
14-
public struct BuildEnvironment: Codable {
14+
public struct BuildEnvironment {
1515
public let platform: Platform
1616
public let configuration: BuildConfiguration?
1717

Sources/PackageModel/BuildSettings.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/// Namespace for build settings.
1414
public enum BuildSettings {
1515
/// Build settings declarations.
16-
public struct Declaration: Hashable, Codable {
16+
public struct Declaration: Hashable {
1717
// Swift.
1818
public static let SWIFT_ACTIVE_COMPILATION_CONDITIONS: Declaration =
1919
.init("SWIFT_ACTIVE_COMPILATION_CONDITIONS")
@@ -40,43 +40,31 @@ public enum BuildSettings {
4040
}
4141

4242
/// An individual build setting assignment.
43-
public struct Assignment: Codable, Equatable, Hashable {
43+
public struct Assignment: Equatable, Hashable {
4444
/// The assignment value.
4545
public var values: [String]
4646

47-
// FIXME: This should use `Set` but we need to investigate potential build failures on Linux caused by using it.
48-
/// The condition associated with this assignment.
49-
public var conditions: [PackageCondition] {
50-
get {
51-
self._conditions.map(\.underlying)
52-
}
53-
set {
54-
self._conditions = newValue.map { PackageConditionWrapper($0) }
55-
}
56-
}
57-
58-
private var _conditions: [PackageConditionWrapper]
47+
public var conditions: [PackageCondition]
5948

6049
/// Indicates whether this assignment represents a default
6150
/// that should be used only if no other assignments match.
6251
public let `default`: Bool
6352

6453
public init(default: Bool = false) {
65-
self._conditions = []
54+
self.conditions = []
6655
self.values = []
6756
self.default = `default`
6857
}
6958

7059
public init(values: [String] = [], conditions: [PackageCondition] = []) {
71-
self._conditions = []
7260
self.values = values
7361
self.default = false // TODO(franz): Check again
7462
self.conditions = conditions
7563
}
7664
}
7765

7866
/// Build setting assignment table which maps a build setting to a list of assignments.
79-
public struct AssignmentTable: Codable {
67+
public struct AssignmentTable {
8068
public private(set) var assignments: [Declaration: [Assignment]]
8169

8270
public init() {
@@ -93,7 +81,7 @@ public enum BuildSettings {
9381
/// Provides a view onto assignment table with a given set of bound parameters.
9482
///
9583
/// This class can be used to get the assignments matching the bound parameters.
96-
public final class Scope {
84+
public struct Scope {
9785
/// The assignment table.
9886
public let table: AssignmentTable
9987

Sources/PackageModel/Manifest/PackageConditionDescription.swift

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,64 +24,6 @@ public struct PackageConditionDescription: Codable, Hashable, Sendable {
2424
}
2525
}
2626

27-
/// A manifest condition.
28-
public protocol PackageConditionProtocol: Codable {
29-
func satisfies(_ environment: BuildEnvironment) -> Bool
30-
}
31-
32-
/// Wrapper for package condition so it can conform to Codable.
33-
struct PackageConditionWrapper: Codable, Equatable, Hashable {
34-
var platform: PlatformsCondition?
35-
var config: ConfigurationCondition?
36-
var traits: TraitCondition?
37-
38-
@available(*, deprecated, renamed: "underlying")
39-
var condition: PackageConditionProtocol {
40-
if let platform {
41-
return platform
42-
} else if let config {
43-
return config
44-
} else {
45-
fatalError("unreachable")
46-
}
47-
}
48-
49-
var underlying: PackageCondition {
50-
if let platform {
51-
return .platforms(platform)
52-
} else if let config {
53-
return .configuration(config)
54-
} else if let traits {
55-
return .traits(traits)
56-
} else {
57-
fatalError("unreachable")
58-
}
59-
}
60-
61-
@available(*, deprecated, message: "Use .init(_ condition: PackageCondition) instead")
62-
init(_ condition: PackageConditionProtocol) {
63-
switch condition {
64-
case let platform as PlatformsCondition:
65-
self.platform = platform
66-
case let config as ConfigurationCondition:
67-
self.config = config
68-
default:
69-
fatalError("unknown condition \(condition)")
70-
}
71-
}
72-
73-
init(_ condition: PackageCondition) {
74-
switch condition {
75-
case let .platforms(platforms):
76-
self.platform = platforms
77-
case let .configuration(configuration):
78-
self.config = configuration
79-
case .traits(let traits):
80-
self.traits = traits
81-
}
82-
}
83-
}
84-
8527
/// One of possible conditions used in package manifests to restrict targets from being built for certain platforms or
8628
/// build configurations.
8729
public enum PackageCondition: Hashable, Sendable {
@@ -134,7 +76,7 @@ public enum PackageCondition: Hashable, Sendable {
13476
}
13577

13678
/// Platforms condition implies that an assignment is valid on these platforms.
137-
public struct PlatformsCondition: PackageConditionProtocol, Hashable, Sendable {
79+
public struct PlatformsCondition: Hashable, Sendable {
13880
public let platforms: [Platform]
13981

14082
public init(platforms: [Platform]) {
@@ -149,7 +91,7 @@ public struct PlatformsCondition: PackageConditionProtocol, Hashable, Sendable {
14991

15092
/// A configuration condition implies that an assignment is valid on
15193
/// a particular build configuration.
152-
public struct ConfigurationCondition: PackageConditionProtocol, Hashable, Sendable {
94+
public struct ConfigurationCondition: Hashable, Sendable {
15395
public let configuration: BuildConfiguration
15496

15597
public init(configuration: BuildConfiguration) {
@@ -168,7 +110,7 @@ public struct ConfigurationCondition: PackageConditionProtocol, Hashable, Sendab
168110

169111
/// A configuration condition implies that an assignment is valid on
170112
/// a particular build configuration.
171-
public struct TraitCondition: PackageConditionProtocol, Hashable, Sendable {
113+
public struct TraitCondition: Hashable, Sendable {
172114
public let traits: Set<String>
173115

174116
public init(traits: Set<String>) {

Sources/PackageModel/PackageModel.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Basics
1414
import struct Foundation.URL
1515

1616
import enum TSCUtility.PackageLocation
17-
import struct TSCUtility.PolymorphicCodableArray
1817
import struct TSCUtility.Version
1918

2019
/// The basic package representation.
@@ -45,7 +44,7 @@ import struct TSCUtility.Version
4544
/// 5. A loaded package, as in #4, for which the targets have also been
4645
/// loaded. There is not currently a data structure for this, but it is the
4746
/// result after `PackageLoading.transmute()`.
48-
public final class Package: Encodable {
47+
public final class Package {
4948
/// The identity of the package.
5049
public let identity: PackageIdentity
5150

@@ -56,7 +55,6 @@ public final class Package: Encodable {
5655
public let path: AbsolutePath
5756

5857
/// The targets contained in the package.
59-
@PolymorphicCodableArray
6058
public var targets: [Target]
6159

6260
/// The products produced by the package.
@@ -84,7 +82,7 @@ public final class Package: Encodable {
8482
self.identity = identity
8583
self.manifest = manifest
8684
self.path = path
87-
self._targets = .init(wrappedValue: targets)
85+
self.targets = targets
8886
self.products = products
8987
self.targetSearchPath = targetSearchPath
9088
self.testTargetSearchPath = testTargetSearchPath

Sources/PackageModel/Product.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
import Basics
1414

15-
import struct TSCUtility.PolymorphicCodableArray
16-
17-
public class Product: Codable {
15+
public class Product {
1816
/// The name of the product.
1917
public let name: String
2018

@@ -28,7 +26,6 @@ public class Product: Codable {
2826
///
2927
/// This is never empty, and is only the targets which are required to be in
3028
/// the product, but not necessarily their transitive dependencies.
31-
@PolymorphicCodableArray
3229
public var targets: [Target]
3330

3431
/// The path to test entry point file.
@@ -54,7 +51,7 @@ public class Product: Codable {
5451
self.name = name
5552
self.type = type
5653
self.identity = package.description.lowercased() + "_" + name
57-
self._targets = .init(wrappedValue: targets)
54+
self.targets = targets
5855
self.testEntryPointPath = testEntryPointPath
5956
}
6057
}

Sources/PackageModel/Target/BinaryTarget.swift

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -47,36 +47,7 @@ public final class BinaryTarget: Target {
4747
)
4848
}
4949

50-
private enum CodingKeys: String, CodingKey {
51-
case kind
52-
case origin
53-
case artifactSource // backwards compatibility 2/2021
54-
}
55-
56-
public override func encode(to encoder: Encoder) throws {
57-
var container = encoder.container(keyedBy: CodingKeys.self)
58-
try container.encode(self.origin, forKey: .origin)
59-
try container.encode(self.kind, forKey: .kind)
60-
}
61-
62-
required public init(from decoder: Decoder) throws {
63-
let container = try decoder.container(keyedBy: CodingKeys.self)
64-
// backwards compatibility 2/2021
65-
if !container.contains(.kind) {
66-
self.kind = .xcframework
67-
} else {
68-
self.kind = try container.decode(Kind.self, forKey: .kind)
69-
}
70-
// backwards compatibility 2/2021
71-
if container.contains(.artifactSource) {
72-
self.origin = try container.decode(Origin.self, forKey: .artifactSource)
73-
} else {
74-
self.origin = try container.decode(Origin.self, forKey: .origin)
75-
}
76-
try super.init(from: decoder)
77-
}
78-
79-
public enum Kind: String, RawRepresentable, Codable, CaseIterable {
50+
public enum Kind: String, RawRepresentable, CaseIterable {
8051
case xcframework
8152
case artifactsArchive
8253
case unknown // for non-downloaded artifacts
@@ -98,42 +69,12 @@ public final class BinaryTarget: Target {
9869
return self.kind == .artifactsArchive
9970
}
10071

101-
public enum Origin: Equatable, Codable {
72+
public enum Origin: Equatable {
10273

10374
/// Represents an artifact that was downloaded from a remote URL.
10475
case remote(url: String)
10576

10677
/// Represents an artifact that was available locally.
10778
case local
108-
109-
private enum CodingKeys: String, CodingKey {
110-
case remote, local
111-
}
112-
113-
public func encode(to encoder: Encoder) throws {
114-
var container = encoder.container(keyedBy: CodingKeys.self)
115-
switch self {
116-
case .remote(let a1):
117-
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .remote)
118-
try unkeyedContainer.encode(a1)
119-
case .local:
120-
try container.encodeNil(forKey: .local)
121-
}
122-
}
123-
124-
public init(from decoder: Decoder) throws {
125-
let values = try decoder.container(keyedBy: CodingKeys.self)
126-
guard let key = values.allKeys.first(where: values.contains) else {
127-
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Did not find a matching key"))
128-
}
129-
switch key {
130-
case .remote:
131-
var unkeyedValues = try values.nestedUnkeyedContainer(forKey: key)
132-
let a1 = try unkeyedValues.decode(String.self)
133-
self = .remote(url: a1)
134-
case .local:
135-
self = .local
136-
}
137-
}
13879
}
13980
}

Sources/PackageModel/Target/ClangTarget.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,30 +82,4 @@ public final class ClangTarget: Target {
8282
usesUnsafeFlags: usesUnsafeFlags
8383
)
8484
}
85-
86-
private enum CodingKeys: String, CodingKey {
87-
case includeDir, moduleMapType, headers, isCXX, cLanguageStandard, cxxLanguageStandard
88-
}
89-
90-
public override func encode(to encoder: Encoder) throws {
91-
var container = encoder.container(keyedBy: CodingKeys.self)
92-
try container.encode(includeDir, forKey: .includeDir)
93-
try container.encode(moduleMapType, forKey: .moduleMapType)
94-
try container.encode(headers, forKey: .headers)
95-
try container.encode(isCXX, forKey: .isCXX)
96-
try container.encode(cLanguageStandard, forKey: .cLanguageStandard)
97-
try container.encode(cxxLanguageStandard, forKey: .cxxLanguageStandard)
98-
try super.encode(to: encoder)
99-
}
100-
101-
required public init(from decoder: Decoder) throws {
102-
let container = try decoder.container(keyedBy: CodingKeys.self)
103-
self.includeDir = try container.decode(AbsolutePath.self, forKey: .includeDir)
104-
self.moduleMapType = try container.decode(ModuleMapType.self, forKey: .moduleMapType)
105-
self.headers = try container.decode([AbsolutePath].self, forKey: .headers)
106-
self.isCXX = try container.decode(Bool.self, forKey: .isCXX)
107-
self.cLanguageStandard = try container.decodeIfPresent(String.self, forKey: .cLanguageStandard)
108-
self.cxxLanguageStandard = try container.decodeIfPresent(String.self, forKey: .cxxLanguageStandard)
109-
try super.init(from: decoder)
110-
}
11185
}

0 commit comments

Comments
 (0)