forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJExtractSwiftCommandPlugin.swift
More file actions
159 lines (130 loc) · 6.49 KB
/
JExtractSwiftCommandPlugin.swift
File metadata and controls
159 lines (130 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import PackagePlugin
@main
final class JExtractSwiftCommandPlugin: SwiftJavaPluginProtocol, BuildToolPlugin, CommandPlugin {
var pluginName: String = "swift-java-command"
var verbose: Bool = getEnvironmentBool("SWIFT_JAVA_VERBOSE")
/// Build the target before attempting to extract from it.
/// This avoids trying to extract from broken sources.
///
/// You may disable this if confident that input targets sources are correct and there's no need to kick off a pre-build for some reason.
var buildInputs: Bool = true
/// Build the target once swift-java sources have been generated.
/// This helps verify that the generated output is correct, and won't miscompile on the next build.
var buildOutputs: Bool = true
func createBuildCommands(context: PluginContext, target: any Target) async throws -> [Command] {
// FIXME: This is not a build plugin but SwiftPM forces us to impleme the protocol anyway? rdar://139556637
return []
}
func performCommand(context: PluginContext, arguments: [String]) throws {
// Plugin can't have dependencies, so we have some naive argument parsing instead:
self.verbose = arguments.contains("-v") || arguments.contains("--verbose")
for target in context.package.targets {
guard getSwiftJavaConfigPath(target: target) != nil else {
log("[swift-java-command] Skipping jextract step: Missing swift-java.config for target '\(target.name)'")
continue
}
do {
let extraArguments = arguments.filter { arg in
arg != "-v" && arg != "--verbose"
}
print("[swift-java-command] Extracting Java wrappers from target: '\(target.name)'...")
try performCommand(context: context, target: target, extraArguments: extraArguments)
} catch {
print("[swift-java-command] error: Failed to extract from target '\(target.name)': \(error)")
}
print("[swift-java-command] Done.")
}
print("[swift-java-command] Generating sources: " + "done".green + ".")
}
func prepareJExtractArguments(context: PluginContext, target: Target) throws -> [String] {
guard let sourceModule = target.sourceModule else { return [] }
// Note: Target doesn't have a directoryURL counterpart to directory,
// so we cannot eliminate this deprecation warning.
let sourceDir = target.directory.string
let configuration = try readConfiguration(sourceDir: "\(sourceDir)")
var arguments: [String] = [
"--input-swift", sourceDir,
"--module-name", sourceModule.name,
"--output-java", context.outputJavaDirectory.path(percentEncoded: false),
"--output-swift", context.outputSwiftDirectory.path(percentEncoded: false),
// TODO: "--build-cache-directory", ...
// Since plugins cannot depend on libraries we cannot detect what the output files will be,
// as it depends on the contents of the input files. Therefore we have to implement this as a prebuild plugin.
// We'll have to make up some caching inside the tool so we don't re-parse files which have not changed etc.
]
// arguments.append(sourceDir) // TODO: we could do this shape maybe? to have the dirs last?
if let package = configuration?.javaPackage, !package.isEmpty {
["--java-package", package]
}
return arguments
}
/// Perform the command on a specific target.
func performCommand(context: PluginContext, target: Target, extraArguments: [String]) throws {
guard let sourceModule = target.sourceModule else { return }
if self.buildInputs {
// Make sure the target can builds properly
log("Pre-building target '\(target.name)' before extracting sources...")
let targetBuildResult = try self.packageManager.build(.target(target.name), parameters: .init())
guard targetBuildResult.succeeded else {
print("[swift-java-command] Build of '\(target.name)' failed: \(targetBuildResult.logText)")
return
}
}
let arguments = try prepareJExtractArguments(context: context, target: target)
try runExtract(context: context, target: target, arguments: arguments + extraArguments)
if self.buildOutputs {
// Building the *products* since we need to build the dylib that contains our newly generated sources,
// so just building the target again would not be enough. We build all products which we affected using
// our source generation, which usually would be just a product dylib with our library.
//
// In practice, we'll always want to build after generating; either here,
// or via some other task before we run any Java code, calling into Swift.
log("Post-extract building products with target '\(target.name)'...")
for product in context.package.products where product.targets.contains(where: { $0.id == target.id }) {
log("Post-extract building product '\(product.name)'...")
let buildResult = try self.packageManager.build(.product(product.name), parameters: .init())
if buildResult.succeeded {
log("Post-extract build: " + "done".green + ".")
} else {
log("Post-extract build: " + "done".red + "!")
}
}
}
}
func runExtract(context: PluginContext, target: Target, arguments: [String]) throws {
let process = Process()
process.executableURL = try context.tool(named: "SwiftJavaTool").url
process.arguments = arguments
do {
log("Execute: \(process.executableURL!.absoluteURL.relativePath) \(arguments.joined(separator: " "))")
try process.run()
process.waitUntilExit()
assert(process.terminationStatus == 0, "Process failed with exit code: \(process.terminationStatus)")
} catch {
print("[swift-java-command] Failed to extract Java sources for target: '\(target.name); Error: \(error)")
}
}
}
// Mini coloring helper, since we cannot have dependencies we keep it minimal here
extension String {
var red: String {
"\u{001B}[0;31m" + "\(self)" + "\u{001B}[0;0m"
}
var green: String {
"\u{001B}[0;32m" + "\(self)" + "\u{001B}[0;0m"
}
}