|
| 1 | +// |
| 2 | +// File.swift |
| 3 | +// SwiftBuild |
| 4 | +// |
| 5 | +// Created by Steven Wu on 12/13/25. |
| 6 | +// |
| 7 | + |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This source file is part of the Swift open source project |
| 11 | +// |
| 12 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 13 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 14 | +// |
| 15 | +// See http://swift.org/LICENSE.txt for license information |
| 16 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 17 | +// |
| 18 | +//===----------------------------------------------------------------------===// |
| 19 | + |
| 20 | +import SWBCore |
| 21 | +import SWBUtil |
| 22 | +import SWBMacro |
| 23 | +import Foundation |
| 24 | +import SWBProtocol |
| 25 | + |
| 26 | +final class CASConfigFileTaskProducer: StandardTaskProducer, TaskProducer { |
| 27 | + private let targetContexts: [TaskProducerContext] |
| 28 | + |
| 29 | + init(context globalContext: TaskProducerContext, targetContexts: [TaskProducerContext]) { |
| 30 | + self.targetContexts = targetContexts |
| 31 | + super.init(globalContext) |
| 32 | + } |
| 33 | + |
| 34 | + func generateTasks() async -> [any SWBCore.PlannedTask] { |
| 35 | + var tasks = [any PlannedTask]() |
| 36 | + do { |
| 37 | + let casConfigFiles = try Dictionary(try await targetContexts.concurrentMap(maximumParallelism: 100) { (targetContext: TaskProducerContext) async throws -> (Path, ByteString)? in |
| 38 | + let scope = targetContext.settings.globalScope |
| 39 | + |
| 40 | + // If compilation caching is not on, then there is no file to write. |
| 41 | + // The condition here is more relax than the actual check in the compile task generation |
| 42 | + // since it won't hurt if the file is not used. |
| 43 | + guard scope.evaluate(BuiltinMacros.CLANG_ENABLE_COMPILE_CACHE) || scope.evaluate(BuiltinMacros.SWIFT_ENABLE_COMPILE_CACHE) else { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + // FIXME: we need consistent CAS configuration across all languages. |
| 48 | + if !scope.evaluate(BuiltinMacros.COMPILATION_CACHE_REMOTE_SERVICE_PATH).isEmpty && !scope.evaluate(BuiltinMacros.COMPILATION_CACHE_REMOTE_SUPPORTED_LANGUAGES).isEmpty { |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + let casOpts = try CASOptions.create(scope, .compiler(.other(dialectName: "swift"))) |
| 53 | + struct CASConfig: Encodable { |
| 54 | + let CASPath: String |
| 55 | + let PluginPath: String? |
| 56 | + } |
| 57 | + let content = try JSONEncoder().encode(CASConfig(CASPath: casOpts.casPath.str, PluginPath: casOpts.pluginPath?.str)) |
| 58 | + let path = scope.evaluate(BuiltinMacros.TARGET_TEMP_DIR).join(".cas-config") |
| 59 | + return (path, ByteString(content)) |
| 60 | + }.compactMap { $0 }, uniquingKeysWith: { first, second in |
| 61 | + guard first == second else { |
| 62 | + throw StubError.error("Unexpected difference in CAS config file.\nPath: \(first.asString)\nContent:\(second.asString)") |
| 63 | + } |
| 64 | + return first |
| 65 | + }) |
| 66 | + |
| 67 | + for (configFilePath, configFileContent) in casConfigFiles { |
| 68 | + await appendGeneratedTasks(&tasks) { delegate in |
| 69 | + context.writeFileSpec.constructFileTasks(CommandBuildContext(producer: context, scope: context.settings.globalScope, inputs: [], output: configFilePath), delegate, contents: configFileContent, permissions: nil, preparesForIndexing: true, additionalTaskOrderingOptions: [.immediate]) |
| 70 | + } |
| 71 | + } |
| 72 | + } catch { |
| 73 | + self.context.error(error.localizedDescription) |
| 74 | + } |
| 75 | + return tasks |
| 76 | + } |
| 77 | +} |
0 commit comments