diff --git a/Plugins/Java2SwiftPlugin/Configuration.swift b/Plugins/Java2SwiftPlugin/Configuration.swift index 6b9b4076..c9b17d06 100644 --- a/Plugins/Java2SwiftPlugin/Configuration.swift +++ b/Plugins/Java2SwiftPlugin/Configuration.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +typealias JavaVersion = Int + /// Configuration for the Java2Swift translation tool, provided on a per-target /// basis. /// @@ -25,4 +27,10 @@ struct Configuration: Codable { /// canonical Java class names (e.g., java.util.Vector) and the values are /// the corresponding Swift names (e.g., JavaVector). var classes: [String: String] = [:] + + // Compile for the specified Java SE release. + var sourceCompatibility: JavaVersion? + + // Generate class files suitable for the specified Java SE release. + var targetCompatibility: JavaVersion? } diff --git a/Plugins/JavaCompilerPlugin/Configuration.swift b/Plugins/JavaCompilerPlugin/Configuration.swift new file mode 100644 index 00000000..96180760 --- /dev/null +++ b/Plugins/JavaCompilerPlugin/Configuration.swift @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +typealias JavaVersion = Int + +/// Configuration for the Java2Swift translation tool, provided on a per-target +/// basis. +/// +/// Note: there is a copy of this struct in the Java2Swift library. They +/// must be kept in sync. +struct Configuration: Codable { + /// The Java class path that should be passed along to the Java2Swift tool. + var classPath: String? = nil + + /// The Java classes that should be translated to Swift. The keys are + /// canonical Java class names (e.g., java.util.Vector) and the values are + /// the corresponding Swift names (e.g., JavaVector). + var classes: [String: String] = [:] + + // Compile for the specified Java SE release. + var sourceCompatibility: JavaVersion? + + // Generate class files suitable for the specified Java SE release. + var targetCompatibility: JavaVersion? +} + +extension Configuration { + var compilerVersionArgs: [String] { + var compilerVersionArgs = [String]() + + if let sourceCompatibility { + compilerVersionArgs += ["--source", String(sourceCompatibility)] + } + if let targetCompatibility { + compilerVersionArgs += ["--target", String(targetCompatibility)] + } + + return compilerVersionArgs + } +} diff --git a/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift b/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift index 64e1d2a7..518073a2 100644 --- a/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift +++ b/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift @@ -32,6 +32,17 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin { // so we cannot eliminate this deprecation warning. let sourceDir = target.directory.string + // The name of the configuration file JavaKit.config from the target for + // which we are generating Swift wrappers for Java classes. + let configFile = URL(filePath: sourceDir).appending(path: "Java2Swift.config") + let config: Configuration? + + if let configData = try? Data(contentsOf: configFile) { + config = try? JSONDecoder().decode(Configuration.self, from: configData) + } else { + config = nil + } + // The class files themselves will be generated into the build directory // for this target. let classFiles = javaFiles.map { sourceFileURL in @@ -60,7 +71,7 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin { arguments: javaFiles.map { $0.path(percentEncoded: false) } + [ "-d", javaClassFileURL.path(), "-parameters", // keep parameter names, which allows us to emit them in generated Swift decls - ], + ] + (config?.compilerVersionArgs ?? []), inputFiles: javaFiles, outputFiles: classFiles ) diff --git a/Sources/Java2SwiftLib/Configuration.swift b/Sources/Java2SwiftLib/Configuration.swift index 6617b4d0..be92dd84 100644 --- a/Sources/Java2SwiftLib/Configuration.swift +++ b/Sources/Java2SwiftLib/Configuration.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +package typealias JavaVersion = Int + /// Configuration for the Java2Swift translation tool, provided on a per-target /// basis. /// @@ -26,8 +28,18 @@ package struct Configuration: Codable { /// the corresponding Swift names (e.g., JavaVector). package var classes: [String: String] = [:] - package init(classPath: String? = nil, classes: [String : String] = [:]) { + package var sourceCompatibility: JavaVersion? + package var targetCompatibility: JavaVersion? + + package init( + classPath: String? = nil, + classes: [String : String] = [:], + sourceCompatibility: JavaVersion? = nil, + targetCompatibility: JavaVersion? = nil + ) { self.classPath = classPath self.classes = classes + self.sourceCompatibility = sourceCompatibility + self.targetCompatibility = targetCompatibility } }