swiftlang / swift-java

Apache License 2.0
636 stars 16 forks source link

Swift Java Interoperability Tools and Libraries

This repository contains two approaches to Swift/Java interoperability.

:construction: :construction: :construction: Early Development :construction: :construction: :construction:

:construction: :construction: :construction: This is a very early prototype and everything is subject to change. :construction: :construction: :construction:

Parts of this project are incomplete, not fleshed out, and subject to change without any notice.

The primary purpose of this repository is to create an environment for collaboration and joint exploration of the Swift/Java interoperability story. The project will transition to a more structured approach once key goals have been outlined.

Dependencies

This project consists of different modules which have different Swift and Java runtime requirements.

JavaKit – the Swift macros allowing the invocation of Java libraries from Swift

jextract-swift – the source generator that ingests .swiftinterface files and makes them available to be called from generated Java sources

The extract tool may become able to generate legacy compatible sources, which would not require JEP-454 and would instead rely on existing JNI facilities. Currently though, efforts are focused on the forward-looking implementation using modern foreign function and memory APIs.

Development and Testing

This project contains quite a few builds, Swift, Java, and depends on some custom steps.

Easiest way to get going is to:

make
swift test # test all Swift code, e.g. jextract-swift
./gradlew test # test all Java code, including integration tests that actually use jextract-ed sources

To test on Linux using Docker you can:

# run only Swift tests (i.e. swift test)
docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.2204.main.yaml run test-swift

# run only Java tests (i.e. gradle test)
docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.2204.main.yaml run test-java

# run all tests
docker-compose -f docker/docker-compose.yaml -f docker/docker-compose.2204.main.yaml run test 

Examples

JavaKit (Swift -> Java)

To run a simple app showcasing a Swift process calling into a Java library you can run:

make run

Which executes a small Java app (com.example.swift.HelloSwift).

jextract (Java -> Swift)

To run a simple example app showcasing the jextract (Java calling Swift) approach you can:

make jextract-run
./gradlew run

which will run JavaSwiftKitDemo sample app.

User Guide

More details about the project and how it can be used are available in USER_GUIDE.md