|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 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 SWBCore |
| 14 | +import SWBUtil |
| 15 | +import SWBMacro |
| 16 | +import SWBTaskConstruction |
| 17 | + |
| 18 | +final class ExtensionPointExtractorTaskProducer: PhasedTaskProducer, TaskProducer { |
| 19 | + |
| 20 | + override var defaultTaskOrderingOptions: TaskOrderingOptions { |
| 21 | + return .unsignedProductRequirement |
| 22 | + } |
| 23 | + |
| 24 | + private func filterBuildFiles(_ buildFiles: [BuildFile]?, identifiers: [String], buildFilesProcessingContext: BuildFilesProcessingContext) -> [FileToBuild] { |
| 25 | + guard let buildFiles else { |
| 26 | + return [] |
| 27 | + } |
| 28 | + |
| 29 | + let fileTypes = identifiers.compactMap { identifier in |
| 30 | + context.lookupFileType(identifier: identifier) |
| 31 | + } |
| 32 | + |
| 33 | + return fileTypes.flatMap { fileType in |
| 34 | + buildFiles.compactMap { buildFile in |
| 35 | + guard let resolvedBuildFileInfo = try? self.context.resolveBuildFileReference(buildFile), |
| 36 | + !buildFilesProcessingContext.isExcluded(resolvedBuildFileInfo.absolutePath, filters: buildFile.platformFilters), |
| 37 | + resolvedBuildFileInfo.fileType.conformsTo(fileType) else { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + return FileToBuild(absolutePath: resolvedBuildFileInfo.absolutePath, fileType: fileType) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + func generateTasks() async -> [any PlannedTask] { |
| 47 | + |
| 48 | + guard ExtensionPointExtractorSpec.shouldConstructTask(scope: context.settings.globalScope, productType: context.productType, isApplePlatform: context.isApplePlatform) else { |
| 49 | + return [] |
| 50 | + } |
| 51 | + |
| 52 | + context.addDeferredProducer { |
| 53 | + |
| 54 | + let scope = self.context.settings.globalScope |
| 55 | + let buildFilesProcessingContext = BuildFilesProcessingContext(scope) |
| 56 | + |
| 57 | + let perArchConstMetadataFiles = self.context.generatedSwiftConstMetadataFiles() |
| 58 | + |
| 59 | + let constMetadataFiles: [Path] |
| 60 | + if let firstArch = perArchConstMetadataFiles.keys.sorted().first { |
| 61 | + constMetadataFiles = perArchConstMetadataFiles[firstArch]! |
| 62 | + } else { |
| 63 | + constMetadataFiles = [] |
| 64 | + } |
| 65 | + |
| 66 | + let constMetadataFilesToBuild = constMetadataFiles.map { absolutePath -> FileToBuild in |
| 67 | + let fileType = self.context.workspaceContext.core.specRegistry.getSpec("file") as! FileTypeSpec |
| 68 | + return FileToBuild(absolutePath: absolutePath, fileType: fileType) |
| 69 | + } |
| 70 | + |
| 71 | + let inputs = constMetadataFilesToBuild |
| 72 | + guard inputs.isEmpty == false else { |
| 73 | + return [] |
| 74 | + } |
| 75 | + |
| 76 | + var deferredTasks: [any PlannedTask] = [] |
| 77 | + |
| 78 | + let cbc = CommandBuildContext(producer: self.context, scope: scope, inputs: inputs, resourcesDir: buildFilesProcessingContext.resourcesDir) |
| 79 | + await self.appendGeneratedTasks(&deferredTasks) { delegate in |
| 80 | + let domain = self.context.settings.platform?.name ?? "" |
| 81 | + guard let spec = self.context.specRegistry.getSpec("com.apple.compilers.extract-appextensionpoints", domain:domain) as? ExtensionPointExtractorSpec else { |
| 82 | + return |
| 83 | + } |
| 84 | + await spec.constructTasks(cbc, delegate) |
| 85 | + } |
| 86 | + |
| 87 | + return deferredTasks |
| 88 | + } |
| 89 | + return [] |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +final class AppExtensionInfoPlistGeneratorTaskProducer: PhasedTaskProducer, TaskProducer { |
| 95 | + |
| 96 | + override var defaultTaskOrderingOptions: TaskOrderingOptions { |
| 97 | + return .unsignedProductRequirement |
| 98 | + } |
| 99 | + |
| 100 | + private func filterBuildFiles(_ buildFiles: [BuildFile]?, identifiers: [String], buildFilesProcessingContext: BuildFilesProcessingContext) -> [FileToBuild] { |
| 101 | + guard let buildFiles else { |
| 102 | + return [] |
| 103 | + } |
| 104 | + |
| 105 | + let fileTypes = identifiers.compactMap { identifier in |
| 106 | + context.lookupFileType(identifier: identifier) |
| 107 | + } |
| 108 | + |
| 109 | + return fileTypes.flatMap { fileType in |
| 110 | + buildFiles.compactMap { buildFile in |
| 111 | + guard let resolvedBuildFileInfo = try? self.context.resolveBuildFileReference(buildFile), |
| 112 | + !buildFilesProcessingContext.isExcluded(resolvedBuildFileInfo.absolutePath, filters: buildFile.platformFilters), |
| 113 | + resolvedBuildFileInfo.fileType.conformsTo(fileType) else { |
| 114 | + return nil |
| 115 | + } |
| 116 | + |
| 117 | + return FileToBuild(absolutePath: resolvedBuildFileInfo.absolutePath, fileType: fileType) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + func generateTasks() async -> [any PlannedTask] { |
| 123 | + |
| 124 | + let scope = context.settings.globalScope |
| 125 | + let productType = context.productType |
| 126 | + let isApplePlatform = context.isApplePlatform |
| 127 | + guard AppExtensionPlistGeneratorSpec.shouldConstructTask(scope: scope, productType: productType, isApplePlatform: isApplePlatform) else { |
| 128 | + return [] |
| 129 | + } |
| 130 | + |
| 131 | + let tasks: [any PlannedTask] = [] |
| 132 | + let buildFilesProcessingContext = BuildFilesProcessingContext(scope) |
| 133 | + |
| 134 | + let moduelName = context.settings.globalScope.evaluate(BuiltinMacros.TARGET_NAME) |
| 135 | + let plistPath = buildFilesProcessingContext.tmpResourcesDir.join(Path("\(moduelName)-appextension-generated-info.plist")) |
| 136 | + |
| 137 | + context.addDeferredProducer { |
| 138 | + |
| 139 | + let perArchConstMetadataFiles = self.context.generatedSwiftConstMetadataFiles() |
| 140 | + |
| 141 | + let constMetadataFiles: [Path] |
| 142 | + if let firstArch = perArchConstMetadataFiles.keys.sorted().first { |
| 143 | + constMetadataFiles = perArchConstMetadataFiles[firstArch]! |
| 144 | + } else { |
| 145 | + constMetadataFiles = [] |
| 146 | + } |
| 147 | + |
| 148 | + let constMetadataFilesToBuild = constMetadataFiles.map { absolutePath -> FileToBuild in |
| 149 | + let fileType = self.context.workspaceContext.core.specRegistry.getSpec("file") as! FileTypeSpec |
| 150 | + return FileToBuild(absolutePath: absolutePath, fileType: fileType) |
| 151 | + } |
| 152 | + |
| 153 | + let inputs = constMetadataFilesToBuild |
| 154 | + var deferredTasks: [any PlannedTask] = [] |
| 155 | + |
| 156 | + let cbc = CommandBuildContext(producer: self.context, scope: scope, inputs: inputs, output: plistPath) |
| 157 | + |
| 158 | + await self.appendGeneratedTasks(&deferredTasks) { delegate in |
| 159 | + let domain = self.context.settings.platform?.name ?? "" |
| 160 | + guard let spec = self.context.specRegistry.getSpec("com.apple.compilers.appextension-plist-generator",domain: domain) as? AppExtensionPlistGeneratorSpec else { |
| 161 | + return |
| 162 | + } |
| 163 | + await spec.constructTasks(cbc, delegate) |
| 164 | + } |
| 165 | + |
| 166 | + return deferredTasks |
| 167 | + } |
| 168 | + self.context.addGeneratedInfoPlistContent(plistPath) |
| 169 | + return tasks |
| 170 | + } |
| 171 | +} |
0 commit comments