1515import Foundation
1616import 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
1943struct 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 )
0 commit comments