15
15
import Foundation
16
16
import PackagePlugin
17
17
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
+
18
42
@main
19
43
struct JavaCompilerBuildToolPlugin : BuildToolPlugin {
20
44
func createBuildCommands( context: PluginContext , target: Target ) throws -> [ Command ] {
@@ -32,6 +56,13 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
32
56
// so we cannot eliminate this deprecation warning.
33
57
let sourceDir = target. directory. string
34
58
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
+
35
66
// The class files themselves will be generated into the build directory
36
67
// for this target.
37
68
let classFiles = javaFiles. map { sourceFileURL in
@@ -60,7 +91,7 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
60
91
arguments: javaFiles. map { $0. path ( percentEncoded: false ) } + [
61
92
" -d " , javaClassFileURL. path ( ) ,
62
93
" -parameters " , // keep parameter names, which allows us to emit them in generated Swift decls
63
- ] ,
94
+ ] + config . compilerVersionArgs ,
64
95
inputFiles: javaFiles,
65
96
outputFiles: classFiles
66
97
)
0 commit comments