Skip to content

PoC of using gradle to obtain classpath for dependency expressed in config #137

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
wants to merge 1 commit into from
Closed
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
76 changes: 76 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import CompilerPluginSupport
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: "swift-java-benchmarks",

platforms: [
.macOS(.v14)
],

dependencies: [
.package(name: "swift-java", path: "../"),

// plugins
.package(url: "https://github.com/ordo-one/package-benchmark", .upToNextMajor(from: "1.4.0")),
],

targets: [
.executableTarget(
name: "JavaApiCallBenchmarks",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "JavaRuntime", package: "swift-java"),
.product(name: "JavaKit", package: "swift-java"),
.product(name: "JavaKitNetwork", package: "swift-java"),
],
path: "Benchmarks/JavaApiCallBenchmarks",
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
.swiftLanguageMode(.v5),
],
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark"),
]
),
]
)
44 changes: 44 additions & 0 deletions JavaKit/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

plugins {
id("build-logic.java-application-conventions")
}

group = "org.swift.javakit"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(22))
}
}

dependencies {
implementation("dev.gradleplugins:gradle-api:8.10.1")

testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.swift.javakit.dependencies;

import org.gradle.tooling.GradleConnector;

import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;

public class DependencyResolver {
/**
* May throw runtime exceptions including {@link org.gradle.api.internal.artifacts.ivyservice.TypedResolveException}
* if unable to resolve a dependency.
*/
public static String getClasspathWithDependency(String[] dependencies) throws IOException {
File projectDir = Files.createTempDirectory("java-swift-dependencies").toFile();
projectDir.mkdirs();

File buildFile = new File(projectDir, "build.gradle");
try (PrintWriter writer = new PrintWriter(buildFile)) {
writer.println("plugins { id 'java-library' }");
writer.println("repositories { mavenCentral() }");

writer.println("dependencies {");
for (String dependency : dependencies) {
writer.println("implementation(\"" + dependency + "\")");
}
writer.println("}");

writer.println("""
task printRuntimeClasspath {
def runtimeClasspath = sourceSets.main.runtimeClasspath
inputs.files(runtimeClasspath)
doLast {
println("CLASSPATH:${runtimeClasspath.asPath}")
}
}
""");
}
Copy link
Collaborator Author

@ktoso ktoso Oct 30, 2024

Choose a reason for hiding this comment

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

Silly trick, but effective ¯_(ツ)_/¯

We could implement the same methods with more direct raw APIs eventually but this gets us going and tbh gets the job done well enough. It would also integrate well with targeting a real build.gradle so we can share dependencies with a real build definition, and not re-declare them for swift in a different place again if a project is using gradle anyway.


File settingsFile = new File(projectDir, "settings.gradle.kts");
try (PrintWriter writer = new PrintWriter(settingsFile)) {
writer.println("""
rootProject.name = "swift-java-resolve-dependencies-temp-project"
""");
}

var connection = GradleConnector.newConnector()
.forProjectDirectory(projectDir)
.connect();

try (connection) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream);

connection.newBuild().forTasks(":printRuntimeClasspath")
.setStandardError(new NoopOutputStream())
.setStandardOutput(printStream)
.run();

var all = outputStream.toString();
var classpath = Arrays.stream(all.split("\n"))
.filter(s -> s.startsWith("CLASSPATH:"))
.map(s -> s.substring("CLASSPATH:".length()))
.findFirst().orElseThrow(() -> new RuntimeException("Could not find classpath output from ':printRuntimeClasspath' task."));
return classpath;
}
}

private static class NoopOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
// ignore
}
}
}
21 changes: 19 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ let javaIncludePath = "\(javaHome)/include"
#endif

let package = Package(
name: "JavaKit",
name: "swift-java",
platforms: [
.macOS(.v10_15)
.macOS(.v13)
],
products: [
// ==== JavaKit (i.e. calling Java directly Swift utilities)
Expand Down Expand Up @@ -281,6 +281,22 @@ let package = Package(
]
),

.target(
name: "JavaKitDependencyResolver",
dependencies: [
"JavaKitReflection",
],
exclude: [
"Java2Swift.config",
"org/javakit/deps/DependencyResolver.java",
],
swiftSettings: [
.swiftLanguageMode(.v5),
.enableUpcomingFeature("BareSlashRegexLiterals"),
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"]),
]
),

.executableTarget(
name: "Java2Swift",
dependencies: [
Expand All @@ -292,6 +308,7 @@ let package = Package(
"JavaKitJar",
"JavaKitNetwork",
"Java2SwiftLib",
"JavaKitDependencyResolver",
],

swiftSettings: [
Expand Down
29 changes: 29 additions & 0 deletions Plugins/Java2SwiftPlugin/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,33 @@ 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] = [:]

var dependencies: [JavaDependencyDescriptor] = []
}

struct JavaDependencyDescriptor: Codable {
var groupID: String
var artifactID: String
var version: String

init(from decoder: any Decoder) throws {
var container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
let parts = string.split(separator: ":")
guard parts.count == 3 else {
throw JavaDependencyDescriptorError(message: "Illegal dependency, did not match: `groupID:artifactID:version")
}
self.groupID = String(parts[0])
self.artifactID = String(parts[1])
self.version = String(parts[2])
}

func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode("\(self.groupID):\(self.artifactID):\(self.version)")
}

struct JavaDependencyDescriptorError: Error {
let message: String
}
}
2 changes: 2 additions & 0 deletions Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
let javaFiles = sourceModule.sourceFiles.map { $0.url }.filter {
$0.pathExtension == "java"
}
print("JAVA FILES IN \(sourceModule.sourceFiles) .... \(javaFiles)")
if javaFiles.isEmpty {
return []
}


// Note: Target doesn't have a directoryURL counterpart to directory,
// so we cannot eliminate this deprecation warning.
let sourceDir = target.directory.string
Expand Down
8 changes: 8 additions & 0 deletions Samples/JavaDependenciesApp/.gitignore
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
63 changes: 63 additions & 0 deletions Samples/JavaDependenciesApp/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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: "JavaDependenciesExample",
platforms: [
.macOS(.v10_15),
],
dependencies: [
.package(name: "swift-java", path: "../../"),
],
targets: [
.executableTarget(
name: "JavaJacksonDatabind",
dependencies: [
],
swiftSettings: [
.unsafeFlags(["-I\(javaIncludePath)", "-I\(javaPlatformIncludePath)"])
],
plugins: [
.plugin(name: "Java2SwiftPlugin", package: "swift-java"),
]
),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": [
"dev.gradleplugins:gradle-api:8.10.1"
],
"classes": {}
}
25 changes: 25 additions & 0 deletions Samples/JavaDependenciesApp/Sources/JavaJacksonDatabind/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// 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 JavaKit

let jvm = try JavaVirtualMachine.shared(classPath: ["QuadraticSieve-1.0.jar"])
do {
let sieveClass = try JavaClass<SieveOfEratosthenes>(environment: jvm.environment())
for prime in sieveClass.findPrimes(100)! {
print("Found prime: \(prime.intValue())")
}
} catch {
print("Failure: \(error)")
}
Loading
Loading