From 5d93b4e77d938b4da6662c3625e9efecd5e32c1b Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Wed, 15 Jan 2025 11:58:51 -0500 Subject: [PATCH 01/11] Update SwiftSDKGenerator and GeneratorCLI to use logger instead of print --- Sources/GeneratorCLI/GeneratorCLI.swift | 12 +++++++----- .../SwiftSDKGenerator+Download.swift | 19 ++++++++++--------- .../Generator/SwiftSDKGenerator.swift | 5 ++++- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index 3168e16..d7bcb9f 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -17,6 +17,8 @@ import struct SystemPackage.FilePath @main struct GeneratorCLI: AsyncParsableCommand { + static let logger = Logger(label: "org.swift.swift-sdk-generator") + static let configuration = CommandConfiguration( commandName: "swift-sdk-generator", subcommands: [MakeLinuxSDK.self, MakeWasmSDK.self], @@ -29,7 +31,6 @@ struct GeneratorCLI: AsyncParsableCommand { options: GeneratorOptions ) async throws { let elapsed = try await ContinuousClock().measure { - let logger = Logger(label: "org.swift.swift-sdk-generator") let generator = try await SwiftSDKGenerator( bundleVersion: options.bundleVersion, targetTriple: targetTriple, @@ -57,7 +58,8 @@ struct GeneratorCLI: AsyncParsableCommand { try await generatorTask.value } - print("\nTime taken for this generator run: \(elapsed.intervalString).") + logger.info("") + logger.info("Time taken for this generator run: \(elapsed.intervalString).") } } @@ -151,7 +153,7 @@ extension GeneratorCLI { let current = try SwiftSDKGenerator.getCurrentTriple(isVerbose: self.verbose) if let arch = hostArch { let target = Triple(arch: arch, vendor: current.vendor!, os: current.os!) - print("deprecated: Please use `--host \(target.triple)` instead of `--host-arch \(arch)`") + GeneratorCLI.logger.warning("deprecated: Please use `--host \(target.triple)` instead of `--host-arch \(arch)`") return target } return current @@ -202,14 +204,14 @@ extension GeneratorCLI { } if let arch = generatorOptions.targetArch { let target = Triple(arch: arch, vendor: nil, os: .linux, environment: .gnu) - print("deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`") + GeneratorCLI.logger.warning("deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`") } return Triple(arch: hostTriple.arch!, vendor: nil, os: .linux, environment: .gnu) } func run() async throws { if self.isInvokedAsDefaultSubcommand() { - print( + GeneratorCLI.logger.warning( "deprecated: Please explicitly specify the subcommand to run. For example: $ swift-sdk-generator make-linux-sdk" ) } diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift index b9f7124..5983221 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift @@ -57,9 +57,9 @@ extension SwiftSDKGenerator { return result } - print("Using downloaded artifacts in these locations:") + logger.info("Using downloaded artifacts in these locations:") for path in results.map(\.path) { - print(path) + logger.info("\(path)") } } @@ -108,12 +108,13 @@ extension SwiftSDKGenerator { ) } - print("Downloading \(urls.count) Ubuntu packages...") + logger.info("Downloading \(urls.count) Ubuntu packages...") try await inTemporaryDirectory { fs, tmpDir in let downloadedFiles = try await self.downloadFiles(from: urls, to: tmpDir, client, engine) - report(downloadedFiles: downloadedFiles) + await report(downloadedFiles: downloadedFiles) for fileName in urls.map(\.lastPathComponent) { + logger.info("Extracting \(fileName)...") try await fs.unpack(file: tmpDir.appending(fileName), into: sdkDirPath) } } @@ -148,13 +149,13 @@ extension SwiftSDKGenerator { return result } } -} -private func report(downloadedFiles: [(URL, UInt64)]) { - let byteCountFormatter = ByteCountFormatter() + private func report(downloadedFiles: [(URL, UInt64)]) { + let byteCountFormatter = ByteCountFormatter() - for (url, bytes) in downloadedFiles { - print("\(url) – \(byteCountFormatter.string(fromByteCount: Int64(bytes)))") + for (url, bytes) in downloadedFiles { + logger.info("\(url) – \(byteCountFormatter.string(fromByteCount: Int64(bytes)))") + } } } diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift index 82debf3..1a4308a 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift @@ -253,7 +253,10 @@ public actor SwiftSDKGenerator { let isVerbose = self.isVerbose try await self.inTemporaryDirectory { _, tmp in try await Shell.run(#"cd "\#(tmp)" && ar -x "\#(debFile)""#, shouldLogCommands: isVerbose) - try await print(Shell.readStdout("ls \(tmp)")) + if isVerbose { + let lsOutput = try await Shell.readStdout("ls \(tmp)") + logger.info("\(lsOutput)") + } try await Shell.run( #"tar -C "\#(directoryPath)" -xf "\#(tmp)"/data.tar.*"#, From 4fe2774e7266142e61adeb175fbfb27f16a50bd9 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Thu, 16 Jan 2025 13:25:54 -0500 Subject: [PATCH 02/11] Use metadata instead of string interpolation for logger.info calls - Easier for structured logging. --- Sources/GeneratorCLI/GeneratorCLI.swift | 2 +- .../Generator/SwiftSDKGenerator+Download.swift | 11 +++++++---- .../Generator/SwiftSDKGenerator.swift | 5 +++-- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index d7bcb9f..eab6c62 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -59,7 +59,7 @@ struct GeneratorCLI: AsyncParsableCommand { } logger.info("") - logger.info("Time taken for this generator run: \(elapsed.intervalString).") + logger.info("Generator run finished successfully.", metadata: ["elapsedTime": .string(elapsed.intervalString)]) } } diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift index 5983221..f86d0ce 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift @@ -59,7 +59,7 @@ extension SwiftSDKGenerator { logger.info("Using downloaded artifacts in these locations:") for path in results.map(\.path) { - logger.info("\(path)") + logger.info("-", metadata: ["path": .string(path.string)]) } } @@ -108,13 +108,13 @@ extension SwiftSDKGenerator { ) } - logger.info("Downloading \(urls.count) Ubuntu packages...") + logger.info("Downloading Ubuntu packages...", metadata: ["packageCount": .stringConvertible(urls.count)]) try await inTemporaryDirectory { fs, tmpDir in let downloadedFiles = try await self.downloadFiles(from: urls, to: tmpDir, client, engine) await report(downloadedFiles: downloadedFiles) for fileName in urls.map(\.lastPathComponent) { - logger.info("Extracting \(fileName)...") + logger.info("Extracting deb package...", metadata: ["fileName": .string(fileName)]) try await fs.unpack(file: tmpDir.appending(fileName), into: sdkDirPath) } } @@ -154,7 +154,10 @@ extension SwiftSDKGenerator { let byteCountFormatter = ByteCountFormatter() for (url, bytes) in downloadedFiles { - logger.info("\(url) – \(byteCountFormatter.string(fromByteCount: Int64(bytes)))") + logger.info("Downloaded", metadata: [ + "url": .string(url.absoluteString), + "size": .string(byteCountFormatter.string(fromByteCount: Int64(bytes))) + ]) } } } diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift index 1a4308a..0379b3e 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift @@ -254,8 +254,9 @@ public actor SwiftSDKGenerator { try await self.inTemporaryDirectory { _, tmp in try await Shell.run(#"cd "\#(tmp)" && ar -x "\#(debFile)""#, shouldLogCommands: isVerbose) if isVerbose { - let lsOutput = try await Shell.readStdout("ls \(tmp)") - logger.info("\(lsOutput)") + let cmd = "ls \(tmp)" + let lsOutput = try await Shell.readStdout(cmd) + logger.info("\(lsOutput)", metadata: ["cmd": .string(cmd)]) } try await Shell.run( From 8246d9ee8fd732b02cd0090594b4cd03b945dec2 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 17 Jan 2025 10:18:16 -0500 Subject: [PATCH 03/11] Make logGenerationStep an extension of Logger - And pass a logger to the SwiftSDKRecipe and implementation to be used as well. --- Sources/GeneratorCLI/GeneratorCLI.swift | 9 +++++---- .../Extensions/Logger+logGenerationStep.swift | 19 +++++++++++++++++++ .../Generator/SwiftSDKGenerator+Copy.swift | 4 ++-- .../SwiftSDKGenerator+Download.swift | 4 ++-- .../SwiftSDKGenerator+Entrypoint.swift | 6 +----- .../Generator/SwiftSDKGenerator+Fixup.swift | 4 ++-- .../SwiftSDKGenerator+Metadata.swift | 6 +++--- .../Generator/SwiftSDKGenerator+Unpack.swift | 6 +++--- .../Generator/SwiftSDKGenerator.swift | 2 -- .../SwiftSDKRecipes/LinuxRecipe.swift | 14 ++++++++++---- .../SwiftSDKRecipes/SwiftSDKRecipe.swift | 4 ++++ .../SwiftSDKRecipes/WebAssemblyRecipe.swift | 16 ++++++++++------ .../ArchitectureMappingTests.swift | 5 ++++- .../SwiftSDKRecipes/LinuxRecipeTests.swift | 6 +++++- .../SwiftSDKRecipes/WebAssemblyRecipe.swift | 6 +++++- 15 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index eab6c62..4afd675 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -58,8 +58,7 @@ struct GeneratorCLI: AsyncParsableCommand { try await generatorTask.value } - logger.info("") - logger.info("Generator run finished successfully.", metadata: ["elapsedTime": .string(elapsed.intervalString)]) + logger.info("\nGenerator run finished successfully.", metadata: ["elapsedTime": .string(elapsed.intervalString)]) } } @@ -238,7 +237,8 @@ extension GeneratorCLI { fromContainerImage: self.fromContainerImage, hostSwiftPackagePath: self.generatorOptions.hostSwiftPackagePath, targetSwiftPackagePath: self.generatorOptions.targetSwiftPackagePath, - includeHostToolchain: self.generatorOptions.hostToolchain + includeHostToolchain: self.generatorOptions.hostToolchain, + logger: logger ) try await GeneratorCLI.run(recipe: recipe, targetTriple: targetTriple, options: self.generatorOptions) } @@ -292,7 +292,8 @@ extension GeneratorCLI { }, targetSwiftPackagePath: FilePath(targetSwiftPackagePath), wasiSysroot: FilePath(self.wasiSysroot), - swiftVersion: self.generatorOptions.swiftVersion + swiftVersion: self.generatorOptions.swiftVersion, + logger: logger ) let targetTriple = self.deriveTargetTriple() try await GeneratorCLI.run(recipe: recipe, targetTriple: targetTriple, options: self.generatorOptions) diff --git a/Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift b/Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift new file mode 100644 index 0000000..647bbb9 --- /dev/null +++ b/Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift @@ -0,0 +1,19 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift open source project +// +// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import Logging + +extension Logger { + func logGenerationStep(_ message: String) { + info("\n\(message)") + } +} diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift index f5d0a4c..75747e0 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift @@ -18,7 +18,7 @@ extension SwiftSDKGenerator { baseDockerImage: String, sdkDirPath: FilePath ) async throws { - logGenerationStep("Launching a Docker container to copy Swift SDK for the target triple from it...") + logger.logGenerationStep("Launching a Docker container to copy Swift SDK for the target triple from it...") try await withDockerContainer(fromImage: baseDockerImage) { containerID in try await inTemporaryDirectory { generator, _ in let sdkUsrPath = sdkDirPath.appending("usr") @@ -102,7 +102,7 @@ extension SwiftSDKGenerator { } func copyTargetSwift(from distributionPath: FilePath, sdkDirPath: FilePath) async throws { - logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") + logger.logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") for (pathWithinPackage, pathWithinSwiftSDK) in [ ("lib/swift", sdkDirPath.appending("usr/lib")), diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift index f86d0ce..2ba5102 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift @@ -29,7 +29,7 @@ extension SwiftSDKGenerator { downloadableArtifacts: inout DownloadableArtifacts, itemsToDownload: @Sendable (DownloadableArtifacts) -> [DownloadableArtifacts.Item] ) async throws { - logGenerationStep("Downloading required toolchain packages...") + logger.logGenerationStep("Downloading required toolchain packages...") let hostLLVMURL = downloadableArtifacts.hostLLVM.remoteURL // Workaround an issue with github.com returning 400 instead of 404 status to HEAD requests from AHC. let isLLVMBinaryArtifactAvailable = try await type(of: client).with(http1Only: true) { @@ -70,7 +70,7 @@ extension SwiftSDKGenerator { versionsConfiguration: VersionsConfiguration, sdkDirPath: FilePath ) async throws { - logGenerationStep("Parsing Ubuntu packages list...") + logger.logGenerationStep("Parsing Ubuntu packages list...") async let mainPackages = try await client.parseUbuntuPackagesList( ubuntuRelease: versionsConfiguration.linuxDistribution.release, diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift index 448f225..50bb862 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift @@ -59,7 +59,7 @@ public extension SwiftSDKGenerator { try await generateArtifactBundleManifest(hostTriples: swiftSDKProduct.hostTriples) - logGenerationStep( + logger.logGenerationStep( """ All done! Install the newly generated SDK with this command: swift experimental-sdk install \(pathsConfiguration.artifactBundlePath) @@ -72,7 +72,3 @@ public extension SwiftSDKGenerator { } } } - -func logGenerationStep(_ message: String) { - print("\n\(message)") -} diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift index 4db44f1..43575bf 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift @@ -17,7 +17,7 @@ import struct Foundation.Data extension SwiftSDKGenerator { func fixAbsoluteSymlinks(sdkDirPath: FilePath) throws { - logGenerationStep("Fixing up absolute symlinks...") + logger.logGenerationStep("Fixing up absolute symlinks...") for (source, absoluteDestination) in try findSymlinks(at: sdkDirPath).filter({ $1.string.hasPrefix("/") @@ -51,7 +51,7 @@ extension SwiftSDKGenerator { func symlinkClangHeaders() throws { let swiftStaticClangPath = self.pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static/clang") if !doesFileExist(at: swiftStaticClangPath) { - logGenerationStep("Symlinking clang headers...") + logger.logGenerationStep("Symlinking clang headers...") try self.createSymlink( at: self.pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static/clang"), pointingTo: "../swift/clang" diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift index 55dea46..072e1b5 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift @@ -22,7 +22,7 @@ private let encoder: JSONEncoder = { extension SwiftSDKGenerator { func generateToolsetJSON(recipe: SwiftSDKRecipe) throws -> FilePath { - logGenerationStep("Generating toolset JSON file...") + logger.logGenerationStep("Generating toolset JSON file...") let toolsetJSONPath = pathsConfiguration.swiftSDKRootPath.appending("toolset.json") @@ -44,7 +44,7 @@ extension SwiftSDKGenerator { } func generateDestinationJSON(toolsetPath: FilePath, sdkDirPath: FilePath, recipe: SwiftSDKRecipe) throws { - logGenerationStep("Generating destination JSON file...") + logger.logGenerationStep("Generating destination JSON file...") let destinationJSONPath = pathsConfiguration.swiftSDKRootPath.appending("swift-sdk.json") @@ -87,7 +87,7 @@ extension SwiftSDKGenerator { } func generateArtifactBundleManifest(hostTriples: [Triple]?) throws { - logGenerationStep("Generating .artifactbundle manifest file...") + logger.logGenerationStep("Generating .artifactbundle manifest file...") let artifactBundleManifestPath = pathsConfiguration.artifactBundlePath.appending("info.json") diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift index 0974344..02c7337 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift @@ -45,7 +45,7 @@ let unusedHostLibraries = [ extension SwiftSDKGenerator { func unpackHostSwift(hostSwiftPackagePath: FilePath) async throws { - logGenerationStep("Unpacking and copying Swift binaries for the host triple...") + logger.logGenerationStep("Unpacking and copying Swift binaries for the host triple...") let pathsConfiguration = self.pathsConfiguration try self.createDirectoryIfNeeded(at: pathsConfiguration.toolchainDirPath) @@ -98,7 +98,7 @@ extension SwiftSDKGenerator { relativePathToRoot: [FilePath.Component], sdkDirPath: FilePath ) async throws { - logGenerationStep("Unpacking Swift distribution for the target triple...") + logger.logGenerationStep("Unpacking Swift distribution for the target triple...") try await inTemporaryDirectory { fs, tmpDir in try await fs.unpack(file: targetSwiftPackagePath, into: tmpDir) @@ -109,7 +109,7 @@ extension SwiftSDKGenerator { } func prepareLLDLinker(_ engine: QueryEngine, llvmArtifact: DownloadableArtifacts.Item) async throws { - logGenerationStep("Unpacking and copying `lld` linker...") + logger.logGenerationStep("Unpacking and copying `lld` linker...") let pathsConfiguration = self.pathsConfiguration let targetOS = self.targetTriple.os diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift index 0379b3e..8396c47 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift @@ -34,8 +34,6 @@ public actor SwiftSDKGenerator { isVerbose: Bool, logger: Logger ) async throws { - logGenerationStep("Looking up configuration values...") - let sourceRoot = FilePath(#filePath) .removingLastComponent() .removingLastComponent() diff --git a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift index a9f95e6..368ae4d 100644 --- a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift +++ b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// import Foundation +import Logging import Helpers import struct SystemPackage.FilePath @@ -33,6 +34,7 @@ public struct LinuxRecipe: SwiftSDKRecipe { let targetSwiftSource: TargetSwiftSource let hostSwiftSource: HostSwiftSource let versionsConfiguration: VersionsConfiguration + public let logger: Logger var shouldUseDocker: Bool { if case .docker = self.targetSwiftSource { @@ -52,7 +54,8 @@ public struct LinuxRecipe: SwiftSDKRecipe { fromContainerImage: String?, hostSwiftPackagePath: String?, targetSwiftPackagePath: String?, - includeHostToolchain: Bool = false + includeHostToolchain: Bool = false, + logger: Logger ) throws { let versionsConfiguration = try VersionsConfiguration( swiftVersion: swiftVersion, @@ -88,7 +91,8 @@ public struct LinuxRecipe: SwiftSDKRecipe { linuxDistribution: linuxDistribution, targetSwiftSource: targetSwiftSource, hostSwiftSource: hostSwiftSource, - versionsConfiguration: versionsConfiguration + versionsConfiguration: versionsConfiguration, + logger: logger ) } @@ -98,7 +102,8 @@ public struct LinuxRecipe: SwiftSDKRecipe { linuxDistribution: LinuxDistribution, targetSwiftSource: TargetSwiftSource, hostSwiftSource: HostSwiftSource, - versionsConfiguration: VersionsConfiguration + versionsConfiguration: VersionsConfiguration, + logger: Logger ) { self.mainTargetTriple = mainTargetTriple self.mainHostTriple = mainHostTriple @@ -106,6 +111,7 @@ public struct LinuxRecipe: SwiftSDKRecipe { self.targetSwiftSource = targetSwiftSource self.hostSwiftSource = hostSwiftSource self.versionsConfiguration = versionsConfiguration + self.logger = logger } public func applyPlatformOptions(toolset: inout Toolset, targetTriple: Triple) { @@ -293,7 +299,7 @@ public struct LinuxRecipe: SwiftSDKRecipe { let autolinkExtractPath = generator.pathsConfiguration.toolchainBinDirPath.appending("swift-autolink-extract") if await !generator.doesFileExist(at: autolinkExtractPath) { - logGenerationStep("Fixing `swift-autolink-extract` symlink...") + logger.logGenerationStep("Fixing `swift-autolink-extract` symlink...") try await generator.createSymlink(at: autolinkExtractPath, pointingTo: "swift") } } diff --git a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/SwiftSDKRecipe.swift b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/SwiftSDKRecipe.swift index baeda00..17908a4 100644 --- a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/SwiftSDKRecipe.swift +++ b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/SwiftSDKRecipe.swift @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +import Logging import Helpers import struct SystemPackage.FilePath @@ -34,6 +35,9 @@ public protocol SwiftSDKRecipe: Sendable { /// The default identifier of the Swift SDK var defaultArtifactID: String { get } + /// The logger to use for recipe generators. + var logger: Logger { get } + /// The main entrypoint of the recipe to make a Swift SDK func makeSwiftSDK(generator: SwiftSDKGenerator, engine: QueryEngine, httpClient: some HTTPClientProtocol) async throws -> SwiftSDKProduct diff --git a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift index e935546..31b0b7c 100644 --- a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift +++ b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +import Logging import Helpers import struct SystemPackage.FilePath @@ -18,6 +19,7 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { let targetSwiftPackagePath: FilePath let wasiSysroot: FilePath let swiftVersion: String + public let logger: Logger public struct HostToolchainPackage: Sendable { let path: FilePath @@ -33,12 +35,14 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { hostSwiftPackage: HostToolchainPackage?, targetSwiftPackagePath: FilePath, wasiSysroot: FilePath, - swiftVersion: String + swiftVersion: String, + logger: Logger ) { self.hostSwiftPackage = hostSwiftPackage self.targetSwiftPackagePath = targetSwiftPackagePath self.wasiSysroot = wasiSysroot self.swiftVersion = swiftVersion + self.logger = logger } public var defaultArtifactID: String { @@ -96,13 +100,13 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { let pathsConfiguration = generator.pathsConfiguration let targetSwiftLibPath = self.targetSwiftPackagePath.appending("usr/lib") - logGenerationStep("Copying Swift binaries for the host triple...") + logger.logGenerationStep("Copying Swift binaries for the host triple...") var hostTriples: [Triple]? = nil if let hostSwiftPackage { hostTriples = [hostSwiftPackage.triple] try await generator.rsync(from: hostSwiftPackage.path.appending("usr"), to: pathsConfiguration.toolchainDirPath) - logGenerationStep("Removing unused toolchain components...") + logger.logGenerationStep("Removing unused toolchain components...") let liblldbNames: [String] = try await { let libDirPath = pathsConfiguration.toolchainDirPath.appending("usr/lib") guard await generator.doesFileExist(at: libDirPath) else { @@ -133,7 +137,7 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { if await !generator.doesFileExist(at: autolinkExtractPath), await generator.doesFileExist(at: generator.pathsConfiguration.toolchainBinDirPath.appending("swift")) { - logGenerationStep("Fixing `swift-autolink-extract` symlink...") + logger.logGenerationStep("Fixing `swift-autolink-extract` symlink...") try await generator.createSymlink(at: autolinkExtractPath, pointingTo: "swift") } @@ -147,7 +151,7 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { /// Merge the target Swift package into the Swift SDK bundle derived from the host Swift package. func mergeTargetSwift(from distributionPath: FilePath, generator: SwiftSDKGenerator) async throws { let pathsConfiguration = generator.pathsConfiguration - logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") + logger.logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") for (pathWithinPackage, pathWithinSwiftSDK, isOptional) in [ ("clang", pathsConfiguration.toolchainDirPath.appending("usr/lib"), false), ("swift/clang", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift"), false), @@ -159,7 +163,7 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { ("swift_static/CoreFoundation", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static"), true), ] { if isOptional, await !(generator.doesFileExist(at: distributionPath.appending(pathWithinPackage))) { - logGenerationStep("Skipping optional path \(pathWithinPackage)") + logger.logGenerationStep("Skipping optional path \(pathWithinPackage)") continue } try await generator.rsync(from: distributionPath.appending(pathWithinPackage), to: pathWithinSwiftSDK) diff --git a/Tests/SwiftSDKGeneratorTests/ArchitectureMappingTests.swift b/Tests/SwiftSDKGeneratorTests/ArchitectureMappingTests.swift index a06551a..9202c2a 100644 --- a/Tests/SwiftSDKGeneratorTests/ArchitectureMappingTests.swift +++ b/Tests/SwiftSDKGeneratorTests/ArchitectureMappingTests.swift @@ -15,6 +15,8 @@ import Logging import XCTest final class ArchitectureMappingTests: XCTestCase { + let logger = Logger(label: "ArchitectureMappingTests") + /// Swift on macOS, Swift on Linux and Debian packages all use /// different names for the x86 and Arm architectures: /// @@ -54,7 +56,8 @@ final class ArchitectureMappingTests: XCTestCase { withDocker: false, fromContainerImage: nil, hostSwiftPackagePath: nil, - targetSwiftPackagePath: nil + targetSwiftPackagePath: nil, + logger: logger ) // LocalSwiftSDKGenerator constructs URLs and paths which depend on architectures let sdk = try await SwiftSDKGenerator( diff --git a/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/LinuxRecipeTests.swift b/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/LinuxRecipeTests.swift index 75f9737..436b705 100644 --- a/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/LinuxRecipeTests.swift +++ b/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/LinuxRecipeTests.swift @@ -10,11 +10,14 @@ // //===----------------------------------------------------------------------===// +import Logging import XCTest @testable import SwiftSDKGenerator final class LinuxRecipeTests: XCTestCase { + let logger = Logger(label: "LinuxRecipeTests") + func createRecipe( hostTriple: Triple = Triple("x86_64-unknown-linux-gnu"), swiftVersion: String = "6.0", @@ -33,7 +36,8 @@ final class LinuxRecipeTests: XCTestCase { withDocker: withDocker, fromContainerImage: fromContainerImage, hostSwiftPackagePath: hostSwiftPackagePath, targetSwiftPackagePath: targetSwiftPackagePath, - includeHostToolchain: includeHostToolchain + includeHostToolchain: includeHostToolchain, + logger: logger ) } diff --git a/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/WebAssemblyRecipe.swift b/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/WebAssemblyRecipe.swift index 8bb770c..71eff42 100644 --- a/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/WebAssemblyRecipe.swift +++ b/Tests/SwiftSDKGeneratorTests/SwiftSDKRecipes/WebAssemblyRecipe.swift @@ -10,17 +10,21 @@ // //===----------------------------------------------------------------------===// +import Logging import XCTest @testable import SwiftSDKGenerator final class WebAssemblyRecipeTests: XCTestCase { + let logger = Logger(label: "WebAssemblyRecipeTests") + func createRecipe() -> WebAssemblyRecipe { WebAssemblyRecipe( hostSwiftPackage: nil, targetSwiftPackagePath: "./target-toolchain", wasiSysroot: "./wasi-sysroot", - swiftVersion: "5.10" + swiftVersion: "5.10", + logger: logger ) } From 7d0501babc83cc6b2d5d71ec2d890ba72f7d40f3 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 17 Jan 2025 18:34:31 -0500 Subject: [PATCH 04/11] Remove logGenerationStep, log using .debug for detailed logs --- Sources/GeneratorCLI/GeneratorCLI.swift | 2 +- .../Extensions/Logger+logGenerationStep.swift | 19 ------------------- .../Generator/SwiftSDKGenerator+Copy.swift | 4 ++-- .../SwiftSDKGenerator+Download.swift | 18 +++++++++++------- .../SwiftSDKGenerator+Entrypoint.swift | 6 +++++- .../Generator/SwiftSDKGenerator+Fixup.swift | 4 ++-- .../SwiftSDKGenerator+Metadata.swift | 6 +++--- .../Generator/SwiftSDKGenerator+Unpack.swift | 6 +++--- .../Generator/SwiftSDKGenerator.swift | 2 +- .../SwiftSDKRecipes/LinuxRecipe.swift | 2 +- .../SwiftSDKRecipes/WebAssemblyRecipe.swift | 10 +++++----- 11 files changed, 34 insertions(+), 45 deletions(-) delete mode 100644 Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index 4afd675..b1c35dc 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -58,7 +58,7 @@ struct GeneratorCLI: AsyncParsableCommand { try await generatorTask.value } - logger.info("\nGenerator run finished successfully.", metadata: ["elapsedTime": .string(elapsed.intervalString)]) + logger.info("Generator run finished successfully.", metadata: ["elapsedTime": .string(elapsed.intervalString)]) } } diff --git a/Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift b/Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift deleted file mode 100644 index 647bbb9..0000000 --- a/Sources/SwiftSDKGenerator/Extensions/Logger+logGenerationStep.swift +++ /dev/null @@ -1,19 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift open source project -// -// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors -// -//===----------------------------------------------------------------------===// - -import Logging - -extension Logger { - func logGenerationStep(_ message: String) { - info("\n\(message)") - } -} diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift index 75747e0..535f7d5 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Copy.swift @@ -18,7 +18,7 @@ extension SwiftSDKGenerator { baseDockerImage: String, sdkDirPath: FilePath ) async throws { - logger.logGenerationStep("Launching a Docker container to copy Swift SDK for the target triple from it...") + logger.info("Launching a Docker container to copy Swift SDK for the target triple from it...") try await withDockerContainer(fromImage: baseDockerImage) { containerID in try await inTemporaryDirectory { generator, _ in let sdkUsrPath = sdkDirPath.appending("usr") @@ -102,7 +102,7 @@ extension SwiftSDKGenerator { } func copyTargetSwift(from distributionPath: FilePath, sdkDirPath: FilePath) async throws { - logger.logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") + logger.info("Copying Swift core libraries for the target triple into Swift SDK bundle...") for (pathWithinPackage, pathWithinSwiftSDK) in [ ("lib/swift", sdkDirPath.appending("usr/lib")), diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift index 2ba5102..ce95607 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift @@ -29,7 +29,7 @@ extension SwiftSDKGenerator { downloadableArtifacts: inout DownloadableArtifacts, itemsToDownload: @Sendable (DownloadableArtifacts) -> [DownloadableArtifacts.Item] ) async throws { - logger.logGenerationStep("Downloading required toolchain packages...") + logger.info("Downloading required toolchain packages...") let hostLLVMURL = downloadableArtifacts.hostLLVM.remoteURL // Workaround an issue with github.com returning 400 instead of 404 status to HEAD requests from AHC. let isLLVMBinaryArtifactAvailable = try await type(of: client).with(http1Only: true) { @@ -57,9 +57,13 @@ extension SwiftSDKGenerator { return result } - logger.info("Using downloaded artifacts in these locations:") - for path in results.map(\.path) { - logger.info("-", metadata: ["path": .string(path.string)]) + if isVerbose { + logger.debug("Using downloaded artifacts in these locations:") + for path in results.map(\.path) { + logger.debug("-", metadata: ["path": .string(path.string)]) + } + } else { + logger.info("Using downloaded artifacts from cache") } } @@ -70,7 +74,7 @@ extension SwiftSDKGenerator { versionsConfiguration: VersionsConfiguration, sdkDirPath: FilePath ) async throws { - logger.logGenerationStep("Parsing Ubuntu packages list...") + logger.debug("Parsing Ubuntu packages list...") async let mainPackages = try await client.parseUbuntuPackagesList( ubuntuRelease: versionsConfiguration.linuxDistribution.release, @@ -114,7 +118,7 @@ extension SwiftSDKGenerator { await report(downloadedFiles: downloadedFiles) for fileName in urls.map(\.lastPathComponent) { - logger.info("Extracting deb package...", metadata: ["fileName": .string(fileName)]) + logger.debug("Extracting deb package...", metadata: ["fileName": .string(fileName)]) try await fs.unpack(file: tmpDir.appending(fileName), into: sdkDirPath) } } @@ -154,7 +158,7 @@ extension SwiftSDKGenerator { let byteCountFormatter = ByteCountFormatter() for (url, bytes) in downloadedFiles { - logger.info("Downloaded", metadata: [ + logger.debug("Downloaded package", metadata: [ "url": .string(url.absoluteString), "size": .string(byteCountFormatter.string(fromByteCount: Int64(bytes))) ]) diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift index 50bb862..c4ec429 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift @@ -59,13 +59,17 @@ public extension SwiftSDKGenerator { try await generateArtifactBundleManifest(hostTriples: swiftSDKProduct.hostTriples) - logger.logGenerationStep( + // Extra spaces added for readability for the user + logger.info( """ + + All done! Install the newly generated SDK with this command: swift experimental-sdk install \(pathsConfiguration.artifactBundlePath) After that, use the newly installed SDK when building with this command: swift build --experimental-swift-sdk \(artifactID) + """ ) } diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift index 43575bf..ea587cb 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Fixup.swift @@ -17,7 +17,7 @@ import struct Foundation.Data extension SwiftSDKGenerator { func fixAbsoluteSymlinks(sdkDirPath: FilePath) throws { - logger.logGenerationStep("Fixing up absolute symlinks...") + logger.info("Fixing up absolute symlinks...") for (source, absoluteDestination) in try findSymlinks(at: sdkDirPath).filter({ $1.string.hasPrefix("/") @@ -51,7 +51,7 @@ extension SwiftSDKGenerator { func symlinkClangHeaders() throws { let swiftStaticClangPath = self.pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static/clang") if !doesFileExist(at: swiftStaticClangPath) { - logger.logGenerationStep("Symlinking clang headers...") + logger.info("Symlinking clang headers...") try self.createSymlink( at: self.pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static/clang"), pointingTo: "../swift/clang" diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift index 072e1b5..4613d7f 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift @@ -22,7 +22,7 @@ private let encoder: JSONEncoder = { extension SwiftSDKGenerator { func generateToolsetJSON(recipe: SwiftSDKRecipe) throws -> FilePath { - logger.logGenerationStep("Generating toolset JSON file...") + logger.info("Generating toolset JSON file...") let toolsetJSONPath = pathsConfiguration.swiftSDKRootPath.appending("toolset.json") @@ -44,7 +44,7 @@ extension SwiftSDKGenerator { } func generateDestinationJSON(toolsetPath: FilePath, sdkDirPath: FilePath, recipe: SwiftSDKRecipe) throws { - logger.logGenerationStep("Generating destination JSON file...") + logger.info("Generating destination JSON file...") let destinationJSONPath = pathsConfiguration.swiftSDKRootPath.appending("swift-sdk.json") @@ -87,7 +87,7 @@ extension SwiftSDKGenerator { } func generateArtifactBundleManifest(hostTriples: [Triple]?) throws { - logger.logGenerationStep("Generating .artifactbundle manifest file...") + logger.info("Generating .artifactbundle info JSON file...") let artifactBundleManifestPath = pathsConfiguration.artifactBundlePath.appending("info.json") diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift index 02c7337..0cc421d 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Unpack.swift @@ -45,7 +45,7 @@ let unusedHostLibraries = [ extension SwiftSDKGenerator { func unpackHostSwift(hostSwiftPackagePath: FilePath) async throws { - logger.logGenerationStep("Unpacking and copying Swift binaries for the host triple...") + logger.info("Unpacking and copying Swift binaries for the host triple...") let pathsConfiguration = self.pathsConfiguration try self.createDirectoryIfNeeded(at: pathsConfiguration.toolchainDirPath) @@ -98,7 +98,7 @@ extension SwiftSDKGenerator { relativePathToRoot: [FilePath.Component], sdkDirPath: FilePath ) async throws { - logger.logGenerationStep("Unpacking Swift distribution for the target triple...") + logger.info("Unpacking Swift distribution for the target triple...") try await inTemporaryDirectory { fs, tmpDir in try await fs.unpack(file: targetSwiftPackagePath, into: tmpDir) @@ -109,7 +109,7 @@ extension SwiftSDKGenerator { } func prepareLLDLinker(_ engine: QueryEngine, llvmArtifact: DownloadableArtifacts.Item) async throws { - logger.logGenerationStep("Unpacking and copying `lld` linker...") + logger.info("Unpacking and copying `lld` linker...") let pathsConfiguration = self.pathsConfiguration let targetOS = self.targetTriple.os diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift index 8396c47..27879f8 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift @@ -254,7 +254,7 @@ public actor SwiftSDKGenerator { if isVerbose { let cmd = "ls \(tmp)" let lsOutput = try await Shell.readStdout(cmd) - logger.info("\(lsOutput)", metadata: ["cmd": .string(cmd)]) + logger.debug("\(lsOutput)", metadata: ["cmd": .string(cmd)]) } try await Shell.run( diff --git a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift index 368ae4d..097733f 100644 --- a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift +++ b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/LinuxRecipe.swift @@ -299,7 +299,7 @@ public struct LinuxRecipe: SwiftSDKRecipe { let autolinkExtractPath = generator.pathsConfiguration.toolchainBinDirPath.appending("swift-autolink-extract") if await !generator.doesFileExist(at: autolinkExtractPath) { - logger.logGenerationStep("Fixing `swift-autolink-extract` symlink...") + logger.info("Fixing `swift-autolink-extract` symlink...") try await generator.createSymlink(at: autolinkExtractPath, pointingTo: "swift") } } diff --git a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift index 31b0b7c..21b2485 100644 --- a/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift +++ b/Sources/SwiftSDKGenerator/SwiftSDKRecipes/WebAssemblyRecipe.swift @@ -100,13 +100,13 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { let pathsConfiguration = generator.pathsConfiguration let targetSwiftLibPath = self.targetSwiftPackagePath.appending("usr/lib") - logger.logGenerationStep("Copying Swift binaries for the host triple...") + logger.info("Copying Swift binaries for the host triple...") var hostTriples: [Triple]? = nil if let hostSwiftPackage { hostTriples = [hostSwiftPackage.triple] try await generator.rsync(from: hostSwiftPackage.path.appending("usr"), to: pathsConfiguration.toolchainDirPath) - logger.logGenerationStep("Removing unused toolchain components...") + logger.info("Removing unused toolchain components...") let liblldbNames: [String] = try await { let libDirPath = pathsConfiguration.toolchainDirPath.appending("usr/lib") guard await generator.doesFileExist(at: libDirPath) else { @@ -137,7 +137,7 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { if await !generator.doesFileExist(at: autolinkExtractPath), await generator.doesFileExist(at: generator.pathsConfiguration.toolchainBinDirPath.appending("swift")) { - logger.logGenerationStep("Fixing `swift-autolink-extract` symlink...") + logger.info("Fixing `swift-autolink-extract` symlink...") try await generator.createSymlink(at: autolinkExtractPath, pointingTo: "swift") } @@ -151,7 +151,7 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { /// Merge the target Swift package into the Swift SDK bundle derived from the host Swift package. func mergeTargetSwift(from distributionPath: FilePath, generator: SwiftSDKGenerator) async throws { let pathsConfiguration = generator.pathsConfiguration - logger.logGenerationStep("Copying Swift core libraries for the target triple into Swift SDK bundle...") + logger.info("Copying Swift core libraries for the target triple into Swift SDK bundle...") for (pathWithinPackage, pathWithinSwiftSDK, isOptional) in [ ("clang", pathsConfiguration.toolchainDirPath.appending("usr/lib"), false), ("swift/clang", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift"), false), @@ -163,7 +163,7 @@ public struct WebAssemblyRecipe: SwiftSDKRecipe { ("swift_static/CoreFoundation", pathsConfiguration.toolchainDirPath.appending("usr/lib/swift_static"), true), ] { if isOptional, await !(generator.doesFileExist(at: distributionPath.appending(pathWithinPackage))) { - logger.logGenerationStep("Skipping optional path \(pathWithinPackage)") + logger.debug("Skipping optional path \(pathWithinPackage)") continue } try await generator.rsync(from: distributionPath.appending(pathWithinPackage), to: pathWithinSwiftSDK) From 8310aa23bd5558b87fb0a40f4ee9f9392726adba Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 17 Jan 2025 18:46:20 -0500 Subject: [PATCH 05/11] Set logLevel from --verbose flag --- Sources/GeneratorCLI/GeneratorCLI.swift | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Sources/GeneratorCLI/GeneratorCLI.swift b/Sources/GeneratorCLI/GeneratorCLI.swift index b1c35dc..bab7777 100644 --- a/Sources/GeneratorCLI/GeneratorCLI.swift +++ b/Sources/GeneratorCLI/GeneratorCLI.swift @@ -17,7 +17,7 @@ import struct SystemPackage.FilePath @main struct GeneratorCLI: AsyncParsableCommand { - static let logger = Logger(label: "org.swift.swift-sdk-generator") + static let appLogger = Logger(label: "org.swift.swift-sdk-generator") static let configuration = CommandConfiguration( commandName: "swift-sdk-generator", @@ -25,11 +25,20 @@ struct GeneratorCLI: AsyncParsableCommand { defaultSubcommand: MakeLinuxSDK.self ) + static func loggerWithLevel(from options: GeneratorOptions) -> Logger { + var logger = self.appLogger + if options.verbose { + logger.logLevel = .debug + } + return logger + } + static func run( recipe: some SwiftSDKRecipe, targetTriple: Triple, options: GeneratorOptions ) async throws { + let logger = loggerWithLevel(from: options) let elapsed = try await ContinuousClock().measure { let generator = try await SwiftSDKGenerator( bundleVersion: options.bundleVersion, @@ -152,7 +161,7 @@ extension GeneratorCLI { let current = try SwiftSDKGenerator.getCurrentTriple(isVerbose: self.verbose) if let arch = hostArch { let target = Triple(arch: arch, vendor: current.vendor!, os: current.os!) - GeneratorCLI.logger.warning("deprecated: Please use `--host \(target.triple)` instead of `--host-arch \(arch)`") + appLogger.warning("deprecated: Please use `--host \(target.triple)` instead of `--host-arch \(arch)`") return target } return current @@ -203,14 +212,14 @@ extension GeneratorCLI { } if let arch = generatorOptions.targetArch { let target = Triple(arch: arch, vendor: nil, os: .linux, environment: .gnu) - GeneratorCLI.logger.warning("deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`") + appLogger.warning("deprecated: Please use `--target \(target.triple)` instead of `--target-arch \(arch)`") } return Triple(arch: hostTriple.arch!, vendor: nil, os: .linux, environment: .gnu) } func run() async throws { if self.isInvokedAsDefaultSubcommand() { - GeneratorCLI.logger.warning( + appLogger.warning( "deprecated: Please explicitly specify the subcommand to run. For example: $ swift-sdk-generator make-linux-sdk" ) } @@ -238,7 +247,7 @@ extension GeneratorCLI { hostSwiftPackagePath: self.generatorOptions.hostSwiftPackagePath, targetSwiftPackagePath: self.generatorOptions.targetSwiftPackagePath, includeHostToolchain: self.generatorOptions.hostToolchain, - logger: logger + logger: loggerWithLevel(from: self.generatorOptions) ) try await GeneratorCLI.run(recipe: recipe, targetTriple: targetTriple, options: self.generatorOptions) } @@ -293,7 +302,7 @@ extension GeneratorCLI { targetSwiftPackagePath: FilePath(targetSwiftPackagePath), wasiSysroot: FilePath(self.wasiSysroot), swiftVersion: self.generatorOptions.swiftVersion, - logger: logger + logger: loggerWithLevel(from: self.generatorOptions) ) let targetTriple = self.deriveTargetTriple() try await GeneratorCLI.run(recipe: recipe, targetTriple: targetTriple, options: self.generatorOptions) From 47f4c42c8e43cd5eb284f3423d56babba58213f3 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 17 Jan 2025 18:53:26 -0500 Subject: [PATCH 06/11] Add logger to DownloadArtifactQuery --- .../SwiftSDKGenerator+Download.swift | 2 +- .../Queries/DownloadArtifactQuery.swift | 44 ++++++++++--------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift index ce95607..ee13d66 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift @@ -46,7 +46,7 @@ extension SwiftSDKGenerator { let results = try await withThrowingTaskGroup(of: FileCacheRecord.self) { group in for item in itemsToDownload(downloadableArtifacts) { group.addTask { - try await engine[DownloadArtifactQuery(artifact: item, httpClient: client)] + try await engine[DownloadArtifactQuery(artifact: item, httpClient: client, logger: self.logger)] } } diff --git a/Sources/SwiftSDKGenerator/Queries/DownloadArtifactQuery.swift b/Sources/SwiftSDKGenerator/Queries/DownloadArtifactQuery.swift index b95fb26..041737c 100644 --- a/Sources/SwiftSDKGenerator/Queries/DownloadArtifactQuery.swift +++ b/Sources/SwiftSDKGenerator/Queries/DownloadArtifactQuery.swift @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// import class Foundation.ByteCountFormatter +import Logging import Helpers import struct SystemPackage.FilePath @@ -18,9 +19,10 @@ struct DownloadArtifactQuery: Query { var cacheKey: some CacheKey { self.artifact } let artifact: DownloadableArtifacts.Item let httpClient: any HTTPClientProtocol + let logger: Logger func run(engine: QueryEngine) async throws -> FilePath { - print("Downloading remote artifact not available in local cache: \(self.artifact.remoteURL)") + logger.info("Downloading remote artifact not available in local cache", metadata: ["remoteUrl": .string(self.artifact.remoteURL.absoluteString)]) let stream = self.httpClient.streamDownloadProgress( from: self.artifact.remoteURL, to: self.artifact.localPath ) @@ -32,6 +34,26 @@ struct DownloadArtifactQuery: Query { } return self.artifact.localPath } + + private func report(progress: DownloadProgress, for artifact: DownloadableArtifacts.Item) { + let byteCountFormatter = ByteCountFormatter() + + if let total = progress.totalBytes { + logger.debug(""" + \(artifact.remoteURL.lastPathComponent) \( + byteCountFormatter + .string(fromByteCount: Int64(progress.receivedBytes)) + )/\( + byteCountFormatter + .string(fromByteCount: Int64(total)) + ) + """) + } else { + logger.debug( + "\(artifact.remoteURL.lastPathComponent) \(byteCountFormatter.string(fromByteCount: Int64(progress.receivedBytes)))" + ) + } + } } /// Checks whether two given progress value are different enough from each other. Used for filtering out progress @@ -52,23 +74,3 @@ private func didProgressChangeSignificantly( return current.receivedBytes - previous.receivedBytes > 1024 * 1024 * 1024 } - -private func report(progress: DownloadProgress, for artifact: DownloadableArtifacts.Item) { - let byteCountFormatter = ByteCountFormatter() - - if let total = progress.totalBytes { - print(""" - \(artifact.remoteURL.lastPathComponent) \( - byteCountFormatter - .string(fromByteCount: Int64(progress.receivedBytes)) - )/\( - byteCountFormatter - .string(fromByteCount: Int64(total)) - ) - """) - } else { - print( - "\(artifact.remoteURL.lastPathComponent) \(byteCountFormatter.string(fromByteCount: Int64(progress.receivedBytes)))" - ) - } -} From 92dba49175f2de75babe52baec81c7569aab382d Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Mon, 20 Jan 2025 20:29:11 -0500 Subject: [PATCH 07/11] Implement comments for logging output --- .../Generator/SwiftSDKGenerator+Download.swift | 11 ++++------- .../Generator/SwiftSDKGenerator.swift | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift index ee13d66..3abeb9e 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift @@ -57,13 +57,10 @@ extension SwiftSDKGenerator { return result } - if isVerbose { - logger.debug("Using downloaded artifacts in these locations:") - for path in results.map(\.path) { - logger.debug("-", metadata: ["path": .string(path.string)]) - } - } else { - logger.info("Using downloaded artifacts from cache") + logger.info("Using downloaded artifacts from cache") + logger.debug("Using downloaded artifacts in these locations:") + for path in results.map(\.path) { + logger.debug("-", metadata: ["path": .string(path.string)]) } } diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift index 27879f8..15d6d5d 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift @@ -254,7 +254,7 @@ public actor SwiftSDKGenerator { if isVerbose { let cmd = "ls \(tmp)" let lsOutput = try await Shell.readStdout(cmd) - logger.debug("\(lsOutput)", metadata: ["cmd": .string(cmd)]) + logger.debug("", metadata: ["cmd": .string(cmd), "output": .string(lsOutput)]) } try await Shell.run( From 0bbfce5e5552b3cf3c5df766c4bf38b236687a72 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 21 Jan 2025 09:26:32 -0500 Subject: [PATCH 08/11] Add log text to lsOutput print --- Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift index 15d6d5d..5e870f4 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator.swift @@ -254,7 +254,7 @@ public actor SwiftSDKGenerator { if isVerbose { let cmd = "ls \(tmp)" let lsOutput = try await Shell.readStdout(cmd) - logger.debug("", metadata: ["cmd": .string(cmd), "output": .string(lsOutput)]) + logger.debug("Files unpacked from deb file", metadata: ["cmd": .string(cmd), "output": .string(lsOutput)]) } try await Shell.run( From b632cf2333724709e9d9fe2f5ad7e0c4100e69fe Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 21 Jan 2025 21:09:27 -0500 Subject: [PATCH 09/11] Collect all downloaded artifact paths in single log message metadata --- .../Generator/SwiftSDKGenerator+Download.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift index 3abeb9e..46aeb44 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Download.swift @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// import AsyncAlgorithms +import Logging import Helpers import RegexBuilder @@ -23,6 +24,12 @@ import struct SystemPackage.FilePath private let ubuntuAMD64Mirror = "http://gb.archive.ubuntu.com/ubuntu" private let ubuntuARM64Mirror = "http://ports.ubuntu.com/ubuntu-ports" +extension FilePath { + var metadataValue: Logger.MetadataValue { + .string(self.string) + } +} + extension SwiftSDKGenerator { func downloadArtifacts( _ client: some HTTPClientProtocol, _ engine: QueryEngine, @@ -58,10 +65,9 @@ extension SwiftSDKGenerator { } logger.info("Using downloaded artifacts from cache") - logger.debug("Using downloaded artifacts in these locations:") - for path in results.map(\.path) { - logger.debug("-", metadata: ["path": .string(path.string)]) - } + logger.debug("Using downloaded artifacts in these locations.", metadata: [ + "paths": .array(results.map(\.path.metadataValue)) + ]) } func downloadUbuntuPackages( From 7142b01bf9ff768e707e555ffe95e92b929c4cd9 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 21 Jan 2025 21:15:47 -0500 Subject: [PATCH 10/11] Update README supported platforms section to note Linux is now supported - This was missed in the last set of changes, but important to note that the generator can now generate Swift SDKs to cross compile between different Linux distributions. --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0e723e9..e7faa25 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,8 @@ installed, in case you've used the `swift experimental-sdk install` command befo ## Supported platforms and minimum versions -macOS as a host platform and a few Linux distributions as target platforms are supported by the generator. -Support for Linux as a host platform is currently in development. Eventually, the generator will allow cross-compiling between any -Linux distributions officially supported by the Swift project. +macOS as a host platform and Linux as both host and target platforms are supported by the generator. +Now, the generator allows cross-compiling between any Linux distributions officially supported by the Swift project. | Platform | Supported Version as Host | Supported Version as Target | | -: | :- | :- | From f0ab4edc204c2914300ec4060717048f7b8bbc25 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Wed, 5 Feb 2025 14:51:08 +0000 Subject: [PATCH 11/11] Apply suggestions from code review --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fb05cb4..9f0fd27 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ installed, in case you've used the `swift experimental-sdk install` command befo ## Supported platforms and minimum versions macOS as a host platform and Linux as both host and target platforms are supported by the generator. -Now, the generator allows cross-compiling between any Linux distributions officially supported by the Swift project. +The generator also allows cross-compiling between any Linux distributions officially supported by the Swift project. | Platform | Supported Version as Host | Supported Version as Target | | -: | :- | :- |