|
| 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 | +@main |
| 19 | +final class JExtractSwiftCommandPlugin: BuildToolPlugin, CommandPlugin { |
| 20 | + |
| 21 | + var verbose: Bool = false |
| 22 | + |
| 23 | + func createBuildCommands(context: PackagePlugin.PluginContext, target: any PackagePlugin.Target) async throws -> [PackagePlugin.Command] { |
| 24 | + // FIXME: This is not a build plugin but SwiftPM forces us to impleme the protocol anyway? rdar://139556637 |
| 25 | + return [] |
| 26 | + } |
| 27 | + |
| 28 | + func performCommand(context: PluginContext, arguments: [String]) throws { |
| 29 | + self.verbose = arguments.contains("-v") || arguments.contains("--verbose") |
| 30 | + |
| 31 | + let selectedTargets: [String] = |
| 32 | + if let last = arguments.lastIndex(where: { $0.starts(with: "-")}), |
| 33 | + last < arguments.endIndex { |
| 34 | + Array(arguments[..<last]) |
| 35 | + } else { |
| 36 | + [] |
| 37 | + } |
| 38 | + |
| 39 | + for target in context.package.targets { |
| 40 | + guard hasSwiftJavaConfig(target: target) else { |
| 41 | + log("Skipping target '\(target.name), has no 'swift-java.config' file") |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + do { |
| 46 | + print("[swift-java] Extracting Java wrappers from target: '\(target.name)'...") |
| 47 | + try performCommand(context: context, target: target, arguments: arguments) |
| 48 | + } catch { |
| 49 | + print("[swift-java] error: Failed to extract from target '\(target.name)': \(error)") |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Perform the command on a specific target. |
| 55 | + func performCommand(context: PluginContext, target: Target, arguments: [String]) throws { |
| 56 | + // Make sure the target can builds properly |
| 57 | + try self.packageManager.build(.target(target.name), parameters: .init()) |
| 58 | + |
| 59 | + guard let sourceModule = target.sourceModule else { return } |
| 60 | + |
| 61 | + // Note: Target doesn't have a directoryURL counterpart to directory, |
| 62 | + // so we cannot eliminate this deprecation warning. |
| 63 | + let sourceDir = target.directory.string |
| 64 | + |
| 65 | + let configuration = try readConfiguration(sourceDir: "\(sourceDir)") |
| 66 | + |
| 67 | + // We use the the usual maven-style structure of "src/[generated|main|test]/java/..." |
| 68 | + // that is common in JVM ecosystem |
| 69 | + let outputDirectoryJava = context.pluginWorkDirectoryURL |
| 70 | + .appending(path: "src") |
| 71 | + .appending(path: "generated") |
| 72 | + .appending(path: "java") |
| 73 | + let outputDirectorySwift = context.pluginWorkDirectoryURL |
| 74 | + .appending(path: "src") |
| 75 | + .appending(path: "generated") |
| 76 | + .appending(path: "Sources") |
| 77 | + |
| 78 | + var arguments: [String] = [ |
| 79 | + "--swift-module", sourceModule.name, |
| 80 | + "--package-name", configuration.javaPackage, |
| 81 | + "--output-directory-java", outputDirectoryJava.path(percentEncoded: false), |
| 82 | + "--output-directory-swift", outputDirectorySwift.path(percentEncoded: false), |
| 83 | + // TODO: "--build-cache-directory", ... |
| 84 | + // Since plugins cannot depend on libraries we cannot detect what the output files will be, |
| 85 | + // as it depends on the contents of the input files. Therefore we have to implement this as a prebuild plugin. |
| 86 | + // We'll have to make up some caching inside the tool so we don't re-parse files which have not changed etc. |
| 87 | + ] |
| 88 | + arguments.append(sourceDir) |
| 89 | + |
| 90 | + print("PLUGIN: jextract \(arguments.joined(separator: " "))") |
| 91 | + |
| 92 | + try runExtract(context: context, target: target, arguments: arguments) |
| 93 | + } |
| 94 | + |
| 95 | + func runExtract(context: PluginContext, target: Target, arguments: [String]) throws { |
| 96 | + let process = Process() |
| 97 | + process.executableURL = try context.tool(named: "JExtractSwiftTool").url |
| 98 | + process.arguments = arguments |
| 99 | + |
| 100 | + do { |
| 101 | + try process.run() |
| 102 | + process.waitUntilExit() |
| 103 | + |
| 104 | + assert(process.terminationStatus == 0, "Process failed with exit code: \(process.terminationStatus)") |
| 105 | + } catch { |
| 106 | + print("[swift-java][command] Failed to extract Java sources for target: '\(target.name); Error: \(error)") |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + func log(message: @autoclosure () -> String) { |
| 111 | + if self.verbose { |
| 112 | + print("[swift-java] \(message())") |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments