Skip to content

Commit 903b273

Browse files
authored
Merge pull request #166 from PADL/lhoward/javac-version-args
2 parents c47a697 + 0233236 commit 903b273

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

Plugins/Java2SwiftPlugin/Configuration.swift

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

15+
typealias JavaVersion = Int
16+
1517
/// Configuration for the Java2Swift translation tool, provided on a per-target
1618
/// basis.
1719
///
@@ -25,4 +27,10 @@ struct Configuration: Codable {
2527
/// canonical Java class names (e.g., java.util.Vector) and the values are
2628
/// the corresponding Swift names (e.g., JavaVector).
2729
var classes: [String: String] = [:]
30+
31+
// Compile for the specified Java SE release.
32+
var sourceCompatibility: JavaVersion?
33+
34+
// Generate class files suitable for the specified Java SE release.
35+
var targetCompatibility: JavaVersion?
2836
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
typealias JavaVersion = Int
16+
17+
/// Configuration for the Java2Swift translation tool, provided on a per-target
18+
/// basis.
19+
///
20+
/// Note: there is a copy of this struct in the Java2Swift library. They
21+
/// must be kept in sync.
22+
struct Configuration: Codable {
23+
/// The Java class path that should be passed along to the Java2Swift tool.
24+
var classPath: String? = nil
25+
26+
/// The Java classes that should be translated to Swift. The keys are
27+
/// canonical Java class names (e.g., java.util.Vector) and the values are
28+
/// the corresponding Swift names (e.g., JavaVector).
29+
var classes: [String: String] = [:]
30+
31+
// Compile for the specified Java SE release.
32+
var sourceCompatibility: JavaVersion?
33+
34+
// Generate class files suitable for the specified Java SE release.
35+
var targetCompatibility: JavaVersion?
36+
}
37+
38+
extension Configuration {
39+
var compilerVersionArgs: [String] {
40+
var compilerVersionArgs = [String]()
41+
42+
if let sourceCompatibility {
43+
compilerVersionArgs += ["--source", String(sourceCompatibility)]
44+
}
45+
if let targetCompatibility {
46+
compilerVersionArgs += ["--target", String(targetCompatibility)]
47+
}
48+
49+
return compilerVersionArgs
50+
}
51+
}

Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
3232
// so we cannot eliminate this deprecation warning.
3333
let sourceDir = target.directory.string
3434

35+
// The name of the configuration file JavaKit.config from the target for
36+
// which we are generating Swift wrappers for Java classes.
37+
let configFile = URL(filePath: sourceDir).appending(path: "Java2Swift.config")
38+
let config: Configuration?
39+
40+
if let configData = try? Data(contentsOf: configFile) {
41+
config = try? JSONDecoder().decode(Configuration.self, from: configData)
42+
} else {
43+
config = nil
44+
}
45+
3546
// The class files themselves will be generated into the build directory
3647
// for this target.
3748
let classFiles = javaFiles.map { sourceFileURL in
@@ -60,7 +71,7 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
6071
arguments: javaFiles.map { $0.path(percentEncoded: false) } + [
6172
"-d", javaClassFileURL.path(),
6273
"-parameters", // keep parameter names, which allows us to emit them in generated Swift decls
63-
],
74+
] + (config?.compilerVersionArgs ?? []),
6475
inputFiles: javaFiles,
6576
outputFiles: classFiles
6677
)

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)