Skip to content

Commit 696e9d7

Browse files
committed
Build: centralise the debug information flag handling
Rather than spreading out the flag addition across the various different targets, we can control the flags at the `BuildParameters` level, driving the necessary flag addition at a single site. This would allow easier management of the flags as we can see the additions uniformly applied. This shuffles the previously introduced functionality from `SwiftTool` and sinks it into `BuildParameters`. This enables the application of the flags without having to go through the tooling, i.e. when SPM is used as a library. The test changes alter the expectations for ordering and additional flags being applied for various compilation cases.
1 parent b7116db commit 696e9d7

File tree

8 files changed

+182
-114
lines changed

8 files changed

+182
-114
lines changed

Sources/Build/BuildDescription/ClangTargetBuildDescription.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ public final class ClangTargetBuildDescription {
220220
args += ["-fobjc-arc"]
221221
}
222222
args += try buildParameters.targetTripleArgs(for: target)
223-
args += ["-g"]
224-
if buildParameters.triple.isWindows() {
225-
args += ["-gcodeview"]
226-
}
223+
227224
args += optimizationArguments
228225
args += activeCompilationConditions
229226
args += ["-fblocks"]

Sources/Build/BuildDescription/ProductBuildDescription.swift

-9
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,6 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
158158
args += ["-v"]
159159
}
160160

161-
// Pass `-g` during a *release* build so the Swift driver emits a dSYM file for the binary.
162-
if self.buildParameters.configuration == .release {
163-
if self.buildParameters.triple.isWindows() {
164-
args += ["-Xlinker", "-debug"]
165-
} else {
166-
args += ["-g"]
167-
}
168-
}
169-
170161
// Only add the build path to the framework search path if there are binary frameworks to link against.
171162
if !self.libraryBinaryPaths.isEmpty {
172163
args += ["-F", self.buildParameters.buildPath.pathString]

Sources/Build/BuildDescription/SwiftTargetBuildDescription.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ public final class SwiftTargetBuildDescription {
441441
args += self.buildParameters.indexStoreArguments(for: self.target)
442442
args += self.optimizationArguments
443443
args += self.testingArguments
444-
args += ["-g"]
444+
445445
args += ["-j\(self.buildParameters.workers)"]
446446
args += self.activeCompilationConditions
447447
args += self.additionalFlags
@@ -627,7 +627,7 @@ public final class SwiftTargetBuildDescription {
627627
result += ["-swift-version", self.swiftVersion.rawValue]
628628
result += self.optimizationArguments
629629
result += self.testingArguments
630-
result += ["-g"]
630+
631631
result += ["-j\(self.buildParameters.workers)"]
632632
result += self.activeCompilationConditions
633633
result += self.additionalFlags
@@ -686,7 +686,7 @@ public final class SwiftTargetBuildDescription {
686686
result += self.buildParameters.indexStoreArguments(for: self.target)
687687
result += self.optimizationArguments
688688
result += self.testingArguments
689-
result += ["-g"]
689+
690690
result += ["-j\(self.buildParameters.workers)"]
691691
result += self.activeCompilationConditions
692692
result += self.additionalFlags

Sources/CoreCommands/Options.swift

+3
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,11 @@ public struct BuildOptions: ParsableArguments {
504504
case thin
505505
}
506506

507+
/// See `BuildParameters.DebugInfoFormat` for details.
507508
public enum DebugInfoFormat: String, Codable, ExpressibleByArgument {
509+
/// See `BuildParameters.DebugInfoFormat.dwarf` for details.
508510
case dwarf
511+
/// See `BuildParameters.DebugInfoFormat.codeview` for details.
509512
case codeview
510513
}
511514
}

Sources/CoreCommands/SwiftTool.swift

+14-23
Original file line numberDiff line numberDiff line change
@@ -678,33 +678,12 @@ public final class SwiftTool {
678678
component: destinationTriple.platformBuildPathComponent(buildSystem: options.build.buildSystem)
679679
)
680680

681-
var flags: BuildFlags = options.build.buildFlags
682-
if destinationTriple.isWindows() {
683-
switch options.build.debugInfoFormat {
684-
case .dwarf:
685-
// DWARF requires lld as link.exe expects CodeView debug info.
686-
flags = flags.merging(BuildFlags(
687-
cCompilerFlags: ["-gdwarf"],
688-
cxxCompilerFlags: ["-gdwarf"],
689-
swiftCompilerFlags: ["-g", "-use-ld=lld"],
690-
linkerFlags: ["-debug:dwarf"]
691-
))
692-
case .codeview:
693-
flags = flags.merging(BuildFlags(
694-
swiftCompilerFlags: ["-g", "-debug-info-format=codeview"],
695-
linkerFlags: ["-debug"]
696-
))
697-
}
698-
} else if options.build.debugInfoFormat == .codeview {
699-
observabilityScope.emit(error: "CodeView debug information is currently not supported for this platform")
700-
}
701-
702681
return try BuildParameters(
703682
dataPath: dataPath,
704683
configuration: options.build.configuration,
705684
toolchain: destinationToolchain,
706685
destinationTriple: destinationTriple,
707-
flags: flags,
686+
flags: options.build.buildFlags,
708687
pkgConfigDirectories: options.locations.pkgConfigDirectories,
709688
architectures: options.build.architectures,
710689
workers: options.build.jobs ?? UInt32(ProcessInfo.processInfo.activeProcessorCount),
@@ -725,7 +704,8 @@ public final class SwiftTool {
725704
explicitTargetDependencyImportCheckingMode: options.build.explicitTargetDependencyImportCheck.modeParameter,
726705
linkerDeadStrip: options.linker.linkerDeadStrip,
727706
verboseOutput: self.logLevel <= .info,
728-
linkTimeOptimizationMode: options.build.linkTimeOptimizationMode?.buildParameter
707+
linkTimeOptimizationMode: options.build.linkTimeOptimizationMode?.buildParameter,
708+
debugInfoFormat: options.build.debugInfoFormat.buildParameter
729709
)
730710
})
731711
}()
@@ -999,6 +979,17 @@ extension BuildOptions.LinkTimeOptimizationMode {
999979
}
1000980
}
1001981

982+
extension BuildOptions.DebugInfoFormat {
983+
fileprivate var buildParameter: BuildParameters.DebugInfoFormat {
984+
switch self {
985+
case .dwarf:
986+
return .dwarf
987+
case .codeview:
988+
return .codeview
989+
}
990+
}
991+
}
992+
1002993
extension Basics.Diagnostic {
1003994
public static func mutuallyExclusiveArgumentsError(arguments: [String]) -> Self {
1004995
.error(arguments.map { "'\($0)'" }.spm_localizedJoin(type: .conjunction) + " are mutually exclusive")

Sources/SPMBuildCore/BuildParameters.swift

+39-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ public struct BuildParameters: Encodable {
5252
case thin
5353
}
5454

55+
/// Represents the debug information format.
56+
///
57+
/// The debug information format controls the format of the debug information
58+
/// that the compiler generates. Some platforms support debug information
59+
// formats other than DWARF.
60+
public enum DebugInfoFormat: String, Encodable {
61+
/// DWARF debug information format, the default format used by Swift.
62+
case dwarf
63+
/// CodeView debug information format, used on Windows.
64+
case codeview
65+
}
66+
5567
/// Represents the debugging strategy.
5668
///
5769
/// Swift binaries requires the swiftmodule files in order for lldb to work.
@@ -247,6 +259,8 @@ public struct BuildParameters: Encodable {
247259

248260
public var linkTimeOptimizationMode: LinkTimeOptimizationMode?
249261

262+
public var debugInfoFormat: DebugInfoFormat
263+
250264
public init(
251265
dataPath: AbsolutePath,
252266
configuration: BuildConfiguration,
@@ -276,7 +290,8 @@ public struct BuildParameters: Encodable {
276290
linkerDeadStrip: Bool = true,
277291
colorizedOutput: Bool = false,
278292
verboseOutput: Bool = false,
279-
linkTimeOptimizationMode: LinkTimeOptimizationMode? = nil
293+
linkTimeOptimizationMode: LinkTimeOptimizationMode? = nil,
294+
debugInfoFormat: DebugInfoFormat = .dwarf
280295
) throws {
281296
let triple = try destinationTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
282297

@@ -285,7 +300,28 @@ public struct BuildParameters: Encodable {
285300
self._toolchain = _Toolchain(toolchain: toolchain)
286301
self.hostTriple = try hostTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
287302
self.triple = triple
288-
self.flags = flags
303+
switch debugInfoFormat {
304+
case .dwarf:
305+
var flags = flags
306+
// DWARF requires lld as link.exe expects CodeView debug info.
307+
self.flags = flags.merging(triple.isWindows() ? BuildFlags(
308+
cCompilerFlags: ["-gdwarf"],
309+
cxxCompilerFlags: ["-gdwarf"],
310+
swiftCompilerFlags: ["-g", "-use-ld=lld"],
311+
linkerFlags: ["-debug:dwarf"]
312+
) : BuildFlags(cCompilerFlags: ["-g"], cxxCompilerFlags: ["-g"], swiftCompilerFlags: ["-g"]))
313+
case .codeview:
314+
if !triple.isWindows() {
315+
throw StringError("CodeView debug information is currently not supported on \(triple.osName)")
316+
}
317+
var flags = flags
318+
self.flags = flags.merging(BuildFlags(
319+
cCompilerFlags: ["-g"],
320+
cxxCompilerFlags: ["-g"],
321+
swiftCompilerFlags: ["-g", "-debug-info-format=codeview"],
322+
linkerFlags: ["-debug"]
323+
))
324+
}
289325
self.pkgConfigDirectories = pkgConfigDirectories
290326
self.architectures = architectures
291327
self.workers = workers
@@ -318,6 +354,7 @@ public struct BuildParameters: Encodable {
318354
self.colorizedOutput = colorizedOutput
319355
self.verboseOutput = verboseOutput
320356
self.linkTimeOptimizationMode = linkTimeOptimizationMode
357+
self.debugInfoFormat = debugInfoFormat
321358
}
322359

323360
public func withDestination(_ destinationTriple: Triple) throws -> BuildParameters {

0 commit comments

Comments
 (0)