Skip to content

Commit 902fc47

Browse files
committed
JavaKit: add source/target versions to Java2Swift.config
Fixes: #152
1 parent 286b856 commit 902fc47

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@
1515
import Foundation
1616
import PackagePlugin
1717

18+
typealias JavaVersion = Int
19+
20+
/// Note: there is a copy of this struct in the Java2Swift plugin. They
21+
/// must be kept in sync.
22+
struct Configuration: Codable {
23+
var sourceCompatibility: JavaVersion?
24+
var targetCompatibility: JavaVersion?
25+
}
26+
27+
extension Configuration {
28+
var compilerVersionArgs: [String] {
29+
var compilerVersionArgs = [String]()
30+
31+
if let sourceCompatibility {
32+
compilerVersionArgs += ["--source", String(sourceCompatibility)]
33+
}
34+
if let targetCompatibility {
35+
compilerVersionArgs += ["--target", String(targetCompatibility)]
36+
}
37+
38+
return compilerVersionArgs
39+
}
40+
}
41+
1842
@main
1943
struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
2044
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] {
@@ -32,6 +56,13 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
3256
// so we cannot eliminate this deprecation warning.
3357
let sourceDir = target.directory.string
3458

59+
// The name of the configuration file JavaKit.config from the target for
60+
// which we are generating Swift wrappers for Java classes.
61+
let configFile = URL(filePath: sourceDir)
62+
.appending(path: "Java2Swift.config")
63+
let configData = try Data(contentsOf: configFile)
64+
let config = try JSONDecoder().decode(Configuration.self, from: configData)
65+
3566
// The class files themselves will be generated into the build directory
3667
// for this target.
3768
let classFiles = javaFiles.map { sourceFileURL in
@@ -60,7 +91,7 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
6091
arguments: javaFiles.map { $0.path(percentEncoded: false) } + [
6192
"-d", javaClassFileURL.path(),
6293
"-parameters", // keep parameter names, which allows us to emit them in generated Swift decls
63-
],
94+
] + config.compilerVersionArgs,
6495
inputFiles: javaFiles,
6596
outputFiles: classFiles
6697
)

Sources/Java2SwiftLib/Configuration.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
package typealias JavaVersion = Int
16+
1517
/// Configuration for the Java2Swift translation tool, provided on a per-target
1618
/// basis.
1719
///
@@ -26,8 +28,18 @@ package struct Configuration: Codable {
2628
/// the corresponding Swift names (e.g., JavaVector).
2729
package var classes: [String: String] = [:]
2830

29-
package init(classPath: String? = nil, classes: [String : String] = [:]) {
31+
package var sourceCompatibility: JavaVersion?
32+
package var targetCompatibility: JavaVersion?
33+
34+
package init(
35+
classPath: String? = nil,
36+
classes: [String : String] = [:],
37+
sourceCompatibility: JavaVersion? = nil,
38+
targetCompatibility: JavaVersion? = nil
39+
) {
3040
self.classPath = classPath
3141
self.classes = classes
42+
self.sourceCompatibility = sourceCompatibility
43+
self.targetCompatibility = targetCompatibility
3244
}
3345
}

0 commit comments

Comments
 (0)