From 09169fd12b0addbd805e29399ca69053e045f0d2 Mon Sep 17 00:00:00 2001 From: Boris Buegling Date: Mon, 22 Jan 2024 10:34:22 -0800 Subject: [PATCH] Filter `codesign` messages Right now we end up with this output on relink: ``` /Users/neonacho/Projects/public/swift-package-manager/.build/arm64-apple-macosx/debug/swift-bootstrap: replacing existing signature ``` This change specifically filters any output from `codesign` commands which haven't failed and we aren't in verbose mode. This should eventually be possible at the command level, but for the time being this allows us to avoid shipping SwiftPM with this messy output. rdar://118937939 --- .../Package.swift | 17 ++++++++ .../DoNotFilterLinkerDiagnostics/main.swift | 4 ++ ...dOperationBuildSystemDelegateHandler.swift | 4 +- .../BuildTests/BuildSystemDelegateTests.swift | 41 +++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Package.swift create mode 100644 Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Sources/DoNotFilterLinkerDiagnostics/main.swift create mode 100644 Tests/BuildTests/BuildSystemDelegateTests.swift diff --git a/Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Package.swift b/Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Package.swift new file mode 100644 index 00000000000..326e3041c52 --- /dev/null +++ b/Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Package.swift @@ -0,0 +1,17 @@ +// swift-tools-version: 5.11 + +import PackageDescription + +let package = Package( + name: "DoNotFilterLinkerDiagnostics", + targets: [ + .executableTarget( + name: "DoNotFilterLinkerDiagnostics", + linkerSettings: [ + .linkedLibrary("z"), + .unsafeFlags(["-lz"]), + // should produce: ld: warning: ignoring duplicate libraries: '-lz' + ] + ), + ] +) diff --git a/Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Sources/DoNotFilterLinkerDiagnostics/main.swift b/Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Sources/DoNotFilterLinkerDiagnostics/main.swift new file mode 100644 index 00000000000..44e20d5acc4 --- /dev/null +++ b/Fixtures/Miscellaneous/DoNotFilterLinkerDiagnostics/Sources/DoNotFilterLinkerDiagnostics/main.swift @@ -0,0 +1,4 @@ +// The Swift Programming Language +// https://docs.swift.org/swift-book + +print("Hello, world!") diff --git a/Sources/Build/BuildOperationBuildSystemDelegateHandler.swift b/Sources/Build/BuildOperationBuildSystemDelegateHandler.swift index 1a41e0fe067..482235fe985 100644 --- a/Sources/Build/BuildOperationBuildSystemDelegateHandler.swift +++ b/Sources/Build/BuildOperationBuildSystemDelegateHandler.swift @@ -826,8 +826,10 @@ final class BuildOperationBuildSystemDelegateHandler: LLBuildBuildSystemDelegate process: ProcessHandle, result: CommandExtendedResult ) { + // FIXME: This should really happen at the command-level and is just a stopgap measure. + let shouldFilterOutput = !self.logLevel.isVerbose && command.verboseDescription.hasPrefix("codesign ") && result.result != .failed queue.async { - if let buffer = self.nonSwiftMessageBuffers[command.name] { + if let buffer = self.nonSwiftMessageBuffers[command.name], !shouldFilterOutput { self.progressAnimation.clear() self.outputStream.send(buffer) self.outputStream.flush() diff --git a/Tests/BuildTests/BuildSystemDelegateTests.swift b/Tests/BuildTests/BuildSystemDelegateTests.swift new file mode 100644 index 00000000000..88308460126 --- /dev/null +++ b/Tests/BuildTests/BuildSystemDelegateTests.swift @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift open source project +// +// Copyright (c) 2024 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import SPMTestSupport +import XCTest + +import var TSCBasic.localFileSystem + +final class BuildSystemDelegateTests: XCTestCase { + func testDoNotFilterLinkerDiagnostics() throws { + try fixture(name: "Miscellaneous/DoNotFilterLinkerDiagnostics") { fixturePath in + #if !os(macOS) + // These linker diagnostics are only produced on macOS. + try XCTSkipIf(true, "test is only supported on macOS") + #endif + let (fullLog, _) = try executeSwiftBuild(fixturePath) + XCTAssertTrue(fullLog.contains("ld: warning: ignoring duplicate libraries: '-lz'"), "log didn't contain expected linker diagnostics") + } + } + + func testFilterNonFatalCodesignMessages() throws { + // Note: we can re-use the `TestableExe` fixture here since we just need an executable. + try fixture(name: "Miscellaneous/TestableExe") { fixturePath in + _ = try executeSwiftBuild(fixturePath) + let execPath = fixturePath.appending(components: ".build", "debug", "TestableExe1") + XCTAssertTrue(localFileSystem.exists(execPath), "executable not found at '\(execPath)'") + try localFileSystem.removeFileTree(execPath) + let (fullLog, _) = try executeSwiftBuild(fixturePath) + XCTAssertFalse(fullLog.contains("replacing existing signature"), "log contained non-fatal codesigning messages") + } + } +}