Skip to content

JavaKit: add source/target versions to Java2Swift.config #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Plugins/Java2SwiftPlugin/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//
//===----------------------------------------------------------------------===//

typealias JavaVersion = Int

/// Configuration for the Java2Swift translation tool, provided on a per-target
/// basis.
///
Expand All @@ -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?
}
51 changes: 51 additions & 0 deletions Plugins/JavaCompilerPlugin/Configuration.swift
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are forced into this because plugins can’t have dependencies, I wonder if we need to set up a ci task to check those are indeed the same… we can do that 🤔

But maybe later on, as the project becomes more stable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, or maybe a makefile target to copy them from a SSOT.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d love to not have to use make in this build but I guess for such stuff maybe we will still… just because we already have swift gradle AND make so it’d be nice to just have two builds to worry about…

but i think it’d be okey to use make just for some simple automation but let actual “building” to swiftpm and gradle. So your idea here may be good :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, right now the copy is fine

/// 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
}
}
13 changes: 12 additions & 1 deletion Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down
14 changes: 13 additions & 1 deletion Sources/Java2SwiftLib/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//
//===----------------------------------------------------------------------===//

package typealias JavaVersion = Int

/// Configuration for the Java2Swift translation tool, provided on a per-target
/// basis.
///
Expand All @@ -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
}
}