|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import PackagePlugin |
| 17 | + |
| 18 | +fileprivate let SwiftJavaConfigFileName = "swift-java.config" |
| 19 | + |
| 20 | +@main |
| 21 | +struct Java2SwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin { |
| 22 | + |
| 23 | + var pluginName: String = "swift-java-bootstrap" |
| 24 | + var verbose: Bool = getEnvironmentBool("SWIFT_JAVA_VERBOSE") |
| 25 | + |
| 26 | + func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] { |
| 27 | + log("Create build commands for target '\(target.name)'") |
| 28 | + guard let sourceModule = target.sourceModule else { return [] } |
| 29 | + |
| 30 | + let executable = try context.tool(named: "SwiftJavaBootstrapJavaTool").url |
| 31 | + var commands: [Command] = [] |
| 32 | + |
| 33 | + // Note: Target doesn't have a directoryURL counterpart to directory, |
| 34 | + // so we cannot eliminate this deprecation warning. |
| 35 | + let sourceDir = target.directory.string |
| 36 | + |
| 37 | + // The name of the configuration file JavaKit.config from the target for |
| 38 | + // which we are generating Swift wrappers for Java classes. |
| 39 | + let configFile = URL(filePath: sourceDir) |
| 40 | + .appending(path: SwiftJavaConfigFileName) |
| 41 | + let config = try readConfiguration(sourceDir: sourceDir) |
| 42 | + |
| 43 | + log("Config on path: \(configFile.path(percentEncoded: false))") |
| 44 | + log("Config was: \(config)") |
| 45 | + var javaDependencies = config.dependencies ?? [] |
| 46 | + |
| 47 | + /// Find the manifest files from other Java2Swift executions in any targets |
| 48 | + /// this target depends on. |
| 49 | + var dependentConfigFiles: [(String, URL)] = [] |
| 50 | + func searchForConfigFiles(in target: any Target) { |
| 51 | + // log("Search for config files in target: \(target.name)") |
| 52 | + let dependencyURL = URL(filePath: target.directory.string) |
| 53 | + |
| 54 | + // Look for a config file within this target. |
| 55 | + let dependencyConfigURL = dependencyURL |
| 56 | + .appending(path: SwiftJavaConfigFileName) |
| 57 | + let dependencyConfigString = dependencyConfigURL |
| 58 | + .path(percentEncoded: false) |
| 59 | + |
| 60 | + if FileManager.default.fileExists(atPath: dependencyConfigString) { |
| 61 | + dependentConfigFiles.append((target.name, dependencyConfigURL)) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Process direct dependencies of this target. |
| 66 | + for dependency in target.dependencies { |
| 67 | + switch dependency { |
| 68 | + case .target(let target): |
| 69 | + searchForConfigFiles(in: target) |
| 70 | + |
| 71 | + case .product(let product): |
| 72 | + for target in product.targets { |
| 73 | + searchForConfigFiles(in: target) |
| 74 | + } |
| 75 | + |
| 76 | + @unknown default: |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Process indirect target dependencies. |
| 82 | + for dependency in target.recursiveTargetDependencies { |
| 83 | + searchForConfigFiles(in: dependency) |
| 84 | + } |
| 85 | + |
| 86 | + var arguments: [String] = [] |
| 87 | + arguments += argumentsModuleName(sourceModule: sourceModule) |
| 88 | + arguments += argumentsOutputDirectory(context: context) |
| 89 | + |
| 90 | + arguments += dependentConfigFiles.flatMap { moduleAndConfigFile in |
| 91 | + let (moduleName, configFile) = moduleAndConfigFile |
| 92 | + return [ |
| 93 | + "--depends-on", |
| 94 | + "\(moduleName)=\(configFile.path(percentEncoded: false))" |
| 95 | + ] |
| 96 | + } |
| 97 | + arguments.append(configFile.path(percentEncoded: false)) |
| 98 | + |
| 99 | + let classes = config.classes ?? [:] |
| 100 | + print("Classes to wrap: \(classes.map(\.key))") |
| 101 | + |
| 102 | + /// Determine the set of Swift files that will be emitted by the Java2Swift tool. |
| 103 | + // TODO: this is not precise and won't work with more advanced Java files, e.g. lambdas etc. |
| 104 | + let outputDirectoryGenerated = self.outputDirectory(context: context, generated: true) |
| 105 | + let outputSwiftFiles = classes.map { (javaClassName, swiftName) in |
| 106 | + let swiftNestedName = swiftName.replacingOccurrences(of: ".", with: "+") |
| 107 | + return outputDirectoryGenerated.appending(path: "\(swiftNestedName).swift") |
| 108 | + } |
| 109 | + |
| 110 | + arguments += [ |
| 111 | + "--cache-directory", |
| 112 | + context.pluginWorkDirectoryURL.path(percentEncoded: false) |
| 113 | + ] |
| 114 | + |
| 115 | + // Find the Java .class files generated from prior plugins. |
| 116 | + let compiledClassFiles = sourceModule.pluginGeneratedResources.filter { url in |
| 117 | + url.pathExtension == "class" |
| 118 | + } |
| 119 | + |
| 120 | + if let firstClassFile = compiledClassFiles.first { |
| 121 | + // Keep stripping off parts of the path until we hit the "Java" part. |
| 122 | + // That's where the class path starts. |
| 123 | + var classpath = firstClassFile |
| 124 | + while classpath.lastPathComponent != "Java" { |
| 125 | + classpath.deleteLastPathComponent() |
| 126 | + } |
| 127 | + arguments += ["--classpath", classpath.path()] |
| 128 | + } |
| 129 | + |
| 130 | + var fetchDependenciesOutputFiles: [URL] = [] |
| 131 | + if let dependencies = config.dependencies, !dependencies.isEmpty { |
| 132 | + let displayName = "Fetch (Java) dependencies for Swift target \(sourceModule.name)" |
| 133 | + log("Prepared: \(displayName)") |
| 134 | + |
| 135 | + let arguments = [ |
| 136 | + "--fetch", configFile.path(percentEncoded: false), |
| 137 | + "--module-name", sourceModule.name, |
| 138 | + "--output-directory", outputDirectory(context: context, generated: false).path(percentEncoded: false) |
| 139 | + ] |
| 140 | + |
| 141 | + log("Command: \(executable) \(arguments.joined(separator: " "))") |
| 142 | + |
| 143 | + fetchDependenciesOutputFiles += [ |
| 144 | + outputFilePath(context: context, generated: false, filename: "\(sourceModule.name).swift-java.classpath") |
| 145 | + ] |
| 146 | + |
| 147 | + commands += [ |
| 148 | + .buildCommand( |
| 149 | + displayName: displayName, |
| 150 | + executable: executable, |
| 151 | + arguments: arguments, |
| 152 | + inputFiles: [configFile], |
| 153 | + outputFiles: fetchDependenciesOutputFiles |
| 154 | + ) |
| 155 | + ] |
| 156 | + } else { |
| 157 | + log("No dependencies to fetch for target \(sourceModule.name)") |
| 158 | + } |
| 159 | + |
| 160 | + return commands |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +extension Java2SwiftBuildToolPlugin { |
| 165 | + func argumentsModuleName(sourceModule: Target) -> [String] { |
| 166 | + return [ |
| 167 | + "--module-name", sourceModule.name |
| 168 | + ] |
| 169 | + } |
| 170 | + |
| 171 | + func argumentsOutputDirectory(context: PluginContext, generated: Bool = true) -> [String] { |
| 172 | + return [ |
| 173 | + "--output-directory", |
| 174 | + outputDirectory(context: context, generated: generated).path(percentEncoded: false) |
| 175 | + ] |
| 176 | + } |
| 177 | + |
| 178 | + func outputDirectory(context: PluginContext, generated: Bool = true) -> URL { |
| 179 | + let dir = context.pluginWorkDirectoryURL |
| 180 | + if generated { |
| 181 | + return dir.appending(path: "generated") |
| 182 | + } else { |
| 183 | + return dir |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + func outputFilePath(context: PluginContext, generated: Bool, filename: String) -> URL { |
| 188 | + outputDirectory(context: context, generated: generated).appending(path: filename) |
| 189 | + } |
| 190 | +} |
0 commit comments