Skip to content

Commit 7d8b901

Browse files
committed
WIP start introducing jextract mode in swift-java tool
1 parent 6dd0078 commit 7d8b901

File tree

6 files changed

+68
-15
lines changed

6 files changed

+68
-15
lines changed

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ let package = Package(
337337
"JavaKitJar",
338338
"JavaKitNetwork",
339339
"SwiftJavaLib",
340+
"JExtractSwiftLib",
340341
"JavaKitShared",
341342
],
342343

@@ -367,7 +368,7 @@ let package = Package(
367368
name: "JExtractSwiftPlugin",
368369
capability: .buildTool(),
369370
dependencies: [
370-
"JExtractSwiftTool"
371+
"SwiftJavaTool"
371372
]
372373
),
373374
.plugin(
@@ -377,7 +378,7 @@ let package = Package(
377378
permissions: [
378379
]),
379380
dependencies: [
380-
"JExtractSwiftTool"
381+
"SwiftJavaTool"
381382
]
382383
),
383384

Plugins/JExtractSwiftCommandPlugin/JExtractSwiftCommandPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ final class JExtractSwiftCommandPlugin: SwiftJavaPluginProtocol, BuildToolPlugin
130130

131131
func runExtract(context: PluginContext, target: Target, arguments: [String]) throws {
132132
let process = Process()
133-
process.executableURL = try context.tool(named: "JExtractSwiftTool").url
133+
process.executableURL = try context.tool(named: "SwiftJavaTool").url
134134
process.arguments = arguments
135135

136136
do {

Plugins/JExtractSwiftPlugin/JExtractSwiftPlugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct JExtractSwiftBuildToolPlugin: SwiftJavaPluginProtocol, BuildToolPlugin {
2222
var verbose: Bool = getEnvironmentBool("SWIFT_JAVA_VERBOSE")
2323

2424
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
25-
let toolURL = try context.tool(named: "JExtractSwiftTool").url
25+
let toolURL = try context.tool(named: "SwiftJavaTool").url
2626

2727
guard let sourceModule = target.sourceModule else { return [] }
2828

Sources/JavaKitConfigurationShared/Configuration.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public struct Configuration: Codable {
2727

2828
public var javaPackage: String?
2929

30+
public var inputSwift: String?
31+
public var outputSwift: String?
32+
public var outputJava: String?
33+
3034
// ==== java 2 swift ---------------------------------------------------------
3135

3236
/// The Java class path that should be passed along to the Java2Swift tool.

Sources/SwiftJavaTool/JExtractSwiftTool.swift renamed to Sources/SwiftJavaTool/SwiftJava+JExtract.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,29 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Foundation
16+
import ArgumentParser
17+
import SwiftJavaLib
18+
import JavaKit
19+
import JavaKitJar
20+
import SwiftJavaLib
1521
import JExtractSwiftLib
22+
import JavaKitConfigurationShared
23+
24+
/// Extract Java bindings from Swift sources or interface files.
25+
///
26+
/// Example usage:
27+
/// ```
28+
/// > swift-java --input-swift Sources/SwiftyBusiness \
29+
/// --output-swift .build/.../outputs/SwiftyBusiness \
30+
/// --output-Java .build/.../outputs/Java
31+
/// ```
32+
extension SwiftJava {
33+
34+
mutating func jextractSwift(
35+
config: Configuration
36+
) throws {
1637

17-
@main
18-
struct JExtractSwift {
19-
static func main() throws {
20-
let command = SwiftToJava.parseOrExit(CommandLine.arguments)
21-
try command.run()
2238
}
39+
2340
}

Sources/SwiftJavaTool/SwiftJava.swift

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,19 @@ struct SwiftJava: AsyncParsableCommand {
5959
)
6060
var swiftNativeImplementation: [String] = []
6161

62-
@Option(name: .shortAndLong, help: "The directory in which to output the generated Swift files or the Java2Swift configuration file.")
62+
@Option(name: .shortAndLong, help: "Directory containing Swift files which should be extracted into Java bindings. Also known as 'jextract' mode. Must be paired with --output-java and --output-swift.")
63+
var inputSwift: String? = nil
64+
65+
@Option(name: .shortAndLong, help: "The directory where generated Swift files should be written. Generally used with jextract mode.")
66+
var outputSwift: String? = nil
67+
68+
@Option(name: .shortAndLong, help: "The directory where generated Java files should be written. Generally used with jextract mode.")
69+
var outputJava: String? = nil
70+
71+
// TODO: clarify this vs outputSwift (history: outputSwift is jextract, and this was java2swift)
72+
@Option(name: .shortAndLong, help: "The directory in which to output the generated Swift files or the SwiftJava configuration file.")
6373
var outputDirectory: String? = nil
6474

65-
6675
@Option(name: .shortAndLong, help: "Directory where to write cached values (e.g. swift-java.classpath files)")
6776
var cacheDirectory: String? = nil
6877

@@ -87,8 +96,7 @@ struct SwiftJava: AsyncParsableCommand {
8796
var javaPackageFilter: String? = nil
8897

8998
@Argument(
90-
help:
91-
"The input file, which is either a Java2Swift configuration file or (if '-jar' was specified) a Jar file."
99+
help: "The input file, which is either a Java2Swift configuration file or (if '-jar' was specified) a Jar file."
92100
)
93101
var input: String
94102

@@ -163,6 +171,9 @@ struct SwiftJava: AsyncParsableCommand {
163171

164172
/// Fetch dependencies for a module
165173
case fetchDependencies
174+
175+
/// Extract Java bindings from provided Swift sources.
176+
case jextract // TODO: carry jextract specific config here?
166177
}
167178

168179
mutating func run() async {
@@ -172,7 +183,24 @@ struct SwiftJava: AsyncParsableCommand {
172183

173184
// Determine the mode in which we'll execute.
174185
let toolMode: ToolMode
175-
if jar {
186+
// TODO: some options are exclusive to eachother so we should detect that
187+
if let inputSwift {
188+
guard let outputSwift else {
189+
print("[swift-java] --input-swift enabled 'jextract' mode, however no --output-swift directory was provided!")
190+
return
191+
}
192+
guard let outputJava else {
193+
print("[swift-java] --input-swift enabled 'jextract' mode, however no --output-java directory was provided!")
194+
return
195+
}
196+
var c = Configuration()
197+
c.inputSwift = inputSwift
198+
c.outputSwift = outputSwift
199+
c.outputJava = outputJava
200+
config = c
201+
202+
toolMode = .jextract
203+
} else if jar {
176204
if let moduleBaseDir {
177205
config = try readConfiguration(sourceDir: moduleBaseDir.path)
178206
} else {
@@ -251,7 +279,7 @@ struct SwiftJava: AsyncParsableCommand {
251279
// print("[debug][swift-java] Found cached dependency resolver classpath: \(dependencyResolverClasspath)")
252280
// classpathEntries += dependencyResolverClasspath
253281
// }
254-
case .classWrappers:
282+
case .classWrappers, .jextract:
255283
break;
256284
}
257285

@@ -310,6 +338,9 @@ struct SwiftJava: AsyncParsableCommand {
310338
moduleName: moduleName,
311339
cacheDir: effectiveCacheDirectory,
312340
resolvedClasspath: dependencyClasspath)
341+
342+
case .jextract:
343+
try jextractSwift(config: config)
313344
}
314345
} catch {
315346
// We fail like this since throwing out of the run often ends up hiding the failure reason when it is executed as SwiftPM plugin (!)

0 commit comments

Comments
 (0)