-
Notifications
You must be signed in to change notification settings - Fork 48
[WIP] JExtractSwiftPlugin #138
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9b37612
WIP on JExtractSwiftPlugin
ktoso cd2f979
Update main.swift
ktoso f4ba708
jextract prebuil plugin; without swift interfaces; triggered by gradle
ktoso c89d6b0
library paths in sample project fixed
ktoso 354ea56
now need to emit the swiftjava_JExtractPluginSampleLib_MyCoolSwiftCla…
ktoso 47aaa01
Merge branch 'main' into wip-jextract-plugin
ktoso f742aba
testkit: improve output assertion tools for jextract testing
ktoso 2ae6bbf
improved duplicated name (overloads) "mangling" for cdecls
ktoso 5e47f35
jextract fix variable test
ktoso 16e98fe
jextract fix variable test
ktoso fbefad8
github actions: no longer require nightly swift
ktoso f29ecc7
license ignore nested gradlew files
ktoso a3c0928
add missing license header
ktoso c5fb46a
Merge branch 'main' into wip-jextract-plugin
ktoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,5 @@ Makefile | |
gradle/wrapper/gradle-wrapper.properties | ||
gradlew | ||
gradlew.bat | ||
**/gradlew | ||
**/gradlew.bat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
/// Configuration for the JExtractSwift translation tool, provided on a per-target | ||
/// basis. | ||
struct Configuration: Codable { | ||
var javaPackage: String | ||
} | ||
|
||
func readConfiguration(sourceDir: String) throws -> Configuration { | ||
let configFile = URL(filePath: sourceDir).appending(path: "JExtractSwift.config") | ||
do { | ||
let configData = try Data(contentsOf: configFile) | ||
return try JSONDecoder().decode(Configuration.self, from: configData) | ||
} catch { | ||
throw ConfigurationError(message: "Failed to parse JExtractSwift configuration at '\(configFile)!'", error: error) | ||
} | ||
} | ||
|
||
struct ConfigurationError: Error { | ||
let message: String | ||
let error: any Error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import PackagePlugin | ||
|
||
@main | ||
struct JExtractSwiftBuildToolPlugin: BuildToolPlugin { | ||
func createBuildCommands(context: PluginContext, target: Target) throws -> [Command] { | ||
guard let sourceModule = target.sourceModule else { return [] } | ||
|
||
// Note: Target doesn't have a directoryURL counterpart to directory, | ||
// so we cannot eliminate this deprecation warning. | ||
let sourceDir = target.directory.string | ||
|
||
let configuration = try readConfiguration(sourceDir: "\(sourceDir)") | ||
|
||
// We use the the usual maven-style structure of "src/[generated|main|test]/java/..." | ||
// that is common in JVM ecosystem | ||
let outputDirectoryJava = context.pluginWorkDirectoryURL | ||
.appending(path: "src") | ||
.appending(path: "generated") | ||
.appending(path: "java") | ||
let outputDirectorySwift = context.pluginWorkDirectoryURL | ||
.appending(path: "src") | ||
.appending(path: "generated") | ||
.appending(path: "Sources") | ||
|
||
var arguments: [String] = [ | ||
"--swift-module", sourceModule.name, | ||
"--package-name", configuration.javaPackage, | ||
"--output-directory-java", outputDirectoryJava.path(percentEncoded: false), | ||
"--output-directory-swift", outputDirectorySwift.path(percentEncoded: false), | ||
// TODO: "--build-cache-directory", ... | ||
// Since plugins cannot depend on libraries we cannot detect what the output files will be, | ||
// as it depends on the contents of the input files. Therefore we have to implement this as a prebuild plugin. | ||
// We'll have to make up some caching inside the tool so we don't re-parse files which have not changed etc. | ||
] | ||
arguments.append(sourceDir) | ||
|
||
return [ | ||
.prebuildCommand( | ||
displayName: "Generate Java wrappers for Swift types", | ||
executable: try context.tool(named: "JExtractSwiftTool").url, | ||
arguments: arguments, | ||
// inputFiles: [ configFile ] + swiftFiles, | ||
// outputFiles: outputJavaFiles | ||
outputFilesDirectory: outputDirectorySwift | ||
) | ||
] | ||
} | ||
} | ||
|
||
// Note: the JAVA_HOME environment variable must be set to point to where | ||
// Java is installed, e.g., | ||
// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home. | ||
func findJavaHome() -> String { | ||
if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] { | ||
return home | ||
} | ||
|
||
// This is a workaround for envs (some IDEs) which have trouble with | ||
// picking up env variables during the build process | ||
let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home" | ||
if let home = try? String(contentsOfFile: path, encoding: .utf8) { | ||
if let lastChar = home.last, lastChar.isNewline { | ||
return String(home.dropLast()) | ||
} | ||
|
||
return home | ||
} | ||
|
||
fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// swift-tools-version: 6.0 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
import class Foundation.FileManager | ||
import class Foundation.ProcessInfo | ||
|
||
// Note: the JAVA_HOME environment variable must be set to point to where | ||
// Java is installed, e.g., | ||
// Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home. | ||
func findJavaHome() -> String { | ||
if let home = ProcessInfo.processInfo.environment["JAVA_HOME"] { | ||
return home | ||
} | ||
|
||
// This is a workaround for envs (some IDEs) which have trouble with | ||
// picking up env variables during the build process | ||
let path = "\(FileManager.default.homeDirectoryForCurrentUser.path()).java_home" | ||
if let home = try? String(contentsOfFile: path, encoding: .utf8) { | ||
if let lastChar = home.last, lastChar.isNewline { | ||
return String(home.dropLast()) | ||
} | ||
|
||
return home | ||
} | ||
|
||
fatalError("Please set the JAVA_HOME environment variable to point to where Java is installed.") | ||
} | ||
let javaHome = findJavaHome() | ||
|
||
let javaIncludePath = "\(javaHome)/include" | ||
#if os(Linux) | ||
let javaPlatformIncludePath = "\(javaIncludePath)/linux" | ||
#elseif os(macOS) | ||
let javaPlatformIncludePath = "\(javaIncludePath)/darwin" | ||
#else | ||
// TODO: Handle windows as well | ||
#error("Currently only macOS and Linux platforms are supported, this may change in the future.") | ||
#endif | ||
|
||
let package = Package( | ||
name: "JExtractPluginSampleApp", | ||
platforms: [ | ||
.macOS(.v10_15), | ||
], | ||
products: [ | ||
.library( | ||
name: "JExtractPluginSampleLib", | ||
type: .dynamic, | ||
targets: ["JExtractPluginSampleLib"] | ||
), | ||
], | ||
dependencies: [ | ||
.package(name: "swift-java", path: "../../"), | ||
], | ||
targets: [ | ||
.target( | ||
name: "JExtractPluginSampleLib", | ||
dependencies: [ | ||
], | ||
swiftSettings: [ | ||
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]) | ||
], | ||
plugins: [ | ||
.plugin(name: "JExtractSwiftPlugin", package: "swift-java"), | ||
] | ||
), | ||
] | ||
) |
3 changes: 3 additions & 0 deletions
3
Samples/JExtractPluginSampleApp/Sources/JExtractPluginSampleLib/JExtractSwift.config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"javaPackage": "com.example.swift" | ||
} |
26 changes: 26 additions & 0 deletions
26
Samples/JExtractPluginSampleApp/Sources/JExtractPluginSampleLib/MyCoolSwiftClass.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
public class MyCoolSwiftClass { | ||
var number: Int | ||
public init(number: Int) { | ||
print("[swift] init(number: \(number))") | ||
self.number = number | ||
} | ||
|
||
public func exposedToJava() { | ||
print("[swift] exposedToJava()") | ||
print("[swift] number = \(number)") | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can't work, since we need this to act on swiftinterface files of the module it's acting on. It has to be a "post build" plugin really.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: make this into a command plugin people would have to invoke manually
swift package jextract
which would generate the bindings.We could also then
swift package jextract jar
to produce a jar or even publish it etc.