Skip to content

Commit 68d877e

Browse files
committed
Add code signing build rules
1 parent 5d7848a commit 68d877e

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
367367

368368
return flags
369369
}
370+
371+
func codeSigningArguments(plistPath: AbsolutePath, binaryPath: AbsolutePath) -> [String] {
372+
["codesign", "--force", "--sign", "-", "--entitlements", plistPath.pathString, binaryPath.pathString]
373+
}
370374
}
371375

372376
extension SortedArray where Element == AbsolutePath {

Sources/Build/BuildManifest/LLBuildManifestBuilder+Product.swift

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ extension LLBuildManifestBuilder {
3333
testInputs = []
3434
}
3535

36+
// Create a phony node to represent the entire target.
37+
let targetName = try buildProduct.product.getLLBuildTargetName(config: self.buildConfig)
38+
let output: Node = .virtual(targetName)
39+
3640
switch buildProduct.product.type {
3741
case .library(.static):
3842
try self.manifest.addShellCmd(
@@ -49,15 +53,26 @@ extension LLBuildManifestBuilder {
4953
+ [buildProduct.linkFileListPath]
5054
+ testInputs
5155

56+
let shouldCodeSign: Bool
57+
if case .executable = buildProduct.product.type,
58+
buildParameters.debuggingParameters.shouldEnableDebuggingEntitlement {
59+
shouldCodeSign = true
60+
} else {
61+
shouldCodeSign = false
62+
}
63+
64+
let linkedBinarySuffix = shouldCodeSign ? "-unsigned" : ""
65+
let linkedBinaryPath = try AbsolutePath(validating: buildProduct.binaryPath.pathString + linkedBinarySuffix)
66+
5267
try self.manifest.addShellCmd(
5368
name: cmdName,
5469
description: "Linking \(buildProduct.binaryPath.prettyPath())",
5570
inputs: inputs.map(Node.file),
56-
outputs: [.file(buildProduct.binaryPath)],
57-
arguments: try buildProduct.linkArguments()
71+
outputs: [.file(linkedBinaryPath)],
72+
arguments: try buildProduct.linkArguments(outputPathSuffix: linkedBinarySuffix)
5873
)
5974

60-
if buildParameters.debuggingParameters.shouldEnableDebuggingEntitlement {
75+
if shouldCodeSign {
6176
let basename = try buildProduct.binaryPath.basename
6277
let plistPath = try buildProduct.binaryPath.parentDirectory
6378
.appending(component: "\(basename)-entitlement.plist")
@@ -67,20 +82,25 @@ extension LLBuildManifestBuilder {
6782
)
6883

6984
let cmdName = try buildProduct.product.getCommandName(config: self.buildConfig)
85+
let codeSigningOutput = Node.virtual(targetName + "-CodeSigning")
7086
try self.manifest.addShellCmd(
7187
name: "\(cmdName)-entitlements",
7288
description: "Applying debug entitlements to \(buildProduct.binaryPath.prettyPath())",
73-
inputs: [buildProduct.binaryPath, plistPath].map(Node.file),
74-
outputs: <#T##[Node]#>,
75-
arguments: <#T##[String]#>
89+
inputs: [linkedBinaryPath, plistPath].map(Node.file),
90+
outputs: [codeSigningOutput],
91+
arguments: buildProduct.codeSigningArguments(plistPath: plistPath, binaryPath: linkedBinaryPath)
92+
)
93+
94+
try self.manifest.addShellCmd(
95+
name: "\(cmdName)-entitlements",
96+
description: "Applying debug entitlements to \(buildProduct.binaryPath.prettyPath())",
97+
inputs: [.virtual(targetName + "-CodeSigning")],
98+
outputs: [.file(buildProduct.binaryPath)],
99+
arguments: ["mv", linkedBinaryPath.pathString, buildProduct.binaryPath.pathString]
76100
)
77101
}
78102
}
79103

80-
// Create a phony node to represent the entire target.
81-
let targetName = try buildProduct.product.getLLBuildTargetName(config: self.buildConfig)
82-
let output: Node = .virtual(targetName)
83-
84104
self.manifest.addNode(output, toTarget: targetName)
85105
try self.manifest.addPhonyCmd(
86106
name: output.name,

Sources/SPMBuildCore/BuildParameters/BuildParameters+Debugging.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ extension BuildParameters {
2828

2929
public var debugInfoFormat: DebugInfoFormat
3030

31-
/// Whether the produced executable
31+
/// Whether the produced executable should be codesigned with the debugging entitlement, enabling enhanced
32+
/// backtraces on macOS.
3233
public var shouldEnableDebuggingEntitlement: Bool
3334
}
3435

0 commit comments

Comments
 (0)