Skip to content

Commit 4222b0f

Browse files
authored
[5.10] Filter codesign messages (#7295)
* **Explanation**: SwiftPM is code signing any binaries it produces on macOS in 5.10. Whenever a binary get relinked and resigned during incremental builds, it verbatim emits informational diagnostics to the log, which should only happen in verbose mode. Especially in projects which multiple binaries, this can make the log difficult to follow and may lead to confusion of users. * **Scope**: Any users on macOS will see this issue. * **Risk**: In theory, this change could lead to other diagnostics not being emitted, but the change is scoped to only code signing tasks, so the risk for this is very low. * **Testing**: Some new tests for SwiftPM's logging were added as part of this. * **Reviewer**: @MaxDesiatov * **Main branch PR**: #7285
1 parent c4a891f commit 4222b0f

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// swift-tools-version: 5.10
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "DoNotFilterLinkerDiagnostics",
7+
targets: [
8+
.executableTarget(
9+
name: "DoNotFilterLinkerDiagnostics",
10+
linkerSettings: [
11+
.linkedLibrary("z"),
12+
.unsafeFlags(["-lz"]),
13+
// should produce: ld: warning: ignoring duplicate libraries: '-lz'
14+
]
15+
),
16+
]
17+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
print("Hello, world!")

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,10 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate
787787
process: ProcessHandle,
788788
result: CommandExtendedResult
789789
) {
790+
// FIXME: This should really happen at the command-level and is just a stopgap measure.
791+
let shouldFilterOutput = !self.logLevel.isVerbose && command.verboseDescription.hasPrefix("codesign ") && result.result != .failed
790792
queue.async {
791-
if let buffer = self.nonSwiftMessageBuffers[command.name] {
793+
if let buffer = self.nonSwiftMessageBuffers[command.name], !shouldFilterOutput {
792794
self.progressAnimation.clear()
793795
self.outputStream.send(buffer)
794796
self.outputStream.flush()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SPMTestSupport
14+
import XCTest
15+
16+
import var TSCBasic.localFileSystem
17+
18+
final class BuildSystemDelegateTests: XCTestCase {
19+
func testDoNotFilterLinkerDiagnostics() throws {
20+
try fixture(name: "Miscellaneous/DoNotFilterLinkerDiagnostics") { fixturePath in
21+
#if !os(macOS)
22+
// These linker diagnostics are only produced on macOS.
23+
try XCTSkipIf(true, "test is only supported on macOS")
24+
#endif
25+
let (fullLog, _) = try executeSwiftBuild(fixturePath)
26+
XCTAssertTrue(fullLog.contains("ld: warning: ignoring duplicate libraries: '-lz'"), "log didn't contain expected linker diagnostics")
27+
}
28+
}
29+
30+
func testFilterNonFatalCodesignMessages() throws {
31+
// Note: we can re-use the `TestableExe` fixture here since we just need an executable.
32+
try fixture(name: "Miscellaneous/TestableExe") { fixturePath in
33+
_ = try executeSwiftBuild(fixturePath)
34+
let execPath = fixturePath.appending(components: ".build", "debug", "TestableExe1")
35+
XCTAssertTrue(localFileSystem.exists(execPath), "executable not found at '\(execPath)'")
36+
try localFileSystem.removeFileTree(execPath)
37+
let (fullLog, _) = try executeSwiftBuild(fixturePath)
38+
XCTAssertFalse(fullLog.contains("replacing existing signature"), "log contained non-fatal codesigning messages")
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)