@@ -73,6 +73,12 @@ struct BuildCommandOptions: ParsableArguments {
73
73
@Flag ( help: " Build both source and test targets " )
74
74
var buildTests : Bool = false
75
75
76
+ /// Whether to enable code coverage.
77
+ @Flag ( name: . customLong( " code-coverage " ) ,
78
+ inversion: . prefixedEnableDisable,
79
+ help: " Enable code coverage " )
80
+ var enableCodeCoverage : Bool = false
81
+
76
82
/// If the binary output path should be printed.
77
83
@Flag ( name: . customLong( " show-bin-path " ) , help: " Print the binary output path " )
78
84
var shouldPrintBinPath : Bool = false
@@ -148,6 +154,18 @@ package struct SwiftBuildCommand: AsyncSwiftCommand {
148
154
guard let subset = options. buildSubset ( observabilityScope: swiftCommandState. observabilityScope) else {
149
155
throw ExitCode . failure
150
156
}
157
+
158
+ var productsBuildParameters = try swiftCommandState. productsBuildParameters
159
+ var toolsBuildParameters = try swiftCommandState. toolsBuildParameters
160
+
161
+ // Clean out the code coverage directory that may contain stale
162
+ // profraw files from a previous run of the code coverage tool.
163
+ if self . options. enableCodeCoverage {
164
+ try swiftCommandState. fileSystem. removeFileTree ( swiftCommandState. productsBuildParameters. codeCovPath)
165
+ productsBuildParameters. testingParameters. enableCodeCoverage = true
166
+ toolsBuildParameters. testingParameters. enableCodeCoverage = true
167
+ }
168
+
151
169
if case . allIncludingTests = subset {
152
170
func updateTestingParameters( of buildParameters: inout BuildParameters , library: BuildParameters . Testing . Library ) {
153
171
buildParameters. testingParameters = . init(
@@ -161,23 +179,21 @@ package struct SwiftBuildCommand: AsyncSwiftCommand {
161
179
library: library
162
180
)
163
181
}
164
- var productsBuildParameters = try swiftCommandState. productsBuildParameters
165
- var toolsBuildParameters = try swiftCommandState. toolsBuildParameters
166
182
for library in try options. testLibraryOptions. enabledTestingLibraries ( swiftCommandState: swiftCommandState) {
167
183
updateTestingParameters ( of: & productsBuildParameters, library: library)
168
184
updateTestingParameters ( of: & toolsBuildParameters, library: library)
169
185
try build ( swiftCommandState, subset: subset, productsBuildParameters: productsBuildParameters, toolsBuildParameters: toolsBuildParameters)
170
186
}
171
187
} else {
172
- try build ( swiftCommandState, subset: subset, productsBuildParameters: nil , toolsBuildParameters: nil )
188
+ try build ( swiftCommandState, subset: subset, productsBuildParameters: productsBuildParameters , toolsBuildParameters: toolsBuildParameters )
173
189
}
174
190
}
175
191
176
192
private func build(
177
193
_ swiftCommandState: SwiftCommandState ,
178
194
subset: BuildSubset ,
179
- productsBuildParameters: BuildParameters ? ,
180
- toolsBuildParameters: BuildParameters ?
195
+ productsBuildParameters: BuildParameters ,
196
+ toolsBuildParameters: BuildParameters
181
197
) throws {
182
198
let buildSystem = try swiftCommandState. createBuildSystem (
183
199
explicitProduct: options. product,
0 commit comments